kong 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,267 @@
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
+ expect(Excon).to receive(:new).with('http://localhost:8001', { omit_default_port: true })
16
+ described_class.send(:new)
17
+ end
18
+
19
+ it 'initializes default headers' do
20
+ expect(described_class.instance.default_headers).to eq({ 'Accept' => 'application/json' })
21
+ described_class.send(:new)
22
+ end
23
+ end
24
+
25
+ describe '#api_url' do
26
+ it 'returns localhost as default' do
27
+ expect(subject.api_url).to eq('http://localhost:8001')
28
+ end
29
+
30
+ it 'returns value from environment variable' do
31
+ allow(ENV).to receive(:[]).with('NO_PROXY')
32
+ allow(ENV).to receive(:[]).with('no_proxy')
33
+ allow(ENV).to receive(:[]).with('SSL_IGNORE_ERRORS')
34
+ allow(ENV).to receive(:[]).with('KONG_URI').and_return('http://kong-api:8001')
35
+ subject = described_class.send(:new)
36
+ expect(subject.api_url).to eq('http://kong-api:8001')
37
+ end
38
+ end
39
+
40
+ describe '#get' do
41
+ before(:each) do
42
+ allow(subject).to receive(:http_client).and_return(http_client)
43
+ end
44
+ it 'creates HTTP GET request with given params' do
45
+ http_client_params = {
46
+ path: 'path',
47
+ query: { key: 'value' },
48
+ headers: {}
49
+ }
50
+ response = spy
51
+ allow(response).to receive(:status).and_return(200)
52
+ expect(http_client).to receive(:get).and_return(response)
53
+ subject.get('path', { key: 'value' })
54
+ end
55
+
56
+ it 'raises Kong::Error if request returns error' do
57
+ http_client_params = {
58
+ path: 'path',
59
+ query: { key: 'value' },
60
+ headers: {}
61
+ }
62
+ response = spy
63
+ allow(response).to receive(:status).and_return(403)
64
+ expect(http_client).to receive(:get).and_return(response)
65
+ expect {
66
+ subject.get('path', { key: 'value' })
67
+ }.to raise_error(Kong::Error)
68
+ end
69
+
70
+ it 'parses response JSON' do
71
+ http_client_params = {
72
+ path: 'path',
73
+ query: { key: 'value' },
74
+ headers: {}
75
+ }
76
+ response = spy
77
+ allow(response).to receive(:status).and_return(200)
78
+ allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
79
+ allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
80
+ allow(http_client).to receive(:get).and_return(response)
81
+ expect(subject.get('path', { key: 'value' })).to eq({ 'id' => '12345' })
82
+ end
83
+
84
+ it 'returns response text' do
85
+ http_client_params = {
86
+ path: 'path',
87
+ query: { key: 'value' },
88
+ headers: {}
89
+ }
90
+ response = spy
91
+ allow(response).to receive(:status).and_return(200)
92
+ allow(response).to receive(:body).and_return('<html></html>')
93
+ allow(response).to receive(:headers).and_return({ 'Content-Type' => 'text/html' })
94
+ allow(http_client).to receive(:get).and_return(response)
95
+ expect(subject.get('path', { key: 'value' })).to eq('<html></html>')
96
+ end
97
+ end
98
+
99
+ describe '#post' do
100
+ before(:each) do
101
+ allow(subject).to receive(:http_client).and_return(http_client)
102
+ end
103
+ it 'creates HTTP POST request with given params' do
104
+ http_client_params = {
105
+ path: 'path',
106
+ query: { key: 'value' },
107
+ headers: {}
108
+ }
109
+ response = spy
110
+ allow(response).to receive(:status).and_return(200)
111
+ expect(http_client).to receive(:post).and_return(response)
112
+ subject.post('path', nil, { key: 'value' })
113
+ end
114
+
115
+
116
+ it 'raises Kong::Error if request returns error' do
117
+ http_client_params = {
118
+ path: 'path',
119
+ query: { key: 'value' },
120
+ headers: {}
121
+ }
122
+ response = spy
123
+ allow(response).to receive(:status).and_return(403)
124
+ expect(http_client).to receive(:post).and_return(response)
125
+ expect {
126
+ subject.post('path', nil, { key: 'value' })
127
+ }.to raise_error(Kong::Error)
128
+ end
129
+
130
+ it 'parses response JSON' do
131
+ http_client_params = {
132
+ path: 'path',
133
+ query: { key: 'value' },
134
+ headers: {}
135
+ }
136
+ response = spy
137
+ allow(response).to receive(:status).and_return(200)
138
+ allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
139
+ allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
140
+ allow(http_client).to receive(:post).and_return(response)
141
+ expect(subject.post('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
142
+ end
143
+ end
144
+ describe '#patch' do
145
+ before(:each) do
146
+ allow(subject).to receive(:http_client).and_return(http_client)
147
+ end
148
+ it 'creates HTTP PATCH request with given params' do
149
+ http_client_params = {
150
+ path: 'path',
151
+ query: { key: 'value' },
152
+ headers: {}
153
+ }
154
+ response = spy
155
+ allow(response).to receive(:status).and_return(200)
156
+ expect(http_client).to receive(:patch).and_return(response)
157
+ subject.patch('path', nil, { key: 'value' })
158
+ end
159
+
160
+
161
+ it 'raises Kong::Error if request returns error' do
162
+ http_client_params = {
163
+ path: 'path',
164
+ query: { key: 'value' },
165
+ headers: {}
166
+ }
167
+ response = spy
168
+ allow(response).to receive(:status).and_return(403)
169
+ expect(http_client).to receive(:patch).and_return(response)
170
+ expect {
171
+ subject.patch('path', nil, { key: 'value' })
172
+ }.to raise_error(Kong::Error)
173
+ end
174
+
175
+ it 'parses response JSON' do
176
+ http_client_params = {
177
+ path: 'path',
178
+ query: { key: 'value' },
179
+ headers: {}
180
+ }
181
+ response = spy
182
+ allow(response).to receive(:status).and_return(200)
183
+ allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
184
+ allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
185
+ allow(http_client).to receive(:patch).and_return(response)
186
+ expect(subject.patch('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
187
+ end
188
+ end
189
+
190
+ describe '#put' do
191
+ before(:each) do
192
+ allow(subject).to receive(:http_client).and_return(http_client)
193
+ end
194
+ it 'creates HTTP PUT request with given params' do
195
+ http_client_params = {
196
+ path: 'path',
197
+ query: { key: 'value' },
198
+ headers: {}
199
+ }
200
+ response = spy
201
+ allow(response).to receive(:status).and_return(200)
202
+ expect(http_client).to receive(:put).and_return(response)
203
+ subject.put('path', nil, { key: 'value' })
204
+ end
205
+
206
+
207
+ it 'raises Kong::Error if request returns error' do
208
+ http_client_params = {
209
+ path: 'path',
210
+ query: { key: 'value' },
211
+ headers: {}
212
+ }
213
+ response = spy
214
+ allow(response).to receive(:status).and_return(403)
215
+ expect(http_client).to receive(:put).and_return(response)
216
+ expect {
217
+ subject.put('path', nil, { key: 'value' })
218
+ }.to raise_error(Kong::Error)
219
+ end
220
+
221
+ it 'parses response JSON' do
222
+ http_client_params = {
223
+ path: 'path',
224
+ query: { key: 'value' },
225
+ headers: {}
226
+ }
227
+ response = spy
228
+ allow(response).to receive(:status).and_return(200)
229
+ allow(response).to receive(:body).and_return({ id: '12345' }.to_json)
230
+ allow(response).to receive(:headers).and_return({ 'Content-Type' => 'application/json' })
231
+ allow(http_client).to receive(:put).and_return(response)
232
+ expect(subject.put('path', nil, { key: 'value' })).to eq({ 'id' => '12345' })
233
+ end
234
+ end
235
+
236
+ describe '#delete' do
237
+ before(:each) do
238
+ allow(subject).to receive(:http_client).and_return(http_client)
239
+ end
240
+ it 'creates HTTP DELETE request with given params' do
241
+ http_client_params = {
242
+ path: 'path',
243
+ query: {},
244
+ headers: {}
245
+ }
246
+ response = spy
247
+ allow(response).to receive(:status).and_return(204)
248
+ expect(http_client).to receive(:delete).and_return(response)
249
+ subject.delete('path', nil, {})
250
+ end
251
+
252
+
253
+ it 'raises Kong::Error if request returns other than 204' do
254
+ http_client_params = {
255
+ path: 'path',
256
+ query: {},
257
+ headers: {}
258
+ }
259
+ response = spy
260
+ allow(response).to receive(:status).and_return(403)
261
+ expect(http_client).to receive(:delete).and_return(response)
262
+ expect {
263
+ subject.delete('path', nil, {})
264
+ }.to raise_error(Kong::Error)
265
+ end
266
+ end
267
+ end
@@ -0,0 +1,44 @@
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.username = ':username'
23
+ expect(Kong::Client.instance).to receive(:get).with("/consumers/:username/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.username = ':username'
30
+ allow(Kong::Client.instance).to receive(:get).with("/consumers/:username/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
+ it 'requests oauth2_tokens assigned to consumer' do
39
+ subject.custom_id = 'custom_id'
40
+ expect(Kong::OAuth2Token).to receive(:list).with({ authenticated_userid: subject.custom_id })
41
+ subject.oauth2_tokens
42
+ end
43
+ end
44
+ 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
@@ -0,0 +1,26 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Plugin do
4
+ let(:valid_attribute_names) do
5
+ %w(id api_id name config enabled 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('/plugins/')
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({ api_id: ':api_id' })
23
+ expect(subject.api_end_point).to eq('/apis/:api_id/plugins/')
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'kong'
9
+
10
+ RSpec.configure do |config|
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end