1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.EnsurePublicOrAuthenticatedPlug
9 alias Pleroma.Plugs.OAuthScopesPlug
13 import Pleroma.Factory
15 setup_with_mocks([{EnsurePublicOrAuthenticatedPlug, [], [call: fn conn, _ -> conn end]}]) do
19 describe "when `assigns[:token]` is nil, " do
20 test "with :skip_instance_privacy_check option, proceeds with no op", %{conn: conn} do
23 |> assign(:user, insert(:user))
24 |> OAuthScopesPlug.call(%{scopes: ["read"], skip_instance_privacy_check: true})
27 assert conn.assigns[:user]
29 refute called(EnsurePublicOrAuthenticatedPlug.call(conn, :_))
32 test "without :skip_instance_privacy_check option, calls EnsurePublicOrAuthenticatedPlug", %{
37 |> assign(:user, insert(:user))
38 |> OAuthScopesPlug.call(%{scopes: ["read"]})
41 assert conn.assigns[:user]
43 assert called(EnsurePublicOrAuthenticatedPlug.call(conn, :_))
47 test "if `token.scopes` fulfills specified 'any of' conditions, " <>
48 "proceeds with no op",
50 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
54 |> assign(:user, token.user)
55 |> assign(:token, token)
56 |> OAuthScopesPlug.call(%{scopes: ["read"]})
59 assert conn.assigns[:user]
62 test "if `token.scopes` fulfills specified 'all of' conditions, " <>
63 "proceeds with no op",
65 token = insert(:oauth_token, scopes: ["scope1", "scope2", "scope3"]) |> Repo.preload(:user)
69 |> assign(:user, token.user)
70 |> assign(:token, token)
71 |> OAuthScopesPlug.call(%{scopes: ["scope2", "scope3"], op: :&})
74 assert conn.assigns[:user]
77 describe "with `fallback: :proceed_unauthenticated` option, " do
78 test "if `token.scopes` doesn't fulfill specified 'any of' conditions, " <>
79 "clears `assigns[:user]` and calls EnsurePublicOrAuthenticatedPlug",
81 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
85 |> assign(:user, token.user)
86 |> assign(:token, token)
87 |> OAuthScopesPlug.call(%{scopes: ["follow"], fallback: :proceed_unauthenticated})
90 refute conn.assigns[:user]
92 assert called(EnsurePublicOrAuthenticatedPlug.call(conn, :_))
95 test "if `token.scopes` doesn't fulfill specified 'all of' conditions, " <>
96 "clears `assigns[:user] and calls EnsurePublicOrAuthenticatedPlug",
98 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
102 |> assign(:user, token.user)
103 |> assign(:token, token)
104 |> OAuthScopesPlug.call(%{
105 scopes: ["read", "follow"],
107 fallback: :proceed_unauthenticated
111 refute conn.assigns[:user]
113 assert called(EnsurePublicOrAuthenticatedPlug.call(conn, :_))
116 test "with :skip_instance_privacy_check option, " <>
117 "if `token.scopes` doesn't fulfill specified conditions, " <>
118 "clears `assigns[:user]` and does not call EnsurePublicOrAuthenticatedPlug",
120 token = insert(:oauth_token, scopes: ["read:statuses", "write"]) |> Repo.preload(:user)
124 |> assign(:user, token.user)
125 |> assign(:token, token)
126 |> OAuthScopesPlug.call(%{
128 fallback: :proceed_unauthenticated,
129 skip_instance_privacy_check: true
133 refute conn.assigns[:user]
135 refute called(EnsurePublicOrAuthenticatedPlug.call(conn, :_))
139 describe "without :fallback option, " do
140 test "if `token.scopes` does not fulfill specified 'any of' conditions, " <>
141 "returns 403 and halts",
143 token = insert(:oauth_token, scopes: ["read", "write"])
144 any_of_scopes = ["follow"]
148 |> assign(:token, token)
149 |> OAuthScopesPlug.call(%{scopes: any_of_scopes})
152 assert 403 == conn.status
154 expected_error = "Insufficient permissions: #{Enum.join(any_of_scopes, ", ")}."
155 assert Jason.encode!(%{error: expected_error}) == conn.resp_body
158 test "if `token.scopes` does not fulfill specified 'all of' conditions, " <>
159 "returns 403 and halts",
161 token = insert(:oauth_token, scopes: ["read", "write"])
162 all_of_scopes = ["write", "follow"]
166 |> assign(:token, token)
167 |> OAuthScopesPlug.call(%{scopes: all_of_scopes, op: :&})
170 assert 403 == conn.status
173 "Insufficient permissions: #{Enum.join(all_of_scopes -- token.scopes, ", ")}."
175 assert Jason.encode!(%{error: expected_error}) == conn.resp_body
179 describe "with hierarchical scopes, " do
180 test "if `token.scopes` fulfills specified 'any of' conditions, " <>
181 "proceeds with no op",
183 token = insert(:oauth_token, scopes: ["read", "write"]) |> Repo.preload(:user)
187 |> assign(:user, token.user)
188 |> assign(:token, token)
189 |> OAuthScopesPlug.call(%{scopes: ["read:something"]})
192 assert conn.assigns[:user]
195 test "if `token.scopes` fulfills specified 'all of' conditions, " <>
196 "proceeds with no op",
198 token = insert(:oauth_token, scopes: ["scope1", "scope2", "scope3"]) |> Repo.preload(:user)
202 |> assign(:user, token.user)
203 |> assign(:token, token)
204 |> OAuthScopesPlug.call(%{scopes: ["scope1:subscope", "scope2:subscope"], op: :&})
207 assert conn.assigns[:user]
211 describe "filter_descendants/2" do
212 test "filters scopes which directly match or are ancestors of supported scopes" do
213 f = fn scopes, supported_scopes ->
214 OAuthScopesPlug.filter_descendants(scopes, supported_scopes)
217 assert f.(["read", "follow"], ["write", "read"]) == ["read"]
219 assert f.(["read", "write:something", "follow"], ["write", "read"]) ==
220 ["read", "write:something"]
222 assert f.(["admin:read"], ["write", "read"]) == []
224 assert f.(["admin:read"], ["write", "admin"]) == ["admin:read"]