From 67e41092e6eb1271e91d75f20cafee0d7308e69d Mon Sep 17 00:00:00 2001 From: Tim Lange Date: Sat, 8 Feb 2020 08:51:41 +0100 Subject: [PATCH] Chore: Added tests for inbox_members (#478) --- .../api/v1/inbox_members_controller_spec.rb | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 spec/controllers/api/v1/inbox_members_controller_spec.rb diff --git a/spec/controllers/api/v1/inbox_members_controller_spec.rb b/spec/controllers/api/v1/inbox_members_controller_spec.rb new file mode 100644 index 000000000..66a3db867 --- /dev/null +++ b/spec/controllers/api/v1/inbox_members_controller_spec.rb @@ -0,0 +1,81 @@ +require 'rails_helper' + +RSpec.describe 'Inbox Member API', type: :request do + let(:account) { create(:account) } + let(:inbox) { create(:inbox, account: account) } + + describe 'POST /api/v1/inbox_members' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post '/api/v1/inbox_members' + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'modifies inbox members' do + params = { inbox_id: inbox.id, user_ids: [agent.id] } + + post '/api/v1/inbox_members', + headers: agent.create_new_auth_token, + params: params, + as: :json + + expect(response).to have_http_status(:success) + expect(inbox.inbox_members&.count).to eq(1) + expect(inbox.inbox_members&.first&.user).to eq(agent) + end + + it 'renders not found when inbox not found' do + params = { inbox_id: nil, user_ids: [agent.id] } + + post '/api/v1/inbox_members', + headers: agent.create_new_auth_token, + params: params, + as: :json + + expect(response).to have_http_status(:not_found) + end + + it 'renders error on invalid params' do + params = { inbox_id: inbox.id, user_ids: ['invalid'] } + + post '/api/v1/inbox_members', + headers: agent.create_new_auth_token, + params: params, + as: :json + + expect(response).to have_http_status(:unprocessable_entity) + expect(response.body).to include('Could not add agents to inbox') + end + end + end + + describe 'GET /api/v1/inbox_members/:id' do + let(:inbox_member) { create(:inbox_member, inbox: inbox) } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + get "/api/v1/inbox_members/#{inbox_member.id}" + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:agent) { create(:user, account: account, role: :agent) } + + it 'returns inbox member' do + get "/api/v1/inbox_members/#{inbox.id}", + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(JSON.parse(response.body)).to eq({ payload: inbox.inbox_members }.as_json) + end + end + end +end