gun_broker 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 337ab5362759c2da234b08bba92463def82f3032
4
- data.tar.gz: bc0f1401ca5ea0acd8761df09ac592c5421d8037
3
+ metadata.gz: d1cec5179e6ff0be0a0a065a27dec40b1b5ba8e0
4
+ data.tar.gz: 60468204e912dbc52fe7421ce11674c40678d2a6
5
5
  SHA512:
6
- metadata.gz: 85131d4627c24cecff1b878ccbcfc62882de9d503d563ec19df6a296ff1e278516dd2d55ee4319fe8522584bd4f5572b988e2aa57f1127c49169ec2816e8d7fc
7
- data.tar.gz: 0fad394047ced20e7dd520dbc84e26c0ebcb2d5880022c9b3efd8d9972eeb6dad4812ea7505fdfe61c67b83e7d85b8db0e2ac8fa59dd0229d5d383d3163c867f
6
+ metadata.gz: 7a6221eb5ee588b6797c04560691d39044e6d0fa0d03015aaf0e2650d32830325e1a5053e0e7b178c73047b5320a6f7097761ce35dc0d63bf90ad28cd8da8f5f
7
+ data.tar.gz: c6286d4e91d4e8a6382283327bf989fae5cd56be77e8be96561099cdbe06a78deb8c4e4304b257e3e64dd12575c3d90a330dc01c30976ddad7af48c9a9644678
@@ -33,6 +33,16 @@ module GunBroker
33
33
  @token = response['accessToken']
34
34
  end
35
35
 
36
+ # @return [Boolean] `true` if the current credentials are valid.
37
+ def authenticated?
38
+ return false unless has_credentials?
39
+ return !!(authenticate!) if has_password?
40
+ return !!(contact_info) if has_token? # #contact_info requires a valid token, so use that as a check.
41
+ false
42
+ rescue GunBroker::Error::NotAuthorized
43
+ false
44
+ end
45
+
36
46
  # Sends a DELETE request to deactivate the current access {#token}.
37
47
  # @note {API#delete! DELETE} /Users/AccessToken
38
48
  # @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response).
@@ -59,5 +69,24 @@ module GunBroker
59
69
  ItemsDelegate.new(self)
60
70
  end
61
71
 
72
+ private
73
+
74
+ # @return [Boolean] `true` if `@username` is present and either `@password` *or* `@token` is present.
75
+ def has_credentials?
76
+ has_username? && (has_password? || has_token?)
77
+ end
78
+
79
+ def has_password?
80
+ !@password.nil? && !@password.empty?
81
+ end
82
+
83
+ def has_token?
84
+ !@token.nil? && !@token.empty?
85
+ end
86
+
87
+ def has_username?
88
+ !@username.nil? && !@username.empty?
89
+ end
90
+
62
91
  end
63
92
  end
@@ -1,3 +1,3 @@
1
1
  module GunBroker
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.8"
3
3
  end
@@ -35,12 +35,7 @@ describe GunBroker::User do
35
35
 
36
36
  context 'on success' do
37
37
  it 'should set the access token' do
38
- stub_request(:post, endpoint)
39
- .with(
40
- headers: headers,
41
- body: { username: username, password: password },
42
- )
43
- .to_return(body: { 'accessToken' => token }.to_json)
38
+ stub_authentication(username, password)
44
39
 
45
40
  user = GunBroker::User.new(username, password: password)
46
41
  user.authenticate!
@@ -51,11 +46,7 @@ describe GunBroker::User do
51
46
 
52
47
  context 'on failure' do
53
48
  it 'should raise a GunBroker::Error::NotAuthorized exception' do
54
- stub_request(:post, endpoint)
55
- .with(
56
- headers: headers,
57
- body: { username: username, password: password }
58
- ).to_return(body: response_fixture('not_authorized'), status: 401)
49
+ stub_authentication_failure(username, password)
59
50
 
60
51
  user = GunBroker::User.new(username, password: password)
61
52
  expect { user.authenticate! }.to raise_error(GunBroker::Error::NotAuthorized)
@@ -63,6 +54,48 @@ describe GunBroker::User do
63
54
  end
64
55
  end
65
56
 
57
+ context 'authenticated?' do
58
+ context 'credentials' do
59
+ it 'returns false unless username is present' do
60
+ user = GunBroker::User.new('', token: 'foo')
61
+ expect(user.authenticated?).to eq(false)
62
+ end
63
+
64
+ it 'returns false unless password or token are present' do
65
+ expect(GunBroker::User.new(username, token: nil).authenticated?).to eq(false)
66
+ expect(GunBroker::User.new(username, password: nil).authenticated?).to eq(false)
67
+ end
68
+ end
69
+
70
+ context '@password' do
71
+ it 'returns true if valid' do
72
+ user = GunBroker::User.new(username, password: password)
73
+ expect(user).to receive(:authenticate!).and_return(true)
74
+ expect(user.authenticated?).to eq(true)
75
+ end
76
+
77
+ it 'returns false if invalid' do
78
+ user = GunBroker::User.new(username, password: password)
79
+ expect(user).to receive(:authenticate!).and_return(false)
80
+ expect(user.authenticated?).to eq(false)
81
+ end
82
+ end
83
+
84
+ context '@token' do
85
+ it 'returns true if valid' do
86
+ user = GunBroker::User.new(username, token: token)
87
+ expect(user).to receive(:contact_info).and_return(true)
88
+ expect(user.authenticated?).to eq(true)
89
+ end
90
+
91
+ it 'returns false if invalid' do
92
+ user = GunBroker::User.new(username, token: token)
93
+ expect(user).to receive(:contact_info).and_return(false)
94
+ expect(user.authenticated?).to eq(false)
95
+ end
96
+ end
97
+ end
98
+
66
99
  context '#deauthenticate!' do
67
100
  let(:endpoint) { [GunBroker::API::GUNBROKER_API, '/Users/AccessToken'].join }
68
101
 
data/spec/spec_helper.rb CHANGED
@@ -8,4 +8,5 @@ require 'gun_broker'
8
8
  RSpec.configure do |config|
9
9
  config.include GunBroker::Test::Fixtures
10
10
  config.include GunBroker::Test::Headers
11
+ config.include GunBroker::Test::Request
11
12
  end
@@ -0,0 +1,28 @@
1
+ require 'gun_broker/api'
2
+
3
+ module GunBroker
4
+ module Test
5
+ module Request
6
+
7
+ AUTH_ENDPOINT = [GunBroker::API::GUNBROKER_API, '/Users/AccessToken'].join
8
+
9
+ def stub_authentication(username, password)
10
+ stub_request(:post, AUTH_ENDPOINT)
11
+ .with(
12
+ headers: headers,
13
+ body: { username: username, password: password },
14
+ )
15
+ .to_return(body: { 'accessToken' => token }.to_json)
16
+ end
17
+
18
+ def stub_authentication_failure(username, password)
19
+ stub_request(:post, AUTH_ENDPOINT)
20
+ .with(
21
+ headers: headers,
22
+ body: { username: username, password: password },
23
+ ).to_return(body: response_fixture('not_authorized'), status: 401)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gun_broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -134,6 +134,7 @@ files:
134
134
  - spec/spec_helper.rb
135
135
  - spec/support/fixtures.rb
136
136
  - spec/support/headers.rb
137
+ - spec/support/request.rb
137
138
  homepage: ''
138
139
  licenses:
139
140
  - MIT
@@ -184,4 +185,5 @@ test_files:
184
185
  - spec/spec_helper.rb
185
186
  - spec/support/fixtures.rb
186
187
  - spec/support/headers.rb
188
+ - spec/support/request.rb
187
189
  has_rdoc: