Commit 60e58a6a authored by Mikko Ahlroth's avatar Mikko Ahlroth

Add tests for auth controller

parent bb3afe85
defmodule CodeStatsWeb.AuthControllerTest do
use CodeStatsWeb.ConnCase
alias CodeStats.UserHelpers
test "GET /login", %{conn: conn} do
conn = get(conn, "/login")
assert is_binary(html_response(conn, 200))
end
test "login", %{conn: conn} do
{:ok, u} = UserHelpers.create_user("foo@bar", "nicd", "abcdefg")
conn = post(conn, "/login", %{username: u.username, password: "abcdefg"})
assert redirected_to(conn) == "/my/profile"
end
test "nonexistent user", %{conn: conn} do
{:ok, _} = UserHelpers.create_user("foo@bar", "nicd", "abcdefg")
conn = post(conn, "/login", %{username: "no-one", password: "abcdefg"})
response(conn, 404)
end
test "wrong password", %{conn: conn} do
{:ok, u} = UserHelpers.create_user("foo@bar", "nicd", "abcdefg")
conn = post(conn, "/login", %{username: u.username, password: "123"})
response(conn, 404)
end
test "GET /signup", %{conn: conn} do
conn = get(conn, "/signup")
assert is_binary(html_response(conn, 200))
end
test "signup", %{conn: conn} do
conn =
post(conn, "/signup", %{
user: %{
username: "foo",
password: "abcdefg",
"not-underage": true,
"accept-terms": true
}
})
assert redirected_to(conn) == "/login"
user = from(u in CodeStats.User, where: u.username == "foo") |> Repo.one!()
assert is_nil(user.email)
assert user.terms_version == CodeStats.LegalTerms.get_latest_version()
end
test "signup with email", %{conn: conn} do
post(conn, "/signup", %{
user: %{
username: "foo",
password: "abcdefg",
email: "foo@bar",
"not-underage": true,
"accept-terms": true
}
})
user = from(u in CodeStats.User, where: u.username == "foo") |> Repo.one!()
assert user.email == "foo@bar"
end
test "signup with too short password", %{conn: conn} do
conn =
post(conn, "/signup", %{
user: %{
username: "foo",
password: "abc",
"not-underage": true,
"accept-terms": true
}
})
assert response(conn, 400)
end
test "signup with invalid email", %{conn: conn} do
conn =
post(conn, "/signup", %{
user: %{
username: "foo",
password: "abcdefg",
email: "arg",
"not-underage": true,
"accept-terms": true
}
})
assert response(conn, 400)
end
end
defmodule CodeStatsWeb.PulseControllerTest do
use CodeStatsWeb.ConnCase
alias CodeStats.User
alias CodeStats.User.Machine
alias CodeStats.User.Pulse
alias CodeStats.XP
alias CodeStats.Language
alias CodeStatsWeb.AuthUtils
alias CodeStats.UserHelpers
describe "as a not authenticated user" do
test "GET /my/pulses should return 403 forbidden", %{conn: conn} do
......@@ -23,8 +23,8 @@ defmodule CodeStatsWeb.PulseControllerTest do
setup do
{:ok, language} = Language.get_or_create("elixir")
{:ok, user} = create_user("user@somewhere", "test_user")
{:ok, another_user} = create_user("another_user@somewhere", "another_test_user")
{:ok, user} = UserHelpers.create_user("user@somewhere", "test_user")
{:ok, another_user} = UserHelpers.create_user("another_user@somewhere", "another_test_user")
create_data_for(user, language)
create_data_for(another_user, language)
......@@ -52,7 +52,7 @@ defmodule CodeStatsWeb.PulseControllerTest do
describe "as an authenticated user with no pulses" do
setup do
{:ok, user} = create_user("user@somewhere", "test_user")
{:ok, user} = UserHelpers.create_user("user@somewhere", "test_user")
%{user: user}
end
......@@ -92,17 +92,6 @@ defmodule CodeStatsWeb.PulseControllerTest do
defp contains?(headers, key, value), do: Enum.member?(headers, {key, value})
defp create_user(email, username) do
%User{
email: email,
username: username,
password: "test_password",
terms_version: CodeStats.LegalTerms.get_latest_version()
}
|> User.changeset(%{})
|> Repo.insert()
end
defp create_data_for(user, language) do
{:ok, machine} =
%Machine{name: "test_machine"}
......
defmodule CodeStats.UserHelpers do
@moduledoc """
Test helpers for user related tests.
"""
alias CodeStats.{User, Repo}
def create_user(email, username, password \\ "test_password") do
%User{}
|> User.changeset(%{
email: email,
username: username,
password: password,
terms_version: CodeStats.LegalTerms.get_latest_version()
})
|> Repo.insert()
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment