alias Pleroma.Web.TwitterAPI.NotificationView
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.TwitterAPI.TwitterAPI
+ alias Comeonin.Pbkdf2
import Pleroma.Factory
- describe "POST /api/account/verify_credentials" do
- setup [:valid_user]
+ describe "POST /api/account/update_profile_banner" do
+ test "it updates the banner", %{conn: conn} do
+ user = insert(:user)
- test "without valid credentials", %{conn: conn} do
- conn = post(conn, "/api/account/verify_credentials.json")
- assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
- end
+ new_banner =
+ "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
- test "with credentials", %{conn: conn, user: user} do
- conn =
+ response =
conn
- |> with_credentials(user.nickname, "test")
- |> post("/api/account/verify_credentials.json")
+ |> assign(:user, user)
+ |> post(authenticated_twitter_api__path(conn, :update_banner), %{"banner" => new_banner})
+ |> json_response(200)
+ end
+ end
- assert response = json_response(conn, 200)
- assert response == UserView.render("show.json", %{user: user, token: response["token"]})
+ describe "POST /api/qvitter/update_background_image" do
+ test "it updates the background", %{conn: conn} do
+ user = insert(:user)
+
+ new_bg =
+ "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
+
+ response =
+ conn
+ |> assign(:user, user)
+ |> post(authenticated_twitter_api__path(conn, :update_background), %{"img" => new_bg})
+ |> json_response(200)
end
end
- describe "POST /api/account/most_recent_notification" do
+ describe "POST /api/account/verify_credentials" do
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
- conn = post(conn, "/api/account/most_recent_notification.json")
+ conn = post(conn, "/api/account/verify_credentials.json")
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
conn =
conn
|> with_credentials(user.nickname, "test")
- |> post("/api/account/most_recent_notification.json", %{id: "200"})
+ |> post("/api/account/verify_credentials.json")
- assert json_response(conn, 200)
- user = User.get_by_nickname(user.nickname)
- assert user.info["most_recent_notification"] == 200
+ assert response = json_response(conn, 200)
+ assert response == UserView.render("show.json", %{user: user, token: response["token"]})
end
end
conn = conn_with_creds |> post(request_path, %{status: " "})
assert json_response(conn, 400) == error_response
- conn = conn_with_creds |> post(request_path, %{status: "Nice meme."})
+ # we post with visibility private in order to avoid triggering relay
+ conn = conn_with_creds |> post(request_path, %{status: "Nice meme.", visibility: "private"})
assert json_response(conn, 200) ==
ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
describe "GET /statuses/public_timeline.json" do
test "returns statuses", %{conn: conn} do
- {:ok, user} = UserBuilder.insert()
+ user = insert(:user)
activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
ActivityBuilder.insert_list(10, %{}, %{user: user})
since_id = List.last(activities).id
assert length(response) == 10
end
+
+ test "returns 403 to unauthenticated request when the instance is not public" do
+ instance =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.put(:public, false)
+
+ Application.put_env(:pleroma, :instance, instance)
+
+ conn
+ |> get("/api/statuses/public_timeline.json")
+ |> json_response(403)
+
+ instance =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.put(:public, true)
+
+ Application.put_env(:pleroma, :instance, instance)
+ end
+
+ test "returns 200 to unauthenticated request when the instance is public" do
+ conn
+ |> get("/api/statuses/public_timeline.json")
+ |> json_response(200)
+ end
+ end
+
+ describe "GET /statuses/public_and_external_timeline.json" do
+ test "returns 403 to unauthenticated request when the instance is not public" do
+ instance =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.put(:public, false)
+
+ Application.put_env(:pleroma, :instance, instance)
+
+ conn
+ |> get("/api/statuses/public_and_external_timeline.json")
+ |> json_response(403)
+
+ instance =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.put(:public, true)
+
+ Application.put_env(:pleroma, :instance, instance)
+ end
+
+ test "returns 200 to unauthenticated request when the instance is public" do
+ conn
+ |> get("/api/statuses/public_and_external_timeline.json")
+ |> json_response(200)
+ end
end
describe "GET /statuses/show/:id.json" do
end
end
+ describe "GET /statuses/dm_timeline.json" do
+ test "it show direct messages", %{conn: conn} do
+ user_one = insert(:user)
+ user_two = insert(:user)
+
+ {:ok, user_two} = User.follow(user_two, user_one)
+
+ {:ok, direct} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, direct_two} =
+ CommonAPI.post(user_two, %{
+ "status" => "Hi @#{user_one.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, _follower_only} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "private"
+ })
+
+ # Only direct should be visible here
+ res_conn =
+ conn
+ |> assign(:user, user_two)
+ |> get("/api/statuses/dm_timeline.json")
+
+ [status, status_two] = json_response(res_conn, 200)
+ assert status["id"] == direct_two.id
+ assert status_two["id"] == direct.id
+ end
+ end
+
describe "GET /statuses/mentions.json" do
setup [:valid_user]
test "with credentials", %{conn: conn, user: current_user} do
other_user = insert(:user)
- {:ok, activity} =
+ {:ok, _activity} =
ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
conn =
end
end
+ describe "POST /api/qvitter/statuses/notifications/read" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567})
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials, without any params", %{conn: conn, user: current_user} do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/qvitter/statuses/notifications/read")
+
+ assert json_response(conn, 400) == %{
+ "error" => "You need to specify latest_id",
+ "request" => "/api/qvitter/statuses/notifications/read"
+ }
+ end
+
+ test "with credentials, with params", %{conn: conn, user: current_user} do
+ other_user = insert(:user)
+
+ {:ok, _activity} =
+ ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
+
+ response_conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/api/qvitter/statuses/notifications.json")
+
+ [notification] = response = json_response(response_conn, 200)
+
+ assert length(response) == 1
+
+ assert notification["is_seen"] == 0
+
+ response_conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
+
+ [notification] = response = json_response(response_conn, 200)
+
+ assert length(response) == 1
+
+ assert notification["is_seen"] == 1
+ end
+ end
+
describe "GET /statuses/user_timeline.json" do
setup [:valid_user]
|> post("/api/blocks/destroy.json", %{user_id: blocked.id})
current_user = Repo.get(User, current_user.id)
- assert current_user.info["blocks"] == []
+ assert current_user.info.blocks == []
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: blocked, for: current_user})
end
end
+ describe "GET /api/qvitter/mutes.json" do
+ setup [:valid_user]
+
+ test "unimplemented mutes without valid credentials", %{conn: conn} do
+ conn = get(conn, "/api/qvitter/mutes.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "unimplemented mutes with credentials", %{conn: conn, user: current_user} do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/api/qvitter/mutes.json")
+
+ current_user = Repo.get(User, current_user.id)
+
+ assert [] = json_response(conn, 200)
+ end
+ end
+
describe "POST /api/favorites/create/:id" do
setup [:valid_user]
assert json_response(conn, 200)
end
+
+ test "with credentials, invalid param", %{conn: conn, user: current_user} do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/favorites/create/wrong.json")
+
+ assert json_response(conn, 400)
+ end
+
+ test "with credentials, invalid activity", %{conn: conn, user: current_user} do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/favorites/create/1.json")
+
+ assert json_response(conn, 500)
+ end
end
describe "POST /api/favorites/destroy/:id" do
end
end
+ describe "POST /api/statuses/unretweet/:id" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = post(conn, "/api/statuses/unretweet/#{note_activity.id}.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ note_activity = insert(:note_activity)
+
+ request_path = "/api/statuses/retweet/#{note_activity.id}.json"
+
+ _response =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post(request_path)
+
+ request_path = String.replace(request_path, "retweet", "unretweet")
+
+ response =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post(request_path)
+
+ activity = Repo.get(Activity, note_activity.id)
+ activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
+
+ assert json_response(response, 200) ==
+ ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
+ end
+ end
+
describe "POST /api/account/register" do
test "it creates a new user", %{conn: conn} do
data = %{
result = json_response(conn, 200)
assert Enum.sort(expected) == Enum.sort(result)
end
+
+ test "it returns a given user's followers with user_id", %{conn: conn} do
+ user = insert(:user)
+ follower_one = insert(:user)
+ follower_two = insert(:user)
+ not_follower = insert(:user)
+
+ {:ok, follower_one} = User.follow(follower_one, user)
+ {:ok, follower_two} = User.follow(follower_two, user)
+
+ conn =
+ conn
+ |> assign(:user, not_follower)
+ |> get("/api/statuses/followers", %{"user_id" => user.id})
+
+ assert MapSet.equal?(
+ MapSet.new(json_response(conn, 200)),
+ MapSet.new(
+ UserView.render("index.json", %{
+ users: [follower_one, follower_two],
+ for: not_follower
+ })
+ )
+ )
+ end
+
+ test "it returns empty for a hidden network", %{conn: conn} do
+ user = insert(:user, %{info: %{hide_network: true}})
+ follower_one = insert(:user)
+ follower_two = insert(:user)
+ not_follower = insert(:user)
+
+ {:ok, follower_one} = User.follow(follower_one, user)
+ {:ok, follower_two} = User.follow(follower_two, user)
+
+ conn =
+ conn
+ |> assign(:user, not_follower)
+ |> get("/api/statuses/followers", %{"user_id" => user.id})
+
+ assert [] == json_response(conn, 200)
+ 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}})
+ follower_one = insert(:user)
+ follower_two = insert(:user)
+ not_follower = insert(:user)
+
+ {:ok, follower_one} = User.follow(follower_one, user)
+ {:ok, follower_two} = User.follow(follower_two, user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> get("/api/statuses/followers", %{"user_id" => user.id})
+
+ refute [] == json_response(conn, 200)
+ end
end
describe "GET /api/statuses/friends" do
conn =
conn
+ |> assign(:user, user)
|> get("/api/statuses/friends", %{"user_id" => user.id})
assert MapSet.equal?(
)
end
+ test "it returns empty for a hidden network", %{conn: conn} do
+ user = insert(:user, %{info: %{hide_network: true}})
+ followed_one = insert(:user)
+ followed_two = insert(:user)
+ not_followed = insert(:user)
+
+ {:ok, user} = User.follow(user, followed_one)
+ {:ok, user} = User.follow(user, followed_two)
+
+ conn =
+ conn
+ |> assign(:user, not_followed)
+ |> get("/api/statuses/friends", %{"user_id" => user.id})
+
+ 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}})
+ followed_one = insert(:user)
+ followed_two = insert(:user)
+ not_followed = insert(:user)
+
+ {:ok, user} = User.follow(user, followed_one)
+ {:ok, user} = User.follow(user, followed_two)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> get("/api/statuses/friends", %{"user_id" => user.id})
+
+ refute [] == json_response(conn, 200)
+ end
+
test "it returns a given user's friends with screen_name", %{conn: conn} do
user = insert(:user)
followed_one = insert(:user)
conn =
conn
+ |> assign(:user, user)
|> get("/api/statuses/friends", %{"screen_name" => user.nickname})
assert MapSet.equal?(
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
+ user = insert(:user)
+
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "hide_network" => "true"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.hide_network == true
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "hide_network" => "false"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.hide_network == false
+ assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
+ end
+
+ test "it locks an account", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "locked" => "true"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.locked == true
+
+ assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
+ end
+
+ test "it unlocks an account", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "locked" => "false"
+ })
+
+ user = Repo.get!(User, user.id)
+ assert user.info.locked == false
+
+ assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
+ end
end
defp valid_user(_context) do
test "Convert newlines to <br> in bio", %{conn: conn} do
user = insert(:user)
- conn =
+ _conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
assert user.bio == "Hello,<br>World! I<br> am a test."
end
+ describe "POST /api/pleroma/change_password" do
+ setup [:valid_user]
+
+ test "without credentials", %{conn: conn} do
+ conn = post(conn, "/api/pleroma/change_password")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials and invalid password", %{conn: conn, user: current_user} do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/pleroma/change_password", %{
+ "password" => "hi",
+ "new_password" => "newpass",
+ "new_password_confirmation" => "newpass"
+ })
+
+ assert json_response(conn, 200) == %{"error" => "Invalid password."}
+ end
+
+ test "with credentials, valid password and new password and confirmation not matching", %{
+ conn: conn,
+ user: current_user
+ } do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/pleroma/change_password", %{
+ "password" => "test",
+ "new_password" => "newpass",
+ "new_password_confirmation" => "notnewpass"
+ })
+
+ assert json_response(conn, 200) == %{
+ "error" => "New password does not match confirmation."
+ }
+ end
+
+ test "with credentials, valid password and invalid new password", %{
+ conn: conn,
+ user: current_user
+ } do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/pleroma/change_password", %{
+ "password" => "test",
+ "new_password" => "",
+ "new_password_confirmation" => ""
+ })
+
+ assert json_response(conn, 200) == %{
+ "error" => "New password can't be blank."
+ }
+ end
+
+ test "with credentials, valid password and matching new password and confirmation", %{
+ conn: conn,
+ user: current_user
+ } do
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/pleroma/change_password", %{
+ "password" => "test",
+ "new_password" => "newpass",
+ "new_password_confirmation" => "newpass"
+ })
+
+ assert json_response(conn, 200) == %{"status" => "success"}
+ fetched_user = Repo.get(User, current_user.id)
+ assert Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
+ end
+ end
+
describe "POST /api/pleroma/delete_account" do
setup [:valid_user]
|> post("/api/pleroma/delete_account", %{"password" => "test"})
assert json_response(conn, 200) == %{"status" => "success"}
+ # Wait a second for the started task to end
+ :timer.sleep(1000)
+ end
+ end
+
+ describe "GET /api/pleroma/friend_requests" do
+ test "it lists friend requests" do
+ user = insert(:user)
+ other_user = insert(:user)
+
+ {:ok, _activity} = ActivityPub.follow(other_user, user)
+
+ user = Repo.get(User, user.id)
+ other_user = Repo.get(User, other_user.id)
+
+ assert User.following?(other_user, user) == false
+
+ conn =
+ build_conn()
+ |> assign(:user, user)
+ |> get("/api/pleroma/friend_requests")
+
+ assert [relationship] = json_response(conn, 200)
+ assert other_user.id == relationship["id"]
+ end
+ end
+
+ describe "POST /api/pleroma/friendships/approve" do
+ test "it approves a friend request" do
+ user = insert(:user)
+ other_user = insert(:user)
+
+ {:ok, _activity} = ActivityPub.follow(other_user, user)
+
+ user = Repo.get(User, user.id)
+ other_user = Repo.get(User, other_user.id)
+
+ assert User.following?(other_user, user) == false
+
+ conn =
+ build_conn()
+ |> assign(:user, user)
+ |> post("/api/pleroma/friendships/approve", %{"user_id" => to_string(other_user.id)})
+
+ assert relationship = json_response(conn, 200)
+ assert other_user.id == relationship["id"]
+ assert relationship["follows_you"] == true
+ end
+ end
+
+ describe "POST /api/pleroma/friendships/deny" do
+ test "it denies a friend request" do
+ user = insert(:user)
+ other_user = insert(:user)
+
+ {:ok, _activity} = ActivityPub.follow(other_user, user)
+
+ user = Repo.get(User, user.id)
+ other_user = Repo.get(User, other_user.id)
+
+ assert User.following?(other_user, user) == false
+
+ conn =
+ build_conn()
+ |> assign(:user, user)
+ |> post("/api/pleroma/friendships/deny", %{"user_id" => to_string(other_user.id)})
+
+ assert relationship = json_response(conn, 200)
+ assert other_user.id == relationship["id"]
+ assert relationship["follows_you"] == false
+ end
+ end
+
+ describe "GET /api/pleroma/search_user" do
+ test "it returns users, ordered by similarity", %{conn: conn} do
+ user = insert(:user, %{name: "eal"})
+ user_two = insert(:user, %{name: "ean"})
+ user_three = insert(:user, %{name: "ebn"})
+
+ resp =
+ conn
+ |> get(twitter_api_search__path(conn, :search_user), query: "eal")
+ |> json_response(200)
+
+ assert length(resp) == 3
+ assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end)
end
end
end