1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.OAuthPlugTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Plugs.OAuthPlug
14 signing_salt: "cooldude"
17 setup %{conn: conn} do
19 {:ok, %{token: token}} = Pleroma.Web.OAuth.Token.create_token(insert(:oauth_app), user)
20 %{user: user, token: token, conn: conn}
23 test "with valid token(uppercase), it assigns the user", %{conn: conn} = opts do
26 |> put_req_header("authorization", "BEARER #{opts[:token]}")
27 |> OAuthPlug.call(%{})
29 assert conn.assigns[:user] == opts[:user]
32 test "with valid token(downcase), it assigns the user", %{conn: conn} = opts do
35 |> put_req_header("authorization", "bearer #{opts[:token]}")
36 |> OAuthPlug.call(%{})
38 assert conn.assigns[:user] == opts[:user]
41 test "with valid token(downcase) in url parameters, it assings the user", opts do
44 |> build_conn("/?access_token=#{opts[:token]}")
45 |> put_req_header("content-type", "application/json")
46 |> fetch_query_params()
47 |> OAuthPlug.call(%{})
49 assert conn.assigns[:user] == opts[:user]
52 test "with valid token(downcase) in body parameters, it assigns the user", opts do
55 |> build_conn("/api/v1/statuses", access_token: opts[:token], status: "test")
56 |> OAuthPlug.call(%{})
58 assert conn.assigns[:user] == opts[:user]
61 test "with invalid token, it not assigns the user", %{conn: conn} do
64 |> put_req_header("authorization", "bearer TTTTT")
65 |> OAuthPlug.call(%{})
67 refute conn.assigns[:user]
70 test "when token is missed but token in session, it assigns the user", %{conn: conn} = opts do
73 |> Plug.Session.call(Plug.Session.init(@session_opts))
75 |> put_session(:oauth_token, opts[:token])
76 |> OAuthPlug.call(%{})
78 assert conn.assigns[:user] == opts[:user]