summaryrefslogtreecommitdiff
path: root/test/auth/password_test.exs
blob: 4072293b3aa5fdec8c5c51203b7e8d4d6e247384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule Chessh.Auth.PasswordAuthenticatorTest do
  use ExUnit.Case
  alias Chessh.{Player, Repo}

  @valid_user %{username: "logan", password: "password", discord_id: "1"}

  setup_all do
    Ecto.Adapters.SQL.Sandbox.checkout(Repo)
    Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})

    {:ok, _player} = Repo.insert(Player.registration_changeset(%Player{}, @valid_user))

    :ok
  end

  test "Password can authenticate a hashed password" do
    assert Chessh.Auth.PasswordAuthenticator.authenticate(
             @valid_user.username,
             @valid_user.password
           )

    refute Chessh.Auth.PasswordAuthenticator.authenticate(
             @valid_user.username,
             "a_bad_password"
           )
  end

  test "Password can authenticate a user instance" do
    player = Repo.get_by(Player, username: "logan")

    assert Chessh.Auth.PasswordAuthenticator.authenticate(
             player,
             @valid_user.password
           )
  end
end