forked from mmangino/mogli
/
authenticator_spec.rb
92 lines (74 loc) · 3.47 KB
/
authenticator_spec.rb
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
require "spec_helper"
describe Mogli::Authenticator do
let :authenticator do
Mogli::Authenticator.new("123456","secret","http://example.com/url")
end
it "has the client id" do
authenticator.client_id.should == "123456"
end
it "has the secret" do
authenticator.secret.should == "secret"
end
it "has the callback url" do
authenticator.callback_url.should == "http://example.com/url"
end
it "creates the authorize_url" do
authenticator.authorize_url.should == "https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl"
end
it "creates the authorize_url with scopes as an array" do
authenticator.authorize_url(:scope=>[:user_photos,:user_videos,:stream_publish]).should ==
"https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&scope=user_photos,user_videos,stream_publish"
end
it "creates the access_token_url" do
authenticator.access_token_url("mycode").should == "https://graph.facebook.com/oauth/access_token?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&client_secret=secret&code=mycode"
end
it "can trade session_keys for access_tokens" do
response = [{"access_token" => "myaccesstoken", "expires" => 5000},
{"access_token" => "youraccesstoken", "expires" => 5000}]
response.stub!(:code).and_return(200)
Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/exchange_sessions",
:body => {:type => "client_cred", :client_id => "123456",
:client_secret => "secret",
:sessions => "mysessionkey,yoursessionkey"}).
and_return(response)
authenticator.
get_access_token_for_session_key(["mysessionkey","yoursessionkey"]).
should == [{"access_token" => "myaccesstoken", "expires" => 5000},
{"access_token" => "youraccesstoken", "expires" => 5000}]
end
it "can trade one session_key for an access_tokens" do
response = {"access_token" => "myaccesstoken", "expires" => 5000}
response.stub!(:code).and_return(200)
Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/exchange_sessions",
:body => {:type => "client_cred", :client_id => "123456",
:client_secret => "secret", :sessions => "mysessionkey"}).
and_return(response)
authenticator.
get_access_token_for_session_key("mysessionkey").
should == {"access_token" => "myaccesstoken", "expires" => 5000}
end
it "can ask for an application token" do
response = "access_token=123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
response.stub!(:code).and_return(200)
Mogli::Client.should_receive(:post).
with("https://graph.facebook.com/oauth/access_token",
:body=> {
:grant_type => 'client_credentials',
:client_id => "123456",
:client_secret => "secret"
}).
and_return(response)
token = authenticator.get_access_token_for_application
token.should =="123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
end
it "raises an error if not a 200" do
response = "access_token=123456|3SDdfgdfgv0bbEvYjBH5tJtl-dcBdsfgo"
response.stub!(:code).and_return(500)
Mogli::Client.should_receive(:post).and_return(response)
lambda do
token = authenticator.get_access_token_for_application
end.should raise_error(Mogli::Client::HTTPException)
end
end