/
rails_test.rb
305 lines (253 loc) · 8.5 KB
/
rails_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# frozen_string_literal: true
ENV['TZ'] ||= 'UTC'
ENV['RAILS_ENV'] ||= 'test'
ENV['OPENAPI_OUTPUT'] ||= 'yaml'
require 'minitest/autorun'
require File.expand_path('../apps/rails/config/environment', __dir__)
RSpec::OpenAPI.title = 'OpenAPI Documentation'
RSpec::OpenAPI.request_headers = %w[X-Authorization-Token]
RSpec::OpenAPI.response_headers = %w[X-Cursor]
RSpec::OpenAPI.path = File.expand_path("../apps/rails/doc/openapi.#{ENV.fetch('OPENAPI_OUTPUT', nil)}", __dir__)
RSpec::OpenAPI.ignored_paths = [Regexp.new('/admin/.*$')]
RSpec::OpenAPI.comment = <<~COMMENT
This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
When you write a spec in spec/requests, running the spec with `OPENAPI=1 rspec` will
update this file automatically. You can also manually edit this file.
COMMENT
RSpec::OpenAPI.servers = [{ url: 'http://localhost:3000' }]
RSpec::OpenAPI.info = {
description: 'My beautiful API',
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
},
}
RSpec::OpenAPI.security_schemes = {
SecretApiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'Secret-Key',
},
}
RSpec::OpenAPI.post_process_hook = ->(_path, _records, spec) { spec['custom_field'] = 'custom_value' }
class TablesIndexTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
# Patch minitest's ordering of examples to match RSpec's
# in order to get comparable results
def self.runnable_methods
%w[test_with_flat_query_parameters test_with_deep_query_parameters test_with_different_deep_query_parameters
test_has_a_request_spec_which_does_not_make_any_request test_does_not_return_tables_if_unauthorized]
end
def test_with_flat_query_parameters
get '/tables', params: { page: '1', per: '10' },
headers: { authorization: 'k0kubun', 'X-Authorization-Token': 'token' }
assert_response 200
end
def test_with_deep_query_parameters
get '/tables', params: { filter: { 'name' => 'Example Table' } }, headers: { authorization: 'k0kubun' }
assert_response 200
end
def test_with_different_deep_query_parameters
get '/tables', params: { filter: { 'price' => 0 } }, headers: { authorization: 'k0kubun' }
assert_response 200
end
def test_has_a_request_spec_which_does_not_make_any_request
assert true
end
def test_does_not_return_tables_if_unauthorized
get '/tables'
assert_response 401
end
end
class TablesShowTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
# Patch minitest's ordering of examples to match RSpec's
# in order to get comparable results
def self.runnable_methods
%w[test_returns_a_table test_does_not_return_a_table_if_unauthorized test_does_not_return_a_table_if_not_found]
end
def test_does_not_return_a_table_if_unauthorized
get '/tables/1'
assert_response 401
end
def test_does_not_return_a_table_if_not_found
get '/tables/2', headers: { authorization: 'k0kubun' }
assert_response 404
end
def test_returns_a_table
get '/tables/1', headers: { authorization: 'k0kubun' }
assert_response 200
end
end
class TablesCreateTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'k0kubun',
description: 'description',
database_id: 2,
}.to_json
assert_response 201
end
test 'fails to create a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'some_invalid_name',
description: 'description',
database_id: 2,
}.to_json
assert_response 422
end
test 'fails to create a table (2)' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
description: 'description',
database_id: 2,
}.to_json
assert_response 422
end
end
class TablesUpdateTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns a table' do
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { name: 'test' }
assert_response 200
end
end
class TablesDestroyTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns a table' do
delete '/tables/1', headers: { authorization: 'k0kubun' }
assert_response 200
end
test 'returns no content if specified' do
delete '/tables/1', headers: { authorization: 'k0kubun' }, params: { no_content: true }
assert_response 202
end
end
class ImageTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns a image payload' do
get '/images/1'
assert_response 200
end
test 'can return an object with an attribute of empty array' do
get '/images'
assert_response 200
end
test 'returns a image payload with upload' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload', params: { image: image }
assert_response 200
end
test 'returns a image payload with upload nested' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload_nested', params: { nested_image: { image: image, caption: 'Some caption' } }
assert_response 200
end
test 'returns a image payload with upload multiple' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload_multiple', params: { images: [image, image] }
assert_response 200
end
test 'returns a image payload with upload multiple nested' do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
image = Rack::Test::UploadedFile.new('test.png', 'image/png')
post '/images/upload_multiple_nested', params: { images: [{ image: image }, { image: image }] }
assert_response 200
end
end
class SecretKeyTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'authorizes with secret key' do
get '/secret_items',
headers: {
'Secret-Key' => '42',
}
assert_response 200
end
end
class ExtraRoutesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns the block content' do
get '/test_block'
assert_response 200
end
end
class EngineTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns some content from the engine' do
get '/my_engine/eng_route'
assert_response 200
end
end
class EngineExtraRoutesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns the block content' do
get '/my_engine/test'
assert_response 200
end
end
class AdditionalPropertiesTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns some content' do
get '/additional_properties'
assert_response 200
end
end
class NamespaceTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns some content' do
get '/admin/masters/extensions'
assert_response 200
end
test 'creates a content' do
post '/admin/masters/extensions'
assert_response 200
end
end
class RackAppTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'returns some content foo' do
get '/rack/foo/'
assert_response 200
end
test 'returns some content bar' do
get '/rack/bar'
assert_response 200
end
end
class RackAppTest < ActionDispatch::IntegrationTest
i_suck_and_my_tests_are_order_dependent!
openapi!
test 'raises not found' do
get '/sites/no-such', as: :json
assert_response 404
end
test 'finds a site' do
get '/sites/abc123', as: :json
assert_response 200
end
end