View | Details | Raw Unified | Return to bug 20568
Collapse All | Expand All

(-)a/t/db_dependent/Koha/ApiKeys.t (-1 / +110 lines)
Line 0 Link Here
0
- 
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::Exception;
22
23
use t::lib::TestBuilder;
24
25
use Data::Printer colored => 1;
26
27
BEGIN {
28
    use_ok('Koha::ApiKeys');
29
}
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
subtest 'store() tests' => sub {
35
36
    plan tests => 12;
37
38
    $schema->storage->txn_begin;
39
40
    my $print_error = $schema->storage->dbh->{PrintError};
41
    $schema->storage->dbh->{PrintError} = 0;
42
43
    Koha::ApiKeys->search->delete;
44
45
    my $patron_1    = $builder->build_object({ class => 'Koha::Patrons' });
46
    my $description = 'Coral API key';
47
    my $the_api_key = 'An_API_Key';
48
    my $api_key     = Koha::ApiKey->new({
49
                        patron_id => $patron_1->id,
50
                        value => $the_api_key,
51
                        description => $description
52
                      })->store;
53
    # re-read from DB
54
    $api_key->discard_changes;
55
56
    is( ref($api_key), 'Koha::ApiKey' );
57
    is( $api_key->patron_id,   $patron_1->id, 'FK is matched' );
58
    is( $api_key->value,       $the_api_key,  'The passed API key is preserved' );
59
    is( $api_key->description, $description,  'Description is correctly stored' );
60
    is( $api_key->active,      1,             'Key is active by default' );
61
62
    my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' });
63
    my $deleted_id = $patron_to_delete->id;
64
    $patron_to_delete->delete;
65
66
    throws_ok
67
        { Koha::ApiKey->new({ patron_id => $deleted_id })->store }
68
        'Koha::Exceptions::Object::FKConstraint',
69
        'Invalid patron ID raises exception';
70
    is(
71
        $@->message,
72
        'Broken FK constraint',
73
        'Exception message is correct'
74
    );
75
    is(
76
       $@->broken_fk,
77
       'patron_id',
78
       'Exception field is correct'
79
    );
80
81
    my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
82
83
    throws_ok
84
       { Koha::ApiKey->new({
85
            patron_id   => $patron_2->id,
86
            value       => $the_api_key,
87
            description => $description
88
        })->store }
89
        'Koha::Exceptions::Object::DuplicateID',
90
        'Duplicate API key raises exception';
91
    is(
92
        $@->message,
93
        'Duplicate ID',
94
        'Exception message is correct'
95
    );
96
    is(
97
       $@->duplicate_id,
98
       'value',
99
       'Exception field is correct'
100
    );
101
    $schema->storage->dbh->{PrintError} = $print_error;
102
103
    $api_key = Koha::ApiKey->new({ patron_id => $patron_2->id, description => $description })->store;
104
    $api_key->discard_changes;
105
106
    ok( defined $api_key->value && $api_key->value ne '' && length( $api_key->value ) > 1,
107
        'API key randomly generated' );
108
109
    $schema->storage->txn_rollback;
110
};

Return to bug 20568