From 7e30bac3b2f02b1eecdc37af222b0e17b94931fc Mon Sep 17 00:00:00 2001 From: t0yohei Date: Mon, 22 May 2023 00:57:59 +0900 Subject: [PATCH] separate image upload tests --- spec/integration_tests/rails_test.rb | 35 +++- .../app/controllers/images_controller.rb | 24 ++- spec/rails/config/routes.rb | 8 +- spec/rails/doc/openapi.json | 182 +++++++++++++++--- spec/rails/doc/openapi.yaml | 120 +++++++++--- spec/requests/rails_spec.rb | 53 ++++- 6 files changed, 352 insertions(+), 70 deletions(-) diff --git a/spec/integration_tests/rails_test.rb b/spec/integration_tests/rails_test.rb index 42f40c03..4fb48930 100644 --- a/spec/integration_tests/rails_test.rb +++ b/spec/integration_tests/rails_test.rb @@ -103,12 +103,8 @@ class TablesCreateTest < ActionDispatch::IntegrationTest class TablesUpdateTest < ActionDispatch::IntegrationTest openapi! - test 'returns a table with array' do - png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP - QBoAAAAASUVORK5CYII='.unpack1('m') - File.binwrite('test.png', png) - image = Rack::Test::UploadedFile.new('test.png', 'image/png') - patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { images: [image, image] } + test 'returns a table' do + patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { name: 'test' } assert_response 200 end end @@ -139,6 +135,33 @@ class ImageTest < ActionDispatch::IntegrationTest 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 end class ExtraRoutesTest < ActionDispatch::IntegrationTest diff --git a/spec/rails/app/controllers/images_controller.rb b/spec/rails/app/controllers/images_controller.rb index a750581c..bcb49eb7 100644 --- a/spec/rails/app/controllers/images_controller.rb +++ b/spec/rails/app/controllers/images_controller.rb @@ -1,8 +1,6 @@ class ImagesController < ApplicationController def show - png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP - QBoAAAAASUVORK5CYII='.unpack('m').first - send_data png, type: 'image/png', disposition: 'inline' + send_image end def index @@ -14,4 +12,24 @@ def index ] render json: list end + + def upload + send_image + end + + def upload_nested + send_image + end + + def upload_multiple + send_image + end + + private + + def send_image + png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP + QBoAAAAASUVORK5CYII='.unpack('m').first + send_data png, type: 'image/png', disposition: 'inline' + end end diff --git a/spec/rails/config/routes.rb b/spec/rails/config/routes.rb index 5d76adf4..ad083baf 100644 --- a/spec/rails/config/routes.rb +++ b/spec/rails/config/routes.rb @@ -3,7 +3,13 @@ defaults format: 'json' do resources :tables, only: [:index, :show, :create, :update, :destroy] - resources :images, only: [:index, :show] + resources :images, only: [:index, :show] do + collection do + post 'upload' + post 'upload_nested' + post 'upload_multiple' + end + end resources :users, only: [:show, :create] get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] } diff --git a/spec/rails/doc/openapi.json b/spec/rails/doc/openapi.json index a12acd76..9cccb728 100644 --- a/spec/rails/doc/openapi.json +++ b/spec/rails/doc/openapi.json @@ -455,50 +455,27 @@ ], "requestBody": { "content": { - "multipart/form-data": { + "application/x-www-form-urlencoded": { "schema": { "type": "object", "properties": { - "nested": { - "type": "object", - "properties": { - "image": { - "type": "string", - "format": "binary" - }, - "caption": { - "type": "string" - } - }, - "required": [ - "image", - "caption" - ] - }, - "images": { - "type": "array", - "items": { - "type": "string", - "format": "binary" - } + "name": { + "type": "string" } }, "required": [ - "images" + "name" ] }, "example": { - "images": [ - "test.png", - "test.png" - ] + "name": "test" } } } }, "responses": { "200": { - "description": "returns a table with array", + "description": "returns a table", "content": { "application/json": { "schema": { @@ -804,6 +781,153 @@ } } } + }, + "/images/upload_multiple": { + "post": { + "summary": "upload_multiple", + "tags": [ + "Image" + ], + "requestBody": { + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": { + "images": { + "type": "array", + "items": { + "type": "string", + "format": "binary" + } + } + }, + "required": [ + "images" + ] + }, + "example": { + "images": [ + "test.png", + "test.png" + ] + } + } + } + }, + "responses": { + "200": { + "description": "returns a image payload with upload multiple", + "content": { + "image/png": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + } + } + } + }, + "/images/upload_nested": { + "post": { + "summary": "upload_nested", + "tags": [ + "Image" + ], + "requestBody": { + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": { + "nested_image": { + "type": "object", + "properties": { + "image": { + "type": "string", + "format": "binary" + }, + "caption": { + "type": "string" + } + }, + "required": [ + "image", + "caption" + ] + } + }, + "required": [ + "nested_image" + ] + }, + "example": { + "nested_image": { + "image": "test.png", + "caption": "Some caption" + } + } + } + } + }, + "responses": { + "200": { + "description": "returns a image payload with upload nested", + "content": { + "image/png": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + } + } + } + }, + "/images/upload": { + "post": { + "summary": "upload", + "tags": [ + "Image" + ], + "requestBody": { + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": { + "image": { + "type": "string", + "format": "binary" + } + }, + "required": [ + "image" + ] + }, + "example": { + "image": "test.png" + } + } + } + }, + "responses": { + "200": { + "description": "returns a image payload with upload", + "content": { + "image/png": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + } + } + } } } } \ No newline at end of file diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index f9488a6a..d37600f5 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -311,35 +311,19 @@ paths: example: 1 requestBody: content: - multipart/form-data: + application/x-www-form-urlencoded: schema: type: object properties: - nested: - type: object - properties: - image: - type: string - format: binary - caption: - type: string - required: - - image - - caption - images: - type: array - items: - type: string - format: binary + name: + type: string required: - - images + - name example: - images: - - test.png - - test.png + name: test responses: '200': - description: returns a table with array + description: returns a table content: application/json: schema: @@ -538,3 +522,95 @@ paths: example: - name: file.png tags: [] + "/images/upload_multiple": + post: + summary: upload_multiple + tags: + - Image + requestBody: + content: + multipart/form-data: + schema: + type: object + properties: + images: + type: array + items: + type: string + format: binary + required: + - images + example: + images: + - test.png + - test.png + responses: + '200': + description: returns a image payload with upload multiple + content: + image/png: + schema: + type: string + format: binary + "/images/upload": + post: + summary: upload + tags: + - Image + requestBody: + content: + multipart/form-data: + schema: + type: object + properties: + image: + type: string + format: binary + required: + - image + example: + image: test.png + responses: + '200': + description: returns a image payload with upload + content: + image/png: + schema: + type: string + format: binary + "/images/upload_nested": + post: + summary: upload_nested + tags: + - Image + requestBody: + content: + multipart/form-data: + schema: + type: object + properties: + nested_image: + type: object + properties: + image: + type: string + format: binary + caption: + type: string + required: + - image + - caption + required: + - nested_image + example: + nested_image: + image: test.png + caption: Some caption + responses: + '200': + description: returns a image payload with upload nested + content: + image/png: + schema: + type: string + format: binary diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index 0256a245..d97e1512 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -84,15 +84,8 @@ end describe '#update' 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 table with array' do - patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { images: [image, image] } + it 'returns a table' do + patch '/tables/1', headers: { authorization: 'k0kubun' }, params: { name: 'test' } expect(response.status).to eq(200) end end @@ -124,6 +117,48 @@ 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 end RSpec.describe 'Extra routes', type: :request do