2023-01-16 17:37:44 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class StatusesTest < ActionDispatch::IntegrationTest
|
|
|
|
def setup
|
|
|
|
@admin = User.find_by(username: 'admin')
|
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'create a status' do
|
|
|
|
post '/api/statuses',
|
|
|
|
params: {
|
2023-01-24 14:03:01 +01:00
|
|
|
name: 'Open'
|
2023-01-16 17:37:44 +01:00
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
|
|
|
assert_equal Mime[:json], response.content_type
|
|
|
|
|
|
|
|
# Check the correct status was created
|
|
|
|
res = json_response(response.body)
|
|
|
|
status = Status.where(id: res[:id]).first
|
|
|
|
assert_not_nil status, 'status was not created in database'
|
|
|
|
|
2023-01-24 14:03:01 +01:00
|
|
|
assert_equal 'Open', res[:name]
|
2023-01-16 17:37:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'update a status' do
|
|
|
|
patch '/api/statuses/1',
|
|
|
|
params: {
|
2023-01-24 14:03:01 +01:00
|
|
|
name: 'Done'
|
2023-01-16 17:37:44 +01:00
|
|
|
}.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 status was updated
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert_equal 1, res[:id]
|
2023-01-24 14:03:01 +01:00
|
|
|
assert_equal 'Done', res[:name]
|
2023-01-16 17:37:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'list all statuses' do
|
|
|
|
get '/api/statuses'
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
|
|
|
assert_equal Mime[:json], response.content_type
|
|
|
|
|
|
|
|
# Check the list items are ok
|
|
|
|
statuses = json_response(response.body)
|
|
|
|
assert_equal Status.count, statuses.count
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'delete a status' do
|
2023-01-24 14:03:01 +01:00
|
|
|
status = Status.create!(name: 'Gone too soon')
|
2023-01-16 17:37:44 +01:00
|
|
|
delete "/api/statuses/#{status.id}"
|
|
|
|
assert_response :success
|
|
|
|
assert_empty response.body
|
|
|
|
assert_raise ActiveRecord::RecordNotFound do
|
|
|
|
status.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|