From caedaff0e2322481fb7e22773b05d7e36ecdc0ce Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Wed, 18 Jun 2025 16:18:16 +0200 Subject: [PATCH] Add actor_controller_test.exs --- .../controllers/actor_controller_test.exs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/nulla_web/controllers/actor_controller_test.exs diff --git a/test/nulla_web/controllers/actor_controller_test.exs b/test/nulla_web/controllers/actor_controller_test.exs new file mode 100644 index 0000000..01cc610 --- /dev/null +++ b/test/nulla_web/controllers/actor_controller_test.exs @@ -0,0 +1,88 @@ +defmodule NullaWeb.ActorControllerTest do + use NullaWeb.ConnCase + alias Nulla.Models.Actor + + setup do + Actor.create_actor(%{ + domain: "localhost", + ap_id: "http://localhost/users/test", + type: "Person", + following: "http://localhost/users/test/following", + followers: "http://localhost/users/test/followers", + inbox: "http://localhost/users/test/inbox", + outbox: "http://localhost/users/test/outbox", + featured: "http://localhost/users/test/collections/featured", + featuredTags: "http://localhost/users/test/collections/tags", + preferredUsername: "test", + name: "Test", + summary: "Test User", + url: "http://localhost/@test", + manuallyApprovesFollowers: false, + discoverable: true, + indexable: true, + published: DateTime.utc_now(), + memorial: false, + publicKey: + Jason.OrderedObject.new( + id: "http://localhost/users/test#main-key", + owner: "http://localhost/users/test", + publicKeyPem: "PUBLIC KEY" + ), + endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox") + }) + + :ok + end + + describe "GET /@username" do + test "renders ActivityPub JSON if Accept header is activity+json", %{conn: conn} do + conn = + conn + |> put_req_header("accept", "application/activity+json") + |> get(~p"/@test") + + assert response = json_response(conn, 200) + + assert is_list(response["@context"]) + assert is_binary(response["id"]) + assert is_binary(response["type"]) + assert is_binary(response["following"]) + assert is_binary(response["followers"]) + assert is_binary(response["inbox"]) + assert is_binary(response["outbox"]) + assert is_binary(response["featured"]) + assert is_binary(response["featuredTags"]) + assert is_binary(response["preferredUsername"]) + assert is_binary(response["name"]) or is_nil(response["name"]) + assert is_binary(response["summary"]) or is_nil(response["summary"]) + assert is_binary(response["url"]) + assert is_boolean(response["manuallyApprovesFollowers"]) + assert is_boolean(response["discoverable"]) + assert is_boolean(response["indexable"]) + assert {:ok, _dt, _offset} = DateTime.from_iso8601(response["published"]) + assert is_boolean(response["memorial"]) + assert is_map(response["publicKey"]) + assert is_list(response["tag"]) + assert is_list(response["attachment"]) + assert is_map(response["endpoints"]) + assert is_map(response["icon"]) + assert is_map(response["image"]) + assert is_binary(response["vcard:bday"]) or is_nil(response["vcard:bday"]) + assert is_binary(response["vcard:Address"]) or is_nil(response["vcard:Address"]) + end + + test "renders HTML if Accept header is regular", %{conn: conn} do + conn = get(conn, ~p"/@test") + + assert html_response(conn, 200) =~ "test" + assert html_response(conn, 200) =~ "Test" + assert html_response(conn, 200) =~ "test@localhost" + end + + test "returns 404 if actor not found", %{conn: conn} do + conn = get(conn, ~p"/@nonexistent") + + assert json_response(conn, 404)["error"] == "Not Found" + end + end +end