Skip to content

Instantly share code, notes, and snippets.

@blacksails
Created September 22, 2015 12:12
Show Gist options
  • Save blacksails/f74077ddf5e8e6e81f90 to your computer and use it in GitHub Desktop.
Save blacksails/f74077ddf5e8e6e81f90 to your computer and use it in GitHub Desktop.
@email "foo@bar.com"
@pw "Foobar12345!"
setup do
conn = conn() |> put_req_header("accept", "application/json")
{:ok, conn: conn}
end
test "creates valid token and sets it in cookie" do
changeset = User.changeset(%User{}, %{email: @email, password: @pw})
{:ok, user} = Repo.insert(changeset)
conn = post conn,
auth_path(conn, :login),
auth: %{email: @email, password: @pw}
{:ok, payload} = Joken.decode(get_session(conn, :token))
assert payload.id == user.id
assert json_response(conn, 200)["data"]["id"] == user.id
end
test "we get an error if the email is not found" do
conn = post(conn,
auth_path(conn, :login),
# The following user does not exist
auth: %{email: @email, password: @pw})
body = json_response(conn, Status.code(:unauthorized))
assert body["errors"] == "invalid email or password"
end
test "we get an error if the password does not match the password_digest" do
changeset = User.changeset(%User{}, %{email: @email, password: @pw})
{:ok, _} = Repo.insert(changeset)
conn = post(conn,
auth_path(conn, :login),
# The following user exists but the password is incorrect
auth: %{email: @email, password: "cheesesticks"})
body = json_response(conn, Status.code(:unauthorized))
assert body["errors"] == "invalid email or password"
end
test "logout should delete the session cookie" do
changeset = User.changeset(%User{}, %{email: @email, password: @pw})
{:ok, _} = Repo.insert(changeset)
conn = post conn,
auth_path(conn, :login),
auth: %{email: @email, password: @pw}
{:ok, payload} = Joken.decode(get_session(conn, :token))
conn = conn()
conn = delete conn,
auth_path(conn, :logout),
id: payload.id
assert get_session(conn, :token) == nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment