Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Jan 30, 2024
1 parent 9045d10 commit 75573f7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
3 changes: 0 additions & 3 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,13 @@ defmodule Safira.Accounts do
def get_attendee!(id) do
Repo.get!(Attendee, id)
|> Repo.preload(:badges)
|> Repo.preload(:redeemables)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
end

def get_attendee(id) do
Repo.get(Attendee, id)
|> Repo.preload(:badges)
|> Repo.preload(:redeemables)
|> Repo.preload(:user)
|> Repo.preload(:prizes)
|> Repo.preload(:course)
Expand Down
37 changes: 10 additions & 27 deletions lib/safira_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule SafiraWeb.AuthController do
alias Safira.Accounts
alias Safira.Auth
alias Safira.Guardian
alias Safira.Roulette
alias Safira.Store

action_fallback SafiraWeb.FallbackController

Expand All @@ -23,8 +25,15 @@ defmodule SafiraWeb.AuthController do
user_preload =
cond do
not is_nil(user_preload.attendee) ->
attendee = Accounts.get_attendee_with_badge_count_by_id!(user_preload.attendee.id)

attendee =
attendee
|> Map.put(:redeemables, Store.get_attendee_redeemables(attendee))
|> Map.put(:prizes, Roulette.get_attendee_prize(attendee))

user_preload
|> Map.put(:attendee, Accounts.get_attendee!(user_preload.attendee.id))
|> Map.put(:attendee, attendee)
|> Map.put(:type, "attendee")

not is_nil(user_preload.company) ->
Expand All @@ -39,32 +48,6 @@ defmodule SafiraWeb.AuthController do
render(conn, :data, user: user_preload)
end

def attendee(conn, _params) do
user = Guardian.Plug.current_resource(conn)
user_preload = Accounts.get_user_preload!(user.id)

case is_nil(user_preload.attendee) do
true ->
{:error, :unauthorized}

false ->
render(conn, :attendee, user: user_preload)
end
end

def company(conn, _params) do
user = Guardian.Plug.current_resource(conn)
user_preload = Accounts.get_user_preload!(user.id)

case is_nil(user_preload.company) do
true ->
{:error, :unauthorized}

false ->
render(conn, :company, user: user_preload)
end
end

def is_registered(conn, %{"id" => id}) do
attendee = Accounts.get_attendee!(id)

Expand Down
2 changes: 0 additions & 2 deletions lib/safira_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ defmodule SafiraWeb.Router do
pipe_through :jwt_authenticated

get "/user", AuthController, :user
get "/attendee", AuthController, :attendee
get "/company", AuthController, :company
get "/leaderboard", LeaderboardController, :index
get "/leaderboard/:date", LeaderboardController, :daily
get "/roulette/latestwins", RouletteController, :latest_wins
Expand Down
42 changes: 27 additions & 15 deletions test/safira_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,60 @@ defmodule SafiraWeb.AuthControllerTest do
end

describe "me" do
test "attendee", %{user: user} do
test "when user is an attendee", %{user: user} do
attendee = insert(:attendee, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

conn =
conn
|> get(Routes.auth_path(conn, :attendee))
|> get(Routes.auth_path(conn, :user))

expected_attendee = %{
"avatar" => nil,
expected_user = %{
"email" => user.email,
"id" => attendee.id,
"type" => "attendee",
"avatar" => nil,
"badge_count" => 0,
"badges" => [],
"course" => nil,
"cv" => nil,
"entries" => 0,
"name" => attendee.name,
"nickname" => attendee.nickname
"nickname" => attendee.nickname,
"prizes" => [],
"redeemables" => [],
"token_balance" => 0,
}

assert json_response(conn, 200) == expected_attendee
assert json_response(conn, 200) == expected_user
end

test "company", %{user: user} do
test "when user is a company", %{user: user} do
company = insert(:company, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

conn =
conn
|> get(Routes.auth_path(conn, :company))
|> get(Routes.auth_path(conn, :user))

expected_company = %{
"badge_id" => company.badge.id,
expected_user = %{
"badge_id" => company.badge_id,
"channel_id" => company.channel_id,
"email" => user.email,
"has_cv_access" => company.has_cv_access,
"id" => company.id,
"name" => company.name,
"sponsorship" => company.sponsorship
"sponsorship" => company.sponsorship,
"type" => "company",
}

assert json_response(conn, 200) == expected_company
assert json_response(conn, 200) == expected_user
end

test "user", %{user: user} do
insert(:company, user: user)
test "when user is a staff", %{user: user} do
insert(:staff, user: user)

%{conn: conn, user: _user} = api_authenticate(user)

Expand All @@ -60,7 +72,7 @@ defmodule SafiraWeb.AuthControllerTest do
expected_user = %{
"email" => user.email,
"id" => user.id,
"type" => "company"
"type" => "staff",
}

assert json_response(conn, 200) == expected_user
Expand Down

0 comments on commit 75573f7

Please sign in to comment.