+
+ 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)
+ end
+ end
+
+ describe "GET /statuses/mentions.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = get(conn, "/api/statuses/mentions.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ {:ok, activity} =
+ ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/api/statuses/mentions.json")
+
+ response = json_response(conn, 200)
+
+ assert length(response) == 1
+
+ assert Enum.at(response, 0) ==
+ ActivityRepresenter.to_map(activity, %{
+ user: current_user,
+ mentioned: [current_user]
+ })
+ end
+ end
+
+ describe "GET /statuses/user_timeline.json" do
+ setup [:valid_user]
+
+ test "without any params", %{conn: conn} do
+ conn = get(conn, "/api/statuses/user_timeline.json")
+
+ assert json_response(conn, 400) == %{
+ "error" => "You need to specify screen_name or user_id",
+ "request" => "/api/statuses/user_timeline.json"
+ }
+ 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})
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/api/statuses/user_timeline.json")
+
+ 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})
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/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 credentials screen_name", %{conn: conn, user: current_user} do
+ user = insert(:user)
+ {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> get("/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
+ end
+
+ describe "POST /friendships/create.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/friendships/create.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ followed = insert(:user)
+
+ 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)
+ assert User.ap_followers(followed) in current_user.following
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: followed, for: current_user})
+ end
+ end
+
+ describe "POST /friendships/destroy.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/friendships/destroy.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ followed = insert(:user)
+
+ {:ok, current_user} = User.follow(current_user, followed)
+ assert User.ap_followers(followed) in current_user.following
+ ActivityPub.follow(current_user, followed)
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/friendships/destroy.json", %{user_id: followed.id})
+
+ current_user = Repo.get(User, current_user.id)
+ assert current_user.following == [current_user.ap_id]
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: followed, for: current_user})
+ end
+ end
+
+ describe "POST /blocks/create.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/blocks/create.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ blocked = insert(:user)
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/blocks/create.json", %{user_id: blocked.id})
+
+ current_user = Repo.get(User, current_user.id)
+ assert User.blocks?(current_user, blocked)
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: blocked, for: current_user})
+ end
+ end
+
+ describe "POST /blocks/destroy.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/blocks/destroy.json")
+ 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)
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
+
+ current_user = Repo.get(User, current_user.id)
+ assert current_user.info["blocks"] == []
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: blocked, for: current_user})
+ end
+ end
+
+ describe "GET /help/test.json" do
+ test "returns \"ok\"", %{conn: conn} do
+ conn = get(conn, "/api/help/test.json")
+ assert json_response(conn, 200) == "ok"
+ end
+ end
+
+ describe "POST /api/qvitter/update_avatar.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/qvitter/update_avatar.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ avatar_image = File.read!("test/fixtures/avatar_data_uri")
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/qvitter/update_avatar.json", %{img: avatar_image})
+
+ current_user = Repo.get(User, current_user.id)
+ assert is_map(current_user.avatar)
+
+ assert json_response(conn, 200) ==
+ UserView.render("show.json", %{user: current_user, for: current_user})
+ end
+ end
+
+ describe "POST /api/favorites/create/:id" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = post(conn, "/api/favorites/create/#{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)
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/favorites/create/#{note_activity.id}.json")
+
+ assert json_response(conn, 200)
+ end
+ end
+
+ describe "POST /api/favorites/destroy/:id" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = post(conn, "/api/favorites/destroy/#{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)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ ActivityPub.like(current_user, object)
+
+ conn =
+ conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/favorites/destroy/#{note_activity.id}.json")
+
+ assert json_response(conn, 200)
+ end
+ end
+
+ describe "POST /api/statuses/retweet/:id" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ note_activity = insert(:note_activity)
+ conn = post(conn, "/api/statuses/retweet/#{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)
+
+ 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 = %{
+ "nickname" => "lain",
+ "email" => "lain@wired.jp",
+ "fullname" => "lain iwakura",
+ "bio" => "close the world.",
+ "password" => "bear",
+ "confirm" => "bear"
+ }
+
+ conn =
+ conn
+ |> post("/api/account/register", data)
+
+ user = json_response(conn, 200)
+
+ fetched_user = Repo.get_by(User, nickname: "lain")
+ 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"
+ }
+
+ conn =
+ conn
+ |> post("/api/account/register", data)
+
+ errors = json_response(conn, 400)
+
+ assert is_binary(errors["error"])
+ end
+ end
+
+ describe "GET /api/externalprofile/show" do
+ test "it returns the user", %{conn: conn} do
+ user = insert(:user)
+ other_user = insert(:user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id})
+
+ assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user})
+ end
+ end
+
+ 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)
+ _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")
+
+ expected = UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
+ result = json_response(conn, 200)
+ assert Enum.sort(expected) == Enum.sort(result)
+ end
+ end
+
+ describe "GET /api/statuses/friends" do
+ test "it returns the logged in user's friends", %{conn: conn} do
+ user = insert(:user)
+ 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")
+
+ expected = UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
+ result = json_response(conn, 200)
+ assert Enum.sort(expected) == Enum.sort(result)
+ 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)
+ _not_followed = insert(:user)
+
+ {:ok, user} = User.follow(user, followed_one)
+ {:ok, user} = User.follow(user, followed_two)
+
+ conn =
+ conn
+ |> get("/api/statuses/friends", %{"user_id" => user.id})
+
+ 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)
+ _not_followed = insert(:user)
+
+ {:ok, user} = User.follow(user, followed_one)
+ {:ok, user} = User.follow(user, followed_two)
+
+ conn =
+ conn
+ |> get("/api/statuses/friends", %{"screen_name" => user.nickname})
+
+ assert MapSet.equal?(
+ MapSet.new(json_response(conn, 200)),
+ MapSet.new(
+ UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
+ )
+ )
+ end
+ end
+
+ 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)
+ _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/friends/ids")
+
+ expected = [followed_one.id, followed_two.id]
+
+ assert MapSet.equal?(
+ MapSet.new(Poison.decode!(json_response(conn, 200))),
+ MapSet.new(expected)
+ )
+ end
+ end
+
+ describe "POST /api/account/update_profile.json" do
+ test "it updates a user's profile", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> assign(:user, user)
+ |> post("/api/account/update_profile.json", %{
+ "name" => "new name",
+ "description" => "new description"
+ })
+
+ 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})