+ assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: user, for: current_user}) end)
+ 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
+ {:ok, followed } = UserBuilder.insert(%{name: "some guy"})
+
+ 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 current_user.following == [User.ap_followers(followed)]
+ assert json_response(conn, 200) == UserRepresenter.to_map(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
+ {:ok, followed } = UserBuilder.insert(%{name: "some guy"})
+
+ {:ok, current_user} = User.follow(current_user, followed)
+ assert current_user.following == [User.ap_followers(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 == []
+ assert json_response(conn, 200) == UserRepresenter.to_map(followed, %{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)
+
+ conn = conn
+ |> with_credentials(current_user.nickname, "test")
+ |> post("/api/statuses/retweet/#{note_activity.id}.json")
+
+ assert json_response(conn, 200)
+ 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 == UserRepresenter.to_map(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"])