1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(bug) soft-destroyed still reported in OpenAPI

Also: improved testing
This commit is contained in:
Sylvain 2022-11-23 11:59:06 +01:00
parent 2a4d50e7d9
commit 8eaeb2df39
15 changed files with 218 additions and 23 deletions

View File

@ -1,5 +1,7 @@
# Changelog Fab-manager
- Fix a bug: soft destroyed machines and spaces are still reported in the OpenAPI
## v5.5.5 2022 November 22
- Soft destroy of spaces and machines

View File

@ -37,11 +37,8 @@ class API::MachinesController < API::ApiController
def destroy
authorize @machine
if @machine.destroyable?
@machine.destroy
else
@machine.soft_destroy!
end
method = @machine.destroyable? ? :destroy : :soft_destroy!
@machine.send(method)
head :no_content
end

View File

@ -3,6 +3,7 @@
# API Controller for resources of type Space
class API::SpacesController < API::ApiController
before_action :authenticate_user!, except: %i[index show]
before_action :set_space, only: %i[update destroy]
respond_to :json
def index
@ -27,7 +28,6 @@ class API::SpacesController < API::ApiController
def update
authorize Space
@space = get_space
if @space.update(space_params)
render :show, status: :ok, location: @space
else
@ -36,19 +36,15 @@ class API::SpacesController < API::ApiController
end
def destroy
@space = get_space
authorize @space
if @space.destroyable?
@space.destroy
else
@space.soft_destroy!
end
method = @space.destroyable? ? :destroy : :soft_destroy!
@space.send(method)
head :no_content
end
private
def get_space
def set_space
Space.friendly.find(params[:id])
end

View File

@ -8,7 +8,7 @@ class OpenAPI::V1::MachinesController < OpenAPI::V1::BaseController
before_action :set_machine, only: %i[show update destroy]
def index
@machines = Machine.order(:created_at)
@machines = Machine.order(:created_at).where(deleted_at: nil)
end
def create
@ -28,15 +28,14 @@ class OpenAPI::V1::MachinesController < OpenAPI::V1::BaseController
end
end
def show; end
def show
head :not_found if @machine.deleted_at
end
def destroy
if @machine.destroyable?
@machine.destroy
head :no_content
else
render json: { error: 'has existing reservations' }, status: :unprocessable_entity
end
method = @machine.destroyable? ? :destroy : :soft_destroy!
@machine.send(method)
head :no_content
end
private

View File

@ -8,7 +8,7 @@ class OpenAPI::V1::SpacesController < OpenAPI::V1::BaseController
before_action :set_space, only: %i[show]
def index
@spaces = Space.order(:created_at)
@spaces = Space.order(:created_at).where(deleted_at: nil)
end
def show; end

BIN
test/fixtures/files/document.pdf vendored Normal file

Binary file not shown.

BIN
test/fixtures/files/document2.pdf vendored Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@ -167,3 +167,11 @@ statistic_sub_type_24:
label: Interrompue
created_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

View File

@ -258,3 +258,18 @@ statistic_type_sub_type_37:
created_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

View 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

View File

@ -12,6 +12,8 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
test 'list all machines' do
get '/open_api/v1/machines', headers: open_api_headers(@token)
assert_response :success
machines = json_response(response.body)
assert_not_empty machines[:machines]
end
test 'create a machine' do
@ -19,7 +21,7 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
params: {
machine: {
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...',
disabled: true
}
@ -49,4 +51,15 @@ class OpenApi::MachinesTest < ActionDispatch::IntegrationTest
delete '/open_api/v1/machines/3', headers: open_api_headers(@token)
assert_response :success
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

View 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

View File

@ -41,6 +41,10 @@ class ActiveSupport::TestCase
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s }
end
def upload_headers
{ 'Accept' => Mime[:json], 'Content-Type' => 'multipart/form-data' }
end
def open_api_headers(token)
{ 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s, 'Authorization' => "Token token=#{token}" }
end