Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / ApiKey.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 => 4;
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 subtest 'store() tests' => sub {
34
35     plan tests => 16;
36
37     $schema->storage->txn_begin;
38
39     my $print_error = $schema->storage->dbh->{PrintError};
40     $schema->storage->dbh->{PrintError} = 0;
41
42     Koha::ApiKeys->search->delete;
43
44     my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
45     my $description = 'Coral API key';
46
47     my $api_key = Koha::ApiKey->new(
48         {
49             patron_id   => $patron_1->id,
50             description => $description
51         }
52     )->store;
53
54     # re-read from DB
55     $api_key->discard_changes;
56
57     is( ref($api_key), 'Koha::ApiKey' );
58     is( $api_key->patron_id, $patron_1->id, 'FK is matched' );
59     ok( defined $api_key->client_id
60             && $api_key->client_id ne ''
61             && length( $api_key->client_id ) > 1,
62         'API client_id is generated'
63     );
64     ok( defined $api_key->secret
65             && $api_key->secret ne ''
66             && length( $api_key->secret ) > 1,
67         'API secret is generated' );
68     is( $api_key->description, $description, 'Description is correctly stored' );
69     is( $api_key->active,      1,            'Key is active by default' );
70
71     # revoke, to call store
72     my $original_api_key = $api_key->unblessed;
73     $api_key->active(0)->store;
74     $api_key->discard_changes;
75
76     is( $api_key->client_id, $original_api_key->{client_id}, '->store() preserves the client_id' );
77     is( $api_key->secret, $original_api_key->{secret}, '->store() preserves the secret' );
78     is( $api_key->patron_id, $original_api_key->{patron_id}, '->store() preserves the patron_id' );
79     is( $api_key->active, 0, '->store() preserves the active value' );
80
81     $api_key->set({ client_id => 'NewID!' });
82
83     throws_ok {
84         $api_key->store
85     }
86     'Koha::Exceptions::Object::ReadOnlyProperty',
87         'Read-only attribute overwrite attempt raises exception';
88
89     is( $@->property, 'client_id', 'Correct attribute reported back' );
90
91     $api_key->discard_changes;
92     # set a writeable attribute
93     $api_key->set({ description => 'Hey' });
94     lives_ok { $api_key->store } 'Updating a writeable attribute works';
95
96     my $patron_to_delete = $builder->build_object( { class => 'Koha::Patrons' } );
97     my $deleted_id = $patron_to_delete->id;
98     $patron_to_delete->delete;
99
100     {    # hide useless warnings
101         local *STDERR;
102         open STDERR, '>', '/dev/null';
103         throws_ok {
104             Koha::ApiKey->new(
105                 { patron_id => $deleted_id, description => 'a description' } )
106               ->store
107         }
108         'Koha::Exceptions::Object::FKConstraint',
109           'Invalid patron ID raises exception';
110         close STDERR;
111     }
112     is( $@->message,   'Broken FK constraint', 'Exception message is correct' );
113     is( $@->broken_fk, 'patron_id',            'Exception field is correct' );
114
115     $schema->storage->txn_rollback;
116 };
117
118 subtest 'validate_secret() tests' => sub {
119
120     plan tests => 2;
121
122     $schema->storage->txn_begin;
123
124     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
125     my $api_key = Koha::ApiKey->new(
126         {   patron_id   => $patron->id,
127             description => 'The description'
128         }
129     )->store;
130
131     my $secret = $api_key->plain_text_secret;
132
133     is( $api_key->validate_secret( $secret ), 1, 'Valid secret returns true' );
134     is( $api_key->validate_secret( 'Wrong secret' ), 0, 'Invalid secret returns false' );
135
136     $schema->storage->txn_rollback;
137 };
138
139 subtest 'plain_text_secret() tests' => sub {
140
141     plan tests => 2;
142
143     $schema->storage->txn_begin;
144
145     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
146     # generate a fresh API key
147     my $api_key = Koha::ApiKey->new({ description => 'blah', patron_id => $patron->id })->store;
148     my $plain_text_secret = $api_key->plain_text_secret;
149
150     ok( defined $plain_text_secret, 'A fresh API key carries its plain text secret' );
151     ok( $plain_text_secret ne q{}, 'Plain text secret is not an empty string' );
152
153     $schema->storage->txn_rollback;
154 };