diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index a8fd05452..7d8dbfac1 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -19,19 +19,15 @@ module Api end contacts = current_user_contacts.limit(limit).offset(offset) - serialized_contacts = contacts.map do |item| - serializer = Serializers::RegistrantApi::Contact.new(item) - serializer.to_json - end - + serialized_contacts = contacts.collect { |contact| serialize_contact(contact) } render json: serialized_contacts end def show - @contact = current_user_contacts.find_by(uuid: params[:uuid]) + contact = current_user_contacts.find_by(uuid: params[:uuid]) - if @contact - render json: @contact + if contact + render json: serialize_contact(contact) else render json: { errors: [{ base: ['Contact not found'] }] }, status: :not_found end @@ -89,8 +85,7 @@ module Api contact.registrar.notify(action) end - serializer = Serializers::RegistrantApi::Contact.new(contact) - render json: serializer.to_json + render json: serialize_contact(contact) end private @@ -100,6 +95,10 @@ module Api rescue CompanyRegister::NotAvailableError current_registrant_user.direct_contacts end + + def serialize_contact(contact) + Serializers::RegistrantApi::Contact.new(contact).to_json + end end end end diff --git a/test/integration/api/registrant/registrant_api_contacts_test.rb b/test/integration/api/registrant/registrant_api_contacts_test.rb index 121eb9135..0658ecac1 100644 --- a/test/integration/api/registrant/registrant_api_contacts_test.rb +++ b/test/integration/api/registrant/registrant_api_contacts_test.rb @@ -32,14 +32,6 @@ class RegistrantApiContactsTest < ApplicationIntegrationTest assert_equal 'john@inbox.test', response_json[:email] end - def test_get_contact_details_by_uuid_returns_404_for_non_existent_contact - get '/api/v1/registrant/contacts/nonexistent-uuid', {}, @auth_headers - assert_equal(404, response.status) - - response_json = JSON.parse(response.body, symbolize_names: true) - assert_equal({ errors: [{ base: ['Contact not found'] }] }, response_json) - end - def test_root_does_not_accept_limit_higher_than_200 get '/api/v1/registrant/contacts', { 'limit' => 400, 'offset' => 0 }, @auth_headers assert_equal(400, response.status) @@ -62,22 +54,6 @@ class RegistrantApiContactsTest < ApplicationIntegrationTest assert_equal({ errors: [base: ['Not authorized']] }, json_body) end - def test_details_returns_401_without_authorization - get '/api/v1/registrant/contacts/c0a191d5-3793-4f0b-8f85-491612d0293e', {}, {} - assert_equal(401, response.status) - json_body = JSON.parse(response.body, symbolize_names: true) - - assert_equal({ errors: [base: ['Not authorized']] }, json_body) - end - - def test_details_returns_404_for_non_existent_contact - get '/api/v1/registrant/contacts/some-random-uuid', {}, @auth_headers - assert_equal(404, response.status) - json_body = JSON.parse(response.body, symbolize_names: true) - - assert_equal({ errors: [base: ['Contact not found']] }, json_body) - end - private def auth_token diff --git a/test/integration/api/v1/registrant/contacts/details_test.rb b/test/integration/api/v1/registrant/contacts/details_test.rb index 24fa467e4..073c8e2b8 100644 --- a/test/integration/api/v1/registrant/contacts/details_test.rb +++ b/test/integration/api/v1/registrant/contacts/details_test.rb @@ -13,6 +13,52 @@ class RegistrantApiV1ContactDetailsTest < ActionDispatch::IntegrationTest @user = users(:registrant) end + def test_returns_contact_details + get api_v1_registrant_contact_path(@contact.uuid), nil, 'HTTP_AUTHORIZATION' => auth_token, + 'Content-Type' => Mime::JSON.to_s + + assert_response :ok + assert_equal ({ id: @contact.uuid, + name: @contact.name, + code: @contact.code, + fax: @contact.fax, + ident: { + code: @contact.ident, + type: @contact.ident_type, + country_code: @contact.ident_country_code, + }, + email: @contact.email, + phone: @contact.phone, + address: { + street: @contact.street, + zip: @contact.zip, + city: @contact.city, + state: @contact.state, + country_code: @contact.country_code, + }, + auth_info: @contact.auth_info, + statuses: @contact.statuses, + disclosed_attributes: @contact.disclosed_attributes }), + JSON.parse(response.body, symbolize_names: true) + end + + def test_non_existent_contact + get api_v1_registrant_contact_path('non-existent'), nil, 'HTTP_AUTHORIZATION' => auth_token, + 'Content-Type' => Mime::JSON.to_s + + assert_response :not_found + assert_equal({ errors: [base: ['Contact not found']] }, JSON.parse(response.body, + symbolize_names: true)) + end + + def test_anonymous_user + get api_v1_registrant_contact_path(@contact.uuid), nil, 'Content-Type' => Mime::JSON.to_s + + assert_response :unauthorized + assert_equal({ errors: [base: ['Not authorized']] }, JSON.parse(response.body, + symbolize_names: true)) + end + def test_returns_direct_contact_when_company_register_is_unavailable assert_equal '1234', @contact.ident assert_equal Contact::PRIV, @contact.ident_type @@ -25,7 +71,7 @@ class RegistrantApiV1ContactDetailsTest < ActionDispatch::IntegrationTest end response_json = JSON.parse(response.body, symbolize_names: true) - assert_equal '1234', response_json[:ident] + assert_equal '1234', response_json[:ident][:code] end def test_unmanaged_contact_cannot_be_accessed