g5_authenticatable 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01d16764300ca8cf106601e9bc00625631f1feb0
4
- data.tar.gz: bba2757fa2298839be4230713b1804adde562198
3
+ metadata.gz: 672c382a3f6ba9af8a13de1e701f267a94ff31c5
4
+ data.tar.gz: 9049ddf0d5819aeb8f4cd3035b63f3316badb70d
5
5
  SHA512:
6
- metadata.gz: 776f2f806c5f29953799728e93503f5947424b426f64f45283b750f290dddbaecece6015c9d2d6acf0b77e3110714b6849ccf5f1754fd3d113deb857dfe8b12c
7
- data.tar.gz: 30d6470860d0f7c0a504ca37d056133a9ed44eb57bebc33a0d2630e2078da0432ce80bb5d31f0402f78577ab4719dcbcf4501e699e2c7e926da4616c232c650e
6
+ metadata.gz: c3b08a2fdac7f7d52ab884128e56aef9e9c9106db56036ef2cc23113faf68fdb1097ff6a8c3304b1a9fd1f34605c723cb944fa09c41868af013185baa8dd141a
7
+ data.tar.gz: 56cb7a3a8dd318d73032742e5fe1a0d7c17acb53729bf47025775605c44a0492a62f3129a3ca2620007318354a3aa79d6eb9b68f2fb08424d10573e01be76398
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.4.1 (2015-01-26)
2
+
3
+ * Fix test helpers when strict token validation is enabled during testing
4
+ ([#24](https://github.com/G5/g5_authenticatable/pull/24))
5
+
1
6
  ## v0.4.0 (2015-01-20)
2
7
 
3
8
  * Several fixes around sign-out, including accepting GET requests and
data/README.md CHANGED
@@ -18,7 +18,7 @@ library in isolation.
18
18
 
19
19
  ## Current Version
20
20
 
21
- 0.4.0
21
+ 0.4.1
22
22
 
23
23
  ## Requirements
24
24
 
@@ -421,6 +421,59 @@ describe 'my secure action' do
421
421
  end
422
422
  ```
423
423
 
424
+ #### Token Validation Helpers ####
425
+
426
+ If you tag your examples with auth metadata (e.g. `:auth`, `:auth_request` or
427
+ `:auth_controller`), then the shared context will automatically take care of
428
+ any stubs required to support strict token validation.
429
+
430
+ However, if you are using the auth test helper methods directly, and you have
431
+ enabled strict token validation, then you will need to use the methods in
432
+ `G5Authenticatable::Test::TokenValidationHelpers` to stub external calls to
433
+ validate the access token.
434
+
435
+ For example, in a feature spec, you could use the `stub_valid_access_token`
436
+ method like so:
437
+
438
+ ```ruby
439
+ describe 'my page' do
440
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
441
+
442
+ before do
443
+ stub_g5_omniauth(user)
444
+ stub_valid_access_token(user.g5_access_token)
445
+ end
446
+
447
+ it 'should let me in'
448
+ end
449
+ ```
450
+
451
+ As another example, in a request spec, you could stub a revoked access
452
+ token using the `stub_invalid_access_token` helper:
453
+
454
+ ```ruby
455
+ describe 'my API call' do
456
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
457
+
458
+ before { login_user }
459
+
460
+ context 'when token becomes invalid after login' do
461
+ before { stub_invalid_access_token(user.g5_access_token) }
462
+
463
+ it 'should return 401'
464
+ end
465
+
466
+ context 'when token remains valid after login' do
467
+ before { stub_valid_access_token(user.g5_access_token) }
468
+
469
+ it 'should return 200'
470
+ end
471
+ end
472
+ ```
473
+
474
+ The same token validation helpers are also available in controller
475
+ specs, or anywhere else that authentication logic may be invoked.
476
+
424
477
  ### Purging local user data
425
478
 
426
479
  G5 Authenticatable automatically maintains user data locally via the
@@ -1,3 +1,4 @@
1
+ require 'g5_authenticatable/test/env_helpers'
1
2
  require 'g5_authenticatable/test/factory'
2
3
  require 'g5_authenticatable/test/token_validation_helpers'
3
4
  require 'g5_authenticatable/test/feature_helpers'
@@ -19,7 +19,11 @@ shared_context 'auth controller', auth_controller: true do
19
19
  include G5Authenticatable::Test::ControllerHelpers
20
20
  let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
21
21
 
22
- before { login_user(user) }
22
+ before do
23
+ stub_valid_access_token(user.g5_access_token)
24
+ login_user(user)
25
+ end
26
+
23
27
  after { logout_user(user) }
24
28
  end
25
29
 
@@ -0,0 +1,17 @@
1
+ module G5Authenticatable
2
+ module Test
3
+ module EnvHelpers
4
+ def stub_env_var(name, value)
5
+ stub_const('ENV', ENV.to_hash.merge(name => value))
6
+ end
7
+ end
8
+ end
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.include G5Authenticatable::Test::EnvHelpers
13
+
14
+ config.before(:each) do
15
+ stub_env_var('G5_AUTH_ENDPOINT', 'https://test.auth.host')
16
+ end
17
+ end
@@ -28,7 +28,10 @@ shared_context 'auth', auth: true do
28
28
 
29
29
  let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
30
30
 
31
- before { stub_g5_omniauth(user) }
31
+ before do
32
+ stub_g5_omniauth(user)
33
+ stub_valid_access_token(user.g5_access_token)
34
+ end
32
35
  end
33
36
 
34
37
  RSpec.configure do |config|
@@ -19,11 +19,6 @@ shared_context 'auth request', auth_request: true do
19
19
 
20
20
  let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
21
21
 
22
- let!(:orig_auth_endpoint) { ENV['G5_AUTH_ENDPOINT'] }
23
- let(:auth_endpoint) { 'https://test.auth.host' }
24
- before { ENV['G5_AUTH_ENDPOINT'] = auth_endpoint }
25
- after { ENV['G5_AUTH_ENDPOINT'] = orig_auth_endpoint }
26
-
27
22
  before do
28
23
  login_user(user)
29
24
  stub_valid_access_token(user.g5_access_token)
@@ -1,3 +1,3 @@
1
1
  module G5Authenticatable
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -17,6 +17,15 @@ describe ::ApplicationController do
17
17
  expect(controller.g5_callback_path(:user)).to eq('/g5_auth/users/auth/g5/callback')
18
18
  end
19
19
 
20
- it_should_behave_like 'a secure controller'
20
+ context 'when strict token validation is enabled' do
21
+ before { G5Authenticatable.strict_token_validation = true }
21
22
 
23
+ it_should_behave_like 'a secure controller'
24
+ end
25
+
26
+ context 'when strict token validation is disabled' do
27
+ before { G5Authenticatable.strict_token_validation = false }
28
+
29
+ it_should_behave_like 'a secure controller'
30
+ end
22
31
  end
@@ -1,28 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'UI Token validation' do
4
- let!(:old_auth_endpoint) { ENV['G5_AUTH_ENDPOINT'] }
5
- before { ENV['G5_AUTH_ENDPOINT'] = auth_endpoint }
6
- after { ENV['G5_AUTH_ENDPOINT'] = old_auth_endpoint }
7
- let(:auth_endpoint) { 'https://auth.test.host' }
8
-
9
- let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
10
-
11
- before do
12
- stub_g5_omniauth(user)
13
- visit protected_page_path
14
-
15
- # Now that we're logged in, any subsequent attempts to
16
- # authenticate with the auth server will trigger an omniauth
17
- # failure, which is a condition we can test for
18
- stub_g5_invalid_credentials
19
- end
20
-
21
4
  context 'when token validation is enabled' do
22
5
  before { G5Authenticatable.strict_token_validation = true }
23
6
 
24
7
  context 'when user has a valid g5 access token' do
25
- before { stub_valid_access_token(user.g5_access_token) }
8
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
9
+
10
+ before do
11
+ stub_g5_omniauth(user)
12
+ stub_valid_access_token(user.g5_access_token)
13
+ visit protected_page_path
14
+
15
+ # Now that we're logged in, any subsequent attempts to
16
+ # authenticate with the auth server will trigger an omniauth
17
+ # failure, which is a condition we can test for
18
+ stub_g5_invalid_credentials
19
+ end
26
20
 
27
21
  it 'should allow the user to visit a protected page' do
28
22
  visit protected_page_path
@@ -30,22 +24,63 @@ describe 'UI Token validation' do
30
24
  end
31
25
  end
32
26
 
33
- context 'when user has an invalid g5 access token' do
34
- before { stub_invalid_access_token(user.g5_access_token) }
27
+ context 'when user access token becomes invalid' do
28
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
29
+
30
+ before do
31
+ # User access token is valid at sign in
32
+ stub_g5_omniauth(user)
33
+ stub_valid_access_token(user.g5_access_token)
34
+ visit protected_page_path
35
+
36
+ # User access token has become invalid, and
37
+ # any subsequent attempts to authenticate will trigger
38
+ # an omniauth error
39
+ stub_invalid_access_token(user.g5_access_token)
40
+ stub_g5_invalid_credentials
41
+ end
35
42
 
36
43
  it 'should force the user to re-authenticate' do
37
44
  visit protected_page_path
38
45
  expect(current_path).to_not eq(protected_page_path)
39
46
  end
40
47
  end
48
+
49
+ context 'when using the :auth shared context', :auth do
50
+ it 'should allow the user to visit a protected page' do
51
+ visit protected_page_path
52
+ expect(current_path).to eq(protected_page_path)
53
+ end
54
+ end
41
55
  end
42
56
 
43
57
  context 'when token validation is disabled' do
44
58
  before { G5Authenticatable.strict_token_validation = false }
45
59
 
46
- it 'should allow the user to visit a protected page' do
47
- visit protected_page_path
48
- expect(current_path).to eq(protected_page_path)
60
+ context 'when using the :auth shared context', :auth do
61
+ it 'should allow the user to visit a protected page' do
62
+ visit protected_page_path
63
+ expect(current_path).to eq(protected_page_path)
64
+ end
65
+ end
66
+
67
+ context 'when user access token has become invalid' do
68
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
69
+
70
+ before do
71
+ stub_g5_omniauth(user)
72
+ visit protected_page_path
73
+
74
+ # Now that we're already logged in, invalidate the
75
+ # access token
76
+ stub_g5_invalid_credentials
77
+ stub_invalid_access_token(user.g5_access_token)
78
+ end
79
+
80
+ it 'should allow the user to visit a protected page' do
81
+ visit protected_page_path
82
+ expect(current_path).to eq(protected_page_path)
83
+ end
49
84
  end
50
85
  end
51
86
  end
@@ -5,12 +5,8 @@ require 'spec_helper'
5
5
  # with mocks for external redirects (the capybara-mechanize driver
6
6
  # comes closest, but not quite)
7
7
  describe 'Signing out' do
8
- before { ENV['G5_AUTH_ENDPOINT'] = auth_endpoint }
9
- after { ENV['G5_AUTH_ENDPOINT'] = nil }
10
- let(:auth_endpoint) { 'https://auth.test.host' }
11
-
12
8
  let(:auth_sign_out_url) do
13
- "#{auth_endpoint}/users/sign_out?redirect_url=http%3A%2F%2Fwww.example.com%2F"
9
+ "#{ENV['G5_AUTH_ENDPOINT']}/users/sign_out?redirect_url=http%3A%2F%2Fwww.example.com%2F"
14
10
  end
15
11
 
16
12
  describe 'GET /g5_auth/users/sign_out' do
@@ -1,16 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'API Token validation' do
4
- let!(:old_auth_endpoint) { ENV['G5_AUTH_ENDPOINT'] }
5
- before { ENV['G5_AUTH_ENDPOINT'] = auth_endpoint }
6
- after { ENV['G5_AUTH_ENDPOINT'] = old_auth_endpoint }
7
- let(:auth_endpoint) { 'https://auth.test.host' }
8
-
9
- let(:token_info_url) { URI.join(auth_endpoint, '/oauth/token/info') }
10
-
11
- let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
12
- before { login_user(user) }
13
- after { logout_user }
4
+ let(:token_info_url) { URI.join(ENV['G5_AUTH_ENDPOINT'], '/oauth/token/info') }
14
5
 
15
6
  subject(:api_call) { get '/rails_api/secure_resource.json' }
16
7
 
@@ -18,7 +9,14 @@ describe 'API Token validation' do
18
9
  before { G5Authenticatable.strict_token_validation = true }
19
10
 
20
11
  context 'when user has a valid g5 access token' do
21
- before { stub_valid_access_token(user.g5_access_token) }
12
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
13
+
14
+ before do
15
+ login_user(user)
16
+ stub_valid_access_token(user.g5_access_token)
17
+ end
18
+
19
+ after { logout_user }
22
20
 
23
21
  it 'should allow the user to make the api call' do
24
22
  api_call
@@ -27,21 +25,53 @@ describe 'API Token validation' do
27
25
  end
28
26
 
29
27
  context 'when user has an invalid g5 access token' do
30
- before { stub_invalid_access_token(user.g5_access_token) }
28
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
29
+
30
+ before do
31
+ login_user(user)
32
+ stub_invalid_access_token(user.g5_access_token)
33
+ end
34
+
35
+ after { logout_user }
31
36
 
32
37
  it 'should return a 401' do
33
38
  api_call
34
39
  expect(response).to be_http_unauthorized
35
40
  end
36
41
  end
42
+
43
+ context 'with the :auth_request shared context', :auth_request do
44
+ it 'should allow the user to make the api call' do
45
+ api_call
46
+ expect(response).to be_success
47
+ end
48
+ end
37
49
  end
38
50
 
39
51
  context 'when token validation is disabled' do
40
52
  before { G5Authenticatable.strict_token_validation = false }
41
53
 
42
- it 'should allow the user to make the api call' do
43
- api_call
44
- expect(response).to be_success
54
+ context 'when the user has an invalid g5 access token' do
55
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
56
+
57
+ before do
58
+ login_user(user)
59
+ stub_invalid_access_token(user.g5_access_token)
60
+ end
61
+
62
+ after { logout_user }
63
+
64
+ it 'should allow the user to make the api call' do
65
+ api_call
66
+ expect(response).to be_success
67
+ end
68
+ end
69
+
70
+ context 'with the :auth_request shared context', :auth_request do
71
+ it 'should allow the user to make the api call' do
72
+ api_call
73
+ expect(response).to be_success
74
+ end
45
75
  end
46
76
  end
47
77
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: g5_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-21 00:00:00.000000000 Z
11
+ date: 2015-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise_g5_authenticatable
@@ -76,6 +76,7 @@ files:
76
76
  - lib/g5_authenticatable/engine.rb
77
77
  - lib/g5_authenticatable/rspec.rb
78
78
  - lib/g5_authenticatable/test/controller_helpers.rb
79
+ - lib/g5_authenticatable/test/env_helpers.rb
79
80
  - lib/g5_authenticatable/test/factory.rb
80
81
  - lib/g5_authenticatable/test/feature_helpers.rb
81
82
  - lib/g5_authenticatable/test/request_helpers.rb