|
Line 0
Link Here
|
|
|
1 |
package t::lib::Page::Opac::OpacApiKeys; |
| 2 |
|
| 3 |
# Copyright 2015 KohaSuomi! |
| 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 Scalar::Util qw(blessed); |
| 22 |
use Test::More; |
| 23 |
|
| 24 |
use base qw(t::lib::Page::Opac t::lib::Page::Opac::LeftNavigation); |
| 25 |
|
| 26 |
use Koha::Exception::BadParameter; |
| 27 |
use Koha::Exception::UnknownObject; |
| 28 |
|
| 29 |
=head NAME t::lib::Page::Members::ApiKeys |
| 30 |
|
| 31 |
=head SYNOPSIS |
| 32 |
|
| 33 |
apikeys.pl PageObject providing page functionality as a service! |
| 34 |
|
| 35 |
=cut |
| 36 |
|
| 37 |
=head new |
| 38 |
|
| 39 |
my $apikeys = t::lib::Page::Members::ApiKeys->new({borrowernumber => "1"}); |
| 40 |
|
| 41 |
Instantiates a WebDriver and loads the members/apikeys.pl. |
| 42 |
@PARAM1 HASHRef of optional and MANDATORY parameters |
| 43 |
MANDATORY extra parameters: |
| 44 |
borrowernumber => loads the page to display Borrower matching the given borrowernumber |
| 45 |
|
| 46 |
@RETURNS t::lib::Page::Members::ApiKeys, ready for user actions! |
| 47 |
=cut |
| 48 |
|
| 49 |
sub new { |
| 50 |
my ($class, $params) = @_; |
| 51 |
unless (ref($params) eq 'HASH' || (blessed($params) && $params->isa('t::lib::Page') )) { |
| 52 |
$params = {}; |
| 53 |
} |
| 54 |
$params->{resource} = '/cgi-bin/koha/members/apikeys.pl'; |
| 55 |
$params->{type} = 'staff'; |
| 56 |
|
| 57 |
$params->{getParams} = []; |
| 58 |
#Handle MANDATORY parameters |
| 59 |
if ($params->{borrowernumber}) { |
| 60 |
push @{$params->{getParams}}, "borrowernumber=".$params->{borrowernumber}; |
| 61 |
} |
| 62 |
else { |
| 63 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."->new():> Parameter 'borrowernumber' is missing."); |
| 64 |
} |
| 65 |
|
| 66 |
my $self = $class->SUPER::new($params); |
| 67 |
|
| 68 |
return $self; |
| 69 |
} |
| 70 |
|
| 71 |
################################################################################ |
| 72 |
=head UI Mapping helper subroutines |
| 73 |
See. Selenium documentation best practices for UI element mapping to common language descriptions. |
| 74 |
=cut |
| 75 |
################################################################################ |
| 76 |
|
| 77 |
=head _getActionsAndTableElements |
| 78 |
@RETURNS List of |
| 79 |
HASHRef of Selenium::Driver::Webelement-objects matching the generic |
| 80 |
actions on this page, eg. 'generateNewKey'. |
| 81 |
HASHRef of Selenium::Driver::Webelement-objects keyed with the apiKey hash/text. |
| 82 |
These are all the apiKey table rows present, and have the |
| 83 |
elements prefetched for easy access. |
| 84 |
|
| 85 |
=cut |
| 86 |
|
| 87 |
sub _getActionsAndTableElements { |
| 88 |
my ($self) = @_; |
| 89 |
my $d = $self->getDriver(); |
| 90 |
|
| 91 |
my $generateNewKeySubmit = $d->find_element("#generatenewkey", 'css'); |
| 92 |
|
| 93 |
my $a = {}; #Collect action elements here |
| 94 |
$a->{generateNewKey} = $generateNewKeySubmit; #Bind the global action here for easy reference. |
| 95 |
|
| 96 |
my $apiKeyRows; |
| 97 |
eval { #We might not have ApiKeys yet. |
| 98 |
$apiKeyRows = $d->find_elements("#apikeystable tr", 'css'); |
| 99 |
shift @$apiKeyRows; #Remove the table header row |
| 100 |
}; |
| 101 |
my %apiKeys; |
| 102 |
for(my $i=0 ; $i<scalar(@$apiKeyRows) ; $i++) { |
| 103 |
#Iterate every apiKey in the apiKeys table and prefetch the interesting data as text and available action elements. |
| 104 |
my $row = $apiKeyRows->[$i]; |
| 105 |
$row->{'nth-of-type'} = $i+1; #starts from 1 |
| 106 |
$row->{key} = $d->find_child_element($row, "td.apikeykey", 'css')->get_text(); |
| 107 |
$row->{active} = $d->find_child_element($row, "td.apikeyactive", 'css')->get_text(); |
| 108 |
$row->{lastTransaction} = $d->find_child_element($row, "td.apikeylastransaction", 'css')->get_text(); |
| 109 |
$row->{delete} = $d->find_child_element($row, "input.apikeydelete", 'css'); |
| 110 |
eval { |
| 111 |
$row->{revoke} = $d->find_child_element($row, "input.apikeyrevoke", 'css'); |
| 112 |
}; |
| 113 |
eval { |
| 114 |
$row->{activate} = $d->find_child_element($row, "input.apikeyactivate", 'css'); |
| 115 |
}; |
| 116 |
$apiKeys{$row->{key}} = $row; |
| 117 |
} |
| 118 |
|
| 119 |
return ($a, \%apiKeys); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
################################################################################ |
| 125 |
=head PageObject Services |
| 126 |
|
| 127 |
=cut |
| 128 |
################################################################################ |
| 129 |
|
| 130 |
sub generateNewApiKey { |
| 131 |
my ($self) = @_; |
| 132 |
my $d = $self->getDriver(); |
| 133 |
$self->debugTakeSessionSnapshot(); |
| 134 |
|
| 135 |
my ($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 136 |
my $apiKeyRowsCountPre = (ref $apiKeyRows eq 'HASH') ? scalar(keys(%$apiKeyRows)) : 0; |
| 137 |
$actionElements->{generateNewKey}->click(); |
| 138 |
$self->debugTakeSessionSnapshot(); |
| 139 |
|
| 140 |
($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 141 |
my $apiKeyRowsCountPost = (ref $apiKeyRows eq 'HASH') ? scalar(keys(%$apiKeyRows)) : 0; |
| 142 |
is($apiKeyRowsCountPre+1, $apiKeyRowsCountPost, "ApiKey generated"); |
| 143 |
return $self; |
| 144 |
} |
| 145 |
|
| 146 |
sub revokeApiKey { |
| 147 |
my ($self, $apiKey) = @_; |
| 148 |
my $d = $self->getDriver(); |
| 149 |
$self->debugTakeSessionSnapshot(); |
| 150 |
|
| 151 |
my ($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 152 |
my $apiKeyRow = $apiKeyRows->{$apiKey}; |
| 153 |
Koha::Exception::UnknownObject->throw(error => __PACKAGE__."revokeApiKey():> No matching apiKey '$apiKey' found.") unless $apiKeyRow; |
| 154 |
$apiKeyRow->{revoke}->click(); |
| 155 |
$self->debugTakeSessionSnapshot(); |
| 156 |
|
| 157 |
($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 158 |
$apiKeyRow = $apiKeyRows->{$apiKey}; |
| 159 |
Koha::Exception::UnknownObject->throw(error => __PACKAGE__."revokeApiKey():> No matching apiKey '$apiKey' found after revoking it.") unless $apiKeyRow; |
| 160 |
is($apiKeyRow->{active}, 'No', "ApiKey revoked"); |
| 161 |
return $self; |
| 162 |
} |
| 163 |
|
| 164 |
sub activateApiKey { |
| 165 |
my ($self, $apiKey) = @_; |
| 166 |
my $d = $self->getDriver(); |
| 167 |
$self->debugTakeSessionSnapshot(); |
| 168 |
|
| 169 |
my ($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 170 |
my $apiKeyRow = $apiKeyRows->{$apiKey}; |
| 171 |
Koha::Exception::UnknownObject->throw(error => __PACKAGE__."revokeApiKey():> No matching apiKey '$apiKey' found.") unless $apiKeyRow; |
| 172 |
$apiKeyRow->{activate}->click(); |
| 173 |
$self->debugTakeSessionSnapshot(); |
| 174 |
|
| 175 |
($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 176 |
$apiKeyRow = $apiKeyRows->{$apiKey}; |
| 177 |
Koha::Exception::UnknownObject->throw(error => __PACKAGE__."revokeApiKey():> No matching apiKey '$apiKey' found after activating it.") unless $apiKeyRow; |
| 178 |
is($apiKeyRow->{active}, 'Yes', "ApiKey activated"); |
| 179 |
return $self; |
| 180 |
} |
| 181 |
|
| 182 |
sub deleteApiKey { |
| 183 |
my ($self, $apiKey) = @_; |
| 184 |
my $d = $self->getDriver(); |
| 185 |
$self->debugTakeSessionSnapshot(); |
| 186 |
|
| 187 |
my ($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 188 |
my $apiKeyRowsCountPre = (ref $apiKeyRows eq 'HASH') ? scalar(keys(%$apiKeyRows)) : 0; |
| 189 |
my $apiKeyRow = $apiKeyRows->{$apiKey}; |
| 190 |
Koha::Exception::UnknownObject->throw(error => __PACKAGE__."revokeApiKey():> No matching apiKey '$apiKey' found.") unless $apiKeyRow; |
| 191 |
$apiKeyRow->{delete}->click(); |
| 192 |
$self->debugTakeSessionSnapshot(); |
| 193 |
|
| 194 |
($actionElements, $apiKeyRows) = $self->_getActionsAndTableElements(); |
| 195 |
my $apiKeyRowsCountPost = (ref $apiKeyRows eq 'HASH') ? scalar(keys(%$apiKeyRows)) : 0; |
| 196 |
is($apiKeyRowsCountPre-1, $apiKeyRowsCountPost, "ApiKey deleted"); |
| 197 |
return $self; |
| 198 |
} |
| 199 |
|
| 200 |
1; #Make the compiler happy! |