Bug 26250: Fix tests when SearchEngine=Elastic
[koha.git] / t / db_dependent / Koha / ApiKeys.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use Test::MockModule;
22 use Test::Exception;
23
24 use t::lib::TestBuilder;
25
26 BEGIN {
27     use_ok('Koha::ApiKeys');
28 }
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 my $uuid_gen_client_id_counter = 0;
34 my $uuid_gen_secret_counter    = 0;
35
36 subtest 'store() tests' => sub {
37
38     plan tests => 13;
39
40     $schema->storage->txn_begin;
41
42     my $print_error = $schema->storage->dbh->{PrintError};
43     $schema->storage->dbh->{PrintError} = 0;
44
45     Koha::ApiKeys->search->delete;
46
47     my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
48     my $description = 'Coral API key';
49     my $api_key = Koha::ApiKey->new({ patron_id => $patron_1->id, description => $description })->store;
50
51     # re-read from DB
52     $api_key->discard_changes;
53
54     is( ref($api_key), 'Koha::ApiKey' );
55     is( $api_key->patron_id, $patron_1->id, 'FK is matched' );
56     ok( defined $api_key->client_id
57             && $api_key->client_id ne ''
58             && length( $api_key->client_id ) > 1,
59         'API client_id is generated'
60     );
61     ok( defined $api_key->secret
62             && $api_key->secret ne ''
63             && length( $api_key->secret ) > 1,
64         'API secret is generated' );
65     is( $api_key->description, $description, 'Description is correctly stored' );
66     is( $api_key->active,      1,            'Key is active by default' );
67
68     # revoke, to call store
69     my $original_api_key = $api_key->unblessed;
70     $api_key->active(0)->store;
71     $api_key->discard_changes;
72
73     is( $api_key->client_id, $original_api_key->{client_id}, '->store() preserves the client_id' );
74     is( $api_key->secret, $original_api_key->{secret}, '->store() preserves the secret' );
75     is( $api_key->patron_id, $original_api_key->{patron_id}, '->store() preserves the patron_id' );
76     is( $api_key->active, 0, '->store() preserves the active value' );
77
78     my $patron_to_delete = $builder->build_object( { class => 'Koha::Patrons' } );
79     my $deleted_id = $patron_to_delete->id;
80     $patron_to_delete->delete;
81
82     {    # hide useless warnings
83         local *STDERR;
84         open STDERR, '>', '/dev/null';
85         throws_ok {
86             Koha::ApiKey->new(
87                 { patron_id => $deleted_id, description => 'a description' } )
88               ->store
89         }
90         'Koha::Exceptions::Object::FKConstraint',
91           'Invalid patron ID raises exception';
92         close STDERR;
93     }
94     is( $@->message,   'Broken FK constraint', 'Exception message is correct' );
95     is( $@->broken_fk, 'patron_id',            'Exception field is correct' );
96
97     $schema->storage->txn_rollback;
98 };