diff --git a/app/resources/api/v2/tube_purpose_resource.rb b/app/resources/api/v2/tube_purpose_resource.rb index 594ed7bcc2..aa08d628d1 100644 --- a/app/resources/api/v2/tube_purpose_resource.rb +++ b/app/resources/api/v2/tube_purpose_resource.rb @@ -22,6 +22,26 @@ class TubePurposeResource < BaseResource # @!attribute [rw] # @return [String] The target type. attribute :target_type + + # @!attribute [r] + # @return [String] The UUID of the tube purpose. + attribute :uuid + + # Gets the list of fields which are creatable on a TubePurpose. + # + # @param _context [JSONAPI::Resource::Context] not used + # @return [Array] the list of creatable fields. + def self.creatable_fields(_context) + super - %i[uuid] # Do not allow creating with any readonly fields + end + + # Gets the list of fields which are updatable on an existing TubePurpose. + # + # @param _context [JSONAPI::Resource::Context] not used + # @return [Array] the list of updatable fields. + def self.updatable_fields(_context) + super - %i[uuid] # Do not allow creating with any readonly fields + end end end end diff --git a/spec/requests/api/v2/tube_purposes_spec.rb b/spec/requests/api/v2/tube_purposes_spec.rb index f1f826846d..1bcf67ae58 100644 --- a/spec/requests/api/v2/tube_purposes_spec.rb +++ b/spec/requests/api/v2/tube_purposes_spec.rb @@ -29,6 +29,7 @@ expect(json.dig('data', 'attributes', 'name')).to eq(resource_model.name) expect(json.dig('data', 'attributes', 'purpose_type')).to eq(resource_model.type) expect(json.dig('data', 'attributes', 'target_type')).to eq(resource_model.target_type) + expect(json.dig('data', 'attributes', 'uuid')).to eq(resource_model.uuid) end end @@ -56,6 +57,7 @@ expect(json.dig('data', 'attributes', 'name')).to eq(updated_name) expect(json.dig('data', 'attributes', 'purpose_type')).to eq(resource_model.type) expect(json.dig('data', 'attributes', 'target_type')).to eq(resource_model.target_type) + expect(json.dig('data', 'attributes', 'uuid')).to eq(resource_model.uuid) end end @@ -80,6 +82,7 @@ expect(json.dig('data', 'attributes', 'name')).to eq(resource_model.name) expect(json.dig('data', 'attributes', 'purpose_type')).to eq(updated_purpose_type) expect(json.dig('data', 'attributes', 'target_type')).to eq(resource_model.target_type) + expect(json.dig('data', 'attributes', 'uuid')).to eq(resource_model.uuid) end end @@ -104,6 +107,27 @@ expect(json.dig('data', 'attributes', 'name')).to eq(resource_model.name) expect(json.dig('data', 'attributes', 'purpose_type')).to eq(resource_model.type) expect(json.dig('data', 'attributes', 'target_type')).to eq(updated_target_type) + expect(json.dig('data', 'attributes', 'uuid')).to eq(resource_model.uuid) + end + end + + context 'when patching the uuid' do + let(:updated_uuid) { 'new-uuid' } + let(:payload) do + { + 'data' => { + 'id' => resource_model.id, + 'type' => 'tube_purposes', + 'attributes' => { + 'uuid' => updated_uuid + } + } + } + end + + it 'responds with 400 bad request, because uuid is read-only' do + api_patch "#{base_endpoint}/#{resource_model.id}", payload + expect(response).to have_http_status(:bad_request) end end end @@ -151,5 +175,26 @@ expect(response).to have_http_status(:unprocessable_entity) end end + + context 'with an invalid payload (includes uuid)' do + let(:payload) do + { + 'data' => { + 'type' => 'tube_purposes', + 'attributes' => { + 'name' => 'New Name', + 'purpose_type' => 'Test Purpose Type', + 'target_type' => 'MultiplexedLibraryTube', + 'uuid' => 'new-uuid' + } + } + } + end + + it 'responds with 400 bad request, because uuid is read-only' do + api_post base_endpoint, payload + expect(response).to have_http_status(:bad_request) + end + end end end diff --git a/spec/resources/api/v2/tube_purpose_resource_spec.rb b/spec/resources/api/v2/tube_purpose_resource_spec.rb index b810069d43..8dff614633 100644 --- a/spec/resources/api/v2/tube_purpose_resource_spec.rb +++ b/spec/resources/api/v2/tube_purpose_resource_spec.rb @@ -14,6 +14,7 @@ expect(resource).to have_attribute :name expect(resource).to have_attribute :purpose_type expect(resource).to have_attribute :target_type + expect(resource).to have_attribute :uuid end # Updatable fields @@ -21,6 +22,7 @@ expect(resource).to have_updatable_field :name expect(resource).to have_updatable_field :purpose_type expect(resource).to have_updatable_field :target_type + expect(resource).not_to have_updatable_field :uuid end # Filters