gun_broker 0.4.7 → 0.4.8
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 +4 -4
- data/lib/gun_broker/user.rb +29 -0
- data/lib/gun_broker/version.rb +1 -1
- data/spec/gun_broker/user_spec.rb +44 -11
- data/spec/spec_helper.rb +1 -0
- data/spec/support/request.rb +28 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1cec5179e6ff0be0a0a065a27dec40b1b5ba8e0
|
4
|
+
data.tar.gz: 60468204e912dbc52fe7421ce11674c40678d2a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a6221eb5ee588b6797c04560691d39044e6d0fa0d03015aaf0e2650d32830325e1a5053e0e7b178c73047b5320a6f7097761ce35dc0d63bf90ad28cd8da8f5f
|
7
|
+
data.tar.gz: c6286d4e91d4e8a6382283327bf989fae5cd56be77e8be96561099cdbe06a78deb8c4e4304b257e3e64dd12575c3d90a330dc01c30976ddad7af48c9a9644678
|
data/lib/gun_broker/user.rb
CHANGED
@@ -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
|
data/lib/gun_broker/version.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
@@ -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.
|
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-
|
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:
|