diff options
Diffstat (limited to 'test/web/mastodon_api/controllers')
5 files changed, 90 insertions, 58 deletions
diff --git a/test/web/mastodon_api/controllers/conversation_controller_test.exs b/test/web/mastodon_api/controllers/conversation_controller_test.exs index 801b0259b..04695572e 100644 --- a/test/web/mastodon_api/controllers/conversation_controller_test.exs +++ b/test/web/mastodon_api/controllers/conversation_controller_test.exs @@ -36,7 +36,7 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do      res_conn = get(conn, "/api/v1/conversations") -    assert response = json_response(res_conn, 200) +    assert response = json_response_and_validate_schema(res_conn, 200)      assert [               %{ @@ -91,18 +91,18 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do          "visibility" => "direct"        }) -    [conversation1, conversation2] = -      conn -      |> get("/api/v1/conversations", %{"recipients" => [user_two.id]}) -      |> json_response(200) +    assert [conversation1, conversation2] = +             conn +             |> get("/api/v1/conversations?recipients[]=#{user_two.id}") +             |> json_response_and_validate_schema(200)      assert conversation1["last_status"]["id"] == direct5.id      assert conversation2["last_status"]["id"] == direct1.id      [conversation1] =        conn -      |> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]}) -      |> json_response(200) +      |> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}") +      |> json_response_and_validate_schema(200)      assert conversation1["last_status"]["id"] == direct3.id    end @@ -126,7 +126,7 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do      [%{"last_status" => res_last_status}] =        conn        |> get("/api/v1/conversations") -      |> json_response(200) +      |> json_response_and_validate_schema(200)      assert res_last_status["id"] == direct_reply.id    end @@ -154,12 +154,12 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do      [%{"id" => direct_conversation_id, "unread" => true}] =        user_two_conn        |> get("/api/v1/conversations") -      |> json_response(200) +      |> json_response_and_validate_schema(200)      %{"unread" => false} =        user_two_conn        |> post("/api/v1/conversations/#{direct_conversation_id}/read") -      |> json_response(200) +      |> json_response_and_validate_schema(200)      assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0      assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0 @@ -175,7 +175,7 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do      [%{"unread" => true}] =        conn        |> get("/api/v1/conversations") -      |> json_response(200) +      |> json_response_and_validate_schema(200)      assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1      assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0 diff --git a/test/web/mastodon_api/controllers/filter_controller_test.exs b/test/web/mastodon_api/controllers/filter_controller_test.exs index 97ab005e0..f29547d13 100644 --- a/test/web/mastodon_api/controllers/filter_controller_test.exs +++ b/test/web/mastodon_api/controllers/filter_controller_test.exs @@ -15,9 +15,12 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do        context: ["home"]      } -    conn = post(conn, "/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context}) +    conn = +      conn +      |> put_req_header("content-type", "application/json") +      |> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context}) -    assert response = json_response(conn, 200) +    assert response = json_response_and_validate_schema(conn, 200)      assert response["phrase"] == filter.phrase      assert response["context"] == filter.context      assert response["irreversible"] == false @@ -48,12 +51,12 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do      response =        conn        |> get("/api/v1/filters") -      |> json_response(200) +      |> json_response_and_validate_schema(200)      assert response ==               render_json(                 FilterView, -               "filters.json", +               "index.json",                 filters: [filter_two, filter_one]               )    end @@ -72,7 +75,7 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do      conn = get(conn, "/api/v1/filters/#{filter.filter_id}") -    assert _response = json_response(conn, 200) +    assert response = json_response_and_validate_schema(conn, 200)    end    test "update a filter" do @@ -82,7 +85,8 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do        user_id: user.id,        filter_id: 2,        phrase: "knight", -      context: ["home"] +      context: ["home"], +      hide: true      }      {:ok, _filter} = Pleroma.Filter.create(query) @@ -93,14 +97,17 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do      }      conn = -      put(conn, "/api/v1/filters/#{query.filter_id}", %{ +      conn +      |> put_req_header("content-type", "application/json") +      |> put("/api/v1/filters/#{query.filter_id}", %{          phrase: new.phrase,          context: new.context        }) -    assert response = json_response(conn, 200) +    assert response = json_response_and_validate_schema(conn, 200)      assert response["phrase"] == new.phrase      assert response["context"] == new.context +    assert response["irreversible"] == true    end    test "delete a filter" do @@ -117,7 +124,6 @@ defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do      conn = delete(conn, "/api/v1/filters/#{filter.filter_id}") -    assert response = json_response(conn, 200) -    assert response == %{} +    assert json_response_and_validate_schema(conn, 200) == %{}    end  end diff --git a/test/web/mastodon_api/controllers/instance_controller_test.exs b/test/web/mastodon_api/controllers/instance_controller_test.exs index 2c7fd9fd0..90840d5ab 100644 --- a/test/web/mastodon_api/controllers/instance_controller_test.exs +++ b/test/web/mastodon_api/controllers/instance_controller_test.exs @@ -10,7 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do    test "get instance information", %{conn: conn} do      conn = get(conn, "/api/v1/instance") -    assert result = json_response(conn, 200) +    assert result = json_response_and_validate_schema(conn, 200)      email = Pleroma.Config.get([:instance, :email])      # Note: not checking for "max_toot_chars" since it's optional @@ -56,7 +56,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do      conn = get(conn, "/api/v1/instance") -    assert result = json_response(conn, 200) +    assert result = json_response_and_validate_schema(conn, 200)      stats = result["stats"] @@ -74,7 +74,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do      conn = get(conn, "/api/v1/instance/peers") -    assert result = json_response(conn, 200) +    assert result = json_response_and_validate_schema(conn, 200)      assert ["peer1.com", "peer2.com"] == Enum.sort(result)    end diff --git a/test/web/mastodon_api/controllers/list_controller_test.exs b/test/web/mastodon_api/controllers/list_controller_test.exs index c9c4cbb49..57a9ef4a4 100644 --- a/test/web/mastodon_api/controllers/list_controller_test.exs +++ b/test/web/mastodon_api/controllers/list_controller_test.exs @@ -12,37 +12,44 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do    test "creating a list" do      %{conn: conn} = oauth_access(["write:lists"]) -    conn = post(conn, "/api/v1/lists", %{"title" => "cuties"}) - -    assert %{"title" => title} = json_response(conn, 200) -    assert title == "cuties" +    assert %{"title" => "cuties"} = +             conn +             |> put_req_header("content-type", "application/json") +             |> post("/api/v1/lists", %{"title" => "cuties"}) +             |> json_response_and_validate_schema(:ok)    end    test "renders error for invalid params" do      %{conn: conn} = oauth_access(["write:lists"]) -    conn = post(conn, "/api/v1/lists", %{"title" => nil}) +    conn = +      conn +      |> put_req_header("content-type", "application/json") +      |> post("/api/v1/lists", %{"title" => nil}) -    assert %{"error" => "can't be blank"} == json_response(conn, :unprocessable_entity) +    assert %{"error" => "title - null value where string expected."} = +             json_response_and_validate_schema(conn, 400)    end    test "listing a user's lists" do      %{conn: conn} = oauth_access(["read:lists", "write:lists"])      conn +    |> put_req_header("content-type", "application/json")      |> post("/api/v1/lists", %{"title" => "cuties"}) -    |> json_response(:ok) +    |> json_response_and_validate_schema(:ok)      conn +    |> put_req_header("content-type", "application/json")      |> post("/api/v1/lists", %{"title" => "cofe"}) -    |> json_response(:ok) +    |> json_response_and_validate_schema(:ok)      conn = get(conn, "/api/v1/lists")      assert [               %{"id" => _, "title" => "cofe"},               %{"id" => _, "title" => "cuties"} -           ] = json_response(conn, :ok) +           ] = json_response_and_validate_schema(conn, :ok)    end    test "adding users to a list" do @@ -50,9 +57,12 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do      other_user = insert(:user)      {:ok, list} = Pleroma.List.create("name", user) -    conn = post(conn, "/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) +    assert %{} == +             conn +             |> put_req_header("content-type", "application/json") +             |> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) +             |> json_response_and_validate_schema(:ok) -    assert %{} == json_response(conn, 200)      %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)      assert following == [other_user.follower_address]    end @@ -65,9 +75,12 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do      {:ok, list} = Pleroma.List.follow(list, other_user)      {:ok, list} = Pleroma.List.follow(list, third_user) -    conn = delete(conn, "/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) +    assert %{} == +             conn +             |> put_req_header("content-type", "application/json") +             |> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) +             |> json_response_and_validate_schema(:ok) -    assert %{} == json_response(conn, 200)      %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)      assert following == [third_user.follower_address]    end @@ -83,7 +96,7 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do        |> assign(:user, user)        |> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) -    assert [%{"id" => id}] = json_response(conn, 200) +    assert [%{"id" => id}] = json_response_and_validate_schema(conn, 200)      assert id == to_string(other_user.id)    end @@ -96,7 +109,7 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do        |> assign(:user, user)        |> get("/api/v1/lists/#{list.id}") -    assert %{"id" => id} = json_response(conn, 200) +    assert %{"id" => id} = json_response_and_validate_schema(conn, 200)      assert id == to_string(list.id)    end @@ -105,17 +118,18 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do      conn = get(conn, "/api/v1/lists/666") -    assert %{"error" => "List not found"} = json_response(conn, :not_found) +    assert %{"error" => "List not found"} = json_response_and_validate_schema(conn, :not_found)    end    test "renaming a list" do      %{user: user, conn: conn} = oauth_access(["write:lists"])      {:ok, list} = Pleroma.List.create("name", user) -    conn = put(conn, "/api/v1/lists/#{list.id}", %{"title" => "newname"}) - -    assert %{"title" => name} = json_response(conn, 200) -    assert name == "newname" +    assert %{"title" => "newname"} = +             conn +             |> put_req_header("content-type", "application/json") +             |> put("/api/v1/lists/#{list.id}", %{"title" => "newname"}) +             |> json_response_and_validate_schema(:ok)    end    test "validates title when renaming a list" do @@ -125,9 +139,11 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do      conn =        conn        |> assign(:user, user) +      |> put_req_header("content-type", "application/json")        |> put("/api/v1/lists/#{list.id}", %{"title" => "  "}) -    assert %{"error" => "can't be blank"} == json_response(conn, :unprocessable_entity) +    assert %{"error" => "can't be blank"} == +             json_response_and_validate_schema(conn, :unprocessable_entity)    end    test "deleting a list" do @@ -136,7 +152,7 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do      conn = delete(conn, "/api/v1/lists/#{list.id}") -    assert %{} = json_response(conn, 200) +    assert %{} = json_response_and_validate_schema(conn, 200)      assert is_nil(Repo.get(Pleroma.List, list.id))    end  end diff --git a/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs b/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs index f86274d57..1ff871c89 100644 --- a/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs +++ b/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs @@ -24,19 +24,19 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do      # min_id      conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&min_id=#{scheduled_activity_id1}") -    result = json_response(conn_res, 200) +    result = json_response_and_validate_schema(conn_res, 200)      assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result      # since_id      conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&since_id=#{scheduled_activity_id1}") -    result = json_response(conn_res, 200) +    result = json_response_and_validate_schema(conn_res, 200)      assert [%{"id" => ^scheduled_activity_id4}, %{"id" => ^scheduled_activity_id3}] = result      # max_id      conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&max_id=#{scheduled_activity_id4}") -    result = json_response(conn_res, 200) +    result = json_response_and_validate_schema(conn_res, 200)      assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result    end @@ -46,12 +46,12 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do      res_conn = get(conn, "/api/v1/scheduled_statuses/#{scheduled_activity.id}") -    assert %{"id" => scheduled_activity_id} = json_response(res_conn, 200) +    assert %{"id" => scheduled_activity_id} = json_response_and_validate_schema(res_conn, 200)      assert scheduled_activity_id == scheduled_activity.id |> to_string()      res_conn = get(conn, "/api/v1/scheduled_statuses/404") -    assert %{"error" => "Record not found"} = json_response(res_conn, 404) +    assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)    end    test "updates a scheduled activity" do @@ -74,22 +74,32 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do      assert job.args == %{"activity_id" => scheduled_activity.id}      assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(scheduled_at) -    new_scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 120) +    new_scheduled_at = +      NaiveDateTime.utc_now() +      |> Timex.shift(minutes: 120) +      |> Timex.format!("%Y-%m-%dT%H:%M:%S.%fZ", :strftime)      res_conn = -      put(conn, "/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{ +      conn +      |> put_req_header("content-type", "application/json") +      |> put("/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{          scheduled_at: new_scheduled_at        }) -    assert %{"scheduled_at" => expected_scheduled_at} = json_response(res_conn, 200) +    assert %{"scheduled_at" => expected_scheduled_at} = +             json_response_and_validate_schema(res_conn, 200) +      assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at)      job = refresh_record(job)      assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(new_scheduled_at) -    res_conn = put(conn, "/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at}) +    res_conn = +      conn +      |> put_req_header("content-type", "application/json") +      |> put("/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at}) -    assert %{"error" => "Record not found"} = json_response(res_conn, 404) +    assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)    end    test "deletes a scheduled activity" do @@ -115,7 +125,7 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do        |> assign(:user, user)        |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}") -    assert %{} = json_response(res_conn, 200) +    assert %{} = json_response_and_validate_schema(res_conn, 200)      refute Repo.get(ScheduledActivity, scheduled_activity.id)      refute Repo.get(Oban.Job, job.id) @@ -124,6 +134,6 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do        |> assign(:user, user)        |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}") -    assert %{"error" => "Record not found"} = json_response(res_conn, 404) +    assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)    end  end  | 
