pleroma/test/web/twitter_api/twitter_api_controller_test.exs

908 lines
28 KiB
Elixir
Raw Normal View History

defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase
2017-06-19 17:12:37 -04:00
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
2017-03-21 16:09:20 -04:00
alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
alias Pleroma.{Repo, Activity, User, Object, Notification}
2017-04-14 12:27:17 -04:00
alias Pleroma.Web.ActivityPub.ActivityPub
2017-06-19 17:12:37 -04:00
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.TwitterAPI.NotificationView
2017-09-16 08:33:47 -04:00
alias Pleroma.Web.CommonAPI
2017-11-14 10:34:48 -05:00
alias Pleroma.Web.TwitterAPI.TwitterAPI
2018-05-21 17:17:34 -04:00
alias Comeonin.Pbkdf2
2017-04-13 11:22:44 -04:00
import Pleroma.Factory
describe "POST /api/account/verify_credentials" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/account/verify_credentials.json")
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: user} do
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(user.nickname, "test")
|> post("/api/account/verify_credentials.json")
2017-12-05 06:18:04 -05:00
assert response = json_response(conn, 200)
assert response == UserView.render("show.json", %{user: user, token: response["token"]})
end
end
describe "POST /api/account/most_recent_notification" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/account/most_recent_notification.json")
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: user} do
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(user.nickname, "test")
|> post("/api/account/most_recent_notification.json", %{id: "200"})
assert json_response(conn, 200)
user = User.get_by_nickname(user.nickname)
assert user.info["most_recent_notification"] == 200
end
end
2017-03-21 13:17:35 -04:00
describe "POST /statuses/update.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-03-21 13:17:35 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/statuses/update.json")
2017-03-21 13:17:35 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: user} do
2017-04-23 12:08:25 -04:00
conn_with_creds = conn |> with_credentials(user.nickname, "test")
request_path = "/api/statuses/update.json"
2018-03-30 09:01:53 -04:00
error_response = %{
"request" => request_path,
"error" => "Client must provide a 'status' parameter with a value."
}
2017-04-23 12:08:25 -04:00
conn = conn_with_creds |> post(request_path)
assert json_response(conn, 400) == error_response
2018-03-30 09:01:53 -04:00
conn = conn_with_creds |> post(request_path, %{status: ""})
2017-04-23 12:08:25 -04:00
assert json_response(conn, 400) == error_response
2017-03-21 13:17:35 -04:00
2018-03-30 09:01:53 -04:00
conn = conn_with_creds |> post(request_path, %{status: " "})
2017-04-24 05:09:11 -04:00
assert json_response(conn, 400) == error_response
2018-03-30 09:01:53 -04:00
conn = conn_with_creds |> post(request_path, %{status: "Nice meme."})
assert json_response(conn, 200) ==
ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
2017-03-21 13:17:35 -04:00
end
end
2017-03-21 16:09:20 -04:00
describe "GET /statuses/public_timeline.json" do
test "returns statuses", %{conn: conn} do
2018-03-30 09:01:53 -04:00
{:ok, user} = UserBuilder.insert()
2017-03-21 16:09:20 -04:00
activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
ActivityBuilder.insert_list(10, %{}, %{user: user})
since_id = List.last(activities).id
2018-03-30 09:01:53 -04:00
conn =
conn
2017-03-21 16:09:20 -04:00
|> get("/api/statuses/public_timeline.json", %{since_id: since_id})
response = json_response(conn, 200)
assert length(response) == 10
end
end
2017-03-23 20:16:28 -04:00
describe "GET /statuses/show/:id.json" do
test "returns one status", %{conn: conn} do
2018-03-27 16:24:19 -04:00
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"})
2017-03-23 20:16:28 -04:00
actor = Repo.get_by!(User, ap_id: activity.data["actor"])
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/statuses/show/#{activity.id}.json")
2017-03-23 20:16:28 -04:00
response = json_response(conn, 200)
2017-03-28 08:04:13 -04:00
assert response == ActivityRepresenter.to_map(activity, %{user: actor})
2017-03-23 20:16:28 -04:00
end
end
2017-11-14 10:34:48 -05:00
describe "GET /users/show.json" do
test "gets user with screen_name", %{conn: conn} do
user = insert(:user)
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/users/show.json", %{"screen_name" => user.nickname})
2017-11-14 10:34:48 -05:00
response = json_response(conn, 200)
assert response["id"] == user.id
end
test "gets user with user_id", %{conn: conn} do
user = insert(:user)
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/users/show.json", %{"user_id" => user.id})
2017-11-14 10:34:48 -05:00
response = json_response(conn, 200)
assert response["id"] == user.id
end
test "gets a user for a logged in user", %{conn: conn} do
user = insert(:user)
logged_in = insert(:user)
{:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id})
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(logged_in.nickname, "test")
|> get("/api/users/show.json", %{"user_id" => user.id})
2017-11-14 10:34:48 -05:00
response = json_response(conn, 200)
assert response["following"] == true
end
end
describe "GET /statusnet/conversation/:id.json" do
test "returns the statuses in the conversation", %{conn: conn} do
2018-03-30 09:01:53 -04:00
{:ok, _user} = UserBuilder.insert()
{:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
{:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
{:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/statusnet/conversation/#{activity.data["context_id"]}.json")
response = json_response(conn, 200)
assert length(response) == 2
end
end
2017-03-22 12:25:59 -04:00
describe "GET /statuses/friends_timeline.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-03-22 12:25:59 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = get(conn, "/api/statuses/friends_timeline.json")
2017-03-22 12:25:59 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
2017-04-16 09:28:28 -04:00
user = insert(:user)
2018-03-30 09:01:53 -04:00
activities =
ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
returned_activities =
ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
2017-04-16 09:28:28 -04:00
other_user = insert(:user)
2017-03-22 12:25:59 -04:00
ActivityBuilder.insert_list(10, %{}, %{user: other_user})
since_id = List.last(activities).id
2018-03-30 09:01:53 -04:00
current_user =
Ecto.Changeset.change(current_user, following: [User.ap_followers(user)])
|> Repo.update!()
2017-03-22 12:25:59 -04:00
2018-03-30 09:01:53 -04:00
conn =
conn
2017-03-22 12:25:59 -04:00
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
response = json_response(conn, 200)
assert length(response) == 10
2018-03-30 09:01:53 -04:00
assert response ==
Enum.map(returned_activities, fn activity ->
ActivityRepresenter.to_map(activity, %{
user: User.get_cached_by_ap_id(activity.data["actor"]),
for: current_user
})
end)
2017-03-22 12:25:59 -04:00
end
end
2017-04-20 06:53:53 -04:00
describe "GET /statuses/mentions.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-20 06:53:53 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = get(conn, "/api/statuses/mentions.json")
2017-04-20 06:53:53 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
2018-03-30 09:01:53 -04:00
{:ok, activity} =
ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
2017-04-20 06:53:53 -04:00
2018-03-30 09:01:53 -04:00
conn =
conn
2017-04-20 06:53:53 -04:00
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/mentions.json")
response = json_response(conn, 200)
assert length(response) == 1
2018-03-30 09:01:53 -04:00
assert Enum.at(response, 0) ==
ActivityRepresenter.to_map(activity, %{
user: current_user,
mentioned: [current_user]
})
2017-04-20 06:53:53 -04:00
end
end
describe "GET /api/qvitter/statuses/notifications.json" do
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
conn = get(conn, "/api/qvitter/statuses/notifications.json")
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
other_user = insert(:user)
{:ok, activity} =
ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/qvitter/statuses/notifications.json")
response = json_response(conn, 200)
assert length(response) == 1
assert response ==
2018-04-20 07:10:57 -04:00
NotificationView.render("notification.json", %{
notifications: Notification.for_user(current_user),
for: current_user
})
end
end
2017-04-17 09:31:19 -04:00
describe "GET /statuses/user_timeline.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-17 09:31:19 -04:00
test "without any params", %{conn: conn} do
conn = get(conn, "/api/statuses/user_timeline.json")
2018-03-30 09:01:53 -04:00
assert json_response(conn, 400) == %{
"error" => "You need to specify screen_name or user_id",
"request" => "/api/statuses/user_timeline.json"
}
2017-04-17 09:31:19 -04:00
end
test "with user_id", %{conn: conn} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with screen_name", %{conn: conn} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with credentials", %{conn: conn, user: current_user} do
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json")
2017-04-17 09:31:19 -04:00
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
end
test "with credentials with user_id", %{conn: conn, user: current_user} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
2017-04-17 09:31:19 -04:00
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with credentials screen_name", %{conn: conn, user: current_user} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
2017-04-17 09:31:19 -04:00
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
end
2017-03-22 13:36:08 -04:00
describe "POST /friendships/create.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-03-22 13:36:08 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/friendships/create.json")
2017-03-22 13:36:08 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
2017-04-16 09:28:28 -04:00
followed = insert(:user)
2017-03-22 13:36:08 -04:00
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/friendships/create.json", %{user_id: followed.id})
2017-03-22 13:36:08 -04:00
current_user = Repo.get(User, current_user.id)
assert User.ap_followers(followed) in current_user.following
2018-03-30 09:01:53 -04:00
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: followed, for: current_user})
2017-03-22 13:36:08 -04:00
end
end
2017-03-23 08:13:09 -04:00
describe "POST /friendships/destroy.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-03-23 08:13:09 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/friendships/destroy.json")
2017-03-23 08:13:09 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
2017-04-16 09:28:28 -04:00
followed = insert(:user)
2017-03-23 08:13:09 -04:00
{:ok, current_user} = User.follow(current_user, followed)
assert User.ap_followers(followed) in current_user.following
ActivityPub.follow(current_user, followed)
2017-03-23 08:13:09 -04:00
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/friendships/destroy.json", %{user_id: followed.id})
2017-03-23 08:13:09 -04:00
current_user = Repo.get(User, current_user.id)
assert current_user.following == [current_user.ap_id]
2018-03-30 09:01:53 -04:00
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: followed, for: current_user})
2017-03-23 08:13:09 -04:00
end
end
2017-11-07 17:04:53 -05:00
describe "POST /blocks/create.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-11-07 17:04:53 -05:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/blocks/create.json")
2017-11-07 17:04:53 -05:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
blocked = insert(:user)
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/blocks/create.json", %{user_id: blocked.id})
2017-11-07 17:04:53 -05:00
current_user = Repo.get(User, current_user.id)
assert User.blocks?(current_user, blocked)
2018-03-30 09:01:53 -04:00
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: blocked, for: current_user})
2017-11-07 17:04:53 -05:00
end
end
describe "POST /blocks/destroy.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-11-07 17:04:53 -05:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/blocks/destroy.json")
2017-11-07 17:04:53 -05:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
blocked = insert(:user)
{:ok, current_user} = User.block(current_user, blocked)
assert User.blocks?(current_user, blocked)
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/blocks/destroy.json", %{user_id: blocked.id})
2017-11-07 17:04:53 -05:00
current_user = Repo.get(User, current_user.id)
assert current_user.info["blocks"] == []
2018-03-30 09:01:53 -04:00
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: blocked, for: current_user})
2017-11-07 17:04:53 -05:00
end
end
2017-04-10 09:00:57 -04:00
describe "GET /help/test.json" do
test "returns \"ok\"", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = get(conn, "/api/help/test.json")
2017-04-10 09:00:57 -04:00
assert json_response(conn, 200) == "ok"
2017-04-17 05:43:44 -04:00
end
end
2017-04-16 10:06:19 -04:00
describe "POST /api/qvitter/update_avatar.json" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-16 10:06:19 -04:00
test "without valid credentials", %{conn: conn} do
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/qvitter/update_avatar.json")
2017-04-16 10:06:19 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
2018-02-25 11:48:31 -05:00
avatar_image = File.read!("test/fixtures/avatar_data_uri")
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/qvitter/update_avatar.json", %{img: avatar_image})
2017-04-16 10:06:19 -04:00
current_user = Repo.get(User, current_user.id)
assert is_map(current_user.avatar)
2018-03-30 09:01:53 -04:00
assert json_response(conn, 200) ==
UserView.render("show.json", %{user: current_user, for: current_user})
2017-04-16 10:06:19 -04:00
end
end
2017-04-13 11:22:44 -04:00
describe "POST /api/favorites/create/:id" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-13 11:22:44 -04:00
test "without valid credentials", %{conn: conn} do
note_activity = insert(:note_activity)
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/favorites/create/#{note_activity.id}.json")
2017-04-13 11:22:44 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
note_activity = insert(:note_activity)
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/favorites/create/#{note_activity.id}.json")
2017-04-13 11:22:44 -04:00
assert json_response(conn, 200)
end
end
2017-04-14 12:27:17 -04:00
describe "POST /api/favorites/destroy/:id" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-14 12:27:17 -04:00
test "without valid credentials", %{conn: conn} do
note_activity = insert(:note_activity)
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/favorites/destroy/#{note_activity.id}.json")
2017-04-14 12:27:17 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
note_activity = insert(:note_activity)
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
ActivityPub.like(current_user, object)
2018-03-30 09:01:53 -04:00
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/favorites/destroy/#{note_activity.id}.json")
2017-04-14 12:27:17 -04:00
assert json_response(conn, 200)
end
end
2017-04-15 08:09:54 -04:00
describe "POST /api/statuses/retweet/:id" do
setup [:valid_user]
2018-03-30 09:01:53 -04:00
2017-04-15 08:09:54 -04:00
test "without valid credentials", %{conn: conn} do
note_activity = insert(:note_activity)
2018-03-30 09:01:53 -04:00
conn = post(conn, "/api/statuses/retweet/#{note_activity.id}.json")
2017-04-15 08:09:54 -04:00
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
note_activity = insert(:note_activity)
2017-04-23 18:11:38 -04:00
request_path = "/api/statuses/retweet/#{note_activity.id}.json"
2017-04-15 08:09:54 -04:00
2018-03-30 09:01:53 -04:00
response =
conn
|> with_credentials(current_user.nickname, "test")
|> post(request_path)
2017-04-23 18:11:38 -04:00
activity = Repo.get(Activity, note_activity.id)
2017-04-26 02:55:00 -04:00
activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
2018-03-30 09:01:53 -04:00
assert json_response(response, 200) ==
ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
2017-04-15 08:09:54 -04:00
end
end
describe "POST /api/account/register" do
test "it creates a new user", %{conn: conn} do
data = %{
"nickname" => "lain",
"email" => "lain@wired.jp",
"fullname" => "lain iwakura",
"bio" => "close the world.",
"password" => "bear",
"confirm" => "bear"
}
2018-03-30 09:01:53 -04:00
conn =
conn
|> post("/api/account/register", data)
user = json_response(conn, 200)
fetched_user = Repo.get_by(User, nickname: "lain")
2017-06-19 17:12:37 -04:00
assert user == UserView.render("show.json", %{user: fetched_user})
end
test "it returns errors on a problem", %{conn: conn} do
data = %{
"email" => "lain@wired.jp",
"fullname" => "lain iwakura",
"bio" => "close the world.",
"password" => "bear",
"confirm" => "bear"
}
2018-03-30 09:01:53 -04:00
conn =
conn
|> post("/api/account/register", data)
errors = json_response(conn, 400)
assert is_binary(errors["error"])
2017-04-10 09:00:57 -04:00
end
end
2017-05-10 12:44:57 -04:00
describe "GET /api/externalprofile/show" do
test "it returns the user", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
2017-05-10 12:44:57 -04:00
2018-03-30 09:01:53 -04:00
conn =
conn
|> assign(:user, user)
|> get("/api/externalprofile/show", %{profileurl: other_user.ap_id})
2017-05-10 12:44:57 -04:00
assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user})
2017-05-10 12:44:57 -04:00
end
end
2017-07-20 14:29:15 -04:00
describe "GET /api/statuses/followers" do
test "it returns a user's followers", %{conn: conn} do
user = insert(:user)
follower_one = insert(:user)
follower_two = insert(:user)
2018-02-12 04:13:54 -05:00
_not_follower = insert(:user)
2017-07-20 14:29:15 -04:00
{:ok, follower_one} = User.follow(follower_one, user)
{:ok, follower_two} = User.follow(follower_two, user)
2018-03-30 09:01:53 -04:00
conn =
conn
|> assign(:user, user)
|> get("/api/statuses/followers")
2017-07-20 14:29:15 -04:00
2018-04-02 07:13:04 -04:00
expected = UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
result = json_response(conn, 200)
assert Enum.sort(expected) == Enum.sort(result)
2017-07-20 14:29:15 -04:00
end
end
2017-07-20 14:35:30 -04:00
describe "GET /api/statuses/friends" do
test "it returns the logged in user's friends", %{conn: conn} do
2017-07-20 14:35:30 -04:00
user = insert(:user)
followed_one = insert(:user)
followed_two = insert(:user)
2018-02-12 04:13:54 -05:00
_not_followed = insert(:user)
2017-07-20 14:35:30 -04:00
{:ok, user} = User.follow(user, followed_one)
{:ok, user} = User.follow(user, followed_two)
2018-03-30 09:01:53 -04:00
conn =
conn
|> assign(:user, user)
|> get("/api/statuses/friends")
2017-07-20 14:35:30 -04:00
2018-04-02 07:13:04 -04:00
expected = UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
result = json_response(conn, 200)
assert Enum.sort(expected) == Enum.sort(result)
2017-07-20 14:35:30 -04:00
end
test "it returns a given user's friends with user_id", %{conn: conn} do
user = insert(:user)
followed_one = insert(:user)
followed_two = insert(:user)
2018-02-12 04:13:54 -05:00
_not_followed = insert(:user)
{:ok, user} = User.follow(user, followed_one)
{:ok, user} = User.follow(user, followed_two)
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/statuses/friends", %{"user_id" => user.id})
2018-03-30 09:01:53 -04:00
assert MapSet.equal?(
MapSet.new(json_response(conn, 200)),
MapSet.new(
UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
)
)
end
test "it returns a given user's friends with screen_name", %{conn: conn} do
user = insert(:user)
followed_one = insert(:user)
followed_two = insert(:user)
2018-02-12 04:13:54 -05:00
_not_followed = insert(:user)
{:ok, user} = User.follow(user, followed_one)
{:ok, user} = User.follow(user, followed_two)
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/statuses/friends", %{"screen_name" => user.nickname})
2018-03-30 09:01:53 -04:00
assert MapSet.equal?(
MapSet.new(json_response(conn, 200)),
MapSet.new(
UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
)
)
end
2017-07-20 14:35:30 -04:00
end
2017-11-14 09:04:58 -05:00
describe "GET /friends/ids" do
test "it returns a user's friends", %{conn: conn} do
user = insert(:user)
followed_one = insert(:user)
followed_two = insert(:user)
2018-02-12 04:13:54 -05:00
_not_followed = insert(:user)
2017-11-14 09:04:58 -05:00
{:ok, user} = User.follow(user, followed_one)
{:ok, user} = User.follow(user, followed_two)
2018-03-30 09:01:53 -04:00
conn =
conn
|> assign(:user, user)
|> get("/api/friends/ids")
2017-11-14 09:04:58 -05:00
expected = [followed_one.id, followed_two.id]
2018-03-30 09:01:53 -04:00
assert MapSet.equal?(
MapSet.new(Poison.decode!(json_response(conn, 200))),
MapSet.new(expected)
)
2017-11-14 09:04:58 -05:00
end
end
2017-08-29 09:14:00 -04:00
describe "POST /api/account/update_profile.json" do
2018-02-12 03:33:01 -05:00
test "it updates a user's profile", %{conn: conn} do
2017-08-29 09:14:00 -04:00
user = insert(:user)
2018-03-30 09:01:53 -04:00
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"name" => "new name",
"description" => "new description"
})
2017-08-29 09:14:00 -04:00
user = Repo.get!(User, user.id)
assert user.name == "new name"
assert user.bio == "new description"
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
end
defp valid_user(_context) do
2017-04-20 18:51:09 -04:00
user = insert(:user)
[user: user]
end
defp with_credentials(conn, username, password) do
header_content = "Basic " <> Base.encode64("#{username}:#{password}")
put_req_header(conn, "authorization", header_content)
end
2017-09-16 08:33:47 -04:00
describe "GET /api/search.json" do
test "it returns search results", %{conn: conn} do
user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
{:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"})
2017-09-16 08:33:47 -04:00
assert [status] = json_response(conn, 200)
assert status["id"] == activity.id
end
end
2017-09-17 05:45:16 -04:00
describe "GET /api/statusnet/tags/timeline/:tag.json" do
2018-02-12 03:33:01 -05:00
test "it returns the tags timeline", %{conn: conn} do
2017-09-17 05:45:16 -04:00
user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about #2hu"})
{:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
2018-03-30 09:01:53 -04:00
conn =
conn
|> get("/api/statusnet/tags/timeline/2hu.json")
2017-09-17 05:45:16 -04:00
assert [status] = json_response(conn, 200)
assert status["id"] == activity.id
end
end
test "Convert newlines to <br> in bio", %{conn: conn} do
2018-04-29 05:28:26 -04:00
user = insert(:user)
2018-04-29 05:28:26 -04:00
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"description" => "Hello,\r\nWorld! I\n am a test."
})
2018-04-29 05:28:26 -04:00
user = Repo.get!(User, user.id)
assert user.bio == "Hello,<br>World! I<br> am a test."
end
2018-05-21 17:17:34 -04:00
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]
test "without credentials", %{conn: conn} do
conn = post(conn, "/api/pleroma/delete_account")
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/delete_account", %{"password" => "hi"})
assert json_response(conn, 200) == %{"error" => "Invalid password."}
end
test "with credentials and valid password", %{conn: conn, user: current_user} do
conn =
conn
|> with_credentials(current_user.nickname, "test")
|> post("/api/pleroma/delete_account", %{"password" => "test"})
assert json_response(conn, 200) == %{"status" => "success"}
end
end
end