logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

oauth_plug_test.exs (2251B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.OAuthPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Plugs.OAuthPlug
  7. import Pleroma.Factory
  8. @session_opts [
  9. store: :cookie,
  10. key: "_test",
  11. signing_salt: "cooldude"
  12. ]
  13. setup %{conn: conn} do
  14. user = insert(:user)
  15. {:ok, %{token: token}} = Pleroma.Web.OAuth.Token.create(insert(:oauth_app), user)
  16. %{user: user, token: token, conn: conn}
  17. end
  18. test "with valid token(uppercase), it assigns the user", %{conn: conn} = opts do
  19. conn =
  20. conn
  21. |> put_req_header("authorization", "BEARER #{opts[:token]}")
  22. |> OAuthPlug.call(%{})
  23. assert conn.assigns[:user] == opts[:user]
  24. end
  25. test "with valid token(downcase), it assigns the user", %{conn: conn} = opts do
  26. conn =
  27. conn
  28. |> put_req_header("authorization", "bearer #{opts[:token]}")
  29. |> OAuthPlug.call(%{})
  30. assert conn.assigns[:user] == opts[:user]
  31. end
  32. test "with valid token(downcase) in url parameters, it assigns the user", opts do
  33. conn =
  34. :get
  35. |> build_conn("/?access_token=#{opts[:token]}")
  36. |> put_req_header("content-type", "application/json")
  37. |> fetch_query_params()
  38. |> OAuthPlug.call(%{})
  39. assert conn.assigns[:user] == opts[:user]
  40. end
  41. test "with valid token(downcase) in body parameters, it assigns the user", opts do
  42. conn =
  43. :post
  44. |> build_conn("/api/v1/statuses", access_token: opts[:token], status: "test")
  45. |> OAuthPlug.call(%{})
  46. assert conn.assigns[:user] == opts[:user]
  47. end
  48. test "with invalid token, it not assigns the user", %{conn: conn} do
  49. conn =
  50. conn
  51. |> put_req_header("authorization", "bearer TTTTT")
  52. |> OAuthPlug.call(%{})
  53. refute conn.assigns[:user]
  54. end
  55. test "when token is missed but token in session, it assigns the user", %{conn: conn} = opts do
  56. conn =
  57. conn
  58. |> Plug.Session.call(Plug.Session.init(@session_opts))
  59. |> fetch_session()
  60. |> put_session(:oauth_token, opts[:token])
  61. |> OAuthPlug.call(%{})
  62. assert conn.assigns[:user] == opts[:user]
  63. end
  64. end