Skip to content

Commit f6616c9

Browse files
committed
tests
1 parent 3d63279 commit f6616c9

File tree

3 files changed

+371
-4
lines changed

3 files changed

+371
-4
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
RSpec.describe OrganizationAlliance do
2+
let(:organization) { Fabricate(:organization) }
3+
let(:other_organization) { Fabricate(:organization) }
4+
5+
around do |example|
6+
I18n.with_locale(:en) do
7+
example.run
8+
end
9+
end
10+
11+
describe "validations" do
12+
it "is valid with valid attributes" do
13+
alliance = OrganizationAlliance.new(
14+
source_organization: organization,
15+
target_organization: other_organization
16+
)
17+
expect(alliance).to be_valid
18+
end
19+
20+
it "is not valid without a source organization" do
21+
alliance = OrganizationAlliance.new(
22+
source_organization: nil,
23+
target_organization: other_organization
24+
)
25+
expect(alliance).not_to be_valid
26+
expect(alliance.errors[:source_organization_id]).to include("can't be blank")
27+
end
28+
29+
it "is not valid without a target organization" do
30+
alliance = OrganizationAlliance.new(
31+
source_organization: organization,
32+
target_organization: nil
33+
)
34+
expect(alliance).not_to be_valid
35+
expect(alliance.errors[:target_organization_id]).to include("can't be blank")
36+
end
37+
38+
it "is not valid if creating an alliance with self" do
39+
alliance = OrganizationAlliance.new(
40+
source_organization: organization,
41+
target_organization: organization
42+
)
43+
expect(alliance).not_to be_valid
44+
expect(alliance.errors[:base]).to include("Cannot create an alliance with yourself")
45+
end
46+
47+
it "is not valid if alliance already exists" do
48+
OrganizationAlliance.create!(
49+
source_organization: organization,
50+
target_organization: other_organization
51+
)
52+
53+
alliance = OrganizationAlliance.new(
54+
source_organization: organization,
55+
target_organization: other_organization
56+
)
57+
expect(alliance).not_to be_valid
58+
expect(alliance.errors[:target_organization_id]).to include("has already been taken")
59+
end
60+
end
61+
62+
describe "status enum" do
63+
let(:alliance) {
64+
OrganizationAlliance.create!(
65+
source_organization: organization,
66+
target_organization: other_organization
67+
)
68+
}
69+
70+
it "defaults to pending" do
71+
expect(alliance).to be_pending
72+
end
73+
74+
it "can be set to accepted" do
75+
alliance.accepted!
76+
expect(alliance).to be_accepted
77+
end
78+
79+
it "can be set to rejected" do
80+
alliance.rejected!
81+
expect(alliance).to be_rejected
82+
end
83+
end
84+
85+
describe "scopes" do
86+
before do
87+
@pending_alliance = OrganizationAlliance.create!(
88+
source_organization: organization,
89+
target_organization: other_organization,
90+
status: "pending"
91+
)
92+
93+
@accepted_alliance = OrganizationAlliance.create!(
94+
source_organization: Fabricate(:organization),
95+
target_organization: Fabricate(:organization),
96+
status: "accepted"
97+
)
98+
99+
@rejected_alliance = OrganizationAlliance.create!(
100+
source_organization: Fabricate(:organization),
101+
target_organization: Fabricate(:organization),
102+
status: "rejected"
103+
)
104+
end
105+
106+
it "returns pending alliances" do
107+
expect(OrganizationAlliance.pending).to include(@pending_alliance)
108+
expect(OrganizationAlliance.pending).not_to include(@accepted_alliance, @rejected_alliance)
109+
end
110+
111+
it "returns accepted alliances" do
112+
expect(OrganizationAlliance.accepted).to include(@accepted_alliance)
113+
expect(OrganizationAlliance.accepted).not_to include(@pending_alliance, @rejected_alliance)
114+
end
115+
116+
it "returns rejected alliances" do
117+
expect(OrganizationAlliance.rejected).to include(@rejected_alliance)
118+
expect(OrganizationAlliance.rejected).not_to include(@pending_alliance, @accepted_alliance)
119+
end
120+
end
121+
end

0 commit comments

Comments
 (0)