-
Notifications
You must be signed in to change notification settings - Fork 5
/
02_get_user_token.t
153 lines (138 loc) · 4.71 KB
/
02_get_user_token.t
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
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Test::Mock::Furl;
use Facebook::OpenGraph;
use JSON 2 qw(encode_json);
subtest 'success' => sub {
my $code = 'XXXXXXXXXXXXXXXXXXXXXX';
my $app_id = 123456789;
my $token = '123456789XXXXXXXXXXX';
my $expires = 5183814;
$Mock_furl_http->mock(
request => sub {
my ($mock, %args) = @_;
is_deeply $args{headers}, [], 'no particular header';
my $uri = $args{url};
is $uri->scheme, 'https', 'scheme';
is $uri->host, 'graph.facebook.com', 'host';
is $uri->path, '/oauth/access_token', 'path';
is_deeply(
+{
$uri->query_form,
},
+{
client_secret => 'secret',
client_id => $app_id,
code => $code,
redirect_uri => 'http://sample.com/auth_cb',
},
'query',
);
is $args{method}, 'GET', 'HTTP GET method';
is $args{content}, '', 'content';
return (
1,
200,
'OK',
[
'Content-Type' => 'text/plain; charset=UTF-8',
'facebook-api-version' => 'v2.2',
],
sprintf('access_token=%s&expires=%d', $token, $expires),
);
},
);
# Redirect_uri should be exactly the same as the one
# that you used on $fb->auth_uri
my $fb = Facebook::OpenGraph->new(+{
app_id => $app_id,
secret => 'secret',
redirect_uri => 'http://sample.com/auth_cb',
});
my $token_ref = $fb->get_user_token_by_code($code);
is_deeply(
$token_ref,
+{
access_token => $token,
expires => $expires,
},
'token',
);
is $fb->access_token, undef, 'no access_token is set';
$fb->set_access_token($token_ref->{access_token});
is $fb->access_token, $token, 'acess token is set';
};
subtest 'using v2.3' => sub {
# https://developers.facebook.com/docs/apps/changelog#v2_3_changes
# he response format of https://www.facebook.com/v2.3/oauth/access_token
# returned when you exchange a code for an access_token now return valid
# JSON instead of being URL encoded. The new format of this response is
# {"access_token": <TOKEN>, "token_type":<TYPE>, "expires_in":<TIME>}.
# We made this update to be compliant with section 5.1 of RFC 6749.
my $code = 'XXXXXXXXXXXXXXXXXXXXXX';
my $app_id = 123456789;
my $token = '123456789XXXXXXXXXXX';
my $expires = 5183814;
my $token_type = 'bearer';
$Mock_furl_http->mock(
request => sub {
my ($mock, %args) = @_;
is_deeply $args{headers}, [], 'no particular header';
my $uri = $args{url};
is $uri->scheme, 'https', 'scheme';
is $uri->host, 'graph.facebook.com', 'host';
is $uri->path, '/oauth/access_token', 'path';
is_deeply(
+{
$uri->query_form,
},
+{
client_secret => 'secret',
client_id => $app_id,
code => $code,
redirect_uri => 'http://sample.com/auth_cb',
},
'query',
);
is $args{method}, 'GET', 'HTTP GET method';
is $args{content}, '', 'content';
return (
1,
200,
'OK',
[
'Content-Type' => 'text/plain; charset=UTF-8',
'facebook-api-version' => 'v2.3',
],
encode_json(+{
access_token => $token,
expires_in => $expires,
token_type => $token_type,
})
);
},
);
# Redirect_uri should be exactly the same as the one
# that you used on $fb->auth_uri
my $fb = Facebook::OpenGraph->new(+{
app_id => $app_id,
secret => 'secret',
redirect_uri => 'http://sample.com/auth_cb',
});
my $token_ref = $fb->get_user_token_by_code($code);
is_deeply(
$token_ref,
+{
access_token => $token,
expires_in => $expires,
token_type => $token_type,
},
'token',
);
is $fb->access_token, undef, 'no access_token is set';
$fb->set_access_token($token_ref->{access_token});
is $fb->access_token, $token, 'acess token is set';
};
done_testing;