-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathrails_spec.rb
272 lines (235 loc) · 7.96 KB
/
rails_spec.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
# frozen_string_literal: true
ENV['TZ'] ||= 'UTC'
ENV['RAILS_ENV'] ||= 'test'
ENV['OPENAPI_OUTPUT'] ||= 'yaml'
require File.expand_path('../rails/config/environment', __dir__)
require 'rspec/rails'
RSpec::OpenAPI.title = 'OpenAPI Documentation'
RSpec::OpenAPI.request_headers = %w[X-Authorization-Token Secret-Key]
RSpec::OpenAPI.response_headers = %w[X-Cursor]
RSpec::OpenAPI.path = File.expand_path("../rails/doc/openapi.#{ENV.fetch('OPENAPI_OUTPUT', nil)}", __dir__)
RSpec::OpenAPI.ignored_paths = ['/admin/masters/extensions']
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.describe 'Tables', type: :request do
describe '#index' do
context it 'returns a list of tables' do
it 'with flat query parameters' do
get '/tables', params: { page: '1', per: '10' },
headers: { authorization: 'k0kubun', 'X-Authorization-Token': 'token' }
expect(response.status).to eq(200)
end
it 'with deep query parameters' do
get '/tables', params: { filter: { 'name' => 'Example Table' } }, headers: { authorization: 'k0kubun' }
expect(response.status).to eq(200)
end
it 'with different deep query parameters' do
get '/tables', params: { filter: { 'price' => 0 } }, headers: { authorization: 'k0kubun' }
expect(response.status).to eq(200)
end
end
it 'has a request spec which does not make any request' do
expect(request).to eq(nil)
end
it 'does not return tables if unauthorized' do
get '/tables'
expect(response.status).to eq(401)
end
end
describe '#show' do
it 'returns a table' do
get '/tables/1', headers: { authorization: 'k0kubun' }
expect(response.status).to eq(200)
end
it 'does not return a table if unauthorized' do
get '/tables/1'
expect(response.status).to eq(401)
end
it 'does not return a table if not found' do
get '/tables/2', headers: { authorization: 'k0kubun' }
expect(response.status).to eq(404)
end
it 'does not return a table if not found (openapi: false)', openapi: false do
get '/tables/3', headers: { authorization: 'k0kubun' }
expect(response.status).to eq(404)
end
end
describe '#create' do
it 'returns a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'k0kubun',
description: 'description',
database_id: 2,
}.to_json
expect(response.status).to eq(201)
end
it 'fails to create a table' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
description: 'description',
database_id: 2,
}.to_json
expect(response.status).to eq(422)
end
it 'fails to create a table (2)' do
post '/tables', headers: { authorization: 'k0kubun', 'Content-Type': 'application/json' }, params: {
name: 'some_invalid_name',
description: 'description',
database_id: 2,
}.to_json
expect(response.status).to eq(422)
end
end
describe '#update' do
it 'returns a table' do
patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { name: 'test' }
expect(response.status).to eq(200)
end
end
describe '#destroy' do
it 'returns a table' do
delete '/tables/1', headers: { authorization: 'k0kubun' }
expect(response.status).to eq(200)
end
it 'returns no content if specified' do
delete '/tables/1', headers: { authorization: 'k0kubun' }, params: { no_content: true }
expect(response.status).to eq(202)
end
end
end
RSpec.describe 'Images', type: :request do
describe '#payload' do
it 'returns a image payload' do
get '/images/1'
expect(response.status).to eq(200)
end
end
describe '#index' do
it 'can return an object with an attribute of empty array' do
get '/images'
expect(response.status).to eq(200)
end
end
describe '#upload' do
before do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
end
let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') }
it 'returns a image payload with upload' do
post '/images/upload', params: { image: image }
expect(response.status).to eq(200)
end
end
describe '#upload_nested' do
before do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
end
let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') }
it 'returns a image payload with upload nested' do
post '/images/upload_nested', params: { nested_image: { image: image, caption: 'Some caption' } }
expect(response.status).to eq(200)
end
end
describe '#upload_multiple' do
before do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
end
let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') }
it 'returns a image payload with upload multiple' do
post '/images/upload_multiple', params: { images: [image, image] }
expect(response.status).to eq(200)
end
end
describe '#upload_multiple_nested' do
before do
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack1('m')
File.binwrite('test.png', png)
end
let(:image) { Rack::Test::UploadedFile.new('test.png', 'image/png') }
it 'returns a image payload with upload multiple nested' do
post '/images/upload_multiple_nested', params: { images: [{ image: image }, { image: image }] }
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'SecretKey securityScheme',
type: :request,
openapi: { security: [{ 'SecretApiKeyAuth' => [] }] } do
describe '#secret_items' do
it 'authorizes with secret key' do
get '/secret_items',
headers: {
'Secret-Key' => '42',
}
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'Extra routes', type: :request do
describe '#test_block' do
it 'returns the block content' do
get '/test_block'
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'Engine test', type: :request do
describe 'engine routes' do
it 'returns some content from the engine' do
get '/my_engine/eng_route'
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'Engine extra routes', type: :request do
describe '#test' do
it 'returns the block content' do
get '/my_engine/test'
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'Additional Properties test', type: :request do
describe '#test' do
it 'returns some content' do
get '/additional_properties'
expect(response.status).to eq(200)
end
end
end
RSpec.describe 'Namespace test', type: :request do
describe '/admin/masters/extensions' do
it 'returns some content' do
get '/admin/masters/extensions'
expect(response.status).to eq(200)
end
it 'creates a content' do
post '/admin/masters/extensions'
expect(response.status).to eq(200)
end
end
end