rm t/var before/after 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;
16970305 16use IO::All;
fbb4eba9 17
18use stemmaweb::Test::DB;
19
20my $dir = stemmaweb::Test::DB->new_db;
21
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...
25
fbb4eba9 26LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
27
28my $ua = Test::WWW::Mechanize->new;
29
16970305 30io("$FindBin::Bin/var")->rmtree;
31
26c264fd 32{
33 my $scope = $dir->new_scope;
34
35 $ua->get_ok('http://localhost/login');
36
37 local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
38 my ( $self, $c, $realm, $authinfo ) = @_;
39
40 return $realm->find_user({ url => 'https://www.google.com/accounts/o8/id' }, $c);
41 };
42
43 ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'No such user, yet.';
44
45 $ua->submit_form(
46 form_number => 2,
47 fields => {
48 openid_identifier => 'https://www.google.com/accounts/o8/id',
49 },
50 );
51
52 $ua->content_contains('You have logged in.', 'Openid login works');
53
54 $ua->get('/');
55
56 $ua->content_contains('Hello! https://www.google.com/accounts/o8/id!', 'We are logged in.');
57
58 ok $dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'The user is now there.';
59 $ua->get('/logout');
60
61 # Converting to Google ID.
62
63 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
64 my ( $self, $c, $realm, $authinfo ) = @_;
65
66 return $realm->find_user({
67 openid_id => 'https://www.google.com/accounts/o8/id',
68 sub => 42,
69 }, $c);
70 };
71 $ua->get_ok('http://localhost/login');
72
73 $ua->submit_form(
74 form_number => 1,
75 fields => {
76 id_token => 'something',
77 email => 'email@example.org',
78 },
79 );
80
81 $ua->content_contains('You have logged in.', 'G+ login works');
82
83 $ua->get('/');
84
85 $ua->content_contains('Hello! 42!', 'We are logged in.');
86}
87
88{
89 my $scope = $dir->new_scope;
90
91 ok !$dir->find_user({ url => 'https://www.google.com/accounts/o8/id' }), 'Old google-openid is gone.';
fbb4eba9 92
26c264fd 93 ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.';
fbb4eba9 94
26c264fd 95 $ua->get('/logout');
fbb4eba9 96
26c264fd 97 $ua->get_ok('http://localhost/login');
fbb4eba9 98
26c264fd 99 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
100 my ( $self, $c, $realm, $authinfo ) = @_;
fbb4eba9 101
26c264fd 102 return $realm->find_user({
103 openid_id => 'https://www.google.com/accounts/o8/id',
104 sub => 42,
105 }, $c);
106 };
fbb4eba9 107
26c264fd 108 $ua->submit_form(
109 form_number => 1,
110 fields => {
111 id_token => 'something',
112 email => 'email@example.org',
113 },
114 );
fbb4eba9 115
26c264fd 116 $ua->content_contains('You have logged in.', 'We can now log in to our created user');
fbb4eba9 117
26c264fd 118 $ua->get('/');
fbb4eba9 119
26c264fd 120 $ua->content_contains('Hello! 42!', 'We are logged in.');
121}
fbb4eba9 122
26c264fd 123# Brand new user just from open id.
fbb4eba9 124
26c264fd 125{
126 my $scope = $dir->new_scope;
fbb4eba9 127
fbb4eba9 128
26c264fd 129 ok !$dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is not yet there.';
fbb4eba9 130
26c264fd 131 $ua->get('/logout');
fbb4eba9 132
26c264fd 133 $ua->get_ok('http://localhost/login');
fbb4eba9 134
26c264fd 135 local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
136 my ( $self, $c, $realm, $authinfo ) = @_;
fbb4eba9 137
26c264fd 138 return $realm->find_user({
139 openid_id => 'https://www.google.com/accounts/o8/id2',
140 sub => 2,
141 }, $c);
142 };
fbb4eba9 143
26c264fd 144 $ua->submit_form(
145 form_number => 1,
146 fields => {
147 id_token => 'something',
148 email => 'email@example.org',
149 },
150 );
fbb4eba9 151
26c264fd 152 $ua->content_contains('You have logged in.', 'We can now log in to our created user');
fbb4eba9 153
26c264fd 154 $ua->get('/');
fbb4eba9 155
26c264fd 156 $ua->content_contains('Hello! 2!', 'We are logged in.');
fbb4eba9 157
26c264fd 158 ok $dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is there.';
fbb4eba9 159
26c264fd 160 $ua->get('/logout');
fbb4eba9 161
26c264fd 162 $ua->get_ok('http://localhost/login');
fbb4eba9 163
26c264fd 164 $ua->submit_form(
165 form_number => 1,
166 fields => {
167 id_token => 'something',
168 email => 'email@example.org',
169 },
170 );
fbb4eba9 171
26c264fd 172 $ua->content_contains('You have logged in.', 'We can login again');
fbb4eba9 173
26c264fd 174 $ua->get('/');
fbb4eba9 175
26c264fd 176 $ua->content_contains('Hello! 2!', 'We are logged in.');
177}
fbb4eba9 178
6ab7cb54 179# Decoding token
180
181{
182 my $scope = $dir->new_scope;
183
184 ok !$dir->find_user({ sub => 4242, openid_id => 'https://www.google.com/accounts/o8/id3' }), 'The G+ user is not yet there.';
185
186 $ua->get('/logout');
187
188 $ua->get_ok('http://localhost/login');
189
190 $ua->submit_form(
191 form_number => 1,
192 fields => {
193 id_token => 'eyJraWQiOiJhIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiI0MjQyIiwib3BlbmlkX2lkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hY2NvdW50cy9vOC9pZDMifQ.moNERe3UHCY4xGMPxdCqmbg2JKW5feVnYlA8jeB4CdE4c_KL3YHvICQeql-S486HT-AlWBeDJWMr6wWH1kkwz11a2D1oyJ8qCWBssHIkhfv8dm3dphmRbtzYssAOFdGsmnPH1oXolCnl-Qu9WgHkhYYnRJWHr3CkeNA6Yh1xOV3nkaa8REtJckuzh3jyKQgx_rjIFsWBPDmT1rqa_Q0XOGVK34N5tADwpcWmkb3fFnbddzd9L6MnybbFzF_S238Bpr5vNa9doXRBwvJ85AdSn1AWX8R6qVpDbbaiGL2RCahuZYF9XECYm6anee-KTKvxh02KXkG2zniKVvweaMlcbQ',
194 email => 'email@example.org',
195 },
196 );
197
198 $ua->content_contains('You have logged in.', 'We can now log in to our created user - the token was decoded');
199
200 $ua->get('/');
201
202 $ua->content_contains('Hello! 4242!', 'We are logged in.');
203
204 ok $dir->find_user({ sub => 4242, openid_id => 'https://www.google.com/accounts/o8/id3' }), 'The G+ user is there.';
205
206 $ua->get('/logout');
207
208 $ua->get_ok('http://localhost/login');
209
210 $ua->submit_form(
211 form_number => 1,
212 fields => {
213 id_token => 'eyJraWQiOiJhIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiI0MjQyIiwib3BlbmlkX2lkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hY2NvdW50cy9vOC9pZDMifQ.moNERe3UHCY4xGMPxdCqmbg2JKW5feVnYlA8jeB4CdE4c_KL3YHvICQeql-S486HT-AlWBeDJWMr6wWH1kkwz11a2D1oyJ8qCWBssHIkhfv8dm3dphmRbtzYssAOFdGsmnPH1oXolCnl-Qu9WgHkhYYnRJWHr3CkeNA6Yh1xOV3nkaa8REtJckuzh3jyKQgx_rjIFsWBPDmT1rqa_Q0XOGVK34N5tADwpcWmkb3fFnbddzd9L6MnybbFzF_S238Bpr5vNa9doXRBwvJ85AdSn1AWX8R6qVpDbbaiGL2RCahuZYF9XECYm6anee-KTKvxh02KXkG2zniKVvweaMlcbQ',
214 email => 'email@example.org',
215 },
216 );
217
218 $ua->content_contains('You have logged in.', 'We can login again');
219
220 $ua->get('/');
221
222 $ua->content_contains('Hello! 4242!', 'We are logged in.');
223}
224
16970305 225io("$FindBin::Bin/var")->rmtree;
226
fbb4eba9 227done_testing;