rails_api_auth 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,26 +3,73 @@ describe 'Oauth2 API' do
3
3
 
4
4
  describe 'POST /token' do
5
5
  let(:params) { { grant_type: 'password', username: login.identification, password: login.password } }
6
+ subject { post '/token', params, 'HTTPS' => ssl }
6
7
 
7
- subject { post '/token', params }
8
+ shared_examples 'when the request gets through' do
9
+ context 'for grant_type "password"' do
10
+ context 'with valid login credentials' do
11
+ it 'responds with status 200' do
12
+ subject
8
13
 
9
- context 'for grant_type "password"' do
10
- context 'with valid login credentials' do
11
- it 'responds with status 200' do
12
- subject
14
+ expect(response).to have_http_status(200)
15
+ end
13
16
 
14
- expect(response).to have_http_status(200)
17
+ it 'responds with an access token' do
18
+ subject
19
+
20
+ expect(response.body).to be_json_eql({ access_token: login.oauth2_token }.to_json)
21
+ end
15
22
  end
16
23
 
17
- it 'responds with an access token' do
18
- subject
24
+ context 'with invalid login credentials' do
25
+ let(:params) { { grant_type: 'password', username: login.identification, password: 'badpassword' } }
26
+
27
+ it 'responds with status 400' do
28
+ subject
29
+
30
+ expect(response).to have_http_status(400)
31
+ end
32
+
33
+ it 'responds with an invalid grant error' do
34
+ subject
35
+
36
+ expect(response.body).to be_json_eql({ error: 'invalid_grant' }.to_json)
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'for grant_type "facebook_auth_code"' do
42
+ let(:authenticated_user_data) do
43
+ {
44
+ id: '1238190321',
45
+ email: email
46
+ }
47
+ end
48
+ let(:uid_mapped_field) { 'id' }
49
+ let(:grant_type) { 'facebook_auth_code' }
50
+ let(:profile_url) { FacebookAuthenticator::PROFILE_URL }
51
+
52
+ include_context 'stubbed facebook requests'
53
+ include_examples 'oauth2 shared contexts'
54
+ end
19
55
 
20
- expect(response.body).to be_json_eql({ access_token: login.oauth2_token }.to_json)
56
+ context 'for grant_type "google_auth_code"' do
57
+ let(:authenticated_user_data) do
58
+ {
59
+ sub: '1238190321',
60
+ email: email
61
+ }
21
62
  end
63
+ let(:uid_mapped_field) { 'sub' }
64
+ let(:grant_type) { 'google_auth_code' }
65
+ let(:profile_url) { GoogleAuthenticator::PROFILE_URL }
66
+
67
+ include_context 'stubbed google requests'
68
+ include_examples 'oauth2 shared contexts'
22
69
  end
23
70
 
24
- context 'with invalid login credentials' do
25
- let(:params) { { grant_type: 'password', username: login.identification, password: 'badpassword' } }
71
+ context 'for an unknown grant type' do
72
+ let(:params) { { grant_type: 'UNKNOWN' } }
26
73
 
27
74
  it 'responds with status 400' do
28
75
  subject
@@ -30,89 +77,122 @@ describe 'Oauth2 API' do
30
77
  expect(response).to have_http_status(400)
31
78
  end
32
79
 
33
- it 'responds with an invalid grant error' do
80
+ it 'responds with an "unsupported_grant_type" error' do
34
81
  subject
35
82
 
36
- expect(response.body).to be_json_eql({ error: 'invalid_grant' }.to_json)
83
+ expect(response.body).to be_json_eql({ error: 'unsupported_grant_type' }.to_json)
37
84
  end
38
85
  end
39
86
  end
40
87
 
41
- context 'for grant_type "facebook_auth_code"' do
42
- let(:authenticated_user_data) do
43
- {
44
- id: '1238190321',
45
- email: email
46
- }
88
+ context 'when SSL is forced' do
89
+ include_context 'with force_ssl configured'
90
+ let(:force_ssl) { true }
91
+
92
+ context 'and the request uses SSL' do
93
+ let(:ssl) { 'on' }
94
+
95
+ include_examples 'when the request gets through'
47
96
  end
48
- let(:uid_mapped_field) { 'id' }
49
- let(:grant_type) { 'facebook_auth_code' }
50
- let(:profile_url) { FacebookAuthenticator::PROFILE_URL }
51
- include_context 'stubbed facebook requests'
52
- it_behaves_like 'oauth2 shared contexts'
53
- end
54
97
 
55
- context 'for grant_type "google_auth_code"' do
56
- let(:authenticated_user_data) do
57
- {
58
- sub: '1238190321',
59
- email: email
60
- }
98
+ context 'and the request does not use SSL' do
99
+ let(:ssl) { false }
100
+
101
+ it 'responds with status 301' do
102
+ subject
103
+
104
+ expect(response).to have_http_status(301)
105
+ end
61
106
  end
62
- let(:uid_mapped_field) { 'sub' }
63
- let(:grant_type) { 'google_auth_code' }
64
- let(:profile_url) { GoogleAuthenticator::PROFILE_URL }
65
- include_context 'stubbed google requests'
66
- it_behaves_like 'oauth2 shared contexts'
67
107
  end
68
108
 
69
- context 'for an unknown grant type' do
70
- let(:params) { { grant_type: 'UNKNOWN' } }
109
+ context 'when SSL is not forced' do
110
+ include_context 'with force_ssl configured'
111
+ let(:force_ssl) { false }
71
112
 
72
- it 'responds with status 400' do
73
- subject
113
+ context 'and the request uses SSL' do
114
+ let(:ssl) { 'on' }
74
115
 
75
- expect(response).to have_http_status(400)
116
+ include_examples 'when the request gets through'
76
117
  end
77
118
 
78
- it 'responds with an "unsupported_grant_type" error' do
79
- subject
119
+ context 'and the request does not use SSL' do
120
+ let(:ssl) { false }
80
121
 
81
- expect(response.body).to be_json_eql({ error: 'unsupported_grant_type' }.to_json)
122
+ include_examples 'when the request gets through'
82
123
  end
83
124
  end
84
125
  end
85
126
 
86
127
  describe 'POST #destroy' do
87
128
  let(:params) { { token_type_hint: 'access_token', token: login.oauth2_token } }
129
+ subject { post '/revoke', params, 'HTTPS' => ssl }
130
+
131
+ shared_examples 'when the request gets through' do
132
+ it 'responds with status 200' do
133
+ subject
88
134
 
89
- subject { post '/revoke', params }
135
+ expect(response).to have_http_status(200)
136
+ end
137
+
138
+ it "resets the login's OAuth 2.0 token" do
139
+ expect { subject }.to change { login.reload.oauth2_token }
140
+
141
+ subject
142
+ end
143
+
144
+ context 'for an invalid token' do
145
+ let(:params) { { token_type_hint: 'access_token', token: 'badtoken' } }
90
146
 
91
- it 'responds with status 200' do
92
- subject
147
+ it 'responds with status 200' do
148
+ subject
93
149
 
94
- expect(response).to have_http_status(200)
150
+ expect(response).to have_http_status(200)
151
+ end
152
+
153
+ it "doesn't reset any logins' token" do
154
+ expect_any_instance_of(LoginNotFound).to receive(:refresh_oauth2_token!)
155
+
156
+ subject
157
+ end
158
+ end
95
159
  end
96
160
 
97
- it "resets the login's OAuth 2.0 token" do
98
- expect { subject }.to change { login.reload.oauth2_token }
161
+ context 'when SSL is forced' do
162
+ include_context 'with force_ssl configured'
163
+ let(:force_ssl) { true }
164
+
165
+ context 'and the request uses SSL' do
166
+ let(:ssl) { 'on' }
167
+
168
+ include_examples 'when the request gets through'
169
+ end
170
+
171
+ context 'and the request does not use SSL' do
172
+ let(:ssl) { false }
173
+
174
+ it 'responds with status 301' do
175
+ subject
99
176
 
100
- subject
177
+ expect(response).to have_http_status(301)
178
+ end
179
+ end
101
180
  end
102
181
 
103
- context 'for an invalid token' do
104
- let(:params) { { token_type_hint: 'access_token', token: 'badtoken' } }
182
+ context 'when SSL is not forced' do
183
+ include_context 'with force_ssl configured'
184
+ let(:force_ssl) { false }
105
185
 
106
- it 'responds with status 200' do
107
- subject
186
+ context 'and the request uses SSL' do
187
+ let(:ssl) { 'on' }
108
188
 
109
- expect(response).to have_http_status(200)
189
+ include_examples 'when the request gets through'
110
190
  end
111
191
 
112
- it "doesn't reset any logins' token" do
113
- expect_any_instance_of(LoginNotFound).to receive(:refresh_oauth2_token!)
192
+ context 'and the request does not use SSL' do
193
+ let(:ssl) { false }
114
194
 
115
- subject
195
+ include_examples 'when the request gets through'
116
196
  end
117
197
  end
118
198
  end
@@ -0,0 +1,8 @@
1
+ shared_context 'with force_ssl configured' do
2
+ around do |example|
3
+ default_force_ssl = RailsApiAuth.force_ssl
4
+ RailsApiAuth.force_ssl = force_ssl
5
+ example.run
6
+ RailsApiAuth.force_ssl = default_force_ssl
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_api_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Otte-Witte
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-16 00:00:00.000000000 Z
12
+ date: 2016-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -84,6 +84,7 @@ files:
84
84
  - lib/rails_api_auth/authentication.rb
85
85
  - lib/rails_api_auth/engine.rb
86
86
  - lib/rails_api_auth/version.rb
87
+ - spec/config/force_ssl_spec.rb
87
88
  - spec/dummy/README.rdoc
88
89
  - spec/dummy/Rakefile
89
90
  - spec/dummy/app/assets/javascripts/application.js
@@ -139,6 +140,7 @@ files:
139
140
  - spec/services/google_authenticator_spec.rb
140
141
  - spec/spec_helper.rb
141
142
  - spec/support/factory_girl.rb
143
+ - spec/support/shared_contexts/force_ssl.rb
142
144
  - spec/support/shared_contexts/stubbed_facebook_requests.rb
143
145
  - spec/support/shared_contexts/stubbed_google_requests.rb
144
146
  - spec/support/shared_examples/authenticator_shared_requests.rb
@@ -168,6 +170,7 @@ signing_key:
168
170
  specification_version: 4
169
171
  summary: Engine that implements OAuth 2.0 and Facebook authentication for API projects
170
172
  test_files:
173
+ - spec/config/force_ssl_spec.rb
171
174
  - spec/dummy/app/assets/javascripts/application.js
172
175
  - spec/dummy/app/assets/stylesheets/application.css
173
176
  - spec/dummy/app/controllers/access_once_controller.rb
@@ -223,6 +226,7 @@ test_files:
223
226
  - spec/services/google_authenticator_spec.rb
224
227
  - spec/spec_helper.rb
225
228
  - spec/support/factory_girl.rb
229
+ - spec/support/shared_contexts/force_ssl.rb
226
230
  - spec/support/shared_contexts/stubbed_facebook_requests.rb
227
231
  - spec/support/shared_contexts/stubbed_google_requests.rb
228
232
  - spec/support/shared_examples/authenticator_shared_requests.rb