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