rm t/var before/after test
[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 use IO::All;
17
18 use stemmaweb::Test::DB;
19
20 my $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
26 LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
27
28 my $ua = Test::WWW::Mechanize->new;
29
30 io("$FindBin::Bin/var")->rmtree;
31
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.';
92
93     ok $dir->find_user({ sub => 42, openid_id => 'https://www.google.com/accounts/o8/id' }), 'The G+ user is there.';
94
95     $ua->get('/logout');
96
97     $ua->get_ok('http://localhost/login');
98
99     local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
100         my ( $self, $c, $realm, $authinfo ) = @_;
101
102         return $realm->find_user({
103                 openid_id => 'https://www.google.com/accounts/o8/id',
104                 sub        => 42,
105             }, $c);
106     };
107
108     $ua->submit_form(
109         form_number => 1,
110         fields => {
111             id_token => 'something',
112             email   => 'email@example.org',
113         },
114     );
115
116     $ua->content_contains('You have logged in.', 'We can now log in to our created user');
117
118     $ua->get('/');
119
120     $ua->content_contains('Hello! 42!', 'We are logged in.');
121 }
122
123 # Brand new user just from open id.
124
125 {
126     my $scope = $dir->new_scope;
127
128
129     ok !$dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is not yet there.';
130
131     $ua->get('/logout');
132
133     $ua->get_ok('http://localhost/login');
134
135     local *stemmaweb::Authentication::Credential::Google::authenticate = sub {
136         my ( $self, $c, $realm, $authinfo ) = @_;
137
138         return $realm->find_user({
139                 openid_id => 'https://www.google.com/accounts/o8/id2',
140                 sub        => 2,
141             }, $c);
142     };
143
144     $ua->submit_form(
145         form_number => 1,
146         fields => {
147             id_token => 'something',
148             email   => 'email@example.org',
149         },
150     );
151
152     $ua->content_contains('You have logged in.', 'We can now log in to our created user');
153
154     $ua->get('/');
155
156     $ua->content_contains('Hello! 2!', 'We are logged in.');
157
158     ok $dir->find_user({ sub => 2, openid_id => 'https://www.google.com/accounts/o8/id2' }), 'The G+ user is there.';
159
160     $ua->get('/logout');
161
162     $ua->get_ok('http://localhost/login');
163
164     $ua->submit_form(
165         form_number => 1,
166         fields => {
167             id_token => 'something',
168             email   => 'email@example.org',
169         },
170     );
171
172     $ua->content_contains('You have logged in.', 'We can login again');
173
174     $ua->get('/');
175
176     $ua->content_contains('Hello! 2!', 'We are logged in.');
177 }
178
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
225 io("$FindBin::Bin/var")->rmtree;
226
227 done_testing;