[#3112] Ensured presence and consistency of :user and :token assigns (EnsureUserTokenAssignsPlug). Refactored auth info dropping functions.
This commit is contained in:
parent
d50a3345ae
commit
e9859b68fc
14 changed files with 178 additions and 131 deletions
|
|
@ -941,7 +941,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||
|
||||
describe "/api/pleroma/admin/stats" do
|
||||
test "status visibility count", %{conn: conn} do
|
||||
admin = insert(:user, is_admin: true)
|
||||
user = insert(:user)
|
||||
CommonAPI.post(user, %{visibility: "public", status: "hey"})
|
||||
CommonAPI.post(user, %{visibility: "unlisted", status: "hey"})
|
||||
|
|
@ -949,7 +948,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||
|
||||
response =
|
||||
conn
|
||||
|> assign(:user, admin)
|
||||
|> get("/api/pleroma/admin/stats")
|
||||
|> json_response(200)
|
||||
|
||||
|
|
@ -958,7 +956,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||
end
|
||||
|
||||
test "by instance", %{conn: conn} do
|
||||
admin = insert(:user, is_admin: true)
|
||||
user1 = insert(:user)
|
||||
instance2 = "instance2.tld"
|
||||
user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
|
||||
|
|
@ -969,7 +966,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|||
|
||||
response =
|
||||
conn
|
||||
|> assign(:user, admin)
|
||||
|> get("/api/pleroma/admin/stats", instance: instance2)
|
||||
|> json_response(200)
|
||||
|
||||
|
|
|
|||
|
|
@ -1415,11 +1415,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do
|
|||
|
||||
describe "GET /api/pleroma/admin/config/descriptions" do
|
||||
test "structure", %{conn: conn} do
|
||||
admin = insert(:user, is_admin: true)
|
||||
|
||||
conn =
|
||||
assign(conn, :user, admin)
|
||||
|> get("/api/pleroma/admin/config/descriptions")
|
||||
conn = get(conn, "/api/pleroma/admin/config/descriptions")
|
||||
|
||||
assert [child | _others] = json_response_and_validate_schema(conn, 200)
|
||||
|
||||
|
|
@ -1437,11 +1433,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do
|
|||
{:esshd}
|
||||
])
|
||||
|
||||
admin = insert(:user, is_admin: true)
|
||||
|
||||
conn =
|
||||
assign(conn, :user, admin)
|
||||
|> get("/api/pleroma/admin/config/descriptions")
|
||||
conn = get(conn, "/api/pleroma/admin/config/descriptions")
|
||||
|
||||
children = json_response_and_validate_schema(conn, 200)
|
||||
|
||||
|
|
|
|||
|
|
@ -264,9 +264,10 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
|
|||
assert length(result) == 3
|
||||
|
||||
# Trying to get the chat of a different user
|
||||
other_user_chat = Chat.get(other_user.id, user.ap_id)
|
||||
|
||||
conn
|
||||
|> assign(:user, other_user)
|
||||
|> get("/api/v1/pleroma/chats/#{chat.id}/messages")
|
||||
|> get("/api/v1/pleroma/chats/#{other_user_chat.id}/messages")
|
||||
|> json_response_and_validate_schema(404)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.EnsureUserKeyPlugTest do
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
alias Pleroma.Web.Plugs.EnsureUserKeyPlug
|
||||
|
||||
test "if the conn has a user key set, it does nothing", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, 1)
|
||||
|
||||
ret_conn =
|
||||
conn
|
||||
|> EnsureUserKeyPlug.call(%{})
|
||||
|
||||
assert conn == ret_conn
|
||||
end
|
||||
|
||||
test "if the conn has no key set, it sets it to nil", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> EnsureUserKeyPlug.call(%{})
|
||||
|
||||
assert Map.has_key?(conn.assigns, :user)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.EnsureUserTokenAssignsPlugTest do
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
alias Pleroma.Web.Plugs.EnsureUserTokenAssignsPlug
|
||||
|
||||
test "with :user assign set to a User record " <>
|
||||
"and :token assign set to a Token belonging to this user, " <>
|
||||
"it does nothing" do
|
||||
%{conn: conn} = oauth_access(["read"])
|
||||
|
||||
ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
|
||||
|
||||
assert conn == ret_conn
|
||||
end
|
||||
|
||||
test "with :user assign set to a User record " <>
|
||||
"but :token assign not set or not a Token, " <>
|
||||
"it assigns :token to `nil`",
|
||||
%{conn: conn} do
|
||||
user = insert(:user)
|
||||
conn = assign(conn, :user, user)
|
||||
|
||||
ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
|
||||
|
||||
assert %{token: nil} = ret_conn.assigns
|
||||
|
||||
ret_conn2 =
|
||||
conn
|
||||
|> assign(:token, 1)
|
||||
|> EnsureUserTokenAssignsPlug.call(%{})
|
||||
|
||||
assert %{token: nil} = ret_conn2.assigns
|
||||
end
|
||||
|
||||
# Abnormal (unexpected) scenario
|
||||
test "with :user assign set to a User record " <>
|
||||
"but :token assign set to a Token NOT belonging to :user, " <>
|
||||
"it drops auth info" do
|
||||
%{conn: conn} = oauth_access(["read"])
|
||||
other_user = insert(:user)
|
||||
|
||||
conn = assign(conn, :user, other_user)
|
||||
|
||||
ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
|
||||
|
||||
assert %{user: nil, token: nil} = ret_conn.assigns
|
||||
end
|
||||
|
||||
test "if :user assign is not set to a User record, it sets :user and :token to nil", %{
|
||||
conn: conn
|
||||
} do
|
||||
ret_conn = EnsureUserTokenAssignsPlug.call(conn, %{})
|
||||
|
||||
assert %{user: nil, token: nil} = ret_conn.assigns
|
||||
|
||||
ret_conn2 =
|
||||
conn
|
||||
|> assign(:user, 1)
|
||||
|> EnsureUserTokenAssignsPlug.call(%{})
|
||||
|
||||
assert %{user: nil, token: nil} = ret_conn2.assigns
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue