/
controller_oauth2_spec.rb
244 lines (212 loc) · 10.4 KB
/
controller_oauth2_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../../shared_examples/controller_oauth2_shared_examples')
def stub_all_oauth2_requests!
auth_code = OAuth2::Strategy::AuthCode.any_instance
access_token = mock(OAuth2::AccessToken)
access_token.stub(:token_param=)
response = mock(OAuth2::Response)
response.stub(:body).and_return({
"id"=>"123",
"name"=>"Noam Ben Ari",
"first_name"=>"Noam",
"last_name"=>"Ben Ari",
"link"=>"http://www.facebook.com/nbenari1",
"hometown"=>{"id"=>"110619208966868", "name"=>"Haifa, Israel"},
"location"=>{"id"=>"106906559341067", "name"=>"Pardes Hanah, Hefa, Israel"},
"bio"=>"I'm a new daddy, and enjoying it!",
"gender"=>"male",
"email"=>"nbenari@gmail.com",
"timezone"=>2,
"locale"=>"en_US",
"languages"=>[{"id"=>"108405449189952", "name"=>"Hebrew"}, {"id"=>"106059522759137", "name"=>"English"}, {"id"=>"112624162082677", "name"=>"Russian"}],
"verified"=>true,
"updated_time"=>"2011-02-16T20:59:38+0000"}.to_json)
access_token.stub(:get).and_return(response)
auth_code.stub(:get_token).and_return(access_token)
end
describe ApplicationController do
before(:all) do
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/external")
sorcery_reload!([:external])
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:github, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:github, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:github, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
end
after(:all) do
ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/external")
end
# ----------------- OAuth -----------------------
describe ApplicationController, "with OAuth features" do
before(:each) do
stub_all_oauth2_requests!
end
after(:each) do
User.delete_all
Authentication.delete_all
end
it "login_at redirects correctly" do
create_new_user
get :login_at_test2
response.should be_a_redirect
response.should redirect_to("https://graph.facebook.com/oauth/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.facebook.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=email%2Coffline_access&display=page")
end
it "'login_from' logins if user exists" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:facebook)
get :test_login_from2
flash[:notice].should == "Success!"
end
it "'login_from' fails if user doesn't exist" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_user
get :test_login_from2
flash[:alert].should == "Failed!"
end
# provider: github
it "login_at redirects correctly (github)" do
create_new_user
get :login_at_test3
response.should be_a_redirect
response.should redirect_to("https://github.com/login/oauth/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.github.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=&display=")
end
it "'login_from' logins if user exists (github)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:github)
get :test_login_from3
flash[:notice].should == "Success!"
end
it "'login_from' fails if user doesn't exist (github)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_user
get :test_login_from3
flash[:alert].should == "Failed!"
end
# provider: google
it "login_at redirects correctly (google)" do
create_new_user
get :login_at_test4
response.should be_a_redirect
response.should redirect_to("https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=#{::Sorcery::Controller::Config.google.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&display=")
end
it "'login_from' logins if user exists (google)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:google)
get :test_login_from4
flash[:notice].should == "Success!"
end
it "'login_from' fails if user doesn't exist (google)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_user
get :test_login_from4
flash[:alert].should == "Failed!"
end
# provider: liveid
it "login_at redirects correctly (liveid)" do
create_new_user
get :login_at_test5
response.should be_a_redirect
response.should redirect_to("https://oauth.live.com/authorize?response_type=code&client_id=#{::Sorcery::Controller::Config.liveid.key}&redirect_uri=http%3A%2F%2Fblabla.com&scope=wl.basic%20wl.emails%20wl.offline_access&display=")
end
it "'login_from' logins if user exists (liveid)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:liveid)
get :test_login_from5
flash[:notice].should == "Success!"
end
it "'login_from' fails if user doesn't exist (liveid)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_user
get :test_login_from5
flash[:alert].should == "Failed!"
end
end
describe ApplicationController do
it_behaves_like "oauth2_controller"
end
describe ApplicationController, "OAuth with User Activation features" do
before(:all) do
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate/activation")
sorcery_reload!([:user_activation,:external], :user_activation_mailer => ::SorceryMailer)
sorcery_controller_property_set(:external_providers, [:facebook, :github, :google, :liveid])
sorcery_controller_external_property_set(:facebook, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:facebook, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:facebook, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:github, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:github, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:github, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:google, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:google, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:google, :callback_url, "http://blabla.com")
sorcery_controller_external_property_set(:liveid, :key, "eYVNBjBDi33aa9GkA3w")
sorcery_controller_external_property_set(:liveid, :secret, "XpbeSdCoaKSmQGSeokz5qcUATClRW5u08QWNfv71N8")
sorcery_controller_external_property_set(:liveid, :callback_url, "http://blabla.com")
end
after(:all) do
ActiveRecord::Migrator.rollback("#{Rails.root}/db/migrate/activation")
end
after(:each) do
User.delete_all
end
it "should not send activation email to external users" do
old_size = ActionMailer::Base.deliveries.size
create_new_external_user(:facebook)
ActionMailer::Base.deliveries.size.should == old_size
end
it "should not send external users an activation success email" do
sorcery_model_property_set(:activation_success_email_method_name, nil)
create_new_external_user(:facebook)
old_size = ActionMailer::Base.deliveries.size
@user.activate!
ActionMailer::Base.deliveries.size.should == old_size
end
# provider: github
it "should not send activation email to external users (github)" do
old_size = ActionMailer::Base.deliveries.size
create_new_external_user(:github)
ActionMailer::Base.deliveries.size.should == old_size
end
it "should not send external users an activation success email (github)" do
sorcery_model_property_set(:activation_success_email_method_name, nil)
create_new_external_user(:github)
old_size = ActionMailer::Base.deliveries.size
@user.activate!
ActionMailer::Base.deliveries.size.should == old_size
end
# provider: google
it "should not send activation email to external users (google)" do
old_size = ActionMailer::Base.deliveries.size
create_new_external_user(:google)
ActionMailer::Base.deliveries.size.should == old_size
end
it "should not send external users an activation success email (google)" do
sorcery_model_property_set(:activation_success_email_method_name, nil)
create_new_external_user(:google)
old_size = ActionMailer::Base.deliveries.size
@user.activate!
ActionMailer::Base.deliveries.size.should == old_size
end
# provider: liveid
it "should not send activation email to external users (liveid)" do
old_size = ActionMailer::Base.deliveries.size
create_new_external_user(:liveid)
ActionMailer::Base.deliveries.size.should == old_size
end
it "should not send external users an activation success email (liveid)" do
sorcery_model_property_set(:activation_success_email_method_name, nil)
create_new_external_user(:liveid)
old_size = ActionMailer::Base.deliveries.size
@user.activate!
ActionMailer::Base.deliveries.size.should == old_size
end
end
end