File: external_service.t

package info (click to toggle)
liboauth-lite2-perl 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: perl: 2,658; makefile: 8
file content (179 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use strict;
use warnings;

use lib 't/lib';
use Test::More;

use Plack::Request;
use Try::Tiny;
use TestDataHandler;
use OAuth::Lite2::Server::GrantHandler::ExternalService;
use OAuth::Lite2::Util qw(build_content);

TestDataHandler->clear;
TestDataHandler->add_client(id => q{foo}, secret => q{bar});
TestDataHandler->add_ext_account(assertion => q{assertion_1}, id => q{user_1}, client_id => q{foo});
TestDataHandler->add_ext_account(assertion => q{assertion_2}, id => q{user_1}, client_id => q{foo_2});
TestDataHandler->add_ext_account(assertion => q{assertion_3}, id => q{user_1}, client_id => q{foo}, type => q{type_3});
TestDataHandler->add_ext_account(assertion => q{assertion_4}, id => q{user_1}, client_id => q{foo}, iss  => q{iss_4});
TestDataHandler->add_ext_account(assertion => q{assertion_5}, id => q{user_1}, client_id => q{foo}, aud  => q{aud_5});

my $dh = TestDataHandler->new;

my $auth_info = $dh->create_or_update_auth_info(
    client_id => q{foo},
    user_id   => q{1},
    scope     => q{email},
);

is($auth_info->refresh_token, "refresh_token_0");

my $action = OAuth::Lite2::Server::GrantHandler::ExternalService->new;

sub test_success {
    my $params = shift;
    my $expected = shift;
    my $request = Plack::Request->new({
        REQUEST_URI    => q{http://example.org/resource},
        REQUEST_METHOD => q{GET},
        QUERY_STRING   => build_content($params),
    });
    my $dh = TestDataHandler->new(request => $request);
    my $res; try {
        $res = $action->handle_request($dh);
    } catch {
        my $error_message = ($_->isa("OAuth::Lite2::Error"))
            ? $_->type : $_;
    };

    if(exists $expected->{token}) {
        is($res->{token_type}, $expected->{token_type});
        is($res->{access_token}, $expected->{token});
    } else {
        ok(!$res->{access_token});
    }

    if(exists $expected->{secret}) {
        is($res->{access_token_secret}, $expected->{secret});
    } else {
        ok(!$res->{access_token_secret});
    }

    if(exists $expected->{expires_in}) {
        is($res->{expires_in}, $expected->{expires_in});
    } else {
        ok(!$res->{expires_in});
    }

    if(exists $expected->{refresh_token}) {
        is($res->{refresh_token}, $expected->{refresh_token});
    } else {
        ok(!$res->{refresh_token});
    }
}

sub test_error {
    my $params = shift;
    my $message = shift;
    my $request = Plack::Request->new({
        REQUEST_URI    => q{http://example.org/resource},
        REQUEST_METHOD => q{GET},
        QUERY_STRING   => build_content($params),
    });
    my $dh = TestDataHandler->new(request => $request);
    my $error_message; try {
        my $res = $action->handle_request($dh);
    } catch {
        $error_message = ($_->isa("OAuth::Lite2::Error"))
            ? $_->type : $_;
    };

    like($error_message, qr/$message/);
}

# no assertion
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
}, q{invalid_request});

# invalid assertion
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_invalid},
}, q{invalid_grant});

# client_id mismatch
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_2},
}, q{invalid_grant});

# invalid type
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_3},
}, q{invalid_grant});

# invalid iss
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_4},
}, q{invalid_grant});

# invalid aud
&test_error({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_5},
}, q{invalid_grant});

&test_success({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_1},
}, {
    token_type    => q{Bearer},
    token         => q{access_token_0},
    expires_in    => q{3600},
    refresh_token => q{refresh_token_1},
});
&test_success({
    client_id      => q{foo},
    client_secret  => q{bar},
    assertion      => q{assertion_3},
    type => q{type_3},
}, {
    token_type    => q{Bearer},
    token         => q{access_token_1},
    expires_in    => q{3600},
    refresh_token => q{refresh_token_2},
});
&test_success({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_4},
    iss => q{iss_4},
}, {
    token_type    => q{Bearer},
    token         => q{access_token_2},
    expires_in    => q{3600},
    refresh_token => q{refresh_token_3},
});
&test_success({
    client_id     => q{foo},
    client_secret => q{bar},
    assertion     => q{assertion_5},
    aud => q{aud_5},
}, {
    token_type    => q{Bearer},
    token         => q{access_token_3},
    expires_in    => q{3600},
    refresh_token => q{refresh_token_4},
});

done_testing();