Add tests

This commit is contained in:
Mirai Kumiko 2025-07-05 21:35:34 +02:00
parent b03b2001ba
commit b17e177335
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
3 changed files with 65 additions and 1 deletions

View file

@ -0,0 +1,41 @@
defmodule Nulla.HTTPSignatureTest do
use NullaWeb.ConnCase, async: false
import Plug.Conn
import Plug.Test
import Nulla.ActorsFixtures
alias Nulla.HTTPSignature
alias Nulla.Snowflake
test "make_headers/3 creates valid signature headers and verify/2 validates them" do
actor = actor_fixture()
target_actor = actor_fixture()
follow_activity = %{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => Snowflake.next_id(),
"type" => "Follow",
"actor" => actor.ap_id,
"object" => target_actor.ap_id
}
body = Jason.encode!(follow_activity)
headers =
HTTPSignature.make_headers(
body,
target_actor.inbox,
actor.publicKey["id"],
actor.privateKeyPem
)
conn =
conn(:post, "/users/#{target_actor.preferredUsername}/inbox", body)
|> put_req_header("content-type", Map.new(headers) |> Map.get("Content-Type"))
|> put_req_header("date", Map.new(headers) |> Map.get("Date"))
|> put_req_header("digest", Map.new(headers) |> Map.get("Digest"))
|> put_req_header("signature", Map.new(headers) |> Map.get("Signature"))
|> Map.put(:host, URI.parse(target_actor.inbox).host)
assert :ok = HTTPSignature.verify(conn, actor.publicKey["publicKeyPem"])
end
end

23
test/nulla/keys.exs Normal file
View file

@ -0,0 +1,23 @@
defmodule Nulla.KeysTest do
use NullaWeb.ConnCase, async: false
alias Nulla.KeyGen
test "test KeyGen module" do
message = "test message"
{publicKeyPem, privateKeyPem} = KeyGen.gen()
private_key =
:public_key.pem_decode(privateKeyPem)
|> hd()
|> :public_key.pem_entry_decode()
public_key =
:public_key.pem_decode(publicKeyPem)
|> hd()
|> :public_key.pem_entry_decode()
signature = :public_key.sign(message, :sha256, private_key)
assert :public_key.verify(message, :sha256, signature, public_key)
end
end

View file

@ -26,7 +26,7 @@ defmodule Nulla.ActorsFixtures do
following: "some following", following: "some following",
icon: %{}, icon: %{},
image: %{}, image: %{},
inbox: "some inbox", inbox: "http://localhost/users/#{username}/inbox",
indexable: true, indexable: true,
manuallyApprovesFollowers: true, manuallyApprovesFollowers: true,
memorial: true, memorial: true,