forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization_spec.rb
More file actions
172 lines (137 loc) · 4.86 KB
/
authorization_spec.rb
File metadata and controls
172 lines (137 loc) · 4.86 KB
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
# encoding: utf-8
require 'spec_helper'
describe Github::Authorization do
let(:client_id) { '234jl23j4l23j4l' }
let(:client_secret) { 'asasd79sdf9a7asfd7sfd97s' }
let(:code) { 'c9798sdf97df98ds'}
let(:github) {
Github.new :client_id => client_id, :client_secret => client_secret
}
after do
github.client_id, github.client_secret = nil, nil
reset_authentication_for github
end
context '.client' do
it { github.should respond_to :client }
it "should return OAuth2::Client instance" do
github.client.should be_a OAuth2::Client
end
it "should assign site from the options hash" do
github.client.site.should == 'https://github.com'
end
it "should assign 'authorize_url" do
github.client.authorize_url.should == 'https://github.com/login/oauth/authorize'
end
it "should assign 'token_url" do
github.client.token_url.should == 'https://github.com/login/oauth/access_token'
end
end
context '.auth_code' do
let(:oauth) { OAuth2::Client.new(client_id, client_secret) }
it "should throw an error if no client_id" do
github.client_id = nil
expect { github.auth_code }.should raise_error(ArgumentError)
end
it "should throw an error if no client_secret" do
github.client_secret = nil
expect { github.auth_code }.should raise_error(ArgumentError)
end
it "should return authentication token code" do
github.client_id = client_id
github.client_secret = client_secret
github.client.stub(:auth_code).and_return code
github.auth_code.should == code
end
end
context "authorize_url" do
it "should respond to 'authorize_url' " do
github.should respond_to :authorize_url
end
it "should return address containing client_id" do
github.authorize_url.should =~ /client_id=#{client_id}/
end
it "should return address containing scopes" do
github.authorize_url(:scope => 'user').should =~ /scope=user/
end
it "should return address containing redirect_uri" do
github.authorize_url(:redirect_uri => 'http://localhost').should =~ /redirect_uri/
end
end
context "get_token" do
before do
stub_request(:post, 'https://github.com/login/oauth/access_token').
to_return(:body => '', :status => 200, :headers => {})
end
it "should respond to 'get_token' " do
github.should respond_to :get_token
end
it "should make the authorization request" do
expect {
github.get_token code
a_request(:post, "https://github.com/login/oauth/access_token").should have_been_made
}.to raise_error(OAuth2::Error)
end
it "should fail to get_token without authorization code" do
expect { github.get_token }.to raise_error(ArgumentError)
end
end
context ".authenticated?" do
it { github.should respond_to :authenticated? }
it "should return false if falied on basic authentication" do
github.stub(:basic_authed?).and_return false
github.authenticated?.should be_false
end
it "should return true if basic authentication performed" do
github.stub(:basic_authed?).and_return true
github.authenticated?.should be_true
end
it "should return true if basic authentication performed" do
github.stub(:oauth_token?).and_return true
github.authenticated?.should be_true
end
end
context ".basic_authed?" do
before do
github.stub(:basic_auth?).and_return false
end
it { github.should respond_to :basic_authed? }
it "should return false if login is missing" do
github.stub(:login?).and_return false
github.basic_authed?.should be_false
end
it "should return true if login && password provided" do
github.stub(:login?).and_return true
github.stub(:password?).and_return true
github.basic_authed?.should be_true
end
end
context "authentication" do
it "should respond to 'authentication'" do
github.should respond_to :authentication
end
it "should return empty hash if no basic authentication params available" do
github.stub(:login?).and_return false
github.stub(:basic_auth?).and_return false
github.authentication.should be_empty
end
context 'basic_auth' do
before do
github = Github.new :basic_auth => 'github:pass'
end
it "should return hash with basic auth params" do
github.authentication.should be_a Hash
github.authentication.should have_key :basic_auth
end
end
context 'login & password' do
before do
github = Github.new :login => 'github', :password => 'pass'
github.basic_auth = nil
end
it "should return hash with login & password params" do
github.authentication.should be_a Hash
github.authentication.should have_key :login
end
end
end # authentication
end # Github::Authorization