mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +01:00
(bug) soft-destroyed still reported in OpenAPI
Also: improved testing
This commit is contained in:
parent
2a4d50e7d9
commit
8eaeb2df39
@ -1,5 +1,7 @@
|
|||||||
# Changelog Fab-manager
|
# Changelog Fab-manager
|
||||||
|
|
||||||
|
- Fix a bug: soft destroyed machines and spaces are still reported in the OpenAPI
|
||||||
|
|
||||||
## v5.5.5 2022 November 22
|
## v5.5.5 2022 November 22
|
||||||
|
|
||||||
- Soft destroy of spaces and machines
|
- Soft destroy of spaces and machines
|
||||||
|
@ -37,11 +37,8 @@ class API::MachinesController < API::ApiController
|
|||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
authorize @machine
|
authorize @machine
|
||||||
if @machine.destroyable?
|
method = @machine.destroyable? ? :destroy : :soft_destroy!
|
||||||
@machine.destroy
|
@machine.send(method)
|
||||||
else
|
|
||||||
@machine.soft_destroy!
|
|
||||||
end
|
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
# API Controller for resources of type Space
|
# API Controller for resources of type Space
|
||||||
class API::SpacesController < API::ApiController
|
class API::SpacesController < API::ApiController
|
||||||
before_action :authenticate_user!, except: %i[index show]
|
before_action :authenticate_user!, except: %i[index show]
|
||||||
|
before_action :set_space, only: %i[update destroy]
|
||||||
respond_to :json
|
respond_to :json
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@ -27,7 +28,6 @@ class API::SpacesController < API::ApiController
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
authorize Space
|
authorize Space
|
||||||
@space = get_space
|
|
||||||
if @space.update(space_params)
|
if @space.update(space_params)
|
||||||
render :show, status: :ok, location: @space
|
render :show, status: :ok, location: @space
|
||||||
else
|
else
|
||||||
@ -36,19 +36,15 @@ class API::SpacesController < API::ApiController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@space = get_space
|
|
||||||
authorize @space
|
authorize @space
|
||||||
if @space.destroyable?
|
method = @space.destroyable? ? :destroy : :soft_destroy!
|
||||||
@space.destroy
|
@space.send(method)
|
||||||
else
|
|
||||||
@space.soft_destroy!
|
|
||||||
end
|
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def get_space
|
def set_space
|
||||||
Space.friendly.find(params[:id])
|
Space.friendly.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ class OpenAPI::V1::MachinesController < OpenAPI::V1::BaseController
|
|||||||
before_action :set_machine, only: %i[show update destroy]
|
before_action :set_machine, only: %i[show update destroy]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@machines = Machine.order(:created_at)
|
@machines = Machine.order(:created_at).where(deleted_at: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ -28,15 +28,14 @@ class OpenAPI::V1::MachinesController < OpenAPI::V1::BaseController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def show; end
|
def show
|
||||||
|
head :not_found if @machine.deleted_at
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
if @machine.destroyable?
|
method = @machine.destroyable? ? :destroy : :soft_destroy!
|
||||||
@machine.destroy
|
@machine.send(method)
|
||||||
head :no_content
|
head :no_content
|
||||||
else
|
|
||||||
render json: { error: 'has existing reservations' }, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -8,7 +8,7 @@ class OpenAPI::V1::SpacesController < OpenAPI::V1::BaseController
|
|||||||
before_action :set_space, only: %i[show]
|
before_action :set_space, only: %i[show]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@spaces = Space.order(:created_at)
|
@spaces = Space.order(:created_at).where(deleted_at: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def show; end
|
def show; end
|
||||||
|
BIN
test/fixtures/files/document.pdf
vendored
Normal file
BIN
test/fixtures/files/document.pdf
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/files/document2.pdf
vendored
Normal file
BIN
test/fixtures/files/document2.pdf
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/files/machines/Laser_cutting_machine.jpg
vendored
Normal file
BIN
test/fixtures/files/machines/Laser_cutting_machine.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 228 KiB |
BIN
test/fixtures/files/spaces/Biology_laboratory.jpg
vendored
Normal file
BIN
test/fixtures/files/spaces/Biology_laboratory.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 MiB |
8
test/fixtures/statistic_sub_types.yml
vendored
8
test/fixtures/statistic_sub_types.yml
vendored
@ -167,3 +167,11 @@ statistic_sub_type_24:
|
|||||||
label: Interrompue
|
label: Interrompue
|
||||||
created_at: 2022-10-12 08:55:05.209986 Z
|
created_at: 2022-10-12 08:55:05.209986 Z
|
||||||
updated_at: 2022-10-12 08:55:05.209986 Z
|
updated_at: 2022-10-12 08:55:05.209986 Z
|
||||||
|
|
||||||
|
statistic_sub_type_25:
|
||||||
|
id: 25
|
||||||
|
key: atelier-bois
|
||||||
|
label: Atelier Bois
|
||||||
|
created_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
updated_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
|
||||||
|
15
test/fixtures/statistic_type_sub_types.yml
vendored
15
test/fixtures/statistic_type_sub_types.yml
vendored
@ -258,3 +258,18 @@ statistic_type_sub_type_37:
|
|||||||
created_at: 2022-10-12 08:55:05.211284
|
created_at: 2022-10-12 08:55:05.211284
|
||||||
updated_at: 2022-10-12 08:55:05.211284
|
updated_at: 2022-10-12 08:55:05.211284
|
||||||
|
|
||||||
|
statistic_type_sub_type_38:
|
||||||
|
id: 38
|
||||||
|
statistic_type_id: 12
|
||||||
|
statistic_sub_type_id: 25
|
||||||
|
created_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
updated_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
|
||||||
|
statistic_type_sub_type_39:
|
||||||
|
id: 39
|
||||||
|
statistic_type_id: 13
|
||||||
|
statistic_sub_type_id: 25
|
||||||
|
created_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
updated_at: 2022-11-23 11:51:14.651651 Z
|
||||||
|
|
||||||
|
|
||||||
|
85
test/integration/machines_test.rb
Normal file
85
test/integration/machines_test.rb
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class MachinesTest < ActionDispatch::IntegrationTest
|
||||||
|
def setup
|
||||||
|
@admin = User.find_by(username: 'admin')
|
||||||
|
login_as(@admin, scope: :user)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'create a machine' do
|
||||||
|
name = 'IJFX 350 Laser'
|
||||||
|
post '/api/machines',
|
||||||
|
params: {
|
||||||
|
machine: {
|
||||||
|
name: name,
|
||||||
|
machine_image_attributes: {
|
||||||
|
attachment: fixture_file_upload('/files/machines/Laser_cutting_machine.jpg')
|
||||||
|
},
|
||||||
|
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore...',
|
||||||
|
spec: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...',
|
||||||
|
machine_files_attributes: [
|
||||||
|
{ attachment: fixture_file_upload('/files/document.pdf', 'document/pdf', true) },
|
||||||
|
{ attachment: fixture_file_upload('/files/document2.pdf', 'document/pdf', true) }
|
||||||
|
],
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
headers: upload_headers
|
||||||
|
|
||||||
|
# Check response format & status
|
||||||
|
assert_equal 201, response.status, response.body
|
||||||
|
assert_equal Mime[:json], response.content_type
|
||||||
|
|
||||||
|
# Check the machine was correctly created
|
||||||
|
db_machine = Machine.where(name: name).first
|
||||||
|
assert_not_nil db_machine
|
||||||
|
assert_not_nil db_machine.machine_image.attachment
|
||||||
|
assert_not_nil db_machine.machine_files[0].attachment
|
||||||
|
assert_not_nil db_machine.machine_files[1].attachment
|
||||||
|
assert_equal name, db_machine.name
|
||||||
|
assert_not_empty db_machine.spec
|
||||||
|
assert_not_empty db_machine.description
|
||||||
|
assert_not db_machine.disabled
|
||||||
|
assert_nil db_machine.deleted_at
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'update a machine' do
|
||||||
|
description = '<p>lorem ipsum <strong>dolor</strong> sit amet</p>'
|
||||||
|
put '/api/machines/3',
|
||||||
|
params: {
|
||||||
|
machine: {
|
||||||
|
description: description
|
||||||
|
}
|
||||||
|
}.to_json,
|
||||||
|
headers: default_headers
|
||||||
|
|
||||||
|
# Check response format & status
|
||||||
|
assert_equal 200, response.status, response.body
|
||||||
|
assert_equal Mime[:json], response.content_type
|
||||||
|
|
||||||
|
# Check the machine was correctly updated
|
||||||
|
db_machine = Machine.find(3)
|
||||||
|
assert_equal description, db_machine.description
|
||||||
|
machine = json_response(response.body)
|
||||||
|
assert_equal description, machine[:description]
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'delete a machine' do
|
||||||
|
delete '/api/machines/3', headers: default_headers
|
||||||
|
assert_response :success
|
||||||
|
assert_empty response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'soft delete a machine' do
|
||||||
|
machine = Machine.find(4)
|
||||||
|
assert_not machine.destroyable?
|
||||||
|
delete '/api/machines/4', headers: default_headers
|
||||||
|
assert_response :success
|
||||||
|
assert_empty response.body
|
||||||
|
|
||||||
|
machine.reload
|
||||||
|
assert_not_nil machine.deleted_at
|
||||||
|
end
|
||||||
|
end
|
@ -12,6 +12,8 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
|
|||||||
test 'list all machines' do
|
test 'list all machines' do
|
||||||
get '/open_api/v1/machines', headers: open_api_headers(@token)
|
get '/open_api/v1/machines', headers: open_api_headers(@token)
|
||||||
assert_response :success
|
assert_response :success
|
||||||
|
machines = json_response(response.body)
|
||||||
|
assert_not_empty machines[:machines]
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'create a machine' do
|
test 'create a machine' do
|
||||||
@ -19,7 +21,7 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
|
|||||||
params: {
|
params: {
|
||||||
machine: {
|
machine: {
|
||||||
name: 'IJFX 350 Laser',
|
name: 'IJFX 350 Laser',
|
||||||
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore...',
|
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et...',
|
||||||
spec: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...',
|
spec: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...',
|
||||||
disabled: true
|
disabled: true
|
||||||
}
|
}
|
||||||
@ -49,4 +51,15 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
|
|||||||
delete '/open_api/v1/machines/3', headers: open_api_headers(@token)
|
delete '/open_api/v1/machines/3', headers: open_api_headers(@token)
|
||||||
assert_response :success
|
assert_response :success
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'soft delete a machine' do
|
||||||
|
assert_not Machine.find(4).destroyable?
|
||||||
|
delete '/open_api/v1/machines/4', headers: open_api_headers(@token)
|
||||||
|
assert_response :success
|
||||||
|
get '/open_api/v1/machines/4', headers: open_api_headers(@token)
|
||||||
|
assert_response :not_found
|
||||||
|
get '/open_api/v1/machines', headers: open_api_headers(@token)
|
||||||
|
machines = json_response(response.body)
|
||||||
|
assert_not(machines[:machines].any? { |m| m[:id] == 4 })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
76
test/integration/spaces_test.rb
Normal file
76
test/integration/spaces_test.rb
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class SpacesTest < ActionDispatch::IntegrationTest
|
||||||
|
def setup
|
||||||
|
@admin = User.find_by(username: 'admin')
|
||||||
|
login_as(@admin, scope: :user)
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'create a space' do
|
||||||
|
name = 'Biolab'
|
||||||
|
post '/api/spaces',
|
||||||
|
params: {
|
||||||
|
space: {
|
||||||
|
name: name,
|
||||||
|
space_image_attributes: {
|
||||||
|
attachment: fixture_file_upload('/files/spaces/Biology_laboratory.jpg')
|
||||||
|
},
|
||||||
|
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ante mi, porta ac dictum quis, feugiat...',
|
||||||
|
characteristics: 'Sed fermentum ante ut elit lobortis, id auctor libero cursus. Sed augue lectus, mollis at luctus eu...',
|
||||||
|
default_places: 6,
|
||||||
|
space_files_attributes: [
|
||||||
|
{ attachment: fixture_file_upload('/files/document.pdf', 'document/pdf', true) },
|
||||||
|
{ attachment: fixture_file_upload('/files/document2.pdf', 'document/pdf', true) }
|
||||||
|
],
|
||||||
|
disabled: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
headers: upload_headers
|
||||||
|
|
||||||
|
# Check response format & status
|
||||||
|
assert_equal 201, response.status, response.body
|
||||||
|
assert_equal Mime[:json], response.content_type
|
||||||
|
|
||||||
|
# Check the space was correctly created
|
||||||
|
db_space = Space.where(name: name).first
|
||||||
|
assert_not_nil db_space
|
||||||
|
assert_not_nil db_space.space_image.attachment
|
||||||
|
assert_not_nil db_space.space_files[0].attachment
|
||||||
|
assert_not_nil db_space.space_files[1].attachment
|
||||||
|
assert_equal name, db_space.name
|
||||||
|
assert_equal 6, db_space.default_places
|
||||||
|
assert_not_empty db_space.characteristics
|
||||||
|
assert_not_empty db_space.description
|
||||||
|
assert_not db_space.disabled
|
||||||
|
assert_nil db_space.deleted_at
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'update a space' do
|
||||||
|
description = '<p>lorem ipsum <strong>dolor</strong> sit amet</p>'
|
||||||
|
put '/api/spaces/1',
|
||||||
|
params: {
|
||||||
|
space: {
|
||||||
|
description: description
|
||||||
|
}
|
||||||
|
}.to_json,
|
||||||
|
headers: default_headers
|
||||||
|
|
||||||
|
# Check response format & status
|
||||||
|
assert_equal 200, response.status, response.body
|
||||||
|
assert_equal Mime[:json], response.content_type
|
||||||
|
|
||||||
|
# Check the space was correctly updated
|
||||||
|
db_space = Space.find(1)
|
||||||
|
assert_equal description, db_space.description
|
||||||
|
space = json_response(response.body)
|
||||||
|
assert_equal description, space[:description]
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'delete a space' do
|
||||||
|
delete '/api/spaces/1', headers: default_headers
|
||||||
|
assert_response :success
|
||||||
|
assert_empty response.body
|
||||||
|
end
|
||||||
|
end
|
@ -41,6 +41,10 @@ class ActiveSupport::TestCase
|
|||||||
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s }
|
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def upload_headers
|
||||||
|
{ 'Accept' => Mime[:json], 'Content-Type' => 'multipart/form-data' }
|
||||||
|
end
|
||||||
|
|
||||||
def open_api_headers(token)
|
def open_api_headers(token)
|
||||||
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s, 'Authorization' => "Token token=#{token}" }
|
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s, 'Authorization' => "Token token=#{token}" }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user