2019-09-30 01:28:12 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 01:28:12 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.Tests.ObanHelpers
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import Swoosh.TestAssertions
|
|
|
|
|
|
|
|
describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
|
|
|
|
setup do
|
|
|
|
{:ok, user} =
|
|
|
|
insert(:user)
|
2020-10-13 15:07:36 -06:00
|
|
|
|> User.confirmation_changeset(set_confirmation: false)
|
2019-10-16 12:59:21 -06:00
|
|
|
|> User.update_and_set_cache()
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-10-13 13:29:34 -06:00
|
|
|
refute user.is_confirmed
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
[user: user]
|
|
|
|
end
|
|
|
|
|
2020-03-20 09:33:00 -06:00
|
|
|
setup do: clear_config([:instance, :account_activation_required], true)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
test "resend account confirmation email", %{conn: conn, user: user} do
|
|
|
|
conn
|
2020-05-13 09:06:25 -06:00
|
|
|
|> put_req_header("content-type", "application/json")
|
2019-09-30 01:28:12 -06:00
|
|
|
|> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(:no_content)
|
|
|
|
|
|
|
|
ObanHelpers.perform_all()
|
|
|
|
|
|
|
|
email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
|
|
|
|
notify_email = Config.get([:instance, :notify_email])
|
|
|
|
instance_name = Config.get([:instance, :name])
|
|
|
|
|
|
|
|
assert_email_sent(
|
|
|
|
from: {instance_name, notify_email},
|
|
|
|
to: {user.name, user.email},
|
|
|
|
html_body: email.html_body
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/pleroma/accounts/confirmation_resend?nickname=#{user.nickname}")
|
|
|
|
|> json_response_and_validate_schema(:no_content)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
ObanHelpers.perform_all()
|
|
|
|
|
|
|
|
email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
|
|
|
|
notify_email = Config.get([:instance, :notify_email])
|
|
|
|
instance_name = Config.get([:instance, :name])
|
|
|
|
|
|
|
|
assert_email_sent(
|
|
|
|
from: {instance_name, notify_email},
|
|
|
|
to: {user.name, user.email},
|
|
|
|
html_body: email.html_body
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "getting favorites timeline of specified user" do
|
|
|
|
setup do
|
2019-10-16 12:59:21 -06:00
|
|
|
[current_user, user] = insert_pair(:user, hide_favorites: false)
|
2019-12-19 07:23:27 -07:00
|
|
|
%{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
|
|
|
|
[current_user: current_user, user: user, conn: conn]
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns list of statuses favorited by specified user", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
[activity | _] = insert_pair(:note_activity)
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
[like] = response
|
|
|
|
|
|
|
|
assert length(response) == 1
|
|
|
|
assert like["id"] == activity.id
|
|
|
|
end
|
|
|
|
|
2020-04-24 13:25:27 -06:00
|
|
|
test "returns favorites for specified user_id when requester is not logged in", %{
|
2019-09-30 01:28:12 -06:00
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
activity = insert(:note_activity)
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-04-24 13:25:27 -06:00
|
|
|
response =
|
|
|
|
build_conn()
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2020-04-24 13:25:27 -06:00
|
|
|
|
|
|
|
assert length(response) == 1
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returns favorited DM only when user is logged in and he is one of recipients", %{
|
|
|
|
current_user: current_user,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(current_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Hi @#{user.nickname}!",
|
|
|
|
visibility: "direct"
|
2019-09-30 01:28:12 -06:00
|
|
|
})
|
|
|
|
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, direct.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
for u <- [user, current_user] do
|
|
|
|
response =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, u)
|
|
|
|
|> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
assert length(response) == 1
|
|
|
|
end
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-04-24 13:25:27 -06:00
|
|
|
response =
|
|
|
|
build_conn()
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2020-04-24 13:25:27 -06:00
|
|
|
|
|
|
|
assert length(response) == 0
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not return others' favorited DM when user is not one of recipients", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
user_two = insert(:user)
|
|
|
|
|
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "Hi @#{user.nickname}!",
|
|
|
|
visibility: "direct"
|
2019-09-30 01:28:12 -06:00
|
|
|
})
|
|
|
|
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, direct.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
assert Enum.empty?(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "paginates favorites using since_id and max_id", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
activities = insert_list(10, :note_activity)
|
|
|
|
|
|
|
|
Enum.each(activities, fn activity ->
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
third_activity = Enum.at(activities, 2)
|
|
|
|
seventh_activity = Enum.at(activities, 6)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
2020-05-13 09:06:25 -06:00
|
|
|
|> get(
|
2021-10-06 00:08:21 -06:00
|
|
|
"/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{seventh_activity.id}"
|
2020-05-13 09:06:25 -06:00
|
|
|
)
|
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
assert length(response) == 3
|
|
|
|
refute third_activity in response
|
|
|
|
refute seventh_activity in response
|
|
|
|
end
|
|
|
|
|
|
|
|
test "limits favorites using limit parameter", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
7
|
|
|
|
|> insert_list(:note_activity)
|
|
|
|
|> Enum.each(fn activity ->
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
2020-05-13 09:06:25 -06:00
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
|
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
assert length(response) == 3
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns empty response when user does not have any favorited statuses", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2020-05-13 09:06:25 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
|
|
|
assert Enum.empty?(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns 404 error when specified user is not exist", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
|
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "returns 403 error when user has hidden own favorites", %{conn: conn} do
|
2019-10-16 12:59:21 -06:00
|
|
|
user = insert(:user, hide_favorites: true)
|
2019-09-30 01:28:12 -06:00
|
|
|
activity = insert(:note_activity)
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "hides favorites for new users by default", %{conn: conn} do
|
2019-09-30 01:28:12 -06:00
|
|
|
user = insert(:user)
|
|
|
|
activity = insert(:note_activity)
|
2019-10-16 08:16:39 -06:00
|
|
|
CommonAPI.favorite(user, activity.id)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert user.hide_favorites
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
|
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscribing / unsubscribing" do
|
2019-12-19 07:23:27 -07:00
|
|
|
test "subscribing / unsubscribing to a user" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["follow"])
|
2019-09-30 01:28:12 -06:00
|
|
|
subscription_target = insert(:user)
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn =
|
2019-09-30 01:28:12 -06:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
|
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert %{"id" => _id, "subscribing" => true} =
|
|
|
|
json_response_and_validate_schema(ret_conn, 200)
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "subscribing" do
|
2019-12-19 07:23:27 -07:00
|
|
|
test "returns 404 when subscription_target not found" do
|
|
|
|
%{conn: conn} = oauth_access(["write:follows"])
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "unsubscribing" do
|
2019-12-19 07:23:27 -07:00
|
|
|
test "returns 404 when subscription_target not found" do
|
|
|
|
%{conn: conn} = oauth_access(["follow"])
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
|
2019-09-30 01:28:12 -06:00
|
|
|
|
2020-05-13 09:06:25 -06:00
|
|
|
assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|
|
|
|
end
|
2022-01-12 13:39:14 -07:00
|
|
|
|
|
|
|
describe "account endorsements" do
|
|
|
|
test "returns a list of pinned accounts", %{conn: conn} do
|
|
|
|
%{id: id1} = user1 = insert(:user)
|
|
|
|
%{id: id2} = user2 = insert(:user)
|
|
|
|
%{id: id3} = user3 = insert(:user)
|
|
|
|
|
|
|
|
CommonAPI.follow(user1, user2)
|
|
|
|
CommonAPI.follow(user1, user3)
|
|
|
|
|
|
|
|
User.endorse(user1, user2)
|
|
|
|
User.endorse(user1, user3)
|
|
|
|
|
|
|
|
[%{"id" => ^id2}, %{"id" => ^id3}] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/pleroma/accounts/#{id1}/endorsements")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns 404 error when specified user is not exist", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/pleroma/accounts/test/endorsements")
|
|
|
|
|
|
|
|
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
|
|
|
|
end
|
|
|
|
end
|
2022-01-18 11:29:21 -07:00
|
|
|
|
|
|
|
describe "birthday reminders" do
|
|
|
|
test "returns a list of friends having birthday on specified day" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
|
|
|
|
|
|
|
%{id: id1} =
|
|
|
|
user1 =
|
|
|
|
insert(:user, %{
|
2022-01-23 00:42:18 -07:00
|
|
|
birthday: "2001-02-12",
|
|
|
|
show_birthday: true
|
2022-01-18 11:29:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
user2 =
|
|
|
|
insert(:user, %{
|
2022-01-23 00:42:18 -07:00
|
|
|
birthday: "2001-02-14",
|
|
|
|
show_birthday: true
|
2022-01-18 11:29:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
user3 = insert(:user)
|
|
|
|
|
|
|
|
CommonAPI.follow(user, user1)
|
|
|
|
CommonAPI.follow(user, user2)
|
|
|
|
CommonAPI.follow(user, user3)
|
|
|
|
|
|
|
|
[%{"id" => ^id1}] =
|
|
|
|
conn
|
2022-01-23 00:42:18 -07:00
|
|
|
|> get("/api/v1/pleroma/birthdays?day=12&month=2")
|
2022-01-18 11:29:21 -07:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "the list doesn't list friends with hidden birth date" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:accounts"])
|
|
|
|
|
|
|
|
user1 =
|
|
|
|
insert(:user, %{
|
2022-01-22 12:21:55 -07:00
|
|
|
birthday: "2001-02-12",
|
2022-01-23 00:42:18 -07:00
|
|
|
show_birthday: false
|
2022-01-18 11:29:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
%{id: id2} =
|
|
|
|
user2 =
|
|
|
|
insert(:user, %{
|
2022-01-22 12:21:55 -07:00
|
|
|
birthday: "2001-02-12",
|
2022-01-23 00:42:18 -07:00
|
|
|
show_birthday: true
|
2022-01-18 11:29:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
CommonAPI.follow(user, user1)
|
|
|
|
CommonAPI.follow(user, user2)
|
|
|
|
|
|
|
|
[%{"id" => ^id2}] =
|
|
|
|
conn
|
2022-01-23 00:42:18 -07:00
|
|
|
|> get("/api/v1/pleroma/birthdays?day=12&month=2")
|
2022-01-18 11:29:21 -07:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
|
|
|
end
|
|
|
|
end
|
2019-09-30 01:28:12 -06:00
|
|
|
end
|