This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
organizations_controller_spec.rb
295 lines (230 loc) · 8.67 KB
/
organizations_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# frozen_string_literal: true
require "rails_helper"
RSpec.describe OrganizationsController, type: :controller do
let(:organization) { classroom_org }
let(:user) { classroom_teacher }
let(:student) { classroom_student }
before do
sign_in_as(user)
end
describe "GET #index", :vcr do
context "unauthenticated user" do
before do
sign_out
end
it "redirects to login_path" do
get :index
expect(response).to redirect_to(login_path)
end
end
context "authenticated user with a valid token" do
it "succeeds" do
get :index
expect(response).to have_http_status(:success)
end
it "sets the users organization" do
organization # call the record so that it is created
get :index
expect(assigns(:organizations).first.id).to eq(organization.id)
end
end
context "user with admin privilege on the organization but not part of the classroom" do
before(:each) do
organization.users = []
end
it "adds the user to the classroom" do
get :index
expect(user.organizations).to include(organization)
end
end
context "user without admin privilege on the organization" do
before(:each) do
sign_in_as(student)
end
it "does not add the user to the classroom" do
get :index
expect(student.organizations).to be_empty
end
end
context "authenticated user with an invalid token" do
before do
allow(user).to receive(:ensure_no_token_scope_loss).and_return(true)
user.update_attributes(token: "1234")
end
it "logs out user" do
get :index
expect(response).to redirect_to(root_path)
end
end
end
describe "GET #new", :vcr do
it "returns success status" do
get :new
expect(response).to have_http_status(:success)
end
it "has a new organization" do
get :new
expect(assigns(:organization)).to_not be_nil
end
it "has an Kaminari::PaginatableArray of the users GitHub organizations that they are an admin of" do
get :new
expect(assigns(:users_github_organizations)).to be_kind_of(Kaminari::PaginatableArray)
end
it "will not include any organizations that are already apart of classroom" do
get :new
expect(assigns(:users_github_organizations)).not_to include([organization.title, organization.github_id])
end
end
describe "POST #create", :vcr do
before do
request.env["HTTP_REFERER"] = "http://classroomtest.com/orgs/new"
end
after(:each) do
organization.destroy!
end
it "will fail to add an organization the user is not an admin of" do
new_organization = build(:organization, github_id: 90)
new_organization_options = { github_id: new_organization.github_id }
expect do
post :create, params: { organization: new_organization_options }
end.to_not change(Organization, :count)
end
it "will add an organization that the user is admin of on GitHub" do
organization_params = { github_id: organization.github_id, users: organization.users }
organization.destroy!
expect { post :create, params: { organization: organization_params } }.to change(Organization, :count)
expect(Organization.last.github_id).to_not be_nil
expect(Organization.last.github_global_relay_id).to_not be_nil
end
it "will redirect the user to the setup page" do
organization_params = { github_id: organization.github_id, users: organization.users }
organization.destroy!
post :create, params: { organization: organization_params }
expect(response).to redirect_to(setup_organization_path(Organization.last))
end
end
describe "GET #show", :vcr do
it "returns success and sets the organization" do
get :show, params: { id: organization.slug }
expect(response.status).to eq(200)
expect(assigns(:current_organization)).to_not be_nil
end
end
describe "GET #edit", :vcr do
it "returns success and sets the organization" do
get :edit, params: { id: organization.slug }
expect(response).to have_http_status(:success)
expect(assigns(:current_organization)).to_not be_nil
end
end
describe "GET #invitation", :vcr do
it "returns success and sets the organization" do
get :invitation, params: { id: organization.slug }
expect(response).to have_http_status(:success)
expect(assigns(:current_organization)).to_not be_nil
end
end
describe "PATCH #remove_user", :vcr do
context "returns 404" do
it "user is not an org owner" do
patch :remove_user, params: { id: organization.slug, user_id: student.id }
expect(response).to have_http_status(404)
end
it "user does not exist" do
patch :remove_user, params: { id: organization.slug, user_id: 105 }
expect(response).to have_http_status(404)
end
end
context "removes user from classroom" do
before(:each) do
teacher = create(:user)
organization.users << teacher
end
it "without assignments" do
patch :remove_user, params: { id: organization.slug, user_id: @teacher.id }
expect(response).to redirect_to(settings_invitations_organization_path)
expect(flash[:success]).to be_present
expect { organization.users.find(id: @teacher.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
it "with assignments" do
assignment = create(:assignment, organization: organization, creator: @teacher)
patch :remove_user, params: { id: organization.slug, user_id: @teacher.id }
expect(assignment.reload.creator_id).not_to eq(@teacher.id)
expect { organization.users.find(id: @teacher.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to redirect_to(settings_invitations_organization_path)
expect(flash[:success]).to be_present
end
end
end
describe "GET #show_groupings", :vcr do
context "flipper is enabled" do
before do
GitHubClassroom.flipper[:team_management].enable
end
it "returns success and sets the organization" do
get :show_groupings, params: { id: organization.slug }
expect(response).to have_http_status(:success)
expect(assigns(:current_organization)).to_not be_nil
end
after do
GitHubClassroom.flipper[:team_management].disable
end
end
context "flipper is not enabled" do
it "returns success and sets the organization" do
get :show_groupings, params: { id: organization.slug }
expect(response.status).to eq(404)
end
end
end
describe "PATCH #update", :vcr do
it "correctly updates the organization" do
options = { title: "New Title" }
patch :update, params: { id: organization.slug, organization: options }
expect(response).to redirect_to(organization_path(Organization.find(organization.id)))
end
end
describe "DELETE #destroy", :vcr do
it "sets the `deleted_at` column for the organization" do
organization # call the record so that it is created
expect { delete :destroy, params: { id: organization.slug } }.to change(Organization, :count)
expect(Organization.unscoped.find(organization.id).deleted_at).not_to be_nil
end
it "calls the DestroyResource background job" do
delete :destroy, params: { id: organization.slug }
assert_enqueued_jobs 1 do
DestroyResourceJob.perform_later(organization)
end
end
it "redirects back to the index page" do
delete :destroy, params: { id: organization.slug }
expect(response).to redirect_to(organizations_path)
end
end
describe "GET #invite", :vcr do
it "returns success and sets the organization" do
get :invite, params: { id: organization.slug }
expect(response.status).to eq(200)
expect(assigns(:current_organization)).to_not be_nil
end
end
describe "GET #setup", :vcr do
it "returns success and sets the organization" do
get :setup, params: { id: organization.slug }
expect(response.status).to eq(200)
expect(assigns(:current_organization)).to_not be_nil
end
end
describe "PATCH #setup_organization", :vcr do
before(:each) do
options = { title: "New Title" }
patch :update, params: { id: organization.slug, organization: options }
end
it "correctly updates the organization" do
expect(Organization.find(organization.id).title).to eql("New Title")
end
it "redirects to the invite page on success" do
expect(response).to redirect_to(organization_path(Organization.find(organization.id)))
end
end
end