Skip to content

Commit 128cbc7

Browse files
Wong-ZZangelsl
authored andcommitted
Added tests
1 parent 5eb7c98 commit 128cbc7

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

lib/cadet/course/groups.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ defmodule Cadet.Course.Groups do
1616
@spec get_group_overviews(%User{}) :: [group_overview]
1717
def get_group_overviews(_user = %User{role: role}) do
1818
if role in @get_overviews_role do
19-
overviews =
19+
overviews =
2020
Group
2121
|> Repo.all()
2222
|> Enum.map(fn group_info -> get_group_info(group_info) end)
2323
{:ok, overviews}
2424
else
25-
{:error, {:unauthorised, "Unauthorised"}}
25+
{:error, {:unauthorized, "Unauthorized"}}
2626
end
2727
end
2828

2929
defp get_group_info(group_info) do
3030
%{
31-
id: group_info.id,
31+
id: group_info.id,
3232
avenger_name: get_user(group_info.leader_id).name,
3333
name: group_info.name
3434
}

test/cadet/course/groups_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule Cadet.Course.GroupsTest do
2+
use Cadet.DataCase
3+
4+
alias Cadet.Accounts
5+
alias Cadet.Accounts.User
6+
alias Cadet.Course.Groups
7+
8+
test "get group overviews" do
9+
group = insert(:group)
10+
{:ok, result} = Groups.get_group_overviews(%User{role: :staff})
11+
avenger_name = Accounts.get_user(group.leader_id).name
12+
group_name = group.name
13+
group_id = group.id
14+
assert result == [%{id: group_id, avenger_name: avenger_name, name: group_name}]
15+
end
16+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule CadetWeb.GroupsControllerTest do
2+
use CadetWeb.ConnCase
3+
4+
describe "GET /, unauthenticated" do
5+
test "unauthorized", %{conn: conn} do
6+
conn = get(conn, build_url())
7+
assert response(conn, 401) =~ "Unauthorised"
8+
end
9+
end
10+
11+
describe "GET /, student only" do
12+
@tag authenticate: :student
13+
test "unauthorized", %{conn: conn} do
14+
conn = get(conn, build_url())
15+
assert response(conn, 401) == "Unauthorized"
16+
end
17+
end
18+
19+
describe "GET /, staff" do
20+
@tag authenticate: :staff
21+
test "successful", %{conn: conn} do
22+
avenger = insert(:user, %{name: "avenger", role: :staff})
23+
mentor = insert(:user, %{name: "mentor", role: :staff})
24+
group = insert(:group, %{leader: avenger, mentor: mentor})
25+
conn = get(conn, build_url())
26+
group_name = group.name
27+
group_id = group.id
28+
expected = [%{"id" => group_id, "avengerName" => "avenger", "groupName" => group_name}]
29+
assert json_response(conn, 200) == expected
30+
end
31+
end
32+
33+
defp build_url, do: "/v1/groups/"
34+
end

0 commit comments

Comments
 (0)