+ 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"
+
+ user = Repo.get_by(User, ap_id: note_activity.data["actor"])
+ response = conn
+ |> with_credentials(user.nickname, "test")
+ |> post(request_path)
+ assert json_response(response, 400) == %{"error" => "You cannot repeat your own notice.",
+ "request" => request_path}
+
+ 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)
+
+ conn = conn
+ |> get("/api/externalprofile/show", %{profileurl: user.ap_id})
+
+ assert json_response(conn, 200) == UserView.render("show.json", %{user: 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")
+
+ assert json_response(conn, 200) == UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
+ end
+ end
+
+ describe "GET /api/statuses/friends" 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/statuses/friends")
+
+ assert json_response(conn, 200) == UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
+ end
+ end
+