Skip to content

Commit 3721343

Browse files
committed
Merge branch 'dev/multitenent' into infinite-loop
2 parents 86e6c22 + 28ac3d1 commit 3721343

File tree

6 files changed

+15
-194
lines changed

6 files changed

+15
-194
lines changed

lib/cadet/helpers/model_helper.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,15 @@ defmodule Cadet.ModelHelper do
132132
|> cast_assoc(assoc_field)
133133
end
134134
end
135+
136+
def remove_preload(struct, field, cardinality \\ :one) do
137+
%{
138+
struct
139+
| field => %Ecto.Association.NotLoaded{
140+
__field__: field,
141+
__owner__: struct.__struct__,
142+
__cardinality__: cardinality
143+
}
144+
}
145+
end
135146
end

priv/repo/migrations/20210531155751_multitenant_upgrade.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Cadet.Repo.Migrations.MultitenantUpgrade do
22
use Ecto.Migration
3-
import Ecto.Query, only: [from: 2, where: 2]
3+
import Ecto.Query
44

55
alias Cadet.Accounts.{CourseRegistration, Notification, Role, User}
66
alias Cadet.Assessments.{Answer, Assessment, Question, Submission, SubmissionVotes}
@@ -185,7 +185,7 @@ defmodule Cadet.Repo.Migrations.MultitenantUpgrade do
185185
# assign a staff to be the leader_id during migration
186186
nil ->
187187
CourseRegistration
188-
|> where(role: :staff)
188+
|> where([cr], cr.role in [:admin, :staff])
189189
|> Repo.one()
190190
|> Map.fetch!(:id)
191191

test/cadet/auth/guardian_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Cadet.Auth.GuardianTest do
22
use Cadet.DataCase
33

4-
import Cadet.TestHelper
4+
import Cadet.ModelHelper
55
alias Cadet.Auth.Guardian
66

77
test "token subject is user id" do

test/cadet_web/admin_controllers/admin_goals_controller_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,6 @@ defmodule CadetWeb.AdminGoalsControllerTest do
263263
end
264264

265265
defp build_path(course_id, uuid, course_reg_id) do
266-
"v2/courses/#{course_id}/admin/users/#{course_reg_id}/goals/#{uuid}/progress/"
266+
"/v2/courses/#{course_id}/admin/users/#{course_reg_id}/goals/#{uuid}/progress/"
267267
end
268268
end

test/cadet_web/controllers/user_controller_test.exs

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -156,184 +156,10 @@ defmodule CadetWeb.UserControllerTest do
156156
assert expected == resp
157157
end
158158

159-
# # This also tests for the case where assessment has no submission
160-
# @tag authenticate: :student
161-
# test "success, student story ordering", %{conn: conn} do
162-
# early_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -3))
163-
# late_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -1))
164-
165-
# for assessment <- early_assessments ++ late_assessments do
166-
# resp_story =
167-
# conn
168-
# |> get("/v2/user")
169-
# |> json_response(200)
170-
# |> Map.get("latestViewedCourse").story
171-
172-
# expected_story = %{
173-
# "story" => assessment.story,
174-
# "playStory" => true
175-
# }
176-
177-
# assert resp_story == expected_story
178-
179-
# {:ok, _} = Repo.delete(assessment)
180-
# end
181-
# end
182-
183-
# @tag authenticate: :student
184-
# test "success, student story skips assessment without story", %{conn: conn} do
185-
# assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -1))
186-
187-
# assessments
188-
# |> List.first()
189-
# |> Assessment.changeset(%{story: nil})
190-
# |> Repo.update()
191-
192-
# resp_story =
193-
# conn
194-
# |> get("/v2/user")
195-
# |> json_response(200)
196-
# |> Map.get("story")
197-
198-
# expected_story = %{
199-
# "story" => Enum.fetch!(assessments, 1).story,
200-
# "playStory" => true
201-
# }
202-
203-
# assert resp_story == expected_story
204-
# end
205-
206-
# @tag authenticate: :student
207-
# test "success, student story skips unopen assessments", %{conn: conn} do
208-
# build_assessments_starting_at(Timex.shift(Timex.now(), days: 1))
209-
# build_assessments_starting_at(Timex.shift(Timex.now(), months: -1))
210-
211-
# valid_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -1))
212-
213-
# for assessment <- valid_assessments do
214-
# assessment
215-
# |> Assessment.changeset(%{is_published: false})
216-
# |> Repo.update!()
217-
# end
218-
219-
# resp_story =
220-
# conn
221-
# |> get("/v2/user")
222-
# |> json_response(200)
223-
# |> Map.get("story")
224-
225-
# expected_story = %{
226-
# "story" => nil,
227-
# "playStory" => false
228-
# }
229-
230-
# assert resp_story == expected_story
231-
# end
232-
233-
# @tag authenticate: :student
234-
# test "success, student story skips attempting/attempted/submitted", %{conn: conn} do
235-
# user = conn.assigns.current_user
236-
237-
# early_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -3))
238-
# late_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -1))
239-
240-
# # Submit for i-th assessment, expect (i+1)th story to be returned
241-
# for status <- [:attempting, :attempted, :submitted] do
242-
# for [tester, checker] <-
243-
# Enum.chunk_every(early_assessments ++ late_assessments, 2, 1, :discard) do
244-
# insert(:submission, %{student: user, assessment: tester, status: status})
245-
246-
# resp_story =
247-
# conn
248-
# |> get("/v2/user")
249-
# |> json_response(200)
250-
# |> Map.get("story")
251-
252-
# expected_story = %{
253-
# "story" => checker.story,
254-
# "playStory" => true
255-
# }
256-
257-
# assert resp_story == expected_story
258-
# end
259-
260-
# Repo.delete_all(Submission)
261-
# end
262-
# end
263-
264-
# @tag authenticate: :student
265-
# test "success, return most recent assessment when all are attempted", %{conn: conn} do
266-
# user = conn.assigns.current_user
267-
268-
# early_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -3))
269-
# late_assessments = build_assessments_starting_at(Timex.shift(Timex.now(), days: -1))
270-
271-
# for assessment <- early_assessments ++ late_assessments do
272-
# insert(:submission, %{student: user, assessment: assessment, status: :attempted})
273-
# end
274-
275-
# resp_story =
276-
# conn
277-
# |> get("/v2/user")
278-
# |> json_response(200)
279-
# |> Map.get("story")
280-
281-
# expected_story = %{
282-
# "story" => late_assessments |> List.first() |> Map.get(:story),
283-
# "playStory" => false
284-
# }
285-
286-
# assert resp_story == expected_story
287-
# end
288-
289-
# @tag authenticate: :staff
290-
# test "success, staff", %{conn: conn} do
291-
# user = conn.assigns.current_user
292-
293-
# resp =
294-
# conn
295-
# |> get("/v2/user")
296-
# |> json_response(200)
297-
# |> Map.delete("story")
298-
299-
# expected = %{
300-
# "name" => user.name,
301-
# "role" => "#{user.role}",
302-
# "group" => nil,
303-
# "grade" => 0,
304-
# "maxGrade" => 0,
305-
# "xp" => 0,
306-
# "gameStates" => %{},
307-
# "userId" => user.id
308-
# }
309-
310-
# assert expected == resp
311-
# end
312-
313159
test "unauthorized", %{conn: conn} do
314160
conn = get(conn, "/v2/user", nil)
315161
assert response(conn, 401) =~ "Unauthorised"
316162
end
317-
318-
# defp build_assessments_starting_at(time) do
319-
# type_order_map =
320-
# Assessment.assessment_types()
321-
# |> Enum.with_index()
322-
# |> Enum.reduce(%{}, fn {type, idx}, acc -> Map.put(acc, type, idx) end)
323-
324-
# Assessment.assessment_types()
325-
# |> Enum.map(
326-
# &build(:assessment, %{
327-
# type: &1,
328-
# is_published: true,
329-
# open_at: time,
330-
# close_at: Timex.shift(time, days: 10)
331-
# })
332-
# )
333-
# |> Enum.shuffle()
334-
# |> Enum.map(&insert(&1))
335-
# |> Enum.sort(&(type_order_map[&1.type] < type_order_map[&2.type]))
336-
# end
337163
end
338164

339165
describe "GET /v2/user/latest_viewed_course" do

test/test_helper.exs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,3 @@ ExUnit.start()
66
Faker.start()
77

88
Ecto.Adapters.SQL.Sandbox.mode(Cadet.Repo, :manual)
9-
10-
defmodule Cadet.TestHelper do
11-
@doc """
12-
Removes a preloaded Ecto association.
13-
"""
14-
def remove_preload(struct, field, cardinality \\ :one) do
15-
%{
16-
struct
17-
| field => %Ecto.Association.NotLoaded{
18-
__field__: field,
19-
__owner__: struct.__struct__,
20-
__cardinality__: cardinality
21-
}
22-
}
23-
end
24-
end

0 commit comments

Comments
 (0)