X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=inline;f=test%2Fweb%2Fpleroma_api%2Fcontrollers%2Faccount_controller_test.exs;h=2aa87ac30599724cda63926b5da0de2bd3f11e65;hb=80e15716102e03323b117ade919035402cc7408f;hp=3b4665afdd7737001761c5b94b8b6c58827951d5;hpb=89ab673d00b4dd96fd29f35d7c355b777b9ec0c7;p=akkoma
diff --git a/test/web/pleroma_api/controllers/account_controller_test.exs b/test/web/pleroma_api/controllers/account_controller_test.exs
index 3b4665afd..2aa87ac30 100644
--- a/test/web/pleroma_api/controllers/account_controller_test.exs
+++ b/test/web/pleroma_api/controllers/account_controller_test.exs
@@ -1,12 +1,11 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Config
- alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers
alias Pleroma.User
alias Pleroma.Web.CommonAPI
@@ -20,21 +19,18 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
setup do
{:ok, user} =
insert(:user)
- |> User.change_info(&User.Info.confirmation_changeset(&1, need_confirmation: true))
- |> Repo.update()
+ |> User.confirmation_changeset(need_confirmation: true)
+ |> User.update_and_set_cache()
- assert user.info.confirmation_pending
+ assert user.confirmation_pending
[user: user]
end
- clear_config([:instance, :account_activation_required]) do
- Config.put([:instance, :account_activation_required], true)
- end
+ setup do: clear_config([:instance, :account_activation_required], true)
test "resend account confirmation email", %{conn: conn, user: user} do
conn
- |> assign(:user, user)
|> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
|> json_response(:no_content)
@@ -53,14 +49,12 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
end
describe "PATCH /api/v1/pleroma/accounts/update_avatar" do
- test "user avatar can be set", %{conn: conn} do
- user = insert(:user)
+ setup do: oauth_access(["write:accounts"])
+
+ test "user avatar can be set", %{user: user, conn: conn} do
avatar_image = File.read!("test/fixtures/avatar_data_uri")
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: avatar_image})
user = refresh_record(user)
@@ -79,13 +73,8 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
assert %{"url" => _} = json_response(conn, 200)
end
- test "user avatar can be reset", %{conn: conn} do
- user = insert(:user)
-
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_avatar", %{img: ""})
+ test "user avatar can be reset", %{user: user, conn: conn} do
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_avatar", %{img: ""})
user = User.get_cached_by_id(user.id)
@@ -96,72 +85,56 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
end
describe "PATCH /api/v1/pleroma/accounts/update_banner" do
- test "can set profile banner", %{conn: conn} do
- user = insert(:user)
+ setup do: oauth_access(["write:accounts"])
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
+ test "can set profile banner", %{user: user, conn: conn} do
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => @image})
user = refresh_record(user)
- assert user.info.banner["type"] == "Image"
+ assert user.banner["type"] == "Image"
assert %{"url" => _} = json_response(conn, 200)
end
- test "can reset profile banner", %{conn: conn} do
- user = insert(:user)
-
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
+ test "can reset profile banner", %{user: user, conn: conn} do
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_banner", %{"banner" => ""})
user = refresh_record(user)
- assert user.info.banner == %{}
+ assert user.banner == %{}
assert %{"url" => nil} = json_response(conn, 200)
end
end
describe "PATCH /api/v1/pleroma/accounts/update_background" do
- test "background image can be set", %{conn: conn} do
- user = insert(:user)
+ setup do: oauth_access(["write:accounts"])
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_background", %{"img" => @image})
+ test "background image can be set", %{user: user, conn: conn} do
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => @image})
user = refresh_record(user)
- assert user.info.background["type"] == "Image"
+ assert user.background["type"] == "Image"
assert %{"url" => _} = json_response(conn, 200)
end
- test "background image can be reset", %{conn: conn} do
- user = insert(:user)
-
- conn =
- conn
- |> assign(:user, user)
- |> patch("/api/v1/pleroma/accounts/update_background", %{"img" => ""})
+ test "background image can be reset", %{user: user, conn: conn} do
+ conn = patch(conn, "/api/v1/pleroma/accounts/update_background", %{"img" => ""})
user = refresh_record(user)
- assert user.info.background == %{}
+ assert user.background == %{}
assert %{"url" => nil} = json_response(conn, 200)
end
end
describe "getting favorites timeline of specified user" do
setup do
- [current_user, user] = insert_pair(:user, %{info: %{hide_favorites: false}})
- [current_user: current_user, user: user]
+ [current_user, user] = insert_pair(:user, hide_favorites: false)
+ %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
+ [current_user: current_user, user: user, conn: conn]
end
test "returns list of statuses favorited by specified user", %{
conn: conn,
- current_user: current_user,
user: user
} do
[activity | _] = insert_pair(:note_activity)
@@ -169,7 +142,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
response =
conn
- |> assign(:user, current_user)
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
@@ -179,23 +151,18 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
assert like["id"] == activity.id
end
- test "returns favorites for specified user_id when user is not logged in", %{
- conn: conn,
+ test "does not return favorites for specified user_id when user is not logged in", %{
user: user
} do
activity = insert(:note_activity)
CommonAPI.favorite(activity.id, user)
- response =
- conn
- |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
- |> json_response(:ok)
-
- assert length(response) == 1
+ build_conn()
+ |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
+ |> json_response(403)
end
test "returns favorited DM only when user is logged in and he is one of recipients", %{
- conn: conn,
current_user: current_user,
user: user
} do
@@ -207,25 +174,24 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
CommonAPI.favorite(direct.id, user)
- response =
- conn
- |> assign(:user, current_user)
- |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
- |> json_response(:ok)
+ 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")
+ |> json_response(:ok)
- assert length(response) == 1
+ assert length(response) == 1
+ end
- anonymous_response =
- conn
- |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
- |> json_response(:ok)
-
- assert Enum.empty?(anonymous_response)
+ build_conn()
+ |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
+ |> json_response(403)
end
test "does not return others' favorited DM when user is not one of recipients", %{
conn: conn,
- current_user: current_user,
user: user
} do
user_two = insert(:user)
@@ -240,7 +206,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
response =
conn
- |> assign(:user, current_user)
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
@@ -249,7 +214,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
test "paginates favorites using since_id and max_id", %{
conn: conn,
- current_user: current_user,
user: user
} do
activities = insert_list(10, :note_activity)
@@ -263,7 +227,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
response =
conn
- |> assign(:user, current_user)
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{
since_id: third_activity.id,
max_id: seventh_activity.id
@@ -277,7 +240,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
test "limits favorites using limit parameter", %{
conn: conn,
- current_user: current_user,
user: user
} do
7
@@ -288,7 +250,6 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
response =
conn
- |> assign(:user, current_user)
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites", %{limit: "3"})
|> json_response(:ok)
@@ -297,12 +258,10 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
test "returns empty response when user does not have any favorited statuses", %{
conn: conn,
- current_user: current_user,
user: user
} do
response =
conn
- |> assign(:user, current_user)
|> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
|> json_response(:ok)
@@ -315,79 +274,61 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
assert json_response(conn, 404) == %{"error" => "Record not found"}
end
- test "returns 403 error when user has hidden own favorites", %{
- conn: conn,
- current_user: current_user
- } do
- user = insert(:user, %{info: %{hide_favorites: true}})
+ test "returns 403 error when user has hidden own favorites", %{conn: conn} do
+ user = insert(:user, hide_favorites: true)
activity = insert(:note_activity)
CommonAPI.favorite(activity.id, user)
- conn =
- conn
- |> assign(:user, current_user)
- |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
+ conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
end
- test "hides favorites for new users by default", %{conn: conn, current_user: current_user} do
+ test "hides favorites for new users by default", %{conn: conn} do
user = insert(:user)
activity = insert(:note_activity)
CommonAPI.favorite(activity.id, user)
- conn =
- conn
- |> assign(:user, current_user)
- |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
+ assert user.hide_favorites
+ conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
- assert user.info.hide_favorites
assert json_response(conn, 403) == %{"error" => "Can't get favorites"}
end
end
describe "subscribing / unsubscribing" do
- test "subscribing / unsubscribing to a user", %{conn: conn} do
- user = insert(:user)
+ test "subscribing / unsubscribing to a user" do
+ %{user: user, conn: conn} = oauth_access(["follow"])
subscription_target = insert(:user)
- conn =
+ ret_conn =
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
- assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200)
+ assert %{"id" => _id, "subscribing" => true} = json_response(ret_conn, 200)
- conn =
- build_conn()
- |> assign(:user, user)
- |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
+ conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200)
end
end
describe "subscribing" do
- test "returns 404 when subscription_target not found", %{conn: conn} do
- user = insert(:user)
+ test "returns 404 when subscription_target not found" do
+ %{conn: conn} = oauth_access(["write:follows"])
- conn =
- conn
- |> assign(:user, user)
- |> post("/api/v1/pleroma/accounts/target_id/subscribe")
+ conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
describe "unsubscribing" do
- test "returns 404 when subscription_target not found", %{conn: conn} do
- user = insert(:user)
+ test "returns 404 when subscription_target not found" do
+ %{conn: conn} = oauth_access(["follow"])
- conn =
- conn
- |> assign(:user, user)
- |> post("/api/v1/pleroma/accounts/target_id/unsubscribe")
+ conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
assert %{"error" => "Record not found"} = json_response(conn, 404)
end