Skip to content

Commit

Permalink
separate image upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yohei committed May 21, 2023
1 parent ad2913a commit 7e30bac
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 70 deletions.
35 changes: 29 additions & 6 deletions spec/integration_tests/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions spec/rails/app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']] }
Expand Down
182 changes: 153 additions & 29 deletions spec/rails/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
}
}
}
}
}
}
}
}
}
Loading

0 comments on commit 7e30bac

Please sign in to comment.