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

(-)a/t/db_dependent/Koha/Auth/Client.t (-125 / +135 lines)
Lines 38-179 my $schema = Koha::Database->new->schema; Link Here
38
my $builder = t::lib::TestBuilder->new;
38
my $builder = t::lib::TestBuilder->new;
39
39
40
subtest 'get_user() tests' => sub {
40
subtest 'get_user() tests' => sub {
41
  plan tests => 4;
41
    plan tests => 4;
42
42
43
  $schema->storage->txn_begin;
43
    $schema->storage->txn_begin;
44
44
45
  my $client   = Koha::Auth::Client::OAuth->new;
45
    my $client   = Koha::Auth::Client::OAuth->new;
46
  my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
46
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
47
  my $domain   = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', update_on_auth => 0, allow_opac => 1, allow_staff => 0 } } );
47
    my $domain   = $builder->build_object(
48
  my $patron   = $builder->build_object( { class => 'Koha::Patrons', value => { email => 'patron@test.com' } } );
48
        {   class => 'Koha::Auth::Identity::Provider::Domains',
49
  my $mapping = {
49
            value => { identity_provider_id => $provider->id, domain => '', update_on_auth => 0, allow_opac => 1, allow_staff => 0 }
50
    email     => 'electronic_mail',
50
        }
51
    firstname => 'given_name',
51
    );
52
    surname   => 'family_name'
52
    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { email => 'patron@test.com' } } );
53
  };
53
    my $mapping = {
54
  $provider->set_mapping($mapping)->store;
54
        email     => 'electronic_mail',
55
55
        firstname => 'given_name',
56
  my $id_token = 'header.'.encode_base64url(encode_json({
56
        surname   => 'family_name'
57
    electronic_mail => 'patron@test.com',
57
    };
58
    given_name => 'test name'
58
    $provider->set_mapping($mapping)->store;
59
  })).'.footer';
59
60
60
    my $id_token = 'header.'
61
  my $data = {
61
      . encode_base64url(
62
    id_token => $id_token
62
        encode_json(
63
  };
63
            {   electronic_mail => 'patron@test.com',
64
64
                given_name      => 'test name'
65
  my ($resolved_patron, $mapped_data, $resolved_domain) = $client->get_user({ provider => $provider->code, data => $data, interface => 'opac' });
65
            }
66
  is_deeply( $resolved_patron->to_api, $patron->to_api, 'Patron correctly retrieved' );
66
        )
67
  is( $mapped_data->{firstname}, 'test name', 'Data mapped correctly' );
67
      ) . '.footer';
68
  is( $mapped_data->{surname}, undef, 'No surname mapped');
68
69
  is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain');
69
    my $data = { id_token => $id_token };
70
70
71
  $schema->storage->txn_rollback;
71
    my ( $resolved_patron, $mapped_data, $resolved_domain ) = $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } );
72
    is_deeply( $resolved_patron->to_api, $patron->to_api, 'Patron correctly retrieved' );
73
    is( $mapped_data->{firstname},            'test name',                                   'Data mapped correctly' );
74
    is( $mapped_data->{surname},              undef,                                         'No surname mapped' );
75
    is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' );
76
77
    $schema->storage->txn_rollback;
72
78
73
};
79
};
74
80
75
subtest 'get_valid_domain_config() tests' => sub {
81
subtest 'get_valid_domain_config() tests' => sub {
76
  plan tests => 10;
82
    plan tests => 10;
77
83
78
  $schema->storage->txn_begin;
84
    $schema->storage->txn_begin;
79
85
80
  my $client   = Koha::Auth::Client->new;
86
    my $client   = Koha::Auth::Client->new;
81
  my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
87
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
82
  my $domain1  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 0, allow_staff => 0 } } );
88
    my $domain1  = $builder->build_object(
83
  my $domain2  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*library.com', allow_opac => 1, allow_staff => 0 } } );
89
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 0, allow_staff => 0 } } );
84
  my $domain3  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*.library.com', allow_opac => 1, allow_staff => 0 } } );
90
    my $domain2 = $builder->build_object(
85
  my $domain4  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'student.library.com', allow_opac => 1, allow_staff => 0 } } );
91
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*library.com', allow_opac => 1, allow_staff => 0 } } );
86
  my $domain5  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => 'staff.library.com', allow_opac => 1, allow_staff => 1 } } );
92
    my $domain3 = $builder->build_object(
87
93
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '*.library.com', allow_opac => 1, allow_staff => 0 } }
88
  my $retrieved_domain;
94
    );
89
95
    my $domain4 = $builder->build_object(
90
  # Test @gmail.com
96
        {   class => 'Koha::Auth::Identity::Provider::Domains',
91
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@gmail.com', interface => 'opac'});
97
            value => { identity_provider_id => $provider->id, domain => 'student.library.com', allow_opac => 1, allow_staff => 0 }
92
  is($retrieved_domain, undef, 'gmail user cannot enter opac');
98
        }
93
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@gmail.com', interface => 'staff'});
99
    );
94
  is($retrieved_domain, undef, 'gmail user cannot enter staff');
100
    my $domain5 = $builder->build_object(
95
101
        {   class => 'Koha::Auth::Identity::Provider::Domains',
96
  # Test @otherlibrary.com
102
            value => { identity_provider_id => $provider->id, domain => 'staff.library.com', allow_opac => 1, allow_staff => 1 }
97
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@otherlibrary.com', interface => 'opac'});
103
        }
98
  is($retrieved_domain->identity_provider_domain_id, $domain2->identity_provider_domain_id, 'otherlibaray user can enter opac with domain2');
104
    );
99
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@otherlibrary.com', interface => 'staff'});
105
100
  is($retrieved_domain, undef, 'otherlibrary user cannot enter staff');
106
    my $retrieved_domain;
101
107
102
  # Test @provider.library.com
108
    # Test @gmail.com
103
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@provider.library.com', interface => 'opac'});
109
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'opac' } );
104
  is($retrieved_domain->identity_provider_domain_id, $domain3->identity_provider_domain_id, 'provider.library user can enter opac with domain3');
110
    is( $retrieved_domain, undef, 'gmail user cannot enter opac' );
105
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@provider.library.com', interface => 'staff'});
111
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'staff' } );
106
  is($retrieved_domain, undef, 'provider.library user cannot enter staff');
112
    is( $retrieved_domain, undef, 'gmail user cannot enter staff' );
107
113
108
  # Test @student.library.com
114
    # Test @otherlibrary.com
109
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@student.library.com', interface => 'opac'});
115
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@otherlibrary.com', interface => 'opac' } );
110
  is($retrieved_domain->identity_provider_domain_id, $domain4->identity_provider_domain_id, 'student.library user can enter opac with domain4');
116
    is( $retrieved_domain->identity_provider_domain_id, $domain2->identity_provider_domain_id, 'otherlibaray user can enter opac with domain2' );
111
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@student.library.com', interface => 'staff'});
117
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@otherlibrary.com', interface => 'staff' } );
112
  is($retrieved_domain, undef, 'student.library user cannot enter staff');
118
    is( $retrieved_domain, undef, 'otherlibrary user cannot enter staff' );
113
119
114
  # Test @staff.library.com
120
    # Test @provider.library.com
115
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@staff.library.com', interface => 'opac'});
121
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@provider.library.com', interface => 'opac' } );
116
  is($retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter opac with domain5');
122
    is( $retrieved_domain->identity_provider_domain_id, $domain3->identity_provider_domain_id, 'provider.library user can enter opac with domain3' );
117
  $retrieved_domain = $client->get_valid_domain_config({ provider => $provider, email => 'user@staff.library.com', interface => 'staff'});
123
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@provider.library.com', interface => 'staff' } );
118
  is($retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter staff with domain5');
124
    is( $retrieved_domain, undef, 'provider.library user cannot enter staff' );
119
125
120
  $schema->storage->txn_rollback;
126
    # Test @student.library.com
127
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@student.library.com', interface => 'opac' } );
128
    is( $retrieved_domain->identity_provider_domain_id, $domain4->identity_provider_domain_id, 'student.library user can enter opac with domain4' );
129
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@student.library.com', interface => 'staff' } );
130
    is( $retrieved_domain, undef, 'student.library user cannot enter staff' );
131
132
    # Test @staff.library.com
133
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@staff.library.com', interface => 'opac' } );
134
    is( $retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter opac with domain5' );
135
    $retrieved_domain = $client->get_valid_domain_config( { provider => $provider, email => 'user@staff.library.com', interface => 'staff' } );
136
    is( $retrieved_domain->identity_provider_domain_id, $domain5->identity_provider_domain_id, 'staff.library user can enter staff with domain5' );
137
138
    $schema->storage->txn_rollback;
121
};
139
};
122
140
123
subtest 'has_valid_domain_config() tests' => sub {
141
subtest 'has_valid_domain_config() tests' => sub {
124
  plan tests => 2;
142
    plan tests => 2;
125
  $schema->storage->txn_begin;
143
    $schema->storage->txn_begin;
126
144
127
  my $client   = Koha::Auth::Client->new;
145
    my $client   = Koha::Auth::Client->new;
128
  my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
146
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
129
  my $domain1  = $builder->build_object( { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 1, allow_staff => 0 } } );
147
    my $domain1  = $builder->build_object(
148
        { class => 'Koha::Auth::Identity::Provider::Domains', value => { identity_provider_id => $provider->id, domain => '', allow_opac => 1, allow_staff => 0 } } );
130
149
131
  # Test @gmail.com
150
    # Test @gmail.com
132
  my $retrieved_domain = $client->has_valid_domain_config({ provider => $provider, email => 'user@gmail.com', interface => 'opac'});
151
    my $retrieved_domain = $client->has_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'opac' } );
133
  is($retrieved_domain->identity_provider_domain_id, $domain1->identity_provider_domain_id, 'gmail user can enter opac with domain1');
152
    is( $retrieved_domain->identity_provider_domain_id, $domain1->identity_provider_domain_id, 'gmail user can enter opac with domain1' );
134
  throws_ok { $client->has_valid_domain_config({ provider => $provider, email => 'user@gmail.com', interface => 'staff'}) } 'Koha::Exceptions::Auth::NoValidDomain', 'gmail user cannot enter staff';
153
    throws_ok { $client->has_valid_domain_config( { provider => $provider, email => 'user@gmail.com', interface => 'staff' } ) } 'Koha::Exceptions::Auth::NoValidDomain',
154
      'gmail user cannot enter staff';
135
155
136
  $schema->storage->txn_rollback;
156
    $schema->storage->txn_rollback;
137
};
157
};
138
158
139
subtest '_traverse_hash() tests' => sub {
159
subtest '_traverse_hash() tests' => sub {
140
  plan tests => 3;
160
    plan tests => 3;
141
161
142
  my $client   = Koha::Auth::Client->new;
162
    my $client = Koha::Auth::Client->new;
143
163
144
  my $hash = {
164
    my $hash = {
145
    a => {
165
        a  => { hash  => { with => 'complicated structure' } },
146
      hash => {
166
        an => { array => [ { inside => 'a hash' }, { inside => 'second element' } ] }
147
        with => 'complicated structure'
167
    };
148
      }
168
149
    },
169
    my $first_result = $client->_traverse_hash(
150
    an => {
170
        {   base => $hash,
151
      array => [
171
            keys => 'a.hash.with'
152
        {
172
        }
153
          inside => 'a hash'
173
    );
154
        },
174
    is( $first_result, 'complicated structure', 'get the value within a hash structure' );
155
        {
175
156
          inside => 'second element'
176
    my $second_result = $client->_traverse_hash(
177
        {   base => $hash,
178
            keys => 'an.array.0.inside'
157
        }
179
        }
158
      ]
180
    );
159
    }
181
    is( $second_result, 'a hash', 'get the value of the first element of an array within a hash structure' );
160
  };
182
161
183
    my $third_result = $client->_traverse_hash(
162
  my $first_result = $client->_traverse_hash({
184
        {   base => $hash,
163
    base => $hash,
185
            keys => 'an.array.1.inside'
164
    keys => 'a.hash.with'
186
        }
165
  });
187
    );
166
  is($first_result, 'complicated structure', 'get the value within a hash structure');
188
    is( $third_result, 'second element', 'get the value of the second element of an array within a hash structure' );
167
189
};
168
  my $second_result = $client->_traverse_hash({
169
    base => $hash,
170
    keys => 'an.array.0.inside'
171
  });
172
  is($second_result, 'a hash', 'get the value of the first element of an array within a hash structure');
173
174
  my $third_result = $client->_traverse_hash({
175
    base => $hash,
176
    keys => 'an.array.1.inside'
177
  });
178
  is($third_result, 'second element', 'get the value of the second element of an array within a hash structure');
179
};
(-)a/t/db_dependent/api/v1/idp.t (-165 / +150 lines)
Lines 46-275 my $remote_address = '127.0.0.1'; Link Here
46
46
47
# my $idp_port = t::lib::IdP::ExternalIdP->start;
47
# my $idp_port = t::lib::IdP::ExternalIdP->start;
48
48
49
50
my $oauth_provider_data = {
49
my $oauth_provider_data = {
51
  code        => 'oauth_test',
50
    code        => 'oauth_test',
52
  description => 'OAuth provider',
51
    description => 'OAuth provider',
53
  protocol    => 'OAuth',
52
    protocol    => 'OAuth',
54
  mapping     => {
53
    mapping     => {
55
        email     => 'users.0.email',
54
        email     => 'users.0.email',
56
        firstname => 'users.0.custom_name',
55
        firstname => 'users.0.custom_name',
57
        surname   => 'users.0.custom_surname',
56
        surname   => 'users.0.custom_surname',
58
        userid    => 'users.0.id'
57
        userid    => 'users.0.id'
59
  },
58
    },
60
  matchpoint  => 'email',
59
    matchpoint => 'email',
61
  config      => {
60
    config     => {
62
    authorize_url => "/idp/test/authorization_endpoint",
61
        authorize_url => "/idp/test/authorization_endpoint",
63
    token_url     => "/idp/test/token_endpoint/without_id_token",
62
        token_url     => "/idp/test/token_endpoint/without_id_token",
64
    userinfo_url  => "/idp/test/userinfo_endpoint",
63
        userinfo_url  => "/idp/test/userinfo_endpoint",
65
    key           => "client_id",
64
        key           => "client_id",
66
    secret        => "client_secret"
65
        secret        => "client_secret"
67
  }
66
    }
68
};
67
};
69
68
70
my $oidc_with_email_provider_data = {
69
my $oidc_with_email_provider_data = {
71
  code => 'oidc_email',
70
    code        => 'oidc_email',
72
  description => 'OIDC with email provider',
71
    description => 'OIDC with email provider',
73
  protocol => 'OIDC',
72
    protocol    => 'OIDC',
74
  mapping => {
73
    mapping     => {
75
    email     => 'email',
74
        email     => 'email',
76
    firstname => 'given_name',
75
        firstname => 'given_name',
77
    surname   => 'family_name',
76
        surname   => 'family_name',
78
    userid    => 'sub'
77
        userid    => 'sub'
79
  },
78
    },
80
  matchpoint => 'email',
79
    matchpoint => 'email',
81
  config => {
80
    config     => {
82
    authorize_url  => "/idp/test/authorization_endpoint",
81
        authorize_url  => "/idp/test/authorization_endpoint",
83
    well_known_url => "/idp/test/with_email/.well_known",
82
        well_known_url => "/idp/test/with_email/.well_known",
84
    key            => "client_id",
83
        key            => "client_id",
85
    secret         => "client_secret"
84
        secret         => "client_secret"
86
  }
85
    }
87
};
86
};
88
87
89
my $oidc_without_email_provider_data = {
88
my $oidc_without_email_provider_data = {
90
  code => 'oidc_no_email',
89
    code        => 'oidc_no_email',
91
  description => 'OIDC without email provider',
90
    description => 'OIDC without email provider',
92
  protocol => 'OIDC',
91
    protocol    => 'OIDC',
93
  mapping => {
92
    mapping     => {
94
    email     => 'users.0.email',
93
        email     => 'users.0.email',
95
    firstname => 'given_name',
94
        firstname => 'given_name',
96
    surname   => 'family_name',
95
        surname   => 'family_name',
97
    userid    => 'sub'
96
        userid    => 'sub'
98
  },
97
    },
99
  matchpoint => 'email',
98
    matchpoint => 'email',
100
  config => {
99
    config     => {
101
    authorize_url  => "/idp/test/authorization_endpoint",
100
        authorize_url  => "/idp/test/authorization_endpoint",
102
    well_known_url => "/idp/test/without_email/.well_known",
101
        well_known_url => "/idp/test/without_email/.well_known",
103
    key            => "client_id",
102
        key            => "client_id",
104
    secret         => "client_secret"
103
        secret         => "client_secret"
105
  }
104
    }
106
};
105
};
107
106
108
my $domain_not_matching = {
107
my $domain_not_matching = {
109
  domain => 'gmail.com',
108
    domain              => 'gmail.com',
110
  auto_register => 0,
109
    auto_register       => 0,
111
  update_on_auth => 0,
110
    update_on_auth      => 0,
112
  default_library_id => undef,
111
    default_library_id  => undef,
113
  default_category_id => undef,
112
    default_category_id => undef,
114
  allow_opac => 1,
113
    allow_opac          => 1,
115
  allow_staff => 0
114
    allow_staff         => 0
116
};
115
};
117
116
118
my $domain_no_register = {
117
my $domain_no_register = {
119
  domain => 'some.library.com',
118
    domain              => 'some.library.com',
120
  auto_register => 0,
119
    auto_register       => 0,
121
  update_on_auth => 0,
120
    update_on_auth      => 0,
122
  default_library_id => undef,
121
    default_library_id  => undef,
123
  default_category_id => undef,
122
    default_category_id => undef,
124
  allow_opac => 1,
123
    allow_opac          => 1,
125
  allow_staff => 0
124
    allow_staff         => 0
126
};
125
};
127
126
128
my $library = $builder->build_object({class => 'Koha::Libraries'});
127
my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
129
my $category = $builder->build_object({class => 'Koha::Patron::Categories'});
128
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } );
130
129
131
my $domain_register = {
130
my $domain_register = {
132
  domain => 'some.library.com',
131
    domain              => 'some.library.com',
133
  auto_register => 1,
132
    auto_register       => 1,
134
  update_on_auth => 0,
133
    update_on_auth      => 0,
135
  default_library_id => $library->branchcode,
134
    default_library_id  => $library->branchcode,
136
  default_category_id => $category->categorycode,
135
    default_category_id => $category->categorycode,
137
  allow_opac => 1,
136
    allow_opac          => 1,
138
  allow_staff => 1
137
    allow_staff         => 1
139
};
138
};
140
139
141
my $domain_register_update = {
140
my $domain_register_update = {
142
  domain => 'some.library.com',
141
    domain              => 'some.library.com',
143
  auto_register => 1,
142
    auto_register       => 1,
144
  update_on_auth => 1,
143
    update_on_auth      => 1,
145
  default_library_id => $library->branchcode,
144
    default_library_id  => $library->branchcode,
146
  default_category_id => $category->categorycode,
145
    default_category_id => $category->categorycode,
147
  allow_opac => 1,
146
    allow_opac          => 1,
148
  allow_staff => 0
147
    allow_staff         => 0
149
};
148
};
150
149
151
subtest 'provider endpoint tests' => sub {
150
subtest 'provider endpoint tests' => sub {
152
  plan tests => 12;
151
    plan tests => 12;
153
152
154
  $schema->storage->txn_begin;
153
    $schema->storage->txn_begin;
155
154
156
  Koha::Auth::Identity::Provider::Domains->delete;
155
    Koha::Auth::Identity::Provider::Domains->delete;
157
  Koha::Auth::Identity::Providers->delete;
156
    Koha::Auth::Identity::Providers->delete;
158
157
159
  my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 });
158
    my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 1 } );
160
159
161
  my $t = Test::Mojo->new('Koha::REST::V1');
160
    my $t = Test::Mojo->new('Koha::REST::V1');
162
161
163
  my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data );
162
    my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers", json => $oauth_provider_data );
164
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
163
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
165
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
164
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
166
165
167
  $t->request_ok($tx)
166
    $t->request_ok($tx)->status_is(201);
168
    ->status_is(201);
169
167
170
  my $provider = Koha::Auth::Identity::Providers->search({code => 'oauth_test'})->next;
168
    my $provider = Koha::Auth::Identity::Providers->search( { code => 'oauth_test' } )->next;
171
  is ($provider->code, 'oauth_test', 'Provider was created');
169
    is( $provider->code, 'oauth_test', 'Provider was created' );
172
170
173
  $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers" );
171
    $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers" );
174
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
172
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
175
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
173
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
176
174
177
  $t->request_ok($tx)
175
    $t->request_ok($tx)->json_has( '/0/code', 'oauth_test' );
178
    ->json_has('/0/code', 'oauth_test');
179
176
180
  my %modified_provider_data_hash = %{$oauth_provider_data};
177
    my %modified_provider_data_hash = %{$oauth_provider_data};
181
  my $modified_provider_data = \%modified_provider_data_hash;
178
    my $modified_provider_data      = \%modified_provider_data_hash;
182
  $modified_provider_data->{code} = 'some_code';
179
    $modified_provider_data->{code} = 'some_code';
183
180
184
  $tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/".$provider->identity_provider_id, json => $modified_provider_data);
181
    $tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id, json => $modified_provider_data );
185
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
182
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
186
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
183
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
187
184
188
  $t->request_ok($tx)
185
    $t->request_ok($tx)->status_is(200);
189
    ->status_is(200);
190
186
191
  $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id);
187
    $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id );
192
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
188
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
193
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
189
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
194
190
195
  $t->request_ok($tx)
191
    $t->request_ok($tx)->json_has( '/code', 'some_code' );
196
    ->json_has('/code', 'some_code');
197
192
198
  $tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/".$provider->identity_provider_id);
193
    $tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id );
199
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
194
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
200
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
195
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
201
196
202
  $t->request_ok($tx)
197
    $t->request_ok($tx)->status_is(204);
203
    ->status_is(204);
204
  # p $t->tx->res;
205
198
206
  $provider = Koha::Auth::Identity::Providers->search->next;
199
    $provider = Koha::Auth::Identity::Providers->search->next;
207
  is ($provider, undef, 'All providers deleted');
200
    is( $provider, undef, 'All providers deleted' );
208
201
209
  $schema->storage->txn_rollback;
202
    $schema->storage->txn_rollback;
210
};
203
};
211
204
212
subtest 'domain endpoint tests' => sub {
205
subtest 'domain endpoint tests' => sub {
213
  plan tests => 12;
214
206
215
  $schema->storage->txn_begin;
207
    plan tests => 12;
216
208
217
  Koha::Auth::Identity::Provider::Domains->delete;
209
    $schema->storage->txn_begin;
218
  Koha::Auth::Identity::Providers->delete;
219
210
220
  my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 1 });
211
    Koha::Auth::Identity::Provider::Domains->delete;
212
    Koha::Auth::Identity::Providers->delete;
221
213
222
  my $t = Test::Mojo->new('Koha::REST::V1');
214
    my ( $borrowernumber, $session_id ) = create_user_and_session( { authorized => 1 } );
223
215
224
  my $provider = $builder->build_object({class => 'Koha::Auth::Identity::Providers'});
216
    my $t = Test::Mojo->new('Koha::REST::V1');
225
217
226
  my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains", json => $domain_not_matching );
218
    my $provider = $builder->build_object( { class => 'Koha::Auth::Identity::Providers' } );
227
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
228
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
229
219
230
  $t->request_ok($tx)
220
    my $tx = $t->ua->build_tx( POST => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains", json => $domain_not_matching );
231
    ->status_is(201);
221
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
222
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
232
223
233
  my $domain = Koha::Auth::Identity::Provider::Domains->search({domain => 'gmail.com'})->next;
224
    $t->request_ok($tx)->status_is(201);
234
  is ($domain->domain, 'gmail.com', 'Provider was created');
235
225
236
  $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains" );
226
    my $domain = Koha::Auth::Identity::Provider::Domains->search( { domain => 'gmail.com' } )->next;
237
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
227
    is( $domain->domain, 'gmail.com', 'Provider was created' );
238
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
239
228
240
  $t->request_ok($tx)
229
    $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains" );
241
    ->json_has('/0/domain', 'gmail.com');
230
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
231
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
242
232
243
  my %modified_domain_data_hash = %{$domain_not_matching};
233
    $t->request_ok($tx)->json_has( '/0/domain', 'gmail.com' );
244
  my $modified_domain_data = \%modified_domain_data_hash;
245
  $modified_domain_data->{domain} = 'some.domain.com';
246
234
247
  $tx = $t->ua->build_tx( PUT => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id, json => $modified_domain_data);
235
    my %modified_domain_data_hash = %{$domain_not_matching};
248
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
236
    my $modified_domain_data      = \%modified_domain_data_hash;
249
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
237
    $modified_domain_data->{domain} = 'some.domain.com';
250
238
251
  $t->request_ok($tx)
239
    $tx = $t->ua->build_tx(
252
    ->status_is(200);
240
        PUT  => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id,
241
        json => $modified_domain_data
242
    );
243
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
244
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
253
245
254
  $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id);
246
    $t->request_ok($tx)->status_is(200);
255
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
256
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
257
247
258
  $t->request_ok($tx)
248
    $tx = $t->ua->build_tx( GET => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id );
259
    ->json_has('/domain', 'some.domain.com');
249
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
250
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
260
251
261
  $tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/".$provider->identity_provider_id."/domains/".$domain->identity_provider_domain_id);
252
    $t->request_ok($tx)->json_has( '/domain', 'some.domain.com' );
262
  $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
263
  $tx->req->env( { REMOTE_ADDR => $remote_address } );
264
253
265
  $t->request_ok($tx)
254
    $tx = $t->ua->build_tx( DELETE => "/api/v1/auth/identity_providers/" . $provider->identity_provider_id . "/domains/" . $domain->identity_provider_domain_id );
266
    ->status_is(204);
255
    $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
267
  # p $t->tx->res;
256
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
268
257
269
  $domain = Koha::Auth::Identity::Provider::Domains->search->next;
258
    $t->request_ok($tx)->status_is(204);
270
  is ($domain, undef, 'All domains deleted');
271
259
272
  $schema->storage->txn_rollback;
260
    $domain = Koha::Auth::Identity::Provider::Domains->search->next;
261
    is( $domain, undef, 'All domains deleted' );
262
263
    $schema->storage->txn_rollback;
273
};
264
};
274
265
275
# subtest 'oauth login tests' => sub {
266
# subtest 'oauth login tests' => sub {
Lines 316-326 sub create_user_and_session { Link Here
316
    my $flags = ( $args->{authorized} ) ? 1 : 0;
307
    my $flags = ( $args->{authorized} ) ? 1 : 0;
317
308
318
    my $user = $builder->build(
309
    my $user = $builder->build(
319
        {
310
        {   source => 'Borrower',
320
            source => 'Borrower',
311
            value  => { flags => $flags }
321
            value  => {
322
                flags => $flags
323
            }
324
        }
312
        }
325
    );
313
    );
326
314
Lines 334-338 sub create_user_and_session { Link Here
334
322
335
    return ( $user->{borrowernumber}, $session->id );
323
    return ( $user->{borrowernumber}, $session->id );
336
}
324
}
337
338
1;
339
- 

Return to bug 31378