Update tests

This commit is contained in:
Mirai Kumiko 2025-06-28 18:56:37 +02:00
parent 406cd1798c
commit c30541830f
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
10 changed files with 134 additions and 284 deletions

View file

@ -36,13 +36,27 @@ defmodule Nulla.Models.Note do
def create_note(attrs) when is_map(attrs) do
id = Map.get(attrs, :id, Snowflake.next_id())
url =
case Map.get(attrs, :url) do
nil ->
actor_url = Map.get(attrs, :actor_url)
"#{actor_url}/#{id}"
_ ->
Map.get(attrs, :url)
end
%__MODULE__{}
|> changeset(attrs)
|> put_change(:id, id)
|> put_change(:url, url)
|> Repo.insert()
end
def get_note(id), do: Repo.get(__MODULE__, id)
def get_note(by) when is_map(by) or is_list(by) do
Repo.get_by(__MODULE__, by)
end
def get_latest_notes(actor_id, limit \\ 20) do
from(n in __MODULE__,

View file

@ -6,7 +6,7 @@ defmodule NullaWeb.ActorController do
alias Nulla.Models.InstanceSettings
def show(conn, %{"username" => username}) do
accept = List.first(get_req_header(conn, "accept"))
format = Phoenix.Controller.get_format(conn)
instance_settings = InstanceSettings.get_instance_settings!()
domain = instance_settings.domain
@ -17,7 +17,7 @@ defmodule NullaWeb.ActorController do
|> json(%{error: "Not Found"})
%Actor{} = actor ->
if accept in ["application/activity+json"] do
if format == "activity+json" do
conn
|> put_resp_content_type("application/activity+json")
|> send_resp(200, Jason.encode!(ActivityPub.actor(actor)))

View file

@ -7,7 +7,7 @@ defmodule NullaWeb.NoteController do
def show(conn, %{"username" => username, "id" => id}) do
case Integer.parse(id) do
{int_id, ""} ->
note = Note.get_note(int_id) |> Repo.preload([:actor, :media_attachments])
note = Note.get_note(id: int_id) |> Repo.preload([:actor, :media_attachments])
cond do
is_nil(note) ->
@ -23,9 +23,9 @@ defmodule NullaWeb.NoteController do
|> halt()
true ->
accept = List.first(get_req_header(conn, "accept"))
format = Phoenix.Controller.get_format(conn)
if accept in ["application/activity+json", "application/ld+json"] do
if format == "activity+json" do
conn
|> put_resp_content_type("application/activity+json")
|> json(ActivityPub.note(note))

View file

@ -1,36 +1,8 @@
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")
})
Nulla.Fixtures.Data.create()
:ok
end
@ -72,7 +44,7 @@ defmodule NullaWeb.ActorControllerTest do
end
test "renders HTML if Accept header is regular", %{conn: conn} do
conn = get(conn, ~p"/users/test")
conn = get(conn, ~p"/@test")
assert html_response(conn, 200) =~ "test"
assert html_response(conn, 200) =~ "Test"
@ -80,7 +52,7 @@ defmodule NullaWeb.ActorControllerTest do
end
test "returns 404 if actor not found", %{conn: conn} do
conn = get(conn, ~p"/users/nonexistent")
conn = get(conn, ~p"/@nonexistent")
assert json_response(conn, 404)["error"] == "Not Found"
end

View file

@ -1,49 +1,8 @@
defmodule NullaWeb.FollowControllerTest do
use NullaWeb.ConnCase
alias Nulla.KeyGen
alias Nulla.Models.User
alias Nulla.Models.Actor
setup do
{publicKeyPem, privateKeyPem} = KeyGen.gen()
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
User.create_user(%{
id: actor.id,
email: "test@localhost",
password: "password",
privateKeyPem: privateKeyPem,
last_active_at: DateTime.utc_now()
})
Nulla.Fixtures.Data.create()
:ok
end

View file

@ -1,49 +1,8 @@
defmodule NullaWeb.NodeinfoControllerTest do
use NullaWeb.ConnCase
alias Nulla.KeyGen
alias Nulla.Models.User
alias Nulla.Models.Actor
setup do
{publicKeyPem, privateKeyPem} = KeyGen.gen()
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
User.create_user(%{
id: actor.id,
email: "test@localhost",
password: "password",
privateKeyPem: privateKeyPem,
last_active_at: DateTime.utc_now()
})
Nulla.Fixtures.Data.create()
:ok
end
@ -83,9 +42,9 @@ defmodule NullaWeb.NodeinfoControllerTest do
assert is_list(response["services"]["inbound"])
assert is_map(response["usage"])
assert is_map(response["usage"]["users"])
assert response["usage"]["users"]["total"] == 1
assert response["usage"]["users"]["activeMonth"] == 1
assert response["usage"]["users"]["activeHalfyear"] == 1
assert response["usage"]["users"]["total"] > 0
assert response["usage"]["users"]["activeMonth"] > 0
assert response["usage"]["users"]["activeHalfyear"] > 0
assert is_boolean(response["openRegistrations"])
assert is_map(response["metadata"])
assert is_binary(response["metadata"]["nodeName"])

View file

@ -1,53 +1,17 @@
defmodule NullaWeb.NoteControllerTest do
use NullaWeb.ConnCase
alias Nulla.KeyGen
alias Nulla.Snowflake
alias Nulla.Models.Actor
alias Nulla.Models.Note
setup do
Nulla.Fixtures.Data.create()
:ok
end
describe "GET /notes/id" do
test "returns ActivityPub JSON with note", %{conn: conn} do
{publicKeyPem, _privateKeyPem} = KeyGen.gen()
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
note_id = Snowflake.next_id()
{:ok, note} =
Note.create_note(%{
id: note_id,
url: "#{actor.url}/#{note_id}",
content: "Hello World from Nulla!",
language: "en",
actor_id: actor.id
})
actor = Actor.get_actor(preferredUsername: "test")
note = Note.get_note(actor_id: actor.id)
conn =
conn
@ -73,55 +37,16 @@ defmodule NullaWeb.NoteControllerTest do
end
test "renders HTML if Accept header is regular", %{conn: conn} do
{publicKeyPem, _privateKeyPem} = KeyGen.gen()
actor = Actor.get_actor(preferredUsername: "test")
note = Note.get_note(actor_id: actor.id)
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
note_id = Snowflake.next_id()
{:ok, note} =
Note.create_note(%{
id: note_id,
url: "#{actor.url}/#{note_id}",
content: "Hello World from Nulla!",
language: "en",
actor_id: actor.id
})
conn = get(conn, ~p"/users/test/notes/#{note.id}")
conn = get(conn, ~p"/@test/#{note.id}")
assert html_response(conn, 200) =~ note.content
end
test "returns 404 if note not found", %{conn: conn} do
conn = get(conn, ~p"/users/test/notes/nonexistent")
conn = get(conn, ~p"/@test/nonexistent")
assert json_response(conn, 404)["error"] == "Not Found"
end

View file

@ -1,52 +1,8 @@
defmodule NullaWeb.OutboxControllerTest do
use NullaWeb.ConnCase
alias Nulla.KeyGen
alias Nulla.Snowflake
alias Nulla.Models.Actor
alias Nulla.Models.Note
setup do
{publicKeyPem, _privateKeyPem} = KeyGen.gen()
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
note_id = Snowflake.next_id()
{:ok, _note} =
Note.create_note(%{
url: "#{actor.url}/#{note_id}",
content: "Hello World from Nulla!",
language: "en",
actor_id: actor.id
})
Nulla.Fixtures.Data.create()
:ok
end

View file

@ -1,36 +1,8 @@
defmodule NullaWeb.WebfingerControllerTest 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")
})
Nulla.Fixtures.Data.create()
:ok
end

View file

@ -0,0 +1,93 @@
defmodule Nulla.Fixtures.Data do
alias Nulla.KeyGen
alias Nulla.Models.User
alias Nulla.Models.Actor
alias Nulla.Models.Note
def create do
{publicKeyPem, privateKeyPem} = KeyGen.gen()
{:ok, actor} =
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: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
User.create_user(%{
id: actor.id,
email: "test@localhost",
password: "password",
privateKeyPem: privateKeyPem,
last_active_at: DateTime.utc_now()
})
Note.create_note(%{
actor_url: actor.url,
content: "Hello World from Nulla!",
language: "en",
actor_id: actor.id
})
{publicKeyPem, privateKeyPem} = KeyGen.gen()
{:ok, actor} =
Actor.create_actor(%{
domain: "localhost",
ap_id: "http://localhost/users/test2",
type: "Person",
following: "http://localhost/users/test2/following",
followers: "http://localhost/users/test2/followers",
inbox: "http://localhost/users/test2/inbox",
outbox: "http://localhost/users/test2/outbox",
featured: "http://localhost/users/test2/collections/featured",
featuredTags: "http://localhost/users/test2/collections/tags",
preferredUsername: "test2",
name: "Test",
summary: "Test User",
url: "http://localhost/@test2",
manuallyApprovesFollowers: false,
discoverable: true,
indexable: true,
published: DateTime.utc_now(),
memorial: false,
publicKey:
Jason.OrderedObject.new(
id: "http://localhost/users/test2#main-key",
owner: "http://localhost/users/test2",
publicKeyPem: publicKeyPem
),
endpoints: Jason.OrderedObject.new(sharedInbox: "http://localhost/inbox")
})
User.create_user(%{
id: actor.id,
email: "test2@localhost",
password: "password",
privateKeyPem: privateKeyPem,
last_active_at: DateTime.utc_now()
})
end
end