Skip to content

Commit 0d59384

Browse files
Added tests on collectible system
1 parent cca29ea commit 0d59384

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

test/cadet_web/controllers/user_controller_test.exs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,189 @@ defmodule CadetWeb.UserControllerTest do
224224
conn = get(conn, "/v1/user", nil)
225225
assert response(conn, 401) =~ "Unauthorised"
226226
end
227+
228+
@tag authenticate: :student
229+
test "success, student adding collectibles", %{conn: conn} do
230+
user = conn.assigns.current_user
231+
new_game_states = %{
232+
"completed_quests" => ["haha"],
233+
"collectibles" => %{
234+
"HAHA" => "HAHA.png"
235+
}
236+
}
237+
Cadet.GameStates.update(user, new_game_states)
238+
resp =
239+
conn
240+
|> get("/v1/user")
241+
|> json_response(200)
242+
assert new_game_states == resp["gameStates"]
243+
end
244+
245+
@tag authenticate: :student
246+
test "success, student deleting collectibles", %{conn: conn} do
247+
user = conn.assigns.current_user
248+
new_game_states = %{
249+
"completed_quests" => ["haha"],
250+
"collectibles" => %{
251+
"HAHA" => "HAHA.png"
252+
}
253+
}
254+
Cadet.GameStates.update(user, new_game_states)
255+
resp =
256+
conn
257+
|> get("/v1/user")
258+
|> json_response(200)
259+
assert new_game_states == resp["gameStates"]
260+
261+
Cadet.GameStates.clear(user)
262+
resp_2 =
263+
conn
264+
|> get("/v1/user")
265+
|> json_response(200)
266+
assert %{
267+
"completed_quests" => [],
268+
"collectibles" => %{}
269+
} == resp_2["gameStates"]
270+
end
271+
272+
@tag authenticate: :student
273+
test "success, student retrieving collectibles", %{conn: conn} do
274+
resp =
275+
conn
276+
|> get("/v1/user")
277+
|> json_response(200)
278+
assert %{
279+
"completed_quests" => [],
280+
"collectibles" => %{}
281+
} == resp["gameStates"]
282+
end
283+
284+
285+
@tag authenticate: :staff
286+
test "forbidden, staff adding collectibles", %{conn: conn} do
287+
user = conn.assigns.current_user
288+
new_game_states = %{
289+
"completed_quests" => ["haha"],
290+
"collectibles" => %{
291+
"HAHA" => "HAHA.png"
292+
}
293+
}
294+
295+
assert Cadet.GameStates.update(user, new_game_states) == {:error, {:forbidden, "Please try again later."}}
296+
resp =
297+
conn
298+
|> get("/v1/user")
299+
|> json_response(200)
300+
assert %{
301+
"completed_quests" => [],
302+
"collectibles" => %{}
303+
} == resp["gameStates"]
304+
end
305+
306+
@tag authenticate: :staff
307+
test "forbidden, staff deleting collectibles", %{conn: conn} do
308+
user = conn.assigns.current_user
309+
new_game_states = %{
310+
"completed_quests" => ["haha"],
311+
"collectibles" => %{
312+
"HAHA" => "HAHA.png"
313+
}
314+
}
315+
assert Cadet.GameStates.update(user, new_game_states) == {:error, {:forbidden, "Please try again later."}}
316+
resp =
317+
conn
318+
|> get("/v1/user")
319+
|> json_response(200)
320+
assert %{
321+
"completed_quests" => [],
322+
"collectibles" => %{}
323+
} == resp["gameStates"]
324+
325+
assert Cadet.GameStates.clear(user) == {:error, {:forbidden, "Please try again later."}}
326+
resp_2 =
327+
conn
328+
|> get("/v1/user")
329+
|> json_response(200)
330+
assert %{
331+
"completed_quests" => [],
332+
"collectibles" => %{}
333+
} == resp_2["gameStates"]
334+
end
335+
336+
@tag authenticate: :staff
337+
test "success, staff retrieving collectibles", %{conn: conn} do
338+
resp =
339+
conn
340+
|> get("/v1/user")
341+
|> json_response(200)
342+
assert %{
343+
"completed_quests" => [],
344+
"collectibles" => %{}
345+
} == resp["gameStates"]
346+
end
347+
348+
@tag authenticate: :admin
349+
test "forbidden, admin adding collectibles", %{conn: conn} do
350+
user = conn.assigns.current_user
351+
new_game_states = %{
352+
"completed_quests" => ["haha"],
353+
"collectibles" => %{
354+
"HAHA" => "HAHA.png"
355+
}
356+
}
357+
358+
assert Cadet.GameStates.update(user, new_game_states) == {:error, {:forbidden, "Please try again later."}}
359+
resp =
360+
conn
361+
|> get("/v1/user")
362+
|> json_response(200)
363+
assert %{
364+
"completed_quests" => [],
365+
"collectibles" => %{}
366+
} == resp["gameStates"]
367+
end
368+
369+
@tag authenticate: :admin
370+
test "forbidden, admin deleting collectibles", %{conn: conn} do
371+
user = conn.assigns.current_user
372+
new_game_states = %{
373+
"completed_quests" => ["haha"],
374+
"collectibles" => %{
375+
"HAHA" => "HAHA.png"
376+
}
377+
}
378+
assert Cadet.GameStates.update(user, new_game_states) == {:error, {:forbidden, "Please try again later."}}
379+
resp =
380+
conn
381+
|> get("/v1/user")
382+
|> json_response(200)
383+
assert %{
384+
"completed_quests" => [],
385+
"collectibles" => %{}
386+
} == resp["gameStates"]
387+
388+
assert Cadet.GameStates.clear(user) == {:error, {:forbidden, "Please try again later."}}
389+
resp_2 =
390+
conn
391+
|> get("/v1/user")
392+
|> json_response(200)
393+
assert %{
394+
"completed_quests" => [],
395+
"collectibles" => %{}
396+
} == resp_2["gameStates"]
397+
end
398+
399+
@tag authenticate: :admin
400+
test "success, admin retrieving collectibles", %{conn: conn} do
401+
resp =
402+
conn
403+
|> get("/v1/user")
404+
|> json_response(200)
405+
assert %{
406+
"completed_quests" => [],
407+
"collectibles" => %{}
408+
} == resp["gameStates"]
409+
end
227410
end
228411

229412
defp build_assessments_starting_at(time) do

0 commit comments

Comments
 (0)