|
| 1 | +RSpec.describe OrganizationAlliancesController do |
| 2 | + let(:organization) { Fabricate(:organization) } |
| 3 | + let(:other_organization) { Fabricate(:organization) } |
| 4 | + let(:member) { Fabricate(:member, organization: organization, manager: true) } |
| 5 | + let(:user) { member.user } |
| 6 | + |
| 7 | + before do |
| 8 | + login(user) |
| 9 | + end |
| 10 | + |
| 11 | + describe "GET #index" do |
| 12 | + let!(:pending_sent) { |
| 13 | + OrganizationAlliance.create!( |
| 14 | + source_organization: organization, |
| 15 | + target_organization: other_organization, |
| 16 | + status: "pending" |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + let!(:pending_received) { |
| 21 | + OrganizationAlliance.create!( |
| 22 | + source_organization: Fabricate(:organization), |
| 23 | + target_organization: organization, |
| 24 | + status: "pending" |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + let!(:accepted) { |
| 29 | + OrganizationAlliance.create!( |
| 30 | + source_organization: organization, |
| 31 | + target_organization: Fabricate(:organization), |
| 32 | + status: "accepted" |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + let!(:rejected) { |
| 37 | + OrganizationAlliance.create!( |
| 38 | + source_organization: organization, |
| 39 | + target_organization: Fabricate(:organization), |
| 40 | + status: "rejected" |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + it "lists pending alliances by default" do |
| 45 | + get :index |
| 46 | + |
| 47 | + expect(assigns(:status)).to eq("pending") |
| 48 | + expect(assigns(:alliances)).to include(pending_sent, pending_received) |
| 49 | + expect(assigns(:alliances)).not_to include(accepted, rejected) |
| 50 | + end |
| 51 | + |
| 52 | + it "lists accepted alliances when status is accepted" do |
| 53 | + get :index, params: { status: "accepted" } |
| 54 | + |
| 55 | + expect(assigns(:status)).to eq("accepted") |
| 56 | + expect(assigns(:alliances)).to include(accepted) |
| 57 | + expect(assigns(:alliances)).not_to include(pending_sent, pending_received, rejected) |
| 58 | + end |
| 59 | + |
| 60 | + it "lists rejected alliances when status is rejected" do |
| 61 | + get :index, params: { status: "rejected" } |
| 62 | + |
| 63 | + expect(assigns(:status)).to eq("rejected") |
| 64 | + expect(assigns(:alliances)).to include(rejected) |
| 65 | + expect(assigns(:alliances)).not_to include(pending_sent, pending_received, accepted) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "POST #create" do |
| 70 | + it "creates a new alliance" do |
| 71 | + expect { |
| 72 | + post :create, params: { organization_alliance: { target_organization_id: other_organization.id } } |
| 73 | + }.to change(OrganizationAlliance, :count).by(1) |
| 74 | + |
| 75 | + expect(flash[:notice]).to eq(I18n.t("organization_alliances.created")) |
| 76 | + expect(response).to redirect_to(organizations_path) |
| 77 | + end |
| 78 | + |
| 79 | + it "sets flash error if alliance cannot be created" do |
| 80 | + # Try to create alliance with self which is invalid |
| 81 | + allow_any_instance_of(OrganizationAlliance).to receive(:save).and_return(false) |
| 82 | + allow_any_instance_of(OrganizationAlliance).to receive_message_chain(:errors, :full_messages, :to_sentence).and_return("Error message") |
| 83 | + |
| 84 | + post :create, params: { organization_alliance: { target_organization_id: organization.id } } |
| 85 | + |
| 86 | + expect(flash[:error]).to eq("Error message") |
| 87 | + expect(response).to redirect_to(organizations_path) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + describe "PUT #update" do |
| 92 | + let!(:alliance) { |
| 93 | + OrganizationAlliance.create!( |
| 94 | + source_organization: Fabricate(:organization), |
| 95 | + target_organization: organization, |
| 96 | + status: "pending" |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + it "updates alliance status to accepted" do |
| 101 | + put :update, params: { id: alliance.id, status: "accepted" } |
| 102 | + |
| 103 | + alliance.reload |
| 104 | + expect(alliance).to be_accepted |
| 105 | + expect(flash[:notice]).to eq(I18n.t("organization_alliances.updated")) |
| 106 | + expect(response).to redirect_to(organization_alliances_path) |
| 107 | + end |
| 108 | + |
| 109 | + it "updates alliance status to rejected" do |
| 110 | + put :update, params: { id: alliance.id, status: "rejected" } |
| 111 | + |
| 112 | + alliance.reload |
| 113 | + expect(alliance).to be_rejected |
| 114 | + expect(flash[:notice]).to eq(I18n.t("organization_alliances.updated")) |
| 115 | + expect(response).to redirect_to(organization_alliances_path) |
| 116 | + end |
| 117 | + |
| 118 | + it "sets flash error if alliance cannot be updated" do |
| 119 | + allow_any_instance_of(OrganizationAlliance).to receive(:update).and_return(false) |
| 120 | + allow_any_instance_of(OrganizationAlliance).to receive_message_chain(:errors, :full_messages, :to_sentence).and_return("Error message") |
| 121 | + |
| 122 | + put :update, params: { id: alliance.id, status: "accepted" } |
| 123 | + |
| 124 | + expect(flash[:error]).to eq("Error message") |
| 125 | + expect(response).to redirect_to(organization_alliances_path) |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe "DELETE #destroy" do |
| 130 | + let!(:alliance) { |
| 131 | + OrganizationAlliance.create!( |
| 132 | + source_organization: organization, |
| 133 | + target_organization: other_organization |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + it "destroys the alliance" do |
| 138 | + expect { |
| 139 | + delete :destroy, params: { id: alliance.id } |
| 140 | + }.to change(OrganizationAlliance, :count).by(-1) |
| 141 | + |
| 142 | + expect(flash[:notice]).to eq(I18n.t("organization_alliances.destroyed")) |
| 143 | + expect(response).to redirect_to(organization_alliances_path) |
| 144 | + end |
| 145 | + |
| 146 | + it "sets flash error if alliance cannot be destroyed" do |
| 147 | + allow_any_instance_of(OrganizationAlliance).to receive(:destroy).and_return(false) |
| 148 | + |
| 149 | + delete :destroy, params: { id: alliance.id } |
| 150 | + |
| 151 | + expect(flash[:error]).to eq(I18n.t("organization_alliances.error_destroying")) |
| 152 | + expect(response).to redirect_to(organization_alliances_path) |
| 153 | + end |
| 154 | + end |
| 155 | +end |
0 commit comments