From 60547b9fb3c5ac62cf16cc59c72fb772c1339439 Mon Sep 17 00:00:00 2001 From: Tim Lange Date: Mon, 17 Feb 2020 12:38:29 +0100 Subject: [PATCH] Chore: Added tests for contacts_controller (#509) - Tests for contacts controller Addresses: #37 --- app/views/api/v1/contacts/index.json.jbuilder | 2 +- .../api/v1/contacts/update.json.jbuilder | 2 +- .../api/v1/contacts_controller_spec.rb | 122 ++++++++++++++++++ spec/factories/contacts.rb | 1 + spec/rails_helper.rb | 5 + 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 spec/controllers/api/v1/contacts_controller_spec.rb diff --git a/app/views/api/v1/contacts/index.json.jbuilder b/app/views/api/v1/contacts/index.json.jbuilder index 519940354..a162fdd82 100644 --- a/app/views/api/v1/contacts/index.json.jbuilder +++ b/app/views/api/v1/contacts/index.json.jbuilder @@ -4,7 +4,7 @@ json.payload do json.name contact.name json.email contact.email json.phone_number contact.phone_number - json.thumbnail contact.avatar.profile_thumb.url + json.thumbnail contact.avatar_url json.additional_attributes contact.additional_attributes end end diff --git a/app/views/api/v1/contacts/update.json.jbuilder b/app/views/api/v1/contacts/update.json.jbuilder index c97f7b68c..086375760 100644 --- a/app/views/api/v1/contacts/update.json.jbuilder +++ b/app/views/api/v1/contacts/update.json.jbuilder @@ -3,6 +3,6 @@ json.payload do json.name @contact.name json.email @contact.email json.phone_number @contact.phone_number - json.thumbnail @contact.avatar.thumb.url + json.thumbnail @contact.avatar_url json.additional_attributes @contact.additional_attributes end diff --git a/spec/controllers/api/v1/contacts_controller_spec.rb b/spec/controllers/api/v1/contacts_controller_spec.rb new file mode 100644 index 000000000..93146423b --- /dev/null +++ b/spec/controllers/api/v1/contacts_controller_spec.rb @@ -0,0 +1,122 @@ +require 'rails_helper' + +RSpec.describe 'Contacts API', type: :request do + let(:account) { create(:account) } + + describe 'GET /api/v1/contacts' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get '/api/v1/contacts' + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + let!(:contact) { create(:contact, account: account) } + + it 'returns all contacts' do + get '/api/v1/contacts', + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(contact.email) + end + end + end + + describe 'GET /api/v1/contacts/:id' do + let!(:contact) { create(:contact, account: account) } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get "/api/v1/contacts/#{contact.id}" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + + it 'shows the contact' do + get "/api/v1/contacts/#{contact.id}", + headers: admin.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.body).to include(contact.email) + end + end + end + + describe 'POST /api/v1/contacts' do + let(:valid_params) { { contact: { account_id: account.id } } } + + context 'when it is an unauthenticated user' do + it 'creates the contact' do + post '/api/v1/contacts', + params: valid_params + + expect(response).to have_http_status(:success) + expect(Contact.count).to eq(1) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + + it 'creates the contact' do + post '/api/v1/contacts', + headers: admin.create_new_auth_token, + params: valid_params, + as: :json + + expect(response).to have_http_status(:success) + expect(Contact.count).to eq(1) + end + end + end + + describe 'PATCH /api/v1/contacts/:id' do + let!(:contact) { create(:contact, account: account) } + let(:valid_params) { { contact: { name: 'Test Blub' } } } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + put "/api/v1/contacts/#{contact.id}", + params: valid_params + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + + it 'updates the contact' do + patch "/api/v1/contacts/#{contact.id}", + headers: admin.create_new_auth_token, + params: valid_params, + as: :json + + expect(response).to have_http_status(:success) + expect(Contact.last.name).to eq('Test Blub') + end + + it 'prevents the update of contact of another account' do + other_account = create(:account) + other_contact = create(:contact, account: other_account) + + patch "/api/v1/contacts/#{other_contact.id}", + headers: admin.create_new_auth_token, + params: valid_params, + as: :json + + expect(response).to have_http_status(:not_found) + end + end + end +end diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb index c3e416b8c..ec5046fbf 100644 --- a/spec/factories/contacts.rb +++ b/spec/factories/contacts.rb @@ -5,6 +5,7 @@ FactoryBot.define do sequence(:name) { |n| "Widget #{n}" } sequence(:email) { |n| "widget-#{n}@example.com" } phone_number { '+123456789011' } + avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') } account end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 1693f1e76..0c0d89421 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -69,3 +69,8 @@ Shoulda::Matchers.configure do |config| with.library :rails end end + +# Required for fixture_file_upload +FactoryBot::SyntaxRunner.class_eval do + include ActionDispatch::TestProcess +end