Merge remote-tracking branch 'pleroma/develop' into mastodon-lookup

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2021-12-28 16:27:27 +01:00
commit 746c9daa62
54 changed files with 1045 additions and 80 deletions

View file

@ -46,6 +46,7 @@ defmodule Pleroma.Gun.ConnectionPoolTest do
end
end
@tag :erratic
test "connection limit is respected with concurrent requests" do
clear_config([:connections_pool, :max_connections]) do
clear_config([:connections_pool, :max_connections], 1)

View file

@ -922,6 +922,27 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|> json_response_and_validate_schema(200)
end
test "following with subscription and unsubscribing" do
%{conn: conn} = oauth_access(["follow"])
followed = insert(:user)
ret_conn =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/accounts/#{followed.id}/follow", %{notify: true})
assert %{"id" => _id, "subscribing" => true} =
json_response_and_validate_schema(ret_conn, 200)
ret_conn =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/accounts/#{followed.id}/follow", %{notify: false})
assert %{"id" => _id, "subscribing" => false} =
json_response_and_validate_schema(ret_conn, 200)
end
test "following / unfollowing errors", %{user: user, conn: conn} do
# self follow
conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow")
@ -1800,4 +1821,21 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|> get("/api/v1/accounts/lookup?acct=unexisting_nickname")
|> json_response_and_validate_schema(404)
end
test "create a note on a user" do
%{conn: conn} = oauth_access(["write:accounts", "read:follows"])
other_user = insert(:user)
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/accounts/#{other_user.id}/note", %{
"comment" => "Example note"
})
assert [%{"note" => "Example note"}] =
conn
|> put_req_header("content-type", "application/json")
|> get("/api/v1/accounts/relationships?id=#{other_user.id}")
|> json_response_and_validate_schema(200)
end
end

View file

@ -35,6 +35,33 @@ defmodule Pleroma.Web.MastodonAPI.AppControllerTest do
end
test "creates an oauth app", %{conn: conn} do
app_attrs = build(:oauth_app)
conn =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/apps", %{
client_name: app_attrs.client_name,
redirect_uris: app_attrs.redirect_uris
})
[app] = Repo.all(App)
expected = %{
"name" => app.client_name,
"website" => app.website,
"client_id" => app.client_id,
"client_secret" => app.client_secret,
"id" => app.id |> to_string(),
"redirect_uri" => app.redirect_uris,
"vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
}
assert expected == json_response_and_validate_schema(conn, 200)
assert app.user_id == nil
end
test "creates an oauth app with a user", %{conn: conn} do
user = insert(:user)
app_attrs = build(:oauth_app)
@ -60,5 +87,6 @@ defmodule Pleroma.Web.MastodonAPI.AppControllerTest do
}
assert expected == json_response_and_validate_schema(conn, 200)
assert app.user_id == user.id
end
end

View file

@ -0,0 +1,46 @@
defmodule Pleroma.Web.MastodonAPI.DirectoryControllerTest do
use Pleroma.Web.ConnCase, async: true
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
test "GET /api/v1/directory with :profile_directory disabled returns empty array", %{conn: conn} do
clear_config([:instance, :profile_directory], false)
insert(:user, is_discoverable: true)
insert(:user, is_discoverable: true)
result =
conn
|> get("/api/v1/directory")
|> json_response_and_validate_schema(200)
assert result == []
end
test "GET /api/v1/directory returns discoverable users only", %{conn: conn} do
%{id: user_id} = insert(:user, is_discoverable: true)
insert(:user, is_discoverable: false)
result =
conn
|> get("/api/v1/directory")
|> json_response_and_validate_schema(200)
assert [%{"id" => ^user_id}] = result
end
test "GET /api/v1/directory returns users sorted by most recent statuses", %{conn: conn} do
insert(:user, is_discoverable: true)
%{id: user_id} = user = insert(:user, is_discoverable: true)
insert(:user, is_discoverable: true)
{:ok, _activity} = CommonAPI.post(user, %{status: "yay i'm discoverable"})
result =
conn
|> get("/api/v1/directory?order=active")
|> json_response_and_validate_schema(200)
assert [%{"id" => ^user_id} | _tail] = result
end
end

View file

@ -74,6 +74,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
fields: []
},
fqn: "shp@shitposter.club",
last_status_at: nil,
pleroma: %{
ap_id: user.ap_id,
also_known_as: ["https://shitposter.zone/users/shp"],
@ -175,6 +176,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
fields: []
},
fqn: "shp@shitposter.club",
last_status_at: nil,
pleroma: %{
ap_id: user.ap_id,
also_known_as: [],
@ -270,10 +272,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
muting: false,
muting_notifications: false,
subscribing: false,
notifying: false,
requested: false,
domain_blocking: false,
showing_reblogs: true,
endorsed: false
endorsed: false,
note: ""
}
test "represent a relationship for the following and followed user" do
@ -295,6 +299,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
muting: true,
muting_notifications: true,
subscribing: true,
notifying: true,
showing_reblogs: false,
id: to_string(other_user.id)
}

View file

@ -41,4 +41,16 @@ defmodule Pleroma.Web.OAuth.AppTest do
assert error.type == :unique
end
end
test "get_user_apps/1" do
user = insert(:user)
apps = [
insert(:oauth_app, user_id: user.id),
insert(:oauth_app, user_id: user.id),
insert(:oauth_app, user_id: user.id)
]
assert App.get_user_apps(user) == apps
end
end

View file

@ -343,4 +343,54 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
|> response(200)
end
end
describe "notice compatibility routes" do
test "Soapbox FE", %{conn: conn} do
user = insert(:user)
note_activity = insert(:note_activity, user: user)
resp =
conn
|> put_req_header("accept", "text/html")
|> get("/@#{user.nickname}/posts/#{note_activity.id}")
|> response(200)
expected =
"<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
assert resp =~ expected
end
test "Mastodon", %{conn: conn} do
user = insert(:user)
note_activity = insert(:note_activity, user: user)
resp =
conn
|> put_req_header("accept", "text/html")
|> get("/@#{user.nickname}/#{note_activity.id}")
|> response(200)
expected =
"<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
assert resp =~ expected
end
test "Twitter", %{conn: conn} do
user = insert(:user)
note_activity = insert(:note_activity, user: user)
resp =
conn
|> put_req_header("accept", "text/html")
|> get("/#{user.nickname}/status/#{note_activity.id}")
|> response(200)
expected =
"<meta content=\"#{Endpoint.url()}/notice/#{note_activity.id}\" property=\"og:url\">"
assert resp =~ expected
end
end
end

View file

@ -0,0 +1,53 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.AppControllerTest do
use Pleroma.Web.ConnCase, async: true
alias Pleroma.Web.OAuth.App
alias Pleroma.Web.Push
import Pleroma.Factory
test "apps", %{conn: conn} do
user = insert(:user)
app_attrs = build(:oauth_app)
creation =
conn
|> put_req_header("content-type", "application/json")
|> assign(:user, user)
|> post("/api/v1/apps", %{
client_name: app_attrs.client_name,
redirect_uris: app_attrs.redirect_uris
})
[app] = App.get_user_apps(user)
expected = %{
"name" => app.client_name,
"website" => app.website,
"client_id" => app.client_id,
"client_secret" => app.client_secret,
"id" => app.id |> to_string(),
"redirect_uri" => app.redirect_uris,
"vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
}
assert expected == json_response_and_validate_schema(creation, 200)
response =
conn
|> put_req_header("content-type", "application/json")
|> assign(:user, user)
|> assign(:token, insert(:oauth_token, user: user, scopes: ["read", "follow"]))
|> get("/api/v1/pleroma/apps")
|> json_response_and_validate_schema(200)
[apps] = response
assert length(response) == 1
assert apps["client_id"] == app.client_id
end
end

View file

@ -0,0 +1,21 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.AppViewTest do
use Pleroma.DataCase, async: true
alias Pleroma.Web.PleromaAPI.AppView
import Pleroma.Factory
test "index.json" do
apps = [
insert(:oauth_app),
insert(:oauth_app),
insert(:oauth_app)
]
results = AppView.render("index.json", %{apps: apps})
assert [%{client_id: _, client_secret: _}, _, _] = results
end
end

View file

@ -0,0 +1,60 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.EnsureStaffPrivilegedPlugTest do
use Pleroma.Web.ConnCase, async: true
alias Pleroma.Web.Plugs.EnsureStaffPrivilegedPlug
import Pleroma.Factory
test "accepts a user that is an admin" do
user = insert(:user, is_admin: true)
conn = assign(build_conn(), :user, user)
ret_conn = EnsureStaffPrivilegedPlug.call(conn, %{})
assert conn == ret_conn
end
test "accepts a user that is a moderator when :privileged_staff is enabled" do
clear_config([:instance, :privileged_staff], true)
user = insert(:user, is_moderator: true)
conn = assign(build_conn(), :user, user)
ret_conn = EnsureStaffPrivilegedPlug.call(conn, %{})
assert conn == ret_conn
end
test "denies a user that is a moderator when :privileged_staff is disabled" do
clear_config([:instance, :privileged_staff], false)
user = insert(:user, is_moderator: true)
conn =
build_conn()
|> assign(:user, user)
|> EnsureStaffPrivilegedPlug.call(%{})
assert conn.status == 403
end
test "denies a user that isn't a staff member" do
user = insert(:user)
conn =
build_conn()
|> assign(:user, user)
|> EnsureStaffPrivilegedPlug.call(%{})
assert conn.status == 403
end
test "denies when a user isn't set" do
conn = EnsureStaffPrivilegedPlug.call(build_conn(), %{})
assert conn.status == 403
end
end

View file

@ -86,6 +86,8 @@ defmodule Pleroma.Web.Plugs.FrontendStaticPlugTest do
"objects",
"activities",
"notice",
"@:nickname",
":nickname",
"users",
"tags",
"mailer",