1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Auth.AuthControllerTest do
6 use Pleroma.Web.ConnCase
10 describe "do_oauth_check" do
11 test "serves with proper OAuth token (fulfilling requested scopes)" do
12 %{conn: good_token_conn, user: user} = oauth_access(["read"])
14 assert %{"user_id" => user.id} ==
16 |> get("/test/authenticated_api/do_oauth_check")
19 # Unintended usage (:api) — use with :authenticated_api instead
20 assert %{"user_id" => user.id} ==
22 |> get("/test/api/do_oauth_check")
26 test "fails on no token / missing scope(s)" do
27 %{conn: bad_token_conn} = oauth_access(["irrelevant_scope"])
30 |> get("/test/authenticated_api/do_oauth_check")
34 |> assign(:token, nil)
35 |> get("/test/api/do_oauth_check")
40 describe "fallback_oauth_check" do
41 test "serves with proper OAuth token (fulfilling requested scopes)" do
42 %{conn: good_token_conn, user: user} = oauth_access(["read"])
44 assert %{"user_id" => user.id} ==
46 |> get("/test/api/fallback_oauth_check")
49 # Unintended usage (:authenticated_api) — use with :api instead
50 assert %{"user_id" => user.id} ==
52 |> get("/test/authenticated_api/fallback_oauth_check")
56 test "for :api on public instance, drops :user and renders on no token / missing scope(s)" do
57 clear_config([:instance, :public], true)
59 %{conn: bad_token_conn} = oauth_access(["irrelevant_scope"])
61 assert %{"user_id" => nil} ==
63 |> get("/test/api/fallback_oauth_check")
66 assert %{"user_id" => nil} ==
68 |> assign(:token, nil)
69 |> get("/test/api/fallback_oauth_check")
73 test "for :api on private instance, fails on no token / missing scope(s)" do
74 clear_config([:instance, :public], false)
76 %{conn: bad_token_conn} = oauth_access(["irrelevant_scope"])
79 |> get("/test/api/fallback_oauth_check")
83 |> assign(:token, nil)
84 |> get("/test/api/fallback_oauth_check")
89 describe "skip_oauth_check" do
90 test "for :authenticated_api, serves if :user is set (regardless of token / token scopes)" do
93 assert %{"user_id" => user.id} ==
95 |> assign(:user, user)
96 |> get("/test/authenticated_api/skip_oauth_check")
99 %{conn: bad_token_conn, user: user} = oauth_access(["irrelevant_scope"])
101 assert %{"user_id" => user.id} ==
103 |> get("/test/authenticated_api/skip_oauth_check")
104 |> json_response(200)
107 test "serves via :api on public instance if :user is not set" do
108 clear_config([:instance, :public], true)
110 assert %{"user_id" => nil} ==
112 |> get("/test/api/skip_oauth_check")
113 |> json_response(200)
116 |> get("/test/authenticated_api/skip_oauth_check")
117 |> json_response(403)
120 test "fails on private instance if :user is not set" do
121 clear_config([:instance, :public], false)
124 |> get("/test/api/skip_oauth_check")
125 |> json_response(403)
128 |> get("/test/authenticated_api/skip_oauth_check")
129 |> json_response(403)
133 describe "fallback_oauth_skip_publicity_check" do
134 test "serves with proper OAuth token (fulfilling requested scopes)" do
135 %{conn: good_token_conn, user: user} = oauth_access(["read"])
137 assert %{"user_id" => user.id} ==
139 |> get("/test/api/fallback_oauth_skip_publicity_check")
140 |> json_response(200)
142 # Unintended usage (:authenticated_api)
143 assert %{"user_id" => user.id} ==
145 |> get("/test/authenticated_api/fallback_oauth_skip_publicity_check")
146 |> json_response(200)
149 test "for :api on private / public instance, drops :user and renders on token issue" do
150 %{conn: bad_token_conn} = oauth_access(["irrelevant_scope"])
152 for is_public <- [true, false] do
153 clear_config([:instance, :public], is_public)
155 assert %{"user_id" => nil} ==
157 |> get("/test/api/fallback_oauth_skip_publicity_check")
158 |> json_response(200)
160 assert %{"user_id" => nil} ==
162 |> assign(:token, nil)
163 |> get("/test/api/fallback_oauth_skip_publicity_check")
164 |> json_response(200)
169 describe "skip_oauth_skip_publicity_check" do
170 test "for :authenticated_api, serves if :user is set (regardless of token / token scopes)" do
173 assert %{"user_id" => user.id} ==
175 |> assign(:user, user)
176 |> get("/test/authenticated_api/skip_oauth_skip_publicity_check")
177 |> json_response(200)
179 %{conn: bad_token_conn, user: user} = oauth_access(["irrelevant_scope"])
181 assert %{"user_id" => user.id} ==
183 |> get("/test/authenticated_api/skip_oauth_skip_publicity_check")
184 |> json_response(200)
187 test "for :api, serves on private and public instances regardless of whether :user is set" do
190 for is_public <- [true, false] do
191 clear_config([:instance, :public], is_public)
193 assert %{"user_id" => nil} ==
195 |> get("/test/api/skip_oauth_skip_publicity_check")
196 |> json_response(200)
198 assert %{"user_id" => user.id} ==
200 |> assign(:user, user)
201 |> get("/test/api/skip_oauth_skip_publicity_check")
202 |> json_response(200)
207 describe "missing_oauth_check_definition" do
208 def test_missing_oauth_check_definition_failure(endpoint, expected_error) do
209 %{conn: conn} = oauth_access(["read", "write", "follow", "push", "admin"])
211 assert %{"error" => expected_error} ==
214 |> json_response(403)
217 test "fails if served via :authenticated_api" do
218 test_missing_oauth_check_definition_failure(
219 "/test/authenticated_api/missing_oauth_check_definition",
220 "Security violation: OAuth scopes check was neither handled nor explicitly skipped."
224 test "fails if served via :api and the instance is private" do
225 clear_config([:instance, :public], false)
227 test_missing_oauth_check_definition_failure(
228 "/test/api/missing_oauth_check_definition",
229 "This resource requires authentication."
233 test "succeeds with dropped :user if served via :api on public instance" do
234 %{conn: conn} = oauth_access(["read", "write", "follow", "push", "admin"])
236 assert %{"user_id" => nil} ==
238 |> get("/test/api/missing_oauth_check_definition")
239 |> json_response(200)