Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.OAuthScopesPlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.Plugs.OAuthScopesPlug
  7. alias Pleroma.Repo
  8. import Pleroma.Factory
  9. test "proceeds with no op if `assigns[:token]` is nil", %{conn: conn} do
  10. conn =
  11. conn
  12. |> assign(:user, insert(:user))
  13. |> OAuthScopesPlug.call(%{scopes: ["read"]})
  14. refute conn.halted
  15. assert conn.assigns[:user]
  16. end
  17. test "proceeds with no op if `token.scopes` fulfill specified 'any of' conditions", %{
  18. conn: conn
  19. } do
  20. token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
  21. conn =
  22. conn
  23. |> assign(:user, token.user)
  24. |> assign(:token, token)
  25. |> OAuthScopesPlug.call(%{scopes: ["read"]})
  26. refute conn.halted
  27. assert conn.assigns[:user]
  28. end
  29. test "proceeds with no op if `token.scopes` fulfill specified 'all of' conditions", %{
  30. conn: conn
  31. } do
  32. token = insert(:oauth_token, scopes: ["scope1", "scope2", "scope3"]) |> Repo.preload(:user)
  33. conn =
  34. conn
  35. |> assign(:user, token.user)
  36. |> assign(:token, token)
  37. |> OAuthScopesPlug.call(%{scopes: ["scope2", "scope3"], op: :&})
  38. refute conn.halted
  39. assert conn.assigns[:user]
  40. end
  41. test "proceeds with cleared `assigns[:user]` if `token.scopes` doesn't fulfill specified 'any of' conditions " <>
  42. "and `fallback: :proceed_unauthenticated` option is specified",
  43. %{conn: conn} do
  44. token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
  45. conn =
  46. conn
  47. |> assign(:user, token.user)
  48. |> assign(:token, token)
  49. |> OAuthScopesPlug.call(%{scopes: ["follow"], fallback: :proceed_unauthenticated})
  50. refute conn.halted
  51. refute conn.assigns[:user]
  52. end
  53. test "proceeds with cleared `assigns[:user]` if `token.scopes` doesn't fulfill specified 'all of' conditions " <>
  54. "and `fallback: :proceed_unauthenticated` option is specified",
  55. %{conn: conn} do
  56. token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
  57. conn =
  58. conn
  59. |> assign(:user, token.user)
  60. |> assign(:token, token)
  61. |> OAuthScopesPlug.call(%{
  62. scopes: ["read", "follow"],
  63. op: :&,
  64. fallback: :proceed_unauthenticated
  65. })
  66. refute conn.halted
  67. refute conn.assigns[:user]
  68. end
  69. test "returns 403 and halts in case of no :fallback option and `token.scopes` not fulfilling specified 'any of' conditions",
  70. %{conn: conn} do
  71. token = insert(:oauth_token, scopes: ["read", "write"])
  72. any_of_scopes = ["follow"]
  73. conn =
  74. conn
  75. |> assign(:token, token)
  76. |> OAuthScopesPlug.call(%{scopes: any_of_scopes})
  77. assert conn.halted
  78. assert 403 == conn.status
  79. expected_error = "Insufficient permissions: #{Enum.join(any_of_scopes, ", ")}."
  80. assert Jason.encode!(%{error: expected_error}) == conn.resp_body
  81. end
  82. test "returns 403 and halts in case of no :fallback option and `token.scopes` not fulfilling specified 'all of' conditions",
  83. %{conn: conn} do
  84. token = insert(:oauth_token, scopes: ["read", "write"])
  85. all_of_scopes = ["write", "follow"]
  86. conn =
  87. conn
  88. |> assign(:token, token)
  89. |> OAuthScopesPlug.call(%{scopes: all_of_scopes, op: :&})
  90. assert conn.halted
  91. assert 403 == conn.status
  92. expected_error =
  93. "Insufficient permissions: #{Enum.join(all_of_scopes -- token.scopes, ", ")}."
  94. assert Jason.encode!(%{error: expected_error}) == conn.resp_body
  95. end
  96. end