kong-client 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.hound.yml +2 -0
- data/.rubocop.yml +360 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +37 -0
- data/Gemfile +8 -0
- data/LICENSE +191 -0
- data/README.md +295 -0
- data/Rakefile +5 -0
- data/kong.gemspec +24 -0
- data/lib/kong/acl.rb +8 -0
- data/lib/kong/api.rb +19 -0
- data/lib/kong/base.rb +180 -0
- data/lib/kong/basic_auth.rb +8 -0
- data/lib/kong/belongs_to_api.rb +30 -0
- data/lib/kong/belongs_to_consumer.rb +30 -0
- data/lib/kong/client.rb +232 -0
- data/lib/kong/consumer.rb +103 -0
- data/lib/kong/error.rb +10 -0
- data/lib/kong/hmac_auth.rb +9 -0
- data/lib/kong/jwt.rb +8 -0
- data/lib/kong/key_auth.rb +8 -0
- data/lib/kong/oauth2_token.rb +14 -0
- data/lib/kong/oauth_app.rb +8 -0
- data/lib/kong/plugin.rb +31 -0
- data/lib/kong/server.rb +23 -0
- data/lib/kong/target.rb +61 -0
- data/lib/kong/upstream.rb +24 -0
- data/lib/kong/util.rb +16 -0
- data/lib/kong/version.rb +3 -0
- data/lib/kong.rb +20 -0
- data/spec/kong/acl_spec.rb +19 -0
- data/spec/kong/api_spec.rb +32 -0
- data/spec/kong/base_spec.rb +169 -0
- data/spec/kong/basic_auth_spec.rb +26 -0
- data/spec/kong/client_spec.rb +297 -0
- data/spec/kong/consumer_spec.rb +72 -0
- data/spec/kong/error_spec.rb +23 -0
- data/spec/kong/hmac_auth_spec.rb +26 -0
- data/spec/kong/key_auth_spec.rb +26 -0
- data/spec/kong/oauth2_token_spec.rb +19 -0
- data/spec/kong/oauth_app_spec.rb +19 -0
- data/spec/kong/plugin_spec.rb +62 -0
- data/spec/kong/server_spec.rb +39 -0
- data/spec/kong/target_spec.rb +53 -0
- data/spec/kong/upstream_spec.rb +37 -0
- data/spec/kong/util_spec.rb +29 -0
- data/spec/spec_helper.rb +19 -0
- data/tasks/rspec.rake +5 -0
- metadata +153 -0
@@ -0,0 +1,169 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::Base do
|
4
|
+
|
5
|
+
let(:resource_class) do
|
6
|
+
Class.new do
|
7
|
+
include Kong::Base
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:subject) do
|
13
|
+
Klass.new
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
stub_const 'Klass', resource_class
|
18
|
+
stub_const 'Klass::API_END_POINT', '/resources/'
|
19
|
+
stub_const 'Klass::ATTRIBUTE_NAMES', %w(id name)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.list' do
|
23
|
+
it 'requests GET /:resource_end_point/' do
|
24
|
+
expect(Kong::Client.instance).to receive(:get).with('/resources/', {}).and_return({ data: [] })
|
25
|
+
Klass.list
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns array of resource instances' do
|
29
|
+
allow(Kong::Client.instance).to receive(:get).with('/resources/', {})
|
30
|
+
.and_return({ 'data' => [{ 'id' => '12345', 'name' => 'resource' }] })
|
31
|
+
result = Klass.list
|
32
|
+
expect(result[0].is_a?(Klass)).to be_truthy
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '.all' do
|
37
|
+
it 'is alias of .list' do
|
38
|
+
expect(Klass.method(:list)).to eq(Klass.method(:all))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.find' do
|
43
|
+
it 'creates GET /:resource_end_point/:id request' do
|
44
|
+
expect(Kong::Client.instance).to receive(:get).with('/resources/12345').and_return({ data: [] })
|
45
|
+
Klass.find('12345')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '.respond_to' do
|
50
|
+
context 'when attribute exits' do
|
51
|
+
it 'will respond to find_by_* methods' do
|
52
|
+
expect(Klass.respond_to?(:find_by_name)).to be_truthy
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'will respond to find_all_by_* methods' do
|
56
|
+
expect(Klass.respond_to?(:find_all_by_name)).to be_truthy
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when attribute does not exit' do
|
61
|
+
it 'will not respond to find_by_* methods' do
|
62
|
+
expect(Klass.respond_to?(:find_by_invalid)).to be_falsey
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'will not respond to find_all_by_* methods' do
|
66
|
+
expect(Klass.respond_to?(:find_all_by_invalid)).to be_falsey
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#get' do
|
72
|
+
it 'creates GET /:resource_end_point/:id request' do
|
73
|
+
expect(Kong::Client.instance).to receive(:get).with('/resources/12345').and_return({ data: [] })
|
74
|
+
subject.get('12345')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns resource instance' do
|
78
|
+
allow(Kong::Client.instance).to receive(:get).with('/resources/12345')
|
79
|
+
.and_return({ 'data' => [{ 'id' => '12345', 'name' => 'resource' }] })
|
80
|
+
result = subject.get('12345')
|
81
|
+
expect(result.is_a?(Klass)).to be_truthy
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'returns nil if resource is not found' do
|
85
|
+
allow(Kong::Client.instance).to receive(:get).with('/resources/123456')
|
86
|
+
.and_return(nil)
|
87
|
+
result = subject.get('123456')
|
88
|
+
expect(result).to be_nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#new?' do
|
93
|
+
it 'returns true if resource id is missing' do
|
94
|
+
expect(subject.new?).to be_truthy
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'returns false if resource has id' do
|
98
|
+
subject.id = '12345'
|
99
|
+
expect(subject.new?).to be_falsey
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe '#create' do
|
104
|
+
it 'creates POST /:resource_end_point/ request with resource attributes' do
|
105
|
+
headers = { 'Content-Type' => 'application/json' }
|
106
|
+
attributes = { 'name' => 'test object' }
|
107
|
+
expect(Kong::Client.instance).to receive(:post).with('/resources/', attributes, nil, headers)
|
108
|
+
.and_return(attributes)
|
109
|
+
subject.name = 'test object'
|
110
|
+
subject.create
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'returns resource instance' do
|
114
|
+
headers = { 'Content-Type' => 'application/json' }
|
115
|
+
attributes = { 'name' => 'test object' }
|
116
|
+
allow(Kong::Client.instance).to receive(:post).with('/resources/', attributes, nil, headers)
|
117
|
+
.and_return(attributes.merge({ 'id' => '12345' }))
|
118
|
+
subject.name = 'test object'
|
119
|
+
expect(subject.create).to eq(subject)
|
120
|
+
expect(subject.id).to eq('12345')
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#create_or_update' do
|
125
|
+
it 'creates PUT /:resource_end_point/ request with resource attributes as json payload' do
|
126
|
+
headers = { 'Content-Type' => 'application/json' }
|
127
|
+
attributes = { 'name' => 'test object' }
|
128
|
+
expect(Kong::Client.instance).to receive(:put).with('/resources/', attributes, nil, headers)
|
129
|
+
.and_return(attributes.merge({ 'id' => '12345' }))
|
130
|
+
subject.name = 'test object'
|
131
|
+
subject.create_or_update
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'returns resource instance' do
|
135
|
+
headers = { 'Content-Type' => 'application/json' }
|
136
|
+
attributes = { 'name' => 'test object' }
|
137
|
+
expect(Kong::Client.instance).to receive(:put).with('/resources/', attributes, nil, headers)
|
138
|
+
.and_return(attributes.merge({ 'id' => '12345' }))
|
139
|
+
subject.name = 'test object'
|
140
|
+
expect(subject.create_or_update).to eq(subject)
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#update' do
|
144
|
+
it 'creates PATCH /:resource_end_point/:resource_id request with resource attributes' do
|
145
|
+
headers = { 'Content-Type' => 'application/json' }
|
146
|
+
subject.id = '12345'
|
147
|
+
subject.name = 'test object'
|
148
|
+
expect(Kong::Client.instance).to receive(:patch).with('/resources/12345', subject.attributes, nil, headers)
|
149
|
+
.and_return(subject.attributes)
|
150
|
+
subject.update
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'returns resource instance' do
|
154
|
+
headers = { 'Content-Type' => 'application/json' }
|
155
|
+
subject.id = '12345'
|
156
|
+
subject.name = 'test object'
|
157
|
+
allow(Kong::Client.instance).to receive(:patch).with('/resources/12345', subject.attributes, nil, headers)
|
158
|
+
.and_return(subject.attributes)
|
159
|
+
expect(subject.update).to eq(subject)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'will respond to attribute' do
|
164
|
+
expect(subject.respond_to?(:name)).to be_truthy
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::BasicAuth do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id username password consumer_id)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/basic-auth/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#init_attributes' do
|
21
|
+
it 'uses correct api end point if api_id is present' do
|
22
|
+
subject = described_class.new({ consumer_id: ':consumer_id' })
|
23
|
+
expect(subject.api_end_point).to eq('/consumers/:consumer_id/basic-auth/')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,297 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::Client do
|
4
|
+
|
5
|
+
let(:subject) do
|
6
|
+
described_class.instance
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:http_client) do
|
10
|
+
spy
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#initialize' do
|
14
|
+
it 'initializes Excon with Kong URI' do
|
15
|
+
described_class.api_url = nil
|
16
|
+
expect(Excon).to receive(:new).with('http://localhost:8001', { omit_default_port: true })
|
17
|
+
described_class.send(:new)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'initializes default headers' do
|
21
|
+
expect(described_class.instance.default_headers).to eq({ 'Accept' => 'application/json' })
|
22
|
+
described_class.send(:new)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#api_url' do
|
27
|
+
|
28
|
+
def client_params
|
29
|
+
uri = URI.parse described_class.api_url
|
30
|
+
params = {
|
31
|
+
host: uri.host,
|
32
|
+
hostname: uri.hostname,
|
33
|
+
path: uri.path,
|
34
|
+
port: uri.port,
|
35
|
+
query: uri.query,
|
36
|
+
scheme: uri.scheme
|
37
|
+
}
|
38
|
+
described_class.instance.http_client.params.merge(params)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'returns localhost as default' do
|
42
|
+
described_class.api_url = nil
|
43
|
+
expect(subject.api_url).to eq('http://localhost:8001')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns value from environment variable' do
|
47
|
+
allow(ENV).to receive(:[]).with('NO_PROXY')
|
48
|
+
allow(ENV).to receive(:[]).with('no_proxy')
|
49
|
+
allow(ENV).to receive(:[]).with('SSL_IGNORE_ERRORS')
|
50
|
+
allow(ENV).to receive(:[]).with('KONG_URI').and_return('http://kong-api:8001')
|
51
|
+
described_class.api_url = nil
|
52
|
+
subject = described_class.send(:new)
|
53
|
+
expect(subject.api_url).to eq('http://kong-api:8001')
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns custom api_url if set' do
|
57
|
+
url = 'http://foo.bar:1337'
|
58
|
+
described_class.api_url = url
|
59
|
+
expect(described_class.send(:new).api_url).to eq(url)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'can edit api_url' do
|
63
|
+
described_class.api_url = nil
|
64
|
+
expect(described_class.instance.http_client.params).to eq(client_params)
|
65
|
+
described_class.api_url = 'http://foo.bar:1337'
|
66
|
+
expect(described_class.instance.http_client.params).to eq(client_params)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#get' do
|
71
|
+
before(:each) do
|
72
|
+
allow(subject).to receive(:http_client).and_return(http_client)
|
73
|
+
end
|
74
|
+
it 'creates HTTP GET request with given params' do
|
75
|
+
http_client_params = {
|
76
|
+
path: 'path',
|
77
|
+
query: { key: 'value' },
|
78
|
+
headers: {}
|
79
|
+
}
|
80
|
+
response = spy
|
81
|
+
allow(response).to receive(:status).and_return(200)
|
82
|
+
expect(http_client).to receive(:get).and_return(response)
|
83
|
+
subject.get('path', { key: 'value' })
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'raises Kong::Error if request returns error' do
|
87
|
+
http_client_params = {
|
88
|
+
path: 'path',
|
89
|
+
query: { key: 'value' },
|
90
|
+
headers: {}
|
91
|
+
}
|
92
|
+
response = spy
|
93
|
+
allow(response).to receive(:status).and_return(403)
|
94
|
+
expect(http_client).to receive(:get).and_return(response)
|
95
|
+
expect {
|
96
|
+
subject.get('path', { key: 'value' })
|
97
|
+
}.to raise_error(Kong::Error)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'parses response JSON' do
|
101
|
+
http_client_params = {
|
102
|
+
path: 'path',
|
103
|
+
query: { key: 'value' },
|
104
|
+
headers: {}
|
105
|
+
}
|
106
|
+
response = spy
|
107
|
+
allow(response).to receive(:status).and_return(200)
|
108
|
+
allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
|
109
|
+
allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
|
110
|
+
allow(http_client).to receive(:get).and_return(response)
|
111
|
+
expect(subject.get('path', { key: 'value' })).to eq({ 'id' => '12345' })
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'returns response text' do
|
115
|
+
http_client_params = {
|
116
|
+
path: 'path',
|
117
|
+
query: { key: 'value' },
|
118
|
+
headers: {}
|
119
|
+
}
|
120
|
+
response = spy
|
121
|
+
allow(response).to receive(:status).and_return(200)
|
122
|
+
allow(response).to receive(:body).and_return('<html></html>')
|
123
|
+
allow(response).to receive(:headers).and_return({ 'Content-Type' => 'text/html' })
|
124
|
+
allow(http_client).to receive(:get).and_return(response)
|
125
|
+
expect(subject.get('path', { key: 'value' })).to eq('<html></html>')
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#post' do
|
130
|
+
before(:each) do
|
131
|
+
allow(subject).to receive(:http_client).and_return(http_client)
|
132
|
+
end
|
133
|
+
it 'creates HTTP POST request with given params' do
|
134
|
+
http_client_params = {
|
135
|
+
path: 'path',
|
136
|
+
query: { key: 'value' },
|
137
|
+
headers: {}
|
138
|
+
}
|
139
|
+
response = spy
|
140
|
+
allow(response).to receive(:status).and_return(200)
|
141
|
+
expect(http_client).to receive(:post).and_return(response)
|
142
|
+
subject.post('path', nil, { key: 'value' })
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
it 'raises Kong::Error if request returns error' do
|
147
|
+
http_client_params = {
|
148
|
+
path: 'path',
|
149
|
+
query: { key: 'value' },
|
150
|
+
headers: {}
|
151
|
+
}
|
152
|
+
response = spy
|
153
|
+
allow(response).to receive(:status).and_return(403)
|
154
|
+
expect(http_client).to receive(:post).and_return(response)
|
155
|
+
expect {
|
156
|
+
subject.post('path', nil, { key: 'value' })
|
157
|
+
}.to raise_error(Kong::Error)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'parses response JSON' do
|
161
|
+
http_client_params = {
|
162
|
+
path: 'path',
|
163
|
+
query: { key: 'value' },
|
164
|
+
headers: {}
|
165
|
+
}
|
166
|
+
response = spy
|
167
|
+
allow(response).to receive(:status).and_return(200)
|
168
|
+
allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
|
169
|
+
allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
|
170
|
+
allow(http_client).to receive(:post).and_return(response)
|
171
|
+
expect(subject.post('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
|
172
|
+
end
|
173
|
+
end
|
174
|
+
describe '#patch' do
|
175
|
+
before(:each) do
|
176
|
+
allow(subject).to receive(:http_client).and_return(http_client)
|
177
|
+
end
|
178
|
+
it 'creates HTTP PATCH request with given params' do
|
179
|
+
http_client_params = {
|
180
|
+
path: 'path',
|
181
|
+
query: { key: 'value' },
|
182
|
+
headers: {}
|
183
|
+
}
|
184
|
+
response = spy
|
185
|
+
allow(response).to receive(:status).and_return(200)
|
186
|
+
expect(http_client).to receive(:patch).and_return(response)
|
187
|
+
subject.patch('path', nil, { key: 'value' })
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
it 'raises Kong::Error if request returns error' do
|
192
|
+
http_client_params = {
|
193
|
+
path: 'path',
|
194
|
+
query: { key: 'value' },
|
195
|
+
headers: {}
|
196
|
+
}
|
197
|
+
response = spy
|
198
|
+
allow(response).to receive(:status).and_return(403)
|
199
|
+
expect(http_client).to receive(:patch).and_return(response)
|
200
|
+
expect {
|
201
|
+
subject.patch('path', nil, { key: 'value' })
|
202
|
+
}.to raise_error(Kong::Error)
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'parses response JSON' do
|
206
|
+
http_client_params = {
|
207
|
+
path: 'path',
|
208
|
+
query: { key: 'value' },
|
209
|
+
headers: {}
|
210
|
+
}
|
211
|
+
response = spy
|
212
|
+
allow(response).to receive(:status).and_return(200)
|
213
|
+
allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
|
214
|
+
allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
|
215
|
+
allow(http_client).to receive(:patch).and_return(response)
|
216
|
+
expect(subject.patch('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe '#put' do
|
221
|
+
before(:each) do
|
222
|
+
allow(subject).to receive(:http_client).and_return(http_client)
|
223
|
+
end
|
224
|
+
it 'creates HTTP PUT request with given params' do
|
225
|
+
http_client_params = {
|
226
|
+
path: 'path',
|
227
|
+
query: { key: 'value' },
|
228
|
+
headers: {}
|
229
|
+
}
|
230
|
+
response = spy
|
231
|
+
allow(response).to receive(:status).and_return(200)
|
232
|
+
expect(http_client).to receive(:put).and_return(response)
|
233
|
+
subject.put('path', nil, { key: 'value' })
|
234
|
+
end
|
235
|
+
|
236
|
+
|
237
|
+
it 'raises Kong::Error if request returns error' do
|
238
|
+
http_client_params = {
|
239
|
+
path: 'path',
|
240
|
+
query: { key: 'value' },
|
241
|
+
headers: {}
|
242
|
+
}
|
243
|
+
response = spy
|
244
|
+
allow(response).to receive(:status).and_return(403)
|
245
|
+
expect(http_client).to receive(:put).and_return(response)
|
246
|
+
expect {
|
247
|
+
subject.put('path', nil, { key: 'value' })
|
248
|
+
}.to raise_error(Kong::Error)
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'parses response JSON' do
|
252
|
+
http_client_params = {
|
253
|
+
path: 'path',
|
254
|
+
query: { key: 'value' },
|
255
|
+
headers: {}
|
256
|
+
}
|
257
|
+
response = spy
|
258
|
+
allow(response).to receive(:status).and_return(200)
|
259
|
+
allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
|
260
|
+
allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
|
261
|
+
allow(http_client).to receive(:put).and_return(response)
|
262
|
+
expect(subject.put('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
describe '#delete' do
|
267
|
+
before(:each) do
|
268
|
+
allow(subject).to receive(:http_client).and_return(http_client)
|
269
|
+
end
|
270
|
+
it 'creates HTTP DELETE request with given params' do
|
271
|
+
http_client_params = {
|
272
|
+
path: 'path',
|
273
|
+
query: {},
|
274
|
+
headers: {}
|
275
|
+
}
|
276
|
+
response = spy
|
277
|
+
allow(response).to receive(:status).and_return(204)
|
278
|
+
expect(http_client).to receive(:delete).and_return(response)
|
279
|
+
subject.delete('path', nil, {})
|
280
|
+
end
|
281
|
+
|
282
|
+
|
283
|
+
it 'raises Kong::Error if request returns other than 204' do
|
284
|
+
http_client_params = {
|
285
|
+
path: 'path',
|
286
|
+
query: {},
|
287
|
+
headers: {}
|
288
|
+
}
|
289
|
+
response = spy
|
290
|
+
allow(response).to receive(:status).and_return(403)
|
291
|
+
expect(http_client).to receive(:delete).and_return(response)
|
292
|
+
expect {
|
293
|
+
subject.delete('path', nil, {})
|
294
|
+
}.to raise_error(Kong::Error)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::Consumer do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id custom_id username created_at)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '::ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '::API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/consumers/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#oauth_apps' do
|
21
|
+
it 'requests consumer\'s oauth_apps' do
|
22
|
+
subject.id = ':id'
|
23
|
+
expect(Kong::Client.instance).to receive(:get).with("/consumers/:id/oauth2")
|
24
|
+
.and_return({ 'data' => [{ 'id' => '123456', 'name' => 'my app' }] })
|
25
|
+
subject.oauth_apps
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns list of OAuthApp' do
|
29
|
+
subject.id = ':id'
|
30
|
+
allow(Kong::Client.instance).to receive(:get).with("/consumers/:id/oauth2")
|
31
|
+
.and_return({ 'data' => [{ 'id' => '123456', 'name' => 'my app' }] })
|
32
|
+
result = subject.oauth_apps
|
33
|
+
expect(result.first.is_a?(Kong::OAuthApp)).to be_truthy
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#oauth2_tokens' do
|
38
|
+
context 'when custom_id is set' do
|
39
|
+
it 'requests oauth2_tokens assigned to consumer' do
|
40
|
+
subject.custom_id = 'custom_id'
|
41
|
+
expect(Kong::OAuth2Token).to receive(:list).with({ authenticated_userid: subject.custom_id })
|
42
|
+
subject.oauth2_tokens
|
43
|
+
end
|
44
|
+
end
|
45
|
+
context 'when custom_id is not set' do
|
46
|
+
it 'requests oauth2_tokens assigned to consumer' do
|
47
|
+
expect(Kong::OAuth2Token).not_to receive(:list)
|
48
|
+
subject.oauth2_tokens
|
49
|
+
end
|
50
|
+
it 'returns empty array' do
|
51
|
+
expect(subject.oauth2_tokens).to eq([])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#acls' do
|
57
|
+
it 'requests consumer\'s acls' do
|
58
|
+
subject.id = ':id'
|
59
|
+
expect(Kong::Client.instance).to receive(:get).with("/consumers/:id/acls")
|
60
|
+
.and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
|
61
|
+
subject.acls
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'returns list of Acls' do
|
65
|
+
subject.id = ':id'
|
66
|
+
allow(Kong::Client.instance).to receive(:get).with("/consumers/:id/acls")
|
67
|
+
.and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
|
68
|
+
result = subject.acls
|
69
|
+
expect(result.first.is_a?(Kong::Acl)).to be_truthy
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::Error do
|
4
|
+
let(:instance) { described_class.new(status, message) }
|
5
|
+
let(:status) { 123 }
|
6
|
+
let(:message) { 'Error Message' }
|
7
|
+
|
8
|
+
describe '.status' do
|
9
|
+
subject { instance.status }
|
10
|
+
|
11
|
+
it 'exposes error status' do
|
12
|
+
is_expected.to eq status
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.message' do
|
17
|
+
subject { instance.message }
|
18
|
+
|
19
|
+
it 'exposes error message' do
|
20
|
+
is_expected.to eq message
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::HmacAuth do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id username consumer_id)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/hmac-auth/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#init_attributes' do
|
21
|
+
it 'uses correct api end point if api_id is present' do
|
22
|
+
subject = described_class.new({ consumer_id: ':consumer_id' })
|
23
|
+
expect(subject.api_end_point).to eq('/consumers/:consumer_id/hmac-auth/')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::KeyAuth do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id key consumer_id)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/key-auth/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#init_attributes' do
|
21
|
+
it 'uses correct api end point if api_id is present' do
|
22
|
+
subject = described_class.new({ consumer_id: ':consumer_id' })
|
23
|
+
expect(subject.api_end_point).to eq('/consumers/:consumer_id/key-auth/')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::OAuth2Token do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id credential_id expires_in created_at token_type access_token refresh_token scope authenticated_userid)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/oauth2_tokens/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Kong::OAuthApp do
|
4
|
+
let(:valid_attribute_names) do
|
5
|
+
%w(id name client_id client_secret redirect_uri consumer_id)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '::ATTRIBUTE_NAMES' do
|
9
|
+
it 'contains valid names' do
|
10
|
+
expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '::API_END_POINT' do
|
15
|
+
it 'contains valid end point' do
|
16
|
+
expect(subject.class::API_END_POINT).to eq('/oauth2/')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|