X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fweb%2Ftwitter_api%2Ftwitter_api_controller_test.exs;h=05a832967643438a7a5de21fde6e17540cab6aec;hb=dff5e1e46ab5e622ae0cc4b4422ae57ce0a0d5de;hp=a4baf2b5ffd5122c692de17fe5d1921f87e2749c;hpb=fc965f982c62c43e11cb42c77f7c371c9835a9f2;p=akkoma
diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs
index a4baf2b5f..05a832967 100644
--- a/test/web/twitter_api/twitter_api_controller_test.exs
+++ b/test/web/twitter_api/twitter_api_controller_test.exs
@@ -5,9 +5,16 @@
defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
- alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
- alias Pleroma.{Repo, Activity, User, Object, Notification}
+ alias Pleroma.Builders.ActivityBuilder
+ alias Pleroma.Builders.UserBuilder
+ alias Pleroma.Repo
+ alias Pleroma.Activity
+ alias Pleroma.User
+ alias Pleroma.Object
+ alias Pleroma.Notification
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.OAuth.Token
+ alias Pleroma.Web.TwitterAPI.Controller
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.TwitterAPI.NotificationView
alias Pleroma.Web.CommonAPI
@@ -16,6 +23,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
alias Ecto.Changeset
import Pleroma.Factory
+ import Mock
@banner "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
@@ -62,7 +70,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> post("/api/account/verify_credentials.json")
|> json_response(200)
- assert response == UserView.render("show.json", %{user: user, token: response["token"]})
+ assert response ==
+ UserView.render("show.json", %{user: user, token: response["token"], for: user})
end
end
@@ -107,7 +116,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> post(request_path, %{status: "Nice meme.", visibility: "private"})
assert json_response(conn, 200) ==
- ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
+ ActivityRepresenter.to_map(Repo.one(Activity), %{user: user, for: user})
end
end
@@ -180,6 +189,20 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> get("/api/statuses/public_timeline.json")
|> json_response(200)
end
+
+ test_with_mock "treats user as unauthenticated if `assigns[:token]` is present but lacks `read` permission",
+ Controller,
+ [:passthrough],
+ [] do
+ token = insert(:oauth_token, scopes: ["write"])
+
+ build_conn()
+ |> put_req_header("authorization", "Bearer #{token.token}")
+ |> get("/api/statuses/public_timeline.json")
+ |> json_response(200)
+
+ assert called(Controller.public_timeline(%{assigns: %{user: nil}}, :_))
+ end
end
describe "GET /statuses/public_and_external_timeline.json" do
@@ -418,6 +441,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{
user: current_user,
+ for: current_user,
mentioned: [current_user]
})
end
@@ -547,7 +571,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
response = json_response(conn, 200)
assert length(response) == 1
- assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
+
+ assert Enum.at(response, 0) ==
+ ActivityRepresenter.to_map(activity, %{user: current_user, for: current_user})
end
test "with credentials with user_id", %{conn: conn, user: current_user} do
@@ -631,6 +657,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: followed, for: current_user})
end
+
+ test "for restricted account", %{conn: conn, user: current_user} do
+ followed = insert(:user, info: %User.Info{locked: true})
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/friendships/create.json", %{user_id: followed.id})
+
+ current_user = Repo.get(User, current_user.id)
+ followed = Repo.get(User, followed.id)
+
+ refute User.ap_followers(followed) in current_user.following
+ assert followed.info.follow_request_count == 1
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: followed, for: current_user})
+ end
end
describe "POST /friendships/destroy.json" do
@@ -797,7 +841,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> with_credentials(current_user.nickname, "test")
|> post("/api/favorites/create/1.json")
- assert json_response(conn, 500)
+ assert json_response(conn, 400)
end
end
@@ -1132,8 +1176,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
)
end
- test "it returns empty for a hidden network", %{conn: conn} do
- user = insert(:user, %{info: %{hide_network: true}})
+ test "it returns empty when hide_followers is set to true", %{conn: conn} do
+ user = insert(:user, %{info: %{hide_followers: true}})
follower_one = insert(:user)
follower_two = insert(:user)
not_follower = insert(:user)
@@ -1150,10 +1194,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert [] == response
end
- test "it returns the followers for a hidden network if requested by the user themselves", %{
- conn: conn
- } do
- user = insert(:user, %{info: %{hide_network: true}})
+ test "it returns the followers when hide_followers is set to true if requested by the user themselves",
+ %{
+ conn: conn
+ } do
+ user = insert(:user, %{info: %{hide_followers: true}})
follower_one = insert(:user)
follower_two = insert(:user)
_not_follower = insert(:user)
@@ -1208,7 +1253,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert Enum.sort(expected) == Enum.sort(result)
end
- test "it returns 20 friends per page", %{conn: conn} do
+ test "it returns 20 friends per page, except if 'export' is set to true", %{conn: conn} do
user = insert(:user)
followeds = insert_list(21, :user)
@@ -1232,6 +1277,14 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
result = json_response(res_conn, 200)
assert length(result) == 1
+
+ res_conn =
+ conn
+ |> assign(:user, user)
+ |> get("/api/statuses/friends", %{all: true})
+
+ result = json_response(res_conn, 200)
+ assert length(result) == 21
end
test "it returns a given user's friends with user_id", %{conn: conn} do
@@ -1256,8 +1309,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
)
end
- test "it returns empty for a hidden network", %{conn: conn} do
- user = insert(:user, %{info: %{hide_network: true}})
+ test "it returns empty when hide_follows is set to true", %{conn: conn} do
+ user = insert(:user, %{info: %{hide_follows: true}})
followed_one = insert(:user)
followed_two = insert(:user)
not_followed = insert(:user)
@@ -1273,10 +1326,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert [] == json_response(conn, 200)
end
- test "it returns friends for a hidden network if the user themselves request it", %{
- conn: conn
- } do
- user = insert(:user, %{info: %{hide_network: true}})
+ test "it returns friends when hide_follows is set to true if the user themselves request it",
+ %{
+ conn: conn
+ } do
+ user = insert(:user, %{info: %{hide_follows: true}})
followed_one = insert(:user)
followed_two = insert(:user)
_not_followed = insert(:user)
@@ -1357,34 +1411,82 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert user.name == "new name"
assert user.bio ==
- "hi @#{
- user2.nickname
- }"
+ "hi @#{user2.nickname}"
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
- test "it sets and un-sets hide_network", %{conn: conn} do
+ test "it sets and un-sets hide_follows", %{conn: conn} do
user = insert(:user)
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
- "hide_network" => "true"
+ "hide_follows" => "true"
})
user = Repo.get!(User, user.id)
- assert user.info.hide_network == true
+ assert user.info.hide_follows == true
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
- "hide_network" => "false"
+ "hide_follows" => "false"
})
user = Repo.get!(User, user.id)
- assert user.info.hide_network == false
+ assert user.info.hide_follows == false
+ assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
+ end
+
+ test "it sets and un-sets hide_followers", %{conn: conn} do
+ user = insert(:user)
+
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "hide_followers" => "true"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.hide_followers == true
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "hide_followers" => "false"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.hide_followers == false
+ assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
+ end
+
+ test "it sets and un-sets show_role", %{conn: conn} do
+ user = insert(:user)
+
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "show_role" => "true"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.show_role == true
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "show_role" => "false"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.show_role == false
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
@@ -1604,6 +1706,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
assert [relationship] = json_response(conn, 200)
assert other_user.id == relationship["id"]
end
+
+ test "requires 'read' permission", %{conn: conn} do
+ token1 = insert(:oauth_token, scopes: ["write"])
+ token2 = insert(:oauth_token, scopes: ["read"])
+
+ for token <- [token1, token2] do
+ conn =
+ conn
+ |> put_req_header("authorization", "Bearer #{token.token}")
+ |> get("/api/pleroma/friend_requests")
+
+ if token == token1 do
+ assert %{"error" => "Insufficient permissions: read."} == json_response(conn, 403)
+ else
+ assert json_response(conn, 200)
+ end
+ end
+ end
end
describe "POST /api/pleroma/friendships/approve" do
@@ -1617,15 +1737,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
other_user = Repo.get(User, other_user.id)
assert User.following?(other_user, user) == false
+ assert user.info.follow_request_count == 1
conn =
build_conn()
|> assign(:user, user)
- |> post("/api/pleroma/friendships/approve", %{"user_id" => to_string(other_user.id)})
+ |> post("/api/pleroma/friendships/approve", %{"user_id" => other_user.id})
+
+ user = Repo.get(User, user.id)
assert relationship = json_response(conn, 200)
assert other_user.id == relationship["id"]
assert relationship["follows_you"] == true
+ assert user.info.follow_request_count == 0
end
end
@@ -1640,15 +1764,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
other_user = Repo.get(User, other_user.id)
assert User.following?(other_user, user) == false
+ assert user.info.follow_request_count == 1
conn =
build_conn()
|> assign(:user, user)
- |> post("/api/pleroma/friendships/deny", %{"user_id" => to_string(other_user.id)})
+ |> post("/api/pleroma/friendships/deny", %{"user_id" => other_user.id})
+
+ user = Repo.get(User, user.id)
assert relationship = json_response(conn, 200)
assert other_user.id == relationship["id"]
assert relationship["follows_you"] == false
+ assert user.info.follow_request_count == 0
end
end
@@ -1656,7 +1784,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
test "it returns users, ordered by similarity", %{conn: conn} do
user = insert(:user, %{name: "eal"})
user_two = insert(:user, %{name: "eal me"})
- _user_three = insert(:user, %{name: "ebn"})
+ _user_three = insert(:user, %{name: "zzz"})
resp =
conn
@@ -1788,7 +1916,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
user = refresh_record(user)
- assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user})
+ assert json_response(response, 200) ==
+ ActivityRepresenter.to_map(activity, %{user: user, for: user})
end
end
@@ -1817,7 +1946,42 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
user = refresh_record(user)
- assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user})
+ assert json_response(response, 200) ==
+ ActivityRepresenter.to_map(activity, %{user: user, for: user})
+ end
+ end
+
+ describe "GET /api/oauth_tokens" do
+ setup do
+ token = insert(:oauth_token) |> Repo.preload(:user)
+
+ %{token: token}
+ end
+
+ test "renders list", %{token: token} do
+ response =
+ build_conn()
+ |> assign(:user, token.user)
+ |> get("/api/oauth_tokens")
+
+ keys =
+ json_response(response, 200)
+ |> hd()
+ |> Map.keys()
+
+ assert keys -- ["id", "app_name", "valid_until"] == []
+ end
+
+ test "revoke token", %{token: token} do
+ response =
+ build_conn()
+ |> assign(:user, token.user)
+ |> delete("/api/oauth_tokens/#{token.id}")
+
+ tokens = Token.get_user_tokens(token.user)
+
+ assert tokens == []
+ assert response.status == 201
end
end
end