61fe985d2933bba17ffa8e978632fcc7a683f12d
[scpubgit/stemmaweb.git] / t / 06openid.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 my $scope = $dir->new_scope;
22
23 LWP::Protocol::PSGI->register(stemmaweb->psgi_app);
24
25 my $ua = Test::WWW::Mechanize->new;
26
27 $ua->get_ok('http://localhost/login');
28
29 # Trying a user that already exists
30
31 local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
32     my ( $self, $c, $realm, $authinfo ) = @_;
33
34     return $realm->find_user({ url => 'http://localhost/' }, $c);
35 };
36
37 $ua->submit_form(
38     form_number => 2,
39     fields => {
40         openid_identifier => 'http://localhost',
41     },
42 );
43
44 $ua->content_contains('You have logged in.', 'Openid login works');
45
46 $ua->get('/');
47
48 $ua->content_contains('Hello! http://localhost/!', 'We are logged in.');
49
50 $ua->get('/logout');
51
52 # Trying a user that doesn't already exist
53
54 local *Catalyst::Authentication::Credential::OpenID::authenticate = sub {
55     my ( $self, $c, $realm, $authinfo ) = @_;
56
57     return $realm->find_user({ url => 'http://example.org/' }, $c);
58 };
59
60
61 ok !$dir->find_user({ url => 'http://example.org/' }), 'No such user, yet.';
62
63 $ua->get_ok('http://localhost/login');
64
65 $ua->submit_form(
66     form_number => 2,
67     fields => {
68         openid_identifier => 'http://example.org',
69     },
70 );
71
72 $ua->content_contains('You have logged in.', 'Openid login works');
73
74 $ua->get('/');
75
76 $ua->content_contains('Hello! http://example.org/!', 'We are logged in.');
77
78 ok $dir->find_user({ url => 'http://example.org/' }), 'User now exists.';
79
80 $ua->get('/logout');
81
82 $ua->get_ok('http://localhost/login');
83
84 $ua->submit_form(
85     form_number => 2,
86     fields => {
87         openid_identifier => 'http://example.org',
88     },
89 );
90
91 $ua->content_contains('You have logged in.', 'We can now log in to our created user');
92
93 $ua->get('/');
94
95 $ua->content_contains('Hello! http://example.org/!', 'We are logged in.');
96
97 done_testing;