|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2015 Vaara-kirjastot |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::More; |
| 22 |
use Try::Tiny; |
| 23 |
use Scalar::Util qw(blessed); |
| 24 |
|
| 25 |
use Koha::Auth::PermissionManager; |
| 26 |
use Koha::ApiKeys; |
| 27 |
|
| 28 |
use t::lib::TestObjects::ObjectFactory; |
| 29 |
use t::lib::TestObjects::BorrowerFactory; |
| 30 |
use t::lib::Page::Members::Moremember; |
| 31 |
use t::lib::Page::Opac::OpacMain; |
| 32 |
|
| 33 |
##Setting up the test context |
| 34 |
my $testContext = {}; |
| 35 |
|
| 36 |
my $password = '1234'; |
| 37 |
my $borrowerFactory = t::lib::TestObjects::BorrowerFactory->new(); |
| 38 |
my $borrowers = $borrowerFactory->createTestGroup([ |
| 39 |
{firstname => 'Olli-Antti', |
| 40 |
surname => 'Kivi', |
| 41 |
cardnumber => '1A01', |
| 42 |
branchcode => 'CPL', |
| 43 |
password => $password, |
| 44 |
}, |
| 45 |
{firstname => 'Alli-Ontti', |
| 46 |
surname => 'Ivik', |
| 47 |
cardnumber => '1A02', |
| 48 |
branchcode => 'CPL', |
| 49 |
password => $password, |
| 50 |
}, |
| 51 |
], undef, $testContext); |
| 52 |
my $borrowerKivi = $borrowers->{'1A01'}; |
| 53 |
my $borrowerIvik = $borrowers->{'1A02'}; |
| 54 |
my $permissionManager = Koha::Auth::PermissionManager->new(); |
| 55 |
$permissionManager->grantPermission($borrowerKivi, 'borrowers', 'manage_api_keys'); |
| 56 |
|
| 57 |
|
| 58 |
##Test context set, starting testing: |
| 59 |
eval { #run in a eval-block so we don't die without tearing down the test context |
| 60 |
subtest "ApiKeys API Unit tests" => sub { |
| 61 |
my $borrowerKivi = $borrowers->{'1A01'}; |
| 62 |
my $borrowerIvik = $borrowers->{'1A02'}; |
| 63 |
|
| 64 |
|
| 65 |
my $apiKey = Koha::ApiKeys->grant($borrowerKivi); |
| 66 |
is($apiKey->borrowernumber, $borrowerKivi->borrowernumber, "ApiKey granted"); |
| 67 |
|
| 68 |
Koha::ApiKeys->revoke($apiKey); |
| 69 |
is($apiKey->active, 0, "ApiKey revoked"); |
| 70 |
|
| 71 |
Koha::ApiKeys->activate($apiKey); |
| 72 |
is($apiKey->active, 1, "ApiKey activated"); |
| 73 |
|
| 74 |
Koha::ApiKeys->grant($borrowerIvik, $apiKey); |
| 75 |
is($apiKey->borrowernumber, $borrowerIvik->borrowernumber, "ApiKey granted to another Borrower"); |
| 76 |
|
| 77 |
Koha::ApiKeys->delete($apiKey); |
| 78 |
$apiKey = Koha::ApiKeys->find({api_key_id => $apiKey->api_key_id}); |
| 79 |
ok(not($apiKey), "ApiKey deleted"); |
| 80 |
} |
| 81 |
}; |
| 82 |
if ($@) { #Catch all leaking errors and gracefully terminate. |
| 83 |
warn $@; |
| 84 |
tearDown(); |
| 85 |
exit 1; |
| 86 |
} |
| 87 |
|
| 88 |
eval { |
| 89 |
subtest "ApiKeys Intra Integration tests" => sub { |
| 90 |
my $agent = t::lib::Page::Members::Moremember->new({borrowernumber => $borrowerKivi->borrowernumber}); |
| 91 |
$agent->doPasswordLogin($borrowerKivi->userid, $password)->navigateManageApiKeys()->generateNewApiKey(); |
| 92 |
my @apiKeys = Koha::ApiKeys->search({borrowernumber => $borrowerKivi->borrowernumber}); |
| 93 |
$agent->revokeApiKey($apiKeys[0]->api_key)->deleteApiKey($apiKeys[0]->api_key) |
| 94 |
->quit(); |
| 95 |
} |
| 96 |
}; |
| 97 |
if ($@) { #Catch all leaking errors and gracefully terminate. |
| 98 |
warn $@; |
| 99 |
tearDown(); |
| 100 |
exit 1; |
| 101 |
} |
| 102 |
|
| 103 |
eval { |
| 104 |
subtest "ApiKeys OPAC Integration tests" => sub { |
| 105 |
my $agent = t::lib::Page::Opac::OpacMain->new(); |
| 106 |
$agent->doPasswordLogin($borrowerKivi->userid, $password)->navigateYourAPIKeys()->generateNewApiKey(); |
| 107 |
my @apiKeys = Koha::ApiKeys->search({borrowernumber => $borrowerKivi->borrowernumber}); |
| 108 |
$agent->revokeApiKey($apiKeys[0]->api_key)->deleteApiKey($apiKeys[0]->api_key) |
| 109 |
->quit(); |
| 110 |
} |
| 111 |
}; |
| 112 |
if ($@) { #Catch all leaking errors and gracefully terminate. |
| 113 |
warn $@; |
| 114 |
tearDown(); |
| 115 |
exit 1; |
| 116 |
} |
| 117 |
|
| 118 |
##All tests done, tear down test context |
| 119 |
tearDown(); |
| 120 |
done_testing; |
| 121 |
|
| 122 |
sub tearDown { |
| 123 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext); |
| 124 |
} |