+ |> 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
+
+ describe "POST /statuses/update.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = post(conn, "/api/statuses/update.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: user} do
+ conn_with_creds = conn |> with_credentials(user.nickname, "test")
+ request_path = "/api/statuses/update.json"
+
+ error_response = %{
+ "request" => request_path,
+ "error" => "Client must provide a 'status' parameter with a value."
+ }
+
+ conn = conn_with_creds |> post(request_path)
+ assert json_response(conn, 400) == error_response
+
+ conn = conn_with_creds |> post(request_path, %{status: ""})
+ assert json_response(conn, 400) == error_response
+
+ 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."})
+
+ assert json_response(conn, 200) ==
+ ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
+ end
+ end
+
+ describe "GET /statuses/public_timeline.json" do
+ test "returns statuses", %{conn: conn} do
+ {:ok, user} = UserBuilder.insert()
+ activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
+ ActivityBuilder.insert_list(10, %{}, %{user: user})
+ since_id = List.last(activities).id
+
+ conn =
+ conn
+ |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
+
+ response = json_response(conn, 200)
+
+ assert length(response) == 10
+ end
+ end
+
+ describe "GET /statuses/show/:id.json" do
+ test "returns one status", %{conn: conn} do
+ user = insert(:user)
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"})
+ actor = Repo.get_by!(User, ap_id: activity.data["actor"])
+
+ conn =
+ conn
+ |> get("/api/statuses/show/#{activity.id}.json")
+
+ response = json_response(conn, 200)
+
+ assert response == ActivityRepresenter.to_map(activity, %{user: actor})
+ end
+ end
+
+ describe "GET /users/show.json" do
+ test "gets user with screen_name", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> get("/api/users/show.json", %{"screen_name" => user.nickname})
+
+ response = json_response(conn, 200)
+
+ assert response["id"] == user.id
+ end
+
+ test "gets user with user_id", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> get("/api/users/show.json", %{"user_id" => user.id})
+
+ 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})
+
+ conn =
+ conn
+ |> with_credentials(logged_in.nickname, "test")
+ |> get("/api/users/show.json", %{"user_id" => user.id})
+
+ 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
+ {: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"})
+
+ {:ok, object} = Object.context_mapping("2hu") |> Repo.insert()
+
+ conn =
+ conn
+ |> get("/api/statusnet/conversation/#{object.id}.json")
+
+ response = json_response(conn, 200)
+
+ assert length(response) == 2
+ end
+ end
+
+ describe "GET /statuses/friends_timeline.json" do
+ setup [:valid_user]
+
+ test "without valid credentials", %{conn: conn} do
+ conn = get(conn, "/api/statuses/friends_timeline.json")
+ assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+ end
+
+ test "with credentials", %{conn: conn, user: current_user} do
+ user = insert(:user)
+
+ 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})
+
+ other_user = insert(:user)
+ ActivityBuilder.insert_list(10, %{}, %{user: other_user})
+ since_id = List.last(activities).id
+
+ current_user =
+ Ecto.Changeset.change(current_user, following: [User.ap_followers(user)])
+ |> Repo.update!()
+
+ conn =
+ conn
+ |> 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
+
+ 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]