Lines 35-41
post '/register_user' => sub {
Link Here
|
35 |
try { |
35 |
try { |
36 |
my $domain = Koha::Auth::Identity::Provider::Domains->find( $params->{domain_id} ); |
36 |
my $domain = Koha::Auth::Identity::Provider::Domains->find( $params->{domain_id} ); |
37 |
my $patron = $c->auth->register( |
37 |
my $patron = $c->auth->register( |
38 |
{ data => $params->{data}, |
38 |
{ |
|
|
39 |
data => $params->{data}, |
39 |
domain => $domain, |
40 |
domain => $domain, |
40 |
interface => $params->{interface} |
41 |
interface => $params->{interface} |
41 |
} |
42 |
} |
Lines 44-49
post '/register_user' => sub {
Link Here
|
44 |
} catch { |
45 |
} catch { |
45 |
if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) { |
46 |
if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) { |
46 |
$c->render( status => 401, json => { message => 'unauthorized' } ); |
47 |
$c->render( status => 401, json => { message => 'unauthorized' } ); |
|
|
48 |
} elsif ( ref($_) eq 'Koha::Exceptions::BadParameter' ) { |
49 |
$c->render( status => 400, json => { message => 'bad parameter: ' . $_->parameter } ); |
50 |
} elsif ( ref($_) eq 'Koha::Exceptions::MissingParameter' ) { |
51 |
$c->render( status => 400, json => { message => 'missing parameter: ' . $_->parameter } ); |
47 |
} else { |
52 |
} else { |
48 |
$c->render( status => 500, json => { message => 'other error' } ); |
53 |
$c->render( status => 500, json => { message => 'other error' } ); |
49 |
} |
54 |
} |
Lines 77-129
use Koha::Database;
Link Here
|
77 |
my $schema = Koha::Database->new()->schema(); |
82 |
my $schema = Koha::Database->new()->schema(); |
78 |
my $builder = t::lib::TestBuilder->new; |
83 |
my $builder = t::lib::TestBuilder->new; |
79 |
|
84 |
|
|
|
85 |
my $t = Test::Mojo->new; |
86 |
|
80 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
87 |
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling |
81 |
# this affects the other REST api tests |
88 |
# this affects the other REST api tests |
82 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
89 |
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); |
83 |
|
90 |
|
84 |
subtest 'auth.register helper' => sub { |
91 |
subtest 'auth.register helper' => sub { |
85 |
plan tests => 6; |
92 |
|
|
|
93 |
plan tests => 18; |
86 |
|
94 |
|
87 |
$schema->storage->txn_begin; |
95 |
$schema->storage->txn_begin; |
88 |
|
96 |
|
89 |
# generate a random patron |
97 |
# generate a random patron |
90 |
my $patron_to_delete = $builder->build_object({ class => 'Koha::Patrons' }); |
98 |
my $patron_to_delete_1 = $builder->build_object( { class => 'Koha::Patrons' } ); |
91 |
my $userid = $patron_to_delete->userid; |
99 |
my $patron_to_delete_2 = $builder->build_object( { class => 'Koha::Patrons' } ); |
92 |
# delete patron |
100 |
my $userid_1 = $patron_to_delete_1->userid; |
93 |
$patron_to_delete->delete; |
101 |
my $userid_2 = $patron_to_delete_2->userid; |
94 |
|
102 |
|
95 |
my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } ); |
103 |
# delete patron |
96 |
|
104 |
$patron_to_delete_1->delete; |
97 |
my $domain_with_register = $builder->build_object( |
105 |
$patron_to_delete_2->delete; |
98 |
{ class => 'Koha::Auth::Identity::Provider::Domains', |
106 |
|
99 |
value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 } |
107 |
my $provider = |
|
|
108 |
$builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } ); |
109 |
|
110 |
my $domain_1 = $builder->build_object( |
111 |
{ |
112 |
class => 'Koha::Auth::Identity::Provider::Domains', |
113 |
value => { |
114 |
identity_provider_id => $provider->id, |
115 |
domain => 'domain1.com', |
116 |
auto_register_opac => 1, |
117 |
auto_register_staff => 0 |
118 |
} |
100 |
} |
119 |
} |
101 |
); |
120 |
); |
102 |
|
121 |
|
103 |
my $domain_without_register = $builder->build_object( |
122 |
my $domain_2 = $builder->build_object( |
104 |
{ class => 'Koha::Auth::Identity::Provider::Domains', |
123 |
{ |
105 |
value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 } |
124 |
class => 'Koha::Auth::Identity::Provider::Domains', |
|
|
125 |
value => { |
126 |
identity_provider_id => $provider->id, |
127 |
domain => 'domain2.com', |
128 |
auto_register_opac => 0, |
129 |
auto_register_staff => 1 |
130 |
} |
106 |
} |
131 |
} |
107 |
); |
132 |
); |
108 |
|
133 |
|
109 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
134 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
110 |
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); |
135 |
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); |
111 |
|
136 |
|
112 |
my $user_data = { |
137 |
$t->post_ok( |
113 |
firstname => 'test', |
138 |
'/register_user' => json => { |
114 |
surname => 'test', |
139 |
data => { |
115 |
userid => $userid, |
140 |
firstname => 'test', |
116 |
branchcode => $library->branchcode, |
141 |
surname => 'test', |
117 |
categorycode => $category->categorycode |
142 |
userid => $userid_1, |
118 |
}; |
143 |
branchcode => $library->branchcode, |
119 |
|
144 |
categorycode => $category->categorycode |
120 |
my $t = Test::Mojo->new; |
145 |
}, |
|
|
146 |
domain_id => $domain_1->identity_provider_domain_id, |
147 |
interface => 'opac' |
148 |
} |
149 |
)->status_is(200)->json_is( '/userid', $userid_1 ); |
150 |
|
151 |
$t->post_ok( '/register_user' => json => |
152 |
{ data => {}, domain_id => $domain_2->identity_provider_domain_id, interface => 'opac' } )->status_is(401) |
153 |
->json_is( '/message', 'unauthorized' ); |
154 |
|
155 |
$t->post_ok( '/register_user' => json => |
156 |
{ data => {}, domain_id => $domain_1->identity_provider_domain_id, interface => 'staff' } )->status_is(401) |
157 |
->json_is( '/message', 'unauthorized' ); |
158 |
|
159 |
$t->post_ok( |
160 |
'/register_user' => json => { |
161 |
data => { |
162 |
firstname => 'test', |
163 |
surname => 'test', |
164 |
userid => $userid_2, |
165 |
branchcode => $library->branchcode, |
166 |
categorycode => $category->categorycode |
167 |
}, |
168 |
domain_id => $domain_2->identity_provider_domain_id, |
169 |
interface => 'staff' |
170 |
} |
171 |
)->status_is(200)->json_is( '/userid', $userid_2 ); |
121 |
|
172 |
|
122 |
$t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac' } )->status_is(200) |
173 |
$t->post_ok( '/register_user' => json => |
123 |
->json_has( '/firstname', 'test' ); |
174 |
{ data => {}, domain_id => $domain_1->identity_provider_domain_id } )->status_is(400) |
|
|
175 |
->json_is( '/message', 'missing parameter: interface' ); |
124 |
|
176 |
|
125 |
$t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac' } )->status_is(401) |
177 |
$t->post_ok( '/register_user' => json => |
126 |
->json_has( '/message', 'unauthorized' ); |
178 |
{ data => {}, domain_id => $domain_1->identity_provider_domain_id, interface => 'invalid' } )->status_is(400) |
|
|
179 |
->json_is( '/message', 'bad parameter: interface' ); |
127 |
|
180 |
|
128 |
$schema->storage->txn_rollback; |
181 |
$schema->storage->txn_rollback; |
129 |
}; |
182 |
}; |
Lines 135-141
subtest 'auth.session helper' => sub {
Link Here
|
135 |
|
188 |
|
136 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
189 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
137 |
|
190 |
|
138 |
my $t = Test::Mojo->new; |
|
|
139 |
$t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)->json_has( '/status', 'ok' ); |
191 |
$t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)->json_has( '/status', 'ok' ); |
140 |
|
192 |
|
141 |
$schema->storage->txn_rollback; |
193 |
$schema->storage->txn_rollback; |
142 |
- |
|
|