1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.OAuthScopesPlugTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Plugs.OAuthScopesPlug
11 import Pleroma.Factory
13 test "proceeds with no op if `assigns[:token]` is nil", %{conn: conn} do
16 |> assign(:user, insert(:user))
17 |> OAuthScopesPlug.call(%{scopes: ["read"]})
20 assert conn.assigns[:user]
23 test "proceeds with no op if `token.scopes` fulfill specified 'any of' conditions", %{
26 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
30 |> assign(:user, token.user)
31 |> assign(:token, token)
32 |> OAuthScopesPlug.call(%{scopes: ["read"]})
35 assert conn.assigns[:user]
38 test "proceeds with no op if `token.scopes` fulfill specified 'all of' conditions", %{
41 token = insert(:oauth_token, scopes: ["scope1", "scope2", "scope3"]) |> Repo.preload(:user)
45 |> assign(:user, token.user)
46 |> assign(:token, token)
47 |> OAuthScopesPlug.call(%{scopes: ["scope2", "scope3"], op: :&})
50 assert conn.assigns[:user]
53 test "proceeds with cleared `assigns[:user]` if `token.scopes` doesn't fulfill specified 'any of' conditions " <>
54 "and `fallback: :proceed_unauthenticated` option is specified",
56 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
60 |> assign(:user, token.user)
61 |> assign(:token, token)
62 |> OAuthScopesPlug.call(%{scopes: ["follow"], fallback: :proceed_unauthenticated})
65 refute conn.assigns[:user]
68 test "proceeds with cleared `assigns[:user]` if `token.scopes` doesn't fulfill specified 'all of' conditions " <>
69 "and `fallback: :proceed_unauthenticated` option is specified",
71 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
75 |> assign(:user, token.user)
76 |> assign(:token, token)
77 |> OAuthScopesPlug.call(%{
78 scopes: ["read", "follow"],
80 fallback: :proceed_unauthenticated
84 refute conn.assigns[:user]
87 test "returns 403 and halts in case of no :fallback option and `token.scopes` not fulfilling specified 'any of' conditions",
89 token = insert(:oauth_token, scopes: ["read", "write"])
90 any_of_scopes = ["follow"]
94 |> assign(:token, token)
95 |> OAuthScopesPlug.call(%{scopes: any_of_scopes})
98 assert 403 == conn.status
100 expected_error = "Insufficient permissions: #{Enum.join(any_of_scopes, ", ")}."
101 assert Jason.encode!(%{error: expected_error}) == conn.resp_body
104 test "returns 403 and halts in case of no :fallback option and `token.scopes` not fulfilling specified 'all of' conditions",
106 token = insert(:oauth_token, scopes: ["read", "write"])
107 all_of_scopes = ["write", "follow"]
111 |> assign(:token, token)
112 |> OAuthScopesPlug.call(%{scopes: all_of_scopes, op: :&})
115 assert 403 == conn.status
118 "Insufficient permissions: #{Enum.join(all_of_scopes -- token.scopes, ", ")}."
120 assert Jason.encode!(%{error: expected_error}) == conn.resp_body