require "rails_helper" RSpec.describe AccountAuthsController, type: :controller do render_views let(:current_user) { create(:user) } let(:other_user) { create(:user, :accountless) } before do sign_in current_user end describe "#index" do it "responds successfully" do get :index expect(response).to be_ok end it "includes all users in the table" do account = current_user.primary_account associate = create(:user, :associate, primary_account: account) project_manager = create(:user, :manager, primary_account: account) account_manager = create(:user, :account_manager, primary_account: account) get :index expect(response.body).to have_content(associate.email) expect(response.body).to have_content(project_manager.email) expect(response.body).to have_content(account_manager.email) end end describe "#create" do let(:new_account) { create(:account) } it "responds successfully" do post :create, params: { account_auth: account_auth_params(other_user, new_account) } expect(response).to redirect_to account_auths_path({ account_id: new_account.id }) end it "creates a new record" do expect { post :create, params: { account_auth: account_auth_params(other_user, new_account) } }.to change(AccountAuth, :count).by(1) end end describe "#update" do let(:account_auth) { current_user.account_auths.first } it "responds with redirect" do patch :update, params: { id: account_auth, account_auth: { role: "project_manager" } } expect(response).to be_redirect expect(response).to redirect_to(account_auths_path) expect(flash.notice).to be_present end it "updates the record" do patch :update, params: { id: account_auth, account_auth: { role: "project_manager" } } expect(account_auth.reload.role).to eq "project_manager" end context "when record could not be updated" do before do allow_any_instance_of(AccountAuth).to receive(:update).and_return(false) end it "responds with redirect" do patch :update, params: { id: account_auth, account_auth: { role: "project_manager" } } expect(response).to be_redirect expect(response).to redirect_to(account_auths_path) expect(flash.alert).to be_present end end end describe "#destroy" do let(:account_auth) { create(:account_auth, account: current_user.primary_account, user: other_user, role: :account_manager) } it "responds with redirect" do delete :destroy, params: { id: account_auth.id } expect(response).to be_redirect expect(response).to redirect_to(account_auths_path) expect(flash.alert).to be_present end end private def account_auth_params(user, account) { user_email: user.email, account_id: account.id } end end