46e6a20c6c7ad4ab6a939c17b0936e25659771ba
[scpubgit/stemmaweb.git] / t / 07google.t
1 use warnings;
2 use strict;
3
4 use FindBin;
5 use lib ("$FindBin::Bin/lib");
6
7 use stemmaweb::Test::Common;
8
9 use stemmaweb;
10 use LWP::Protocol::PSGI;
11 use Test::WWW::Mechanize;
12
13 use Test::More;
14 use HTML::TreeBuilder;
15 use Data::Dumper;
16
17 use stemmaweb::Test::DB;
18
19 my $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
25 LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
26
27 my $ua = Test::WWW::Mechanize->new;
28
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.';
89
90     ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.';
91
92     $ua->get('/logout');
93
94     $ua->get_ok('http://localhost/login');
95
96     local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
97         my ( $self, $c, $realm, $authinfo ) = @_;
98
99         return $realm->find_user({
100                 openid_id => 'https://www.google.com/accounts/o8/id',
101                 sub        => 42,
102             }, $c);
103     };
104
105     $ua->submit_form(
106         form_number => 1,
107         fields => {
108             id_token => 'something',
109             email   => 'email@example.org',
110         },
111     );
112
113     $ua->content_contains('You have logged in.', 'We can now log in to our created user');
114
115     $ua->get('/');
116
117     $ua->content_contains('Hello! 42!', 'We are logged in.');
118 }
119
120 # Brand new user just from open id.
121
122 {
123     my $scope = $dir->new_scope;
124
125
126     ok !$dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is not yet there.';
127
128     $ua->get('/logout');
129
130     $ua->get_ok('http://localhost/login');
131
132     local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
133         my ( $self, $c, $realm, $authinfo ) = @_;
134
135         return $realm->find_user({
136                 openid_id => 'https://www.google.com/accounts/o8/id2',
137                 sub        => 2,
138             }, $c);
139     };
140
141     $ua->submit_form(
142         form_number => 1,
143         fields => {
144             id_token => 'something',
145             email   => 'email@example.org',
146         },
147     );
148
149     $ua->content_contains('You have logged in.', 'We can now log in to our created user');
150
151     $ua->get('/');
152
153     $ua->content_contains('Hello! 2!', 'We are logged in.');
154
155     ok $dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is there.';
156
157     $ua->get('/logout');
158
159     $ua->get_ok('http://localhost/login');
160
161     $ua->submit_form(
162         form_number => 1,
163         fields => {
164             id_token => 'something',
165             email   => 'email@example.org',
166         },
167     );
168
169     $ua->content_contains('You have logged in.', 'We can login again');
170
171     $ua->get('/');
172
173     $ua->content_contains('Hello! 2!', 'We are logged in.');
174 }
175
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
222 done_testing;