logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

ensure_user_token_assigns_plug_test.exs (1951B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Plugs.EnsureUserTokenAssignsPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. import Pleroma.Factory
  7. alias Pleroma.Web.Plugs.EnsureUserTokenAssignsPlug
  8. test "with :user assign set to a User record " <>
  9. "and :token assign set to a Token belonging to this user, " <>
  10. "it does nothing" do
  11. %{conn: conn} = oauth_access(["read"])
  12. ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
  13. assert conn == ret_conn
  14. end
  15. test "with :user assign set to a User record " <>
  16. "but :token assign not set or not a Token, " <>
  17. "it assigns :token to `nil`",
  18. %{conn: conn} do
  19. user = insert(:user)
  20. conn = assign(conn, :user, user)
  21. ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
  22. assert %{token: nil} = ret_conn.assigns
  23. ret_conn2 =
  24. conn
  25. |> assign(:token, 1)
  26. |> EnsureUserTokenAssignsPlug.call(%{})
  27. assert %{token: nil} = ret_conn2.assigns
  28. end
  29. # Abnormal (unexpected) scenario
  30. test "with :user assign set to a User record " <>
  31. "but :token assign set to a Token NOT belonging to :user, " <>
  32. "it drops auth info" do
  33. %{conn: conn} = oauth_access(["read"])
  34. other_user = insert(:user)
  35. conn = assign(conn, :user, other_user)
  36. ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
  37. assert %{user: nil, token: nil} = ret_conn.assigns
  38. end
  39. test "if :user assign is not set to a User record, it sets :user and :token to nil", %{
  40. conn: conn
  41. } do
  42. ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
  43. assert %{user: nil, token: nil} = ret_conn.assigns
  44. ret_conn2 =
  45. conn
  46. |> assign(:user, 1)
  47. |> EnsureUserTokenAssignsPlug.call(%{})
  48. assert %{user: nil, token: nil} = ret_conn2.assigns
  49. end
  50. end