Merge branch 'develop' into dtluna/pleroma-feature/unfollow-activity
[akkoma] / test / web / twitter_api / twitter_api_controller_test.exs
index 6f1788a5884de5fad060f0545db913ecca97fecc..f8afbaee52b3d4a6995c8aeebb06bb0c6c6e76e9 100644 (file)
@@ -31,10 +31,21 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
 
     test "with credentials", %{conn: conn, user: user} do
-      conn = conn
-        |> with_credentials(user.nickname, "test")
-        |> post("/api/statuses/update.json", %{ status: "Nice meme." })
+      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
@@ -73,12 +84,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   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(%{"statusnetConversationId" => 1, "context" => "2hu"})
-      {:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
+      {:ok, _activity} = ActivityBuilder.insert(%{"context" => "2hu"})
+      {:ok, _activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
       {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
 
+      {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
       conn = conn
-      |> get("/api/statusnet/conversation/1.json")
+      |> get("/api/statusnet/conversation/#{object.id}.json")
 
       response = json_response(conn, 200)
 
@@ -114,11 +126,32 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     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"}
+      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
@@ -212,6 +245,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
 
       {:ok, current_user} = User.follow(current_user, followed)
       assert current_user.following == [User.ap_followers(followed)]
+      ActivityPub.follow(current_user, followed)
 
       conn = conn
       |> with_credentials(current_user.nickname, "test")
@@ -223,6 +257,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     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
@@ -292,11 +333,21 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     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")
+      request_path = "/api/statuses/retweet/#{note_activity.id}.json"
 
-      assert json_response(conn, 200)
+      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
 
@@ -339,7 +390,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   end
 
   defp valid_user(_context) do
-    { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
+    user = insert(:user)
     [user: user]
   end
 
@@ -347,10 +398,4 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     header_content = "Basic " <> Base.encode64("#{username}:#{password}")
     put_req_header(conn, "authorization", header_content)
   end
-
-  setup do
-    Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
-    Supervisor.restart_child(Pleroma.Supervisor, ConCache)
-    :ok
-  end
 end