From: Errietta Kostala Date: Fri, 23 Jan 2015 16:38:35 +0000 (+0000) Subject: Googley test X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=26c264fd732a43415827984032e37d55b8ffcc7d;p=scpubgit%2Fstemmaweb.git Googley test --- diff --git a/t/07google.t b/t/07google.t index f4f5eae..76ee9ed 100644 --- a/t/07google.t +++ b/t/07google.t @@ -22,91 +22,155 @@ my $dir = stemmaweb::Test::DB->new_db; # to check user accounts really have been created. # It'll need to be changed once that is replaced... -my $scope = $dir->new_scope; - LWP::Protocol::PSGI->register(stemmaweb->psgi_app); my $ua = Test::WWW::Mechanize->new; -$ua->get_ok('http://localhost/login'); +{ + my $scope = $dir->new_scope; + + $ua->get_ok('http://localhost/login'); + + local *Catalyst::Authentication::Credential::OpenID::authenticate = sub { + my ( $self, $c, $realm, $authinfo ) = @_; + + return $realm->find_user({ url => 'https://www.google.com/accounts/o8/id' }, $c); + }; + + ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'No such user, yet.'; + + $ua->submit_form( + form_number => 2, + fields => { + openid_identifier => 'https://www.google.com/accounts/o8/id', + }, + ); + + $ua->content_contains('You have logged in.', 'Openid login works'); + + $ua->get('/'); + + $ua->content_contains('Hello! https://www.google.com/accounts/o8/id!', 'We are logged in.'); + + ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'The user is now there.'; + $ua->get('/logout'); + + # Converting to Google ID. + + local *stemmaweb::Authentication::Credential::Google::authenticate = sub { + my ( $self, $c, $realm, $authinfo ) = @_; + + return $realm->find_user({ + openid_id => 'https://www.google.com/accounts/o8/id', + sub => 42, + }, $c); + }; + $ua->get_ok('http://localhost/login'); + + $ua->submit_form( + form_number => 1, + fields => { + id_token => 'something', + email => 'email@example.org', + }, + ); + + $ua->content_contains('You have logged in.', 'G+ login works'); + + $ua->get('/'); + + $ua->content_contains('Hello! 42!', 'We are logged in.'); +} + +{ + my $scope = $dir->new_scope; + + ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'Old google-openid is gone.'; -# Creating an openID user. + ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.'; -local *Catalyst::Authentication::Credential::OpenID::authenticate = sub { - my ( $self, $c, $realm, $authinfo ) = @_; + $ua->get('/logout'); - return $realm->find_user({ url => 'https://www.google.com/accounts/o8/id' }, $c); -}; + $ua->get_ok('http://localhost/login'); -ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'No such user, yet.'; + local *stemmaweb::Authentication::Credential::Google::authenticate = sub { + my ( $self, $c, $realm, $authinfo ) = @_; -$ua->submit_form( - form_number => 2, - fields => { - openid_identifier => 'https://www.google.com/accounts/o8/id', - }, -); + return $realm->find_user({ + openid_id => 'https://www.google.com/accounts/o8/id', + sub => 42, + }, $c); + }; -$ua->content_contains('You have logged in.', 'Openid login works'); + $ua->submit_form( + form_number => 1, + fields => { + id_token => 'something', + email => 'email@example.org', + }, + ); -$ua->get('/'); + $ua->content_contains('You have logged in.', 'We can now log in to our created user'); -$ua->content_contains('Hello! https://www.google.com/accounts/o8/id!', 'We are logged in.'); + $ua->get('/'); -ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'The user is now there.'; -$ua->get('/logout'); + $ua->content_contains('Hello! 42!', 'We are logged in.'); +} -# Converting to Google ID. +# Brand new user just from open id. -local *stemmaweb::Authentication::Credential::Google::authenticate = sub { - my ( $self, $c, $realm, $authinfo ) = @_; +{ + my $scope = $dir->new_scope; - return $realm->find_user({ - openid_id => 'https://www.google.com/accounts/o8/id', - sub => 42, - }, $c); -}; -$ua->get_ok('http://localhost/login'); -$ua->submit_form( - form_number => 1, - fields => { - id_token => 'something', - email => 'email@example.org', - }, -); + ok !$dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is not yet there.'; -$ua->content_contains('You have logged in.', 'G+ login works'); + $ua->get('/logout'); -$ua->get('/'); + $ua->get_ok('http://localhost/login'); -$ua->content_contains('Hello! 42!', 'We are logged in.'); + local *stemmaweb::Authentication::Credential::Google::authenticate = sub { + my ( $self, $c, $realm, $authinfo ) = @_; + return $realm->find_user({ + openid_id => 'https://www.google.com/accounts/o8/id2', + sub => 2, + }, $c); + }; -my $ouser =$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }); + $ua->submit_form( + form_number => 1, + fields => { + id_token => 'something', + email => 'email@example.org', + }, + ); -diag $ouser->id . "\n\n\n"; + $ua->content_contains('You have logged in.', 'We can now log in to our created user'); + $ua->get('/'); -ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.'; + $ua->content_contains('Hello! 2!', 'We are logged in.'); + ok $dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is there.'; -$ua->get('/logout'); + $ua->get('/logout'); -$ua->get_ok('http://localhost/login'); + $ua->get_ok('http://localhost/login'); -$ua->submit_form( - form_number => 1, - fields => { - id_token => 'something', - email => 'email@example.org', - }, -); + $ua->submit_form( + form_number => 1, + fields => { + id_token => 'something', + email => 'email@example.org', + }, + ); -$ua->content_contains('You have logged in.', 'We can now log in to our created user'); + $ua->content_contains('You have logged in.', 'We can login again'); -$ua->get('/'); + $ua->get('/'); -$ua->content_contains('Hello! 42!', 'We are logged in.'); + $ua->content_contains('Hello! 2!', 'We are logged in.'); +} done_testing;