Google test
[scpubgit/stemmaweb.git] / t / 07google.t
CommitLineData
fbb4eba9 1use warnings;
2use strict;
3
4use FindBin;
5use lib ("$FindBin::Bin/lib");
6
7use stemmaweb::Test::Common;
8
9use stemmaweb;
10use LWP::Protocol::PSGI;
11use Test::WWW::Mechanize;
12
13use Test::More;
14use HTML::TreeBuilder;
15use Data::Dumper;
16
17use stemmaweb::Test::DB;
18
19my $dir = stemmaweb::Test::DB->new_db;
20
21# NOTE: this test uses Text::Tradition::Directory
22# to check user accounts really have been created.
23# It'll need to be changed once that is replaced...
24
25my $scope = $dir->new_scope;
26
27LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
28
29my $ua = Test::WWW::Mechanize->new;
30
31$ua->get_ok('http://localhost/login');
32
33# Creating an openID user.
34
35local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
36 my ( $self, $c, $realm, $authinfo ) = @_;
37
38 return $realm->find_user({ url => 'https://www.google.com/accounts/o8/id' }, $c);
39};
40
41ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'No such user, yet.';
42
43$ua->submit_form(
44 form_number => 2,
45 fields => {
46 openid_identifier => 'https://www.google.com/accounts/o8/id',
47 },
48);
49
50$ua->content_contains('You have logged in.', 'Openid login works');
51
52$ua->get('/');
53
54$ua->content_contains('Hello! https://www.google.com/accounts/o8/id!', 'We are logged in.');
55
56ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'The user is now there.';
57$ua->get('/logout');
58
59# Converting to Google ID.
60
61local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
62 my ( $self, $c, $realm, $authinfo ) = @_;
63
64 return $realm->find_user({
65 openid_id => 'https://www.google.com/accounts/o8/id',
66 sub => 42,
67 }, $c);
68};
69$ua->get_ok('http://localhost/login');
70
71$ua->submit_form(
72 form_number => 1,
73 fields => {
74 id_token => 'something',
75 email => 'email@example.org',
76 },
77);
78
79$ua->content_contains('You have logged in.', 'G+ login works');
80
81$ua->get('/');
82
83$ua->content_contains('Hello! 42!', 'We are logged in.');
84
85
86my $ouser =$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' });
87
88diag $ouser->id . "\n\n\n";
89
90
91ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.';
92
93
94$ua->get('/logout');
95
96$ua->get_ok('http://localhost/login');
97
98$ua->submit_form(
99 form_number => 1,
100 fields => {
101 id_token => 'something',
102 email => 'email@example.org',
103 },
104);
105
106$ua->content_contains('You have logged in.', 'We can now log in to our created user');
107
108$ua->get('/');
109
110$ua->content_contains('Hello! 42!', 'We are logged in.');
111
112done_testing;