/
blogs_spec.rb
107 lines (85 loc) · 2.88 KB
/
blogs_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
require 'swagger_helper'
RSpec.describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
let(:api_key) { 'fake_key' }
path '/blogs' do
post 'Creates a blog' do
tags 'Blogs'
description 'Creates a new blog from provided data'
operationId 'createBlog'
consumes 'application/json'
produces 'application/json'
parameter name: :blog, in: :body, schema: { '$ref' => '#/definitions/blog' }
let(:blog) { { title: 'foo', content: 'bar' } }
response '201', 'blog created' do
run_test!
end
response '422', 'invalid request' do
schema '$ref' => '#/definitions/errors_object'
let(:blog) { { title: 'foo' } }
run_test! do |response|
expect(response.body).to include("can't be blank")
end
end
end
get 'Searches blogs' do
tags 'Blogs'
description 'Searches blogs by keywords'
operationId 'searchBlogs'
produces 'application/json'
parameter name: :keywords, in: :query, type: 'string'
let(:keywords) { 'foo bar' }
response '200', 'success' do
schema type: 'array', items: { '$ref' => '#/definitions/blog' }
end
response '406', 'unsupported accept header' do
let(:'Accept') { 'application/foo' }
run_test!
end
end
end
path '/blogs/{id}' do
parameter name: :id, in: :path, type: :string
let(:id) { blog.id }
let(:blog) { Blog.create(title: 'foo', content: 'bar', thumbnail: 'thumbnail.png') }
get 'Retrieves a blog' do
tags 'Blogs'
description 'Retrieves a specific blog by id'
operationId 'getBlog'
produces 'application/json'
response '200', 'blog found' do
header 'ETag', type: :string
header 'Last-Modified', type: :string
header 'Cache-Control', type: :string
schema '$ref' => '#/definitions/blog'
examples 'application/json' => {
id: 1,
title: 'Hello world!',
content: 'Hello world and hello universe. Thank you all very much!!!',
thumbnail: "thumbnail.png"
}
let(:id) { blog.id }
run_test!
end
response '404', 'blog not found' do
let(:id) { 'invalid' }
run_test!
end
end
end
path '/blogs/{id}/upload' do
parameter name: :id, in: :path, type: :string
let(:id) { blog.id }
let(:blog) { Blog.create(title: 'foo', content: 'bar') }
put 'Uploads a blog thumbnail' do
tags 'Blogs'
description 'Upload a thumbnail for specific blog by id'
operationId 'uploadThumbnailBlog'
consumes 'multipart/form-data'
parameter name: :file, :in => :formData, :type => :file, required: true
response '200', 'blog updated' do
let(:file) { Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/thumbnail.png")) }
run_test!
end
end
end
end