5 use lib ("$FindBin::Bin/lib");
7 use stemmaweb::Test::Common;
10 use LWP::Protocol::PSGI;
11 use Test::WWW::Mechanize;
14 use HTML::TreeBuilder;
18 use stemmaweb::Test::DB;
20 my $dir = stemmaweb::Test::DB->new_db;
22 # NOTE: this test uses Text::Tradition::Directory
23 # to check user accounts really have been created.
24 # It'll need to be changed once that is replaced...
26 LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
28 my $ua = Test::WWW::Mechanize->new;
30 io("$FindBin::Bin/var")->rmtree if io("$FindBin::Bin/var")->exists;
33 diag("Create OpenID based Google account");
34 my $scope = $dir->new_scope;
36 $ua->get_ok('http://localhost/login');
38 local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
39 my ( $self, $c, $realm, $authinfo ) = @_;
41 return $realm->find_user({
42 url => 'https://www.google.com/accounts/o8/id?id=XYZ',
43 email => 'test@example.com',
47 ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=XYZ' }), 'No such user, yet.';
52 openid_identifier => 'https://www.google.com/accounts/o8/id?id=XYZ',
56 $ua->content_contains('You have logged in.', 'Openid login works');
60 $ua->content_contains('Hello! test@example.com!', 'We are logged in.');
62 diag("Verify new OpenID Google account exists");
63 ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=XYZ',
64 email => 'test@example.com',
65 }), 'The user is now there.';
68 # Converting to Google ID.
70 diag("Login/Convert to new Google+ account");
71 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
72 my ( $self, $c, $realm, $authinfo ) = @_;
74 return $realm->find_user({
75 openid_id => 'https://www.google.com/accounts/o8/id?id=XYZ',
77 email => $authinfo->{email},
80 $ua->get_ok('http://localhost/login');
85 id_token => 'something',
86 email => 'test@example.com',
90 $ua->content_contains('You have logged in.', 'G+ login works');
94 $ua->content_contains('Hello! test@example.com!', 'We are logged in.');
96 $ua->get_ok('/logout', 'Logged out');
100 diag("Create OpenID based Google account for email match");
101 my $scope = $dir->new_scope;
103 $ua->get_ok('http://localhost/login');
105 local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
106 my ( $self, $c, $realm, $authinfo ) = @_;
108 return $realm->find_user({
109 url => 'https://www.google.com/accounts/o8/id?id=42XYZ',
110 email => 'test42@example.com',
114 ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=42XYZ' }), 'No such user, yet.';
119 openid_identifier => 'https://www.google.com/accounts/o8/id?id=42XYZ',
123 $ua->content_contains('You have logged in.', 'Openid login works');
127 $ua->content_contains('Hello! test42@example.com!', 'We are logged in.');
129 diag("Verify new OpenID Google account for email match exists");
130 ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=42XYZ',
131 email => 'test42@example.com',
132 }), 'The user is now there.';
135 # Converting to Google ID.
137 diag("Login/Convert to new Google+ account matching only on email");
138 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
139 my ( $self, $c, $realm, $authinfo ) = @_;
141 return $realm->find_user({
142 openid_id => 'https://www.google.com/accounts/o8/id?id=45XYZ',
144 email => $authinfo->{email},
147 $ua->get_ok('http://localhost/login');
152 id_token => 'something',
153 email => 'test42@example.com',
157 $ua->content_contains('You have logged in.', 'G+ login works');
161 $ua->content_contains('Hello! test42@example.com!', 'We are logged in.');
170 diag("Test converting OpenID based Google account with traditions");
171 my $scope = $dir->new_scope;
173 my $openid_u = $dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=AItOawlFTlpuHGcI67tqahtw7xOod9VNWffB-Qg',
174 email => 'openid@example.org',
176 ok($openid_u, 'The user is there.');
178 diag("Login/Convert to new Google+ account");
179 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
180 my ( $self, $c, $realm, $authinfo ) = @_;
182 return $realm->find_user({
183 openid_id => 'https://www.google.com/accounts/o8/id?id=AItOawlFTlpuHGcI67tqahtw7xOod9VNWffB-Qg',
185 email => $authinfo->{email},
188 $ua->get_ok('http://localhost/login');
193 id_token => 'something',
194 email => 'openid@example.org',
198 $ua->content_contains('You have logged in.', 'G+ login works');
200 $ua->content_contains('Hello! openid@example.org!', 'We are logged in.');
202 my $gplus_u = $dir->find_user({
203 openid_id => 'https://www.google.com/accounts/o8/id?id=AItOawlFTlpuHGcI67tqahtw7xOod9VNWffB-Qg',
205 email => 'openid@example.org'
207 $openid_uid = $openid_u->kiokudb_object_id;
208 $gplus_uid = $gplus_u->kiokudb_object_id;
210 foreach my $trad_id (0..$#{ $openid_u->traditions }) {
211 my $trad = $gplus_u->traditions->[$trad_id];
212 $tradition_names{$trad->name} = 1;
213 is($trad->name, $openid_u->traditions->[$trad_id]->name, 'Traditions were copied over to G+ user');
220 diag("Check that the user traditions were removed from the old user");
221 my $scope = $dir->new_scope;
222 my $openid_u = $dir->lookup( $openid_uid );
223 my $gplus_u = $dir->lookup( $gplus_uid );
224 is( scalar @{$openid_u->traditions}, 0, "Traditions were removed from old user" );
225 foreach my $tradition ( @{$gplus_u->traditions} ) {
226 ok( $tradition_names{ $tradition->name }, "Tradition has remained with new user" );
231 diag("Verify we can login the new Google+ account again");
232 my $scope = $dir->new_scope;
233 # ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id?id=XYZ' }), 'Old google-openid is gone.';
237 openid_id => 'https://www.google.com/accounts/o8/id?id=XYZ',
238 email => 'test@example.com',
239 }), 'The G+ user is there.';
243 $ua->get_ok('http://localhost/login');
245 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
246 my ( $self, $c, $realm, $authinfo ) = @_;
248 return $realm->find_user({
249 openid_id => 'https://www.google.com/accounts/o8/id?id=XYZ',
251 email => $authinfo->{email},
258 id_token => 'something',
259 email => 'test@example.com',
263 $ua->content_contains('You have logged in.', 'We can now log in to our created user');
267 $ua->content_contains('Hello! test@example.com!', 'We are logged in.');
270 # Brand new user just from open id.
273 diag("Create a fresh Google+ user");
274 my $scope = $dir->new_scope;
276 ok !$dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2?id=XYZ', email => 'test2@exmple.com' }), 'The G+ user is not yet there.';
280 $ua->get_ok('http://localhost/login');
282 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
283 my ( $self, $c, $realm, $authinfo ) = @_;
285 return $realm->find_user({
286 openid_id => 'https://www.google.com/accounts/o8/id2?id=XYZ',
288 email => $authinfo->{email},
295 id_token => 'something',
296 email => 'test2@example.com',
300 $ua->content_contains('You have logged in.', 'We can now log in to our created user');
304 $ua->content_contains('Hello! test2@example.com!', 'We are logged in.');
306 ok $dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2?id=XYZ', email => 'test2@example.com' }), 'The G+ user is there.';
310 $ua->get_ok('http://localhost/login');
315 id_token => 'something',
316 email => 'test2@example.com',
320 $ua->content_contains('You have logged in.', 'We can login again');
324 $ua->content_contains('Hello! test2@example.com!', 'We are logged in.');
330 my $scope = $dir->new_scope;
332 ok !$dir->find_user({ sub => 4242, openid_id => 'https://www.google.com/accounts/o8/id3', email => 'email@example.org' }), 'The G+ user is not yet there.';
336 $ua->get_ok('http://localhost/login');
341 id_token => 'eyJraWQiOiJhIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiI0MjQyIiwib3BlbmlkX2lkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hY2NvdW50cy9vOC9pZDMifQ.moNERe3UHCY4xGMPxdCqmbg2JKW5feVnYlA8jeB4CdE4c_KL3YHvICQeql-S486HT-AlWBeDJWMr6wWH1kkwz11a2D1oyJ8qCWBssHIkhfv8dm3dphmRbtzYssAOFdGsmnPH1oXolCnl-Qu9WgHkhYYnRJWHr3CkeNA6Yh1xOV3nkaa8REtJckuzh3jyKQgx_rjIFsWBPDmT1rqa_Q0XOGVK34N5tADwpcWmkb3fFnbddzd9L6MnybbFzF_S238Bpr5vNa9doXRBwvJ85AdSn1AWX8R6qVpDbbaiGL2RCahuZYF9XECYm6anee-KTKvxh02KXkG2zniKVvweaMlcbQ',
342 email => 'email@example.org',
346 $ua->content_contains('You have logged in.', 'We can now log in to our created user - the token was decoded');
350 $ua->content_contains('Hello! email@example.org!', 'We are logged in.');
352 ok $dir->find_user({ sub => 4242, openid_id => 'https://www.google.com/accounts/o8/id3', email => 'email@example.org' }), 'The G+ user is there.';
356 $ua->get_ok('http://localhost/login');
361 id_token => 'eyJraWQiOiJhIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiI0MjQyIiwib3BlbmlkX2lkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hY2NvdW50cy9vOC9pZDMifQ.moNERe3UHCY4xGMPxdCqmbg2JKW5feVnYlA8jeB4CdE4c_KL3YHvICQeql-S486HT-AlWBeDJWMr6wWH1kkwz11a2D1oyJ8qCWBssHIkhfv8dm3dphmRbtzYssAOFdGsmnPH1oXolCnl-Qu9WgHkhYYnRJWHr3CkeNA6Yh1xOV3nkaa8REtJckuzh3jyKQgx_rjIFsWBPDmT1rqa_Q0XOGVK34N5tADwpcWmkb3fFnbddzd9L6MnybbFzF_S238Bpr5vNa9doXRBwvJ85AdSn1AWX8R6qVpDbbaiGL2RCahuZYF9XECYm6anee-KTKvxh02KXkG2zniKVvweaMlcbQ',
362 email => 'email@example.org',
366 $ua->content_contains('You have logged in.', 'We can login again');
370 $ua->content_contains('Hello! email@example.org!', 'We are logged in.');
373 io("$FindBin::Bin/var")->rmtree if io("$FindBin::Bin/var")->exists;