summaryrefslogtreecommitdiff
path: root/test/matrix_app_service_web/auth_plug_test.exs
blob: 8a2116bbd655f29ec900aa60fedd3168249c52d5 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
defmodule MatrixAppServiceWeb.AuthPlugTest do
  use ExUnit.Case
  use Plug.Test

  import ExUnit.CaptureLog

  test "call with correct acces token returns conn unchanged" do
    conn =
      conn(:get, "/users/2", %{
        "access_token" => "correct token"
      })

    assert MatrixAppServiceWeb.AuthPlug.call(conn, "correct token") == conn
  end

  test "call with incorrect access token halts with error 403" do
    conn =
      conn(:get, "/users/2", %{"access_token" => "incorrect token"})
      |> MatrixAppServiceWeb.AuthPlug.call("correct token")

    assert conn.status == 403
    assert conn.private[:phoenix_template] == "403.json"
    assert conn.private[:phoenix_view] == MatrixAppServiceWeb.ErrorView
    assert conn.halted == true
  end

  test "call with incorrect access token gets logged" do
    conn = conn(:get, "/users/2", %{"access_token" => "incorrect token"})

    assert capture_log(fn -> MatrixAppServiceWeb.AuthPlug.call(conn, "correct token") end) =~
             "Received invalid homeserver token"
  end

  test "call without access token halts with error 401" do
    conn =
      conn(:get, "/users/2")
      |> MatrixAppServiceWeb.AuthPlug.call("correct token")

    assert conn.status == 401
    assert conn.private[:phoenix_template] == "401.json"
    assert conn.private[:phoenix_view] == MatrixAppServiceWeb.ErrorView
    assert conn.halted == true
  end

  test "call without access token gets logged" do
    conn = conn(:get, "user/3")

    assert capture_log(fn -> MatrixAppServiceWeb.AuthPlug.call(conn, "correct token") end) =~
             "No homeserver token provided"
  end
end