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

(-)a/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t (-71 / +68 lines)
Lines 30-69 use Mojolicious::Lite; Link Here
30
plugin 'Koha::REST::Plugin::Auth::IdP';
30
plugin 'Koha::REST::Plugin::Auth::IdP';
31
31
32
post '/register_user' => sub {
32
post '/register_user' => sub {
33
  my $c = shift;
33
    my $c      = shift;
34
  my $params = $c->req->json;
34
    my $params = $c->req->json;
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
            {   data      => $params->{data},
39
      domain => $domain,
39
                domain    => $domain,
40
      interface => $params->{interface}
40
                interface => $params->{interface}
41
    });
41
            }
42
    $c->render(status => 200, json => $patron->to_api);
42
        );
43
  } catch {
43
        $c->render( status => 200, json => $patron->to_api );
44
    if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
44
    } catch {
45
      $c->render(status => 401, json => {message => 'unauthorized'});
45
        if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
46
    } else {
46
            $c->render( status => 401, json => { message => 'unauthorized' } );
47
      $c->render(status => 500, json => {message => 'other error'});
47
        } else {
48
            $c->render( status => 500, json => { message => 'other error' } );
49
        }
48
    }
50
    }
49
  }
50
};
51
};
51
52
52
post '/start_session' => sub {
53
post '/start_session' => sub {
53
  my $c = shift;
54
    my $c      = shift;
54
  my $userid = my $params = $c->req->json->{userid};
55
    my $userid = my $params = $c->req->json->{userid};
55
56
56
  try {
57
    try {
57
    my $patron = Koha::Patrons->search({userid => $userid});
58
        my $patron = Koha::Patrons->search( { userid => $userid } );
58
    my ($status, $cookie, $session_id) = $c->auth->session($patron->next);
59
        my ( $status, $cookie, $session_id ) = $c->auth->session({ patron => $patron->next, interface => 'opac' });
59
    $c->render(status => 200, json => {status => $status});
60
        $c->render( status => 200, json => { status => $status } );
60
  } catch {
61
    } catch {
61
    if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) {
62
        if ( ref($_) eq 'Koha::Exceptions::Auth::CannotCreateSession' ) {
62
      $c->render(status => 401, json => {message => 'unauthorized'});
63
            $c->render( status => 401, json => { message => 'unauthorized' } );
63
    } else {
64
        } else {
64
      $c->render(status => 500, json => {message => 'other error'});
65
            $c->render( status => 500, json => { message => 'other error' } );
66
        }
65
    }
67
    }
66
  }
67
};
68
};
68
69
69
use Test::More tests => 2;
70
use Test::More tests => 2;
Lines 81-133 my $builder = t::lib::TestBuilder->new; Link Here
81
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
82
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
82
83
83
subtest 'auth.register helper' => sub {
84
subtest 'auth.register helper' => sub {
84
  plan tests => 6;
85
    plan tests => 6;
85
86
86
  $schema->storage->txn_begin;
87
    $schema->storage->txn_begin;
87
88
88
  # Remove existing patrons
89
    # Remove existing patrons
89
  Koha::Patrons->delete;
90
    Koha::Patrons->delete;
90
  my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
91
    my $provider             = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
91
  my $domain_with_register  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 } } );
92
    my $domain_with_register = $builder->build_object(
92
  my $domain_without_register  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 } } );
93
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 } } );
93
  my $library = $builder->build_object({ class => 'Koha::Libraries'});
94
    my $domain_without_register = $builder->build_object(
94
  my $category = $builder->build_object( {class => 'Koha::Patron::Categories'});
95
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 } } );
95
  my $user_data = {
96
    my $library   = $builder->build_object( { class => 'Koha::Libraries' } );
96
    firstname => 'test',
97
    my $category  = $builder->build_object( { class => 'Koha::Patron::Categories' } );
97
    surname => 'test',
98
    my $user_data = {
98
    userid => 'id1',
99
        firstname    => 'test',
99
    branchcode => $library->branchcode,
100
        surname      => 'test',
100
    categorycode => $category->categorycode
101
        userid       => 'id1',
101
  };
102
        branchcode   => $library->branchcode,
102
103
        categorycode => $category->categorycode
103
  my $t = Test::Mojo->new;
104
    };
104
105
105
  $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac'})
106
    my $t = Test::Mojo->new;
106
    ->status_is(200)
107
107
    ->json_has('/firstname', 'test');
108
    $t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac' } )->status_is(200)
108
109
      ->json_has( '/firstname', 'test' );
109
  $t->post_ok('/register_user' => json => {data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac'})
110
110
    ->status_is(401)
111
    $t->post_ok( '/register_user' => json => { data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac' } )->status_is(401)
111
    ->json_has('/message', 'unauthorized');
112
      ->json_has( '/message', 'unauthorized' );
112
  $schema->storage->txn_rollback;
113
    $schema->storage->txn_rollback;
113
};
114
};
114
115
115
subtest 'auth.session helper' => sub {
116
subtest 'auth.session helper' => sub {
116
  plan tests => 3;
117
    plan tests => 3;
117
118
118
  $schema->storage->txn_begin;
119
    $schema->storage->txn_begin;
119
120
120
  # Remove existing patrons
121
    # Remove existing patrons
121
  Koha::Patrons->delete;
122
    Koha::Patrons->delete;
122
  my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
123
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
123
124
125
    my $t = Test::Mojo->new;
126
    $t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)->json_has( '/status', 'ok' );
124
127
125
  my $t = Test::Mojo->new;
128
    $schema->storage->txn_rollback;
126
  $t->post_ok('/start_session' => json => {userid => $patron->userid})
127
    ->status_is(200)
128
    ->json_has('/status', 'ok');
129
130
  $schema->storage->txn_rollback;
131
};
129
};
132
130
133
1;
131
1;
134
- 

Return to bug 32178