From 4c67023c7ed8cde1060454d81261d06644bcc9ce Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 1 Nov 2024 21:02:52 +0100 Subject: [PATCH] tests: improve actvititypub integration test code - Make use of `test.MockVariableValue` to override variables for the duration of the test. - Don't needlessly call `onGiteaRun`, its only needed when a HTTP server needs to be called by the code. - When `onGiteaRun` is used, make use of the passed parameters, such as the passed `*testing.T` variable and `*url.URL` (this also avoids needing to serve the routers in the test code again). - Use `(*url.URL).JoinPath` to craft new URLs. - Don't override `setting.AppURL` & `setting.Database.LogSQL` when its does not affect the test. - Add empty fixture files for `FederatedUser` & `FederationHost` so they are truncated and do not persist between tests. --- models/activities/main_test.go | 1 + models/auth/main_test.go | 1 + models/fixtures/federated_user.yml | 1 + models/fixtures/federation_host.yml | 1 + models/forgejo/semver/main_test.go | 1 + models/git/main_test.go | 1 + models/main_test.go | 1 + models/organization/main_test.go | 1 + models/packages/debian/search_test.go | 1 + models/packages/package_test.go | 1 + models/perm/access/main_test.go | 1 + models/repo/main_test.go | 1 + models/system/main_test.go | 1 + modules/activitypub/main_test.go | 1 + modules/indexer/code/indexer_test.go | 1 + modules/indexer/issues/indexer_test.go | 1 + modules/indexer/stats/indexer_test.go | 1 + modules/repository/main_test.go | 1 + modules/templates/main_test.go | 1 + services/actions/main_test.go | 1 + services/asymkey/main_test.go | 1 + services/attachment/attachment_test.go | 1 + services/convert/main_test.go | 1 + services/feed/action_test.go | 1 + services/forgejo/main_test.go | 1 + services/gitdiff/main_test.go | 1 + services/pull/main_test.go | 1 + services/release/release_test.go | 1 + services/repository/archiver/archiver_test.go | 1 + services/repository/files/content_test.go | 1 + services/webhook/main_test.go | 1 + .../integration/api_activitypub_actor_test.go | 44 +++-- .../api_activitypub_person_test.go | 106 +++++------ .../api_activitypub_repository_test.go | 167 +++++++----------- 34 files changed, 154 insertions(+), 194 deletions(-) create mode 100644 models/fixtures/federated_user.yml create mode 100644 models/fixtures/federation_host.yml diff --git a/models/activities/main_test.go b/models/activities/main_test.go index 43afb84ef1..bda5593d02 100644 --- a/models/activities/main_test.go +++ b/models/activities/main_test.go @@ -10,6 +10,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/models/auth/main_test.go b/models/auth/main_test.go index d772ea6b1c..7b374b38da 100644 --- a/models/auth/main_test.go +++ b/models/auth/main_test.go @@ -12,6 +12,7 @@ import ( _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" _ "code.gitea.io/gitea/models/auth" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/perm/access" ) diff --git a/models/fixtures/federated_user.yml b/models/fixtures/federated_user.yml new file mode 100644 index 0000000000..ca780a73aa --- /dev/null +++ b/models/fixtures/federated_user.yml @@ -0,0 +1 @@ +[] # empty diff --git a/models/fixtures/federation_host.yml b/models/fixtures/federation_host.yml new file mode 100644 index 0000000000..ca780a73aa --- /dev/null +++ b/models/fixtures/federation_host.yml @@ -0,0 +1 @@ +[] # empty diff --git a/models/forgejo/semver/main_test.go b/models/forgejo/semver/main_test.go index fa56182627..10875f14fe 100644 --- a/models/forgejo/semver/main_test.go +++ b/models/forgejo/semver/main_test.go @@ -10,6 +10,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/models/git/main_test.go b/models/git/main_test.go index aab1fa9a26..f25ad42584 100644 --- a/models/git/main_test.go +++ b/models/git/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/models/main_test.go b/models/main_test.go index a694130e53..f9e019f5e2 100644 --- a/models/main_test.go +++ b/models/main_test.go @@ -13,6 +13,7 @@ import ( user_model "code.gitea.io/gitea/models/user" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/system" "github.com/stretchr/testify/require" diff --git a/models/organization/main_test.go b/models/organization/main_test.go index c35898a465..96eeadafa9 100644 --- a/models/organization/main_test.go +++ b/models/organization/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/organization" _ "code.gitea.io/gitea/models/repo" _ "code.gitea.io/gitea/models/user" diff --git a/models/packages/debian/search_test.go b/models/packages/debian/search_test.go index 104a01498b..949094d8c1 100644 --- a/models/packages/debian/search_test.go +++ b/models/packages/debian/search_test.go @@ -19,6 +19,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/models/packages/package_test.go b/models/packages/package_test.go index 1c96e08f0c..5b9b9b7403 100644 --- a/models/packages/package_test.go +++ b/models/packages/package_test.go @@ -16,6 +16,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/require" ) diff --git a/models/perm/access/main_test.go b/models/perm/access/main_test.go index 0a350dc41e..da6f03715f 100644 --- a/models/perm/access/main_test.go +++ b/models/perm/access/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/repo" _ "code.gitea.io/gitea/models/user" ) diff --git a/models/repo/main_test.go b/models/repo/main_test.go index b49855f2c8..a9fdcc9000 100644 --- a/models/repo/main_test.go +++ b/models/repo/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" // register table model _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/perm/access" // register table model _ "code.gitea.io/gitea/models/repo" // register table model _ "code.gitea.io/gitea/models/user" // register table model diff --git a/models/system/main_test.go b/models/system/main_test.go index 6bc27a7cff..4181d44ddf 100644 --- a/models/system/main_test.go +++ b/models/system/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" // register models _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/system" // register models of system ) diff --git a/modules/activitypub/main_test.go b/modules/activitypub/main_test.go index 4591f1fa55..30216812cf 100644 --- a/modules/activitypub/main_test.go +++ b/modules/activitypub/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go index 967aad1b54..d474eba174 100644 --- a/modules/indexer/code/indexer_test.go +++ b/modules/indexer/code/indexer_test.go @@ -18,6 +18,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go index a010218b72..b96fecc6bc 100644 --- a/modules/indexer/issues/indexer_test.go +++ b/modules/indexer/issues/indexer_test.go @@ -17,6 +17,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index 3ab2e58546..b7e66f937c 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -17,6 +17,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/modules/repository/main_test.go b/modules/repository/main_test.go index f81dfcdafb..7b245cf118 100644 --- a/modules/repository/main_test.go +++ b/modules/repository/main_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/unittest" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/modules/templates/main_test.go b/modules/templates/main_test.go index bbdf5d2f99..977699f9c7 100644 --- a/modules/templates/main_test.go +++ b/modules/templates/main_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/markup" _ "code.gitea.io/gitea/models" + _ "code.gitea.io/gitea/models/forgefed" _ "code.gitea.io/gitea/models/issues" ) diff --git a/services/actions/main_test.go b/services/actions/main_test.go index ea37ff507a..49629ecb03 100644 --- a/services/actions/main_test.go +++ b/services/actions/main_test.go @@ -10,6 +10,7 @@ import ( _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/asymkey/main_test.go b/services/asymkey/main_test.go index 3505b26f69..060cc78cec 100644 --- a/services/asymkey/main_test.go +++ b/services/asymkey/main_test.go @@ -10,6 +10,7 @@ import ( _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index fe861c6dc8..c24b3f8006 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -14,6 +14,7 @@ import ( user_model "code.gitea.io/gitea/models/user" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/convert/main_test.go b/services/convert/main_test.go index 363cc4a97f..b28b8f9446 100644 --- a/services/convert/main_test.go +++ b/services/convert/main_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/unittest" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/feed/action_test.go b/services/feed/action_test.go index 404d89c7b8..87bb13330a 100644 --- a/services/feed/action_test.go +++ b/services/feed/action_test.go @@ -14,6 +14,7 @@ import ( user_model "code.gitea.io/gitea/models/user" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/require" ) diff --git a/services/forgejo/main_test.go b/services/forgejo/main_test.go index e88b7d0263..40ce1715b1 100644 --- a/services/forgejo/main_test.go +++ b/services/forgejo/main_test.go @@ -10,6 +10,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/gitdiff/main_test.go b/services/gitdiff/main_test.go index cd9dcd8cd6..3d4d480530 100644 --- a/services/gitdiff/main_test.go +++ b/services/gitdiff/main_test.go @@ -11,6 +11,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/pull/main_test.go b/services/pull/main_test.go index efbb63a36e..4bcb50fb96 100644 --- a/services/pull/main_test.go +++ b/services/pull/main_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models/unittest" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/services/release/release_test.go b/services/release/release_test.go index 026bba8258..5a22c473cf 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/services/attachment" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/repository/archiver/archiver_test.go b/services/repository/archiver/archiver_test.go index 9f822a31ce..a90bfa7bef 100644 --- a/services/repository/archiver/archiver_test.go +++ b/services/repository/archiver/archiver_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/services/contexttest" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go index c22dcd2e8d..f5e2b84690 100644 --- a/services/repository/files/content_test.go +++ b/services/repository/files/content_test.go @@ -13,6 +13,7 @@ import ( api "code.gitea.io/gitea/modules/structs" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/services/webhook/main_test.go b/services/webhook/main_test.go index 756b9db230..6147aac499 100644 --- a/services/webhook/main_test.go +++ b/services/webhook/main_test.go @@ -12,6 +12,7 @@ import ( _ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/forgefed" ) func TestMain(m *testing.M) { diff --git a/tests/integration/api_activitypub_actor_test.go b/tests/integration/api_activitypub_actor_test.go index 7506c786da..de4d669c9d 100644 --- a/tests/integration/api_activitypub_actor_test.go +++ b/tests/integration/api_activitypub_actor_test.go @@ -5,12 +5,12 @@ package integration import ( "net/http" - "net/url" "testing" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/routers" + "code.gitea.io/gitea/tests" ap "github.com/go-ap/activitypub" "github.com/stretchr/testify/assert" @@ -20,31 +20,29 @@ import ( func TestActivityPubActor(t *testing.T) { defer test.MockVariableValue(&setting.Federation.Enabled, true)() defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() + defer tests.PrepareTestEnv(t)() - onGiteaRun(t, func(*testing.T, *url.URL) { - req := NewRequest(t, "GET", "/api/v1/activitypub/actor") - resp := MakeRequest(t, req, http.StatusOK) - body := resp.Body.Bytes() - assert.Contains(t, string(body), "@context") + req := NewRequest(t, "GET", "/api/v1/activitypub/actor") + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "@context") - var actor ap.Actor - err := actor.UnmarshalJSON(body) - require.NoError(t, err) + var actor ap.Actor + err := actor.UnmarshalJSON(resp.Body.Bytes()) + require.NoError(t, err) - assert.Equal(t, ap.ApplicationType, actor.Type) - assert.Equal(t, setting.Domain, actor.PreferredUsername.String()) - keyID := actor.GetID().String() - assert.Regexp(t, "activitypub/actor$", keyID) - assert.Regexp(t, "activitypub/actor/outbox$", actor.Outbox.GetID().String()) - assert.Regexp(t, "activitypub/actor/inbox$", actor.Inbox.GetID().String()) + assert.Equal(t, ap.ApplicationType, actor.Type) + assert.Equal(t, setting.Domain, actor.PreferredUsername.String()) + keyID := actor.GetID().String() + assert.Regexp(t, "activitypub/actor$", keyID) + assert.Regexp(t, "activitypub/actor/outbox$", actor.Outbox.GetID().String()) + assert.Regexp(t, "activitypub/actor/inbox$", actor.Inbox.GetID().String()) - pubKey := actor.PublicKey - assert.NotNil(t, pubKey) - publicKeyID := keyID + "#main-key" - assert.Equal(t, pubKey.ID.String(), publicKeyID) + pubKey := actor.PublicKey + assert.NotNil(t, pubKey) + publicKeyID := keyID + "#main-key" + assert.Equal(t, pubKey.ID.String(), publicKeyID) - pubKeyPem := pubKey.PublicKeyPem - assert.NotNil(t, pubKeyPem) - assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) - }) + pubKeyPem := pubKey.PublicKeyPem + assert.NotNil(t, pubKeyPem) + assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) } diff --git a/tests/integration/api_activitypub_person_test.go b/tests/integration/api_activitypub_person_test.go index 55935e4ab6..846ae79891 100644 --- a/tests/integration/api_activitypub_person_test.go +++ b/tests/integration/api_activitypub_person_test.go @@ -4,18 +4,19 @@ package integration import ( - "context" "fmt" "net/http" - "net/http/httptest" "net/url" "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/routers" + "code.gitea.io/gitea/tests" ap "github.com/go-ap/activitypub" "github.com/stretchr/testify/assert" @@ -23,86 +24,61 @@ import ( ) func TestActivityPubPerson(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() + defer tests.PrepareTestEnv(t)() - onGiteaRun(t, func(*testing.T, *url.URL) { - userID := 2 - username := "user2" - req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID)) - resp := MakeRequest(t, req, http.StatusOK) - body := resp.Body.Bytes() - assert.Contains(t, string(body), "@context") + userID := 2 + username := "user2" + req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID)) + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "@context") - var person ap.Person - err := person.UnmarshalJSON(body) - require.NoError(t, err) + var person ap.Person + err := person.UnmarshalJSON(resp.Body.Bytes()) + require.NoError(t, err) - assert.Equal(t, ap.PersonType, person.Type) - assert.Equal(t, username, person.PreferredUsername.String()) - keyID := person.GetID().String() - assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v$", userID), keyID) - assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/outbox$", userID), person.Outbox.GetID().String()) - assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/inbox$", userID), person.Inbox.GetID().String()) + assert.Equal(t, ap.PersonType, person.Type) + assert.Equal(t, username, person.PreferredUsername.String()) + keyID := person.GetID().String() + assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v$", userID), keyID) + assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/outbox$", userID), person.Outbox.GetID().String()) + assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/inbox$", userID), person.Inbox.GetID().String()) - pubKey := person.PublicKey - assert.NotNil(t, pubKey) - publicKeyID := keyID + "#main-key" - assert.Equal(t, pubKey.ID.String(), publicKeyID) + pubKey := person.PublicKey + assert.NotNil(t, pubKey) + publicKeyID := keyID + "#main-key" + assert.Equal(t, pubKey.ID.String(), publicKeyID) - pubKeyPem := pubKey.PublicKeyPem - assert.NotNil(t, pubKeyPem) - assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) - }) + pubKeyPem := pubKey.PublicKeyPem + assert.NotNil(t, pubKeyPem) + assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) } func TestActivityPubMissingPerson(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() + defer tests.PrepareTestEnv(t)() - onGiteaRun(t, func(*testing.T, *url.URL) { - req := NewRequest(t, "GET", "/api/v1/activitypub/user-id/999999999") - resp := MakeRequest(t, req, http.StatusNotFound) - assert.Contains(t, resp.Body.String(), "user does not exist") - }) + req := NewRequest(t, "GET", "/api/v1/activitypub/user-id/999999999") + resp := MakeRequest(t, req, http.StatusNotFound) + assert.Contains(t, resp.Body.String(), "user does not exist") } func TestActivityPubPersonInbox(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() - srv := httptest.NewServer(testWebRoutes) - defer srv.Close() + onGiteaRun(t, func(t *testing.T, u *url.URL) { + defer test.MockVariableValue(&setting.AppURL, u.String())() + user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) - onGiteaRun(t, func(*testing.T, *url.URL) { - appURL := setting.AppURL - setting.AppURL = srv.URL + "/" - defer func() { - setting.Database.LogSQL = false - setting.AppURL = appURL - }() - username1 := "user1" - ctx := context.Background() - user1, err := user_model.GetUserByName(ctx, username1) - require.NoError(t, err) - user1url := fmt.Sprintf("%s/api/v1/activitypub/user-id/1#main-key", srv.URL) - cf, err := activitypub.GetClientFactory(ctx) + user1url := u.JoinPath("/api/v1/activitypub/user-id/1").String() + "#main-key" + cf, err := activitypub.GetClientFactory(db.DefaultContext) require.NoError(t, err) c, err := cf.WithKeys(db.DefaultContext, user1, user1url) require.NoError(t, err) - user2inboxurl := fmt.Sprintf("%s/api/v1/activitypub/user-id/2/inbox", srv.URL) + user2inboxurl := u.JoinPath("/api/v1/activitypub/user-id/2/inbox").String() // Signed request succeeds resp, err := c.Post([]byte{}, user2inboxurl) diff --git a/tests/integration/api_activitypub_repository_test.go b/tests/integration/api_activitypub_repository_test.go index 737f580061..2ca7d42e2a 100644 --- a/tests/integration/api_activitypub_repository_test.go +++ b/tests/integration/api_activitypub_repository_test.go @@ -18,111 +18,90 @@ import ( "code.gitea.io/gitea/modules/activitypub" forgefed_modules "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/routers" + "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestActivityPubRepository(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() + defer tests.PrepareTestEnv(t)() - onGiteaRun(t, func(*testing.T, *url.URL) { - repositoryID := 2 - req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/repository-id/%v", repositoryID)) - resp := MakeRequest(t, req, http.StatusOK) - body := resp.Body.Bytes() - assert.Contains(t, string(body), "@context") + repositoryID := 2 + req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/repository-id/%v", repositoryID)) + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "@context") - var repository forgefed_modules.Repository - err := repository.UnmarshalJSON(body) - require.NoError(t, err) + var repository forgefed_modules.Repository + err := repository.UnmarshalJSON(resp.Body.Bytes()) + require.NoError(t, err) - assert.Regexp(t, fmt.Sprintf("activitypub/repository-id/%v$", repositoryID), repository.GetID().String()) - }) + assert.Regexp(t, fmt.Sprintf("activitypub/repository-id/%v$", repositoryID), repository.GetID().String()) } func TestActivityPubMissingRepository(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() + defer tests.PrepareTestEnv(t)() - onGiteaRun(t, func(*testing.T, *url.URL) { - repositoryID := 9999999 - req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/repository-id/%v", repositoryID)) - resp := MakeRequest(t, req, http.StatusNotFound) - assert.Contains(t, resp.Body.String(), "repository does not exist") - }) + repositoryID := 9999999 + req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/activitypub/repository-id/%v", repositoryID)) + resp := MakeRequest(t, req, http.StatusNotFound) + assert.Contains(t, resp.Body.String(), "repository does not exist") } func TestActivityPubRepositoryInboxValid(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() - - srv := httptest.NewServer(testWebRoutes) - defer srv.Close() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() federatedRoutes := http.NewServeMux() federatedRoutes.HandleFunc("/.well-known/nodeinfo", func(res http.ResponseWriter, req *http.Request) { // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/.well-known/nodeinfo - responseBody := fmt.Sprintf(`{"links":[{"href":"http://%s/api/v1/nodeinfo","rel":"http://nodeinfo.diaspora.software/ns/schema/2.1"}]}`, req.Host) - t.Logf("response: %s", responseBody) // TODO: as soon as content-type will become important: content-type: application/json;charset=utf-8 - fmt.Fprint(res, responseBody) + fmt.Fprintf(res, `{"links":[{"href":"http://%s/api/v1/nodeinfo","rel":"http://nodeinfo.diaspora.software/ns/schema/2.1"}]}`, req.Host) }) federatedRoutes.HandleFunc("/api/v1/nodeinfo", func(res http.ResponseWriter, req *http.Request) { // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/api/v1/nodeinfo - responseBody := fmt.Sprintf(`{"version":"2.1","software":{"name":"forgejo","version":"1.20.0+dev-3183-g976d79044",` + - `"repository":"https://codeberg.org/forgejo/forgejo.git","homepage":"https://forgejo.org/"},` + - `"protocols":["activitypub"],"services":{"inbound":[],"outbound":["rss2.0"]},` + + fmt.Fprint(res, `{"version":"2.1","software":{"name":"forgejo","version":"1.20.0+dev-3183-g976d79044",`+ + `"repository":"https://codeberg.org/forgejo/forgejo.git","homepage":"https://forgejo.org/"},`+ + `"protocols":["activitypub"],"services":{"inbound":[],"outbound":["rss2.0"]},`+ `"openRegistrations":true,"usage":{"users":{"total":14,"activeHalfyear":2}},"metadata":{}}`) - fmt.Fprint(res, responseBody) }) federatedRoutes.HandleFunc("/api/v1/activitypub/user-id/15", func(res http.ResponseWriter, req *http.Request) { // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/2 - responseBody := fmt.Sprintf(`{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1"],` + - `"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15","type":"Person",` + - `"icon":{"type":"Image","mediaType":"image/png","url":"https://federated-repo.prod.meissa.de/avatars/1bb05d9a5f6675ed0272af9ea193063c"},` + - `"url":"https://federated-repo.prod.meissa.de/stargoose1","inbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15/inbox",` + - `"outbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15/outbox","preferredUsername":"stargoose1",` + - `"publicKey":{"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15#main-key","owner":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15",` + - `"publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA18H5s7N6ItZUAh9tneII\nIuZdTTa3cZlLa/9ejWAHTkcp3WLW+/zbsumlMrWYfBy2/yTm56qasWt38iY4D6ul\n` + - `CPiwhAqX3REvVq8tM79a2CEqZn9ka6vuXoDgBg/sBf/BUWqf7orkjUXwk/U0Egjf\nk5jcurF4vqf1u+rlAHH37dvSBaDjNj6Qnj4OP12bjfaY/yvs7+jue/eNXFHjzN4E\n` + - `T2H4B/yeKTJ4UuAwTlLaNbZJul2baLlHelJPAsxiYaziVuV5P+IGWckY6RSerRaZ\nAkc4mmGGtjAyfN9aewe+lNVfwS7ElFx546PlLgdQgjmeSwLX8FWxbPE5A/PmaXCs\n` + - `nx+nou+3dD7NluULLtdd7K+2x02trObKXCAzmi5/Dc+yKTzpFqEz+hLNCz7TImP/\ncK//NV9Q+X67J9O27baH9R9ZF4zMw8rv2Pg0WLSw1z7lLXwlgIsDapeMCsrxkVO4\n` + + fmt.Fprint(res, `{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1"],`+ + `"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15","type":"Person",`+ + `"icon":{"type":"Image","mediaType":"image/png","url":"https://federated-repo.prod.meissa.de/avatars/1bb05d9a5f6675ed0272af9ea193063c"},`+ + `"url":"https://federated-repo.prod.meissa.de/stargoose1","inbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15/inbox",`+ + `"outbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15/outbox","preferredUsername":"stargoose1",`+ + `"publicKey":{"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15#main-key","owner":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/15",`+ + `"publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA18H5s7N6ItZUAh9tneII\nIuZdTTa3cZlLa/9ejWAHTkcp3WLW+/zbsumlMrWYfBy2/yTm56qasWt38iY4D6ul\n`+ + `CPiwhAqX3REvVq8tM79a2CEqZn9ka6vuXoDgBg/sBf/BUWqf7orkjUXwk/U0Egjf\nk5jcurF4vqf1u+rlAHH37dvSBaDjNj6Qnj4OP12bjfaY/yvs7+jue/eNXFHjzN4E\n`+ + `T2H4B/yeKTJ4UuAwTlLaNbZJul2baLlHelJPAsxiYaziVuV5P+IGWckY6RSerRaZ\nAkc4mmGGtjAyfN9aewe+lNVfwS7ElFx546PlLgdQgjmeSwLX8FWxbPE5A/PmaXCs\n`+ + `nx+nou+3dD7NluULLtdd7K+2x02trObKXCAzmi5/Dc+yKTzpFqEz+hLNCz7TImP/\ncK//NV9Q+X67J9O27baH9R9ZF4zMw8rv2Pg0WLSw1z7lLXwlgIsDapeMCsrxkVO4\n`+ `LXX5AQ1xQNtlssnVoUBqBrvZsX2jUUKUocvZqMGuE4hfAgMBAAE=\n-----END PUBLIC KEY-----\n"}}`) - fmt.Fprint(res, responseBody) }) federatedRoutes.HandleFunc("/api/v1/activitypub/user-id/30", func(res http.ResponseWriter, req *http.Request) { // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/3 - responseBody := fmt.Sprintf(`{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1"],` + - `"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30","type":"Person",` + - `"icon":{"type":"Image","mediaType":"image/png","url":"https://federated-repo.prod.meissa.de/avatars/9c03f03d1c1f13f21976a22489326fe1"},` + - `"url":"https://federated-repo.prod.meissa.de/stargoose2","inbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30/inbox",` + - `"outbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30/outbox","preferredUsername":"stargoose2",` + - `"publicKey":{"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30#main-key","owner":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30",` + - `"publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAyv5NytsfqpWXSrwuk8a3\n0W1zE13QJioXb/e3opgN2CfKZkdm3hb+4+mGKoU/rCqegnL9/AO0Aw+R8fCHXx44\n` + - `iNkdVpdY8Dzq+tQ9IetPWbyVIBvSzGgvpqfS05JuVPsy8cBX9wByODjr5kq7k1/v\nY1G7E3uh0a/XJc+mZutwGC3gPgR93NSrqsvTPN4wdhCCu9uj02S8OBoKuSYaPkU+\n` + - `tZ4CEDpnclAOw/eNiH4x2irMvVtruEgtlTA5K2I4YJrmtGLidus47FCyc8/zEKUh\nAeiD8KWDvqsQgOhUwcQgRxAnYVCoMD9cnE+WFFRHTuQecNlmdNFs3Cr0yKcWjDde\n` + - `trvnehW7LfPveGb0tHRHPuVAJpncTOidUR5h/7pqMyvKHzuAHWomm9rEaGUxd/7a\nL1CFjAf39+QIEgu0Anj8mIc7CTiz+DQhDz+0jBOsQ0iDXc5GeBz7X9Xv4Jp966nq\n` + + fmt.Fprint(res, `{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1"],`+ + `"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30","type":"Person",`+ + `"icon":{"type":"Image","mediaType":"image/png","url":"https://federated-repo.prod.meissa.de/avatars/9c03f03d1c1f13f21976a22489326fe1"},`+ + `"url":"https://federated-repo.prod.meissa.de/stargoose2","inbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30/inbox",`+ + `"outbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30/outbox","preferredUsername":"stargoose2",`+ + `"publicKey":{"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30#main-key","owner":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/30",`+ + `"publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAyv5NytsfqpWXSrwuk8a3\n0W1zE13QJioXb/e3opgN2CfKZkdm3hb+4+mGKoU/rCqegnL9/AO0Aw+R8fCHXx44\n`+ + `iNkdVpdY8Dzq+tQ9IetPWbyVIBvSzGgvpqfS05JuVPsy8cBX9wByODjr5kq7k1/v\nY1G7E3uh0a/XJc+mZutwGC3gPgR93NSrqsvTPN4wdhCCu9uj02S8OBoKuSYaPkU+\n`+ + `tZ4CEDpnclAOw/eNiH4x2irMvVtruEgtlTA5K2I4YJrmtGLidus47FCyc8/zEKUh\nAeiD8KWDvqsQgOhUwcQgRxAnYVCoMD9cnE+WFFRHTuQecNlmdNFs3Cr0yKcWjDde\n`+ + `trvnehW7LfPveGb0tHRHPuVAJpncTOidUR5h/7pqMyvKHzuAHWomm9rEaGUxd/7a\nL1CFjAf39+QIEgu0Anj8mIc7CTiz+DQhDz+0jBOsQ0iDXc5GeBz7X9Xv4Jp966nq\n`+ `MUR0GQGXvfZQN9IqMO+WoUVy10Ddhns1EWGlA0x4fecnAgMBAAE=\n-----END PUBLIC KEY-----\n"}}`) - fmt.Fprint(res, responseBody) }) federatedRoutes.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { @@ -131,32 +110,24 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { federatedSrv := httptest.NewServer(federatedRoutes) defer federatedSrv.Close() - onGiteaRun(t, func(*testing.T, *url.URL) { - appURL := setting.AppURL - setting.AppURL = srv.URL + "/" - defer func() { - setting.Database.LogSQL = false - setting.AppURL = appURL - }() + onGiteaRun(t, func(t *testing.T, u *url.URL) { actionsUser := user.NewActionsUser() repositoryID := 2 + timeNow := time.Now().UTC() + cf, err := activitypub.GetClientFactory(db.DefaultContext) require.NoError(t, err) c, err := cf.WithKeys(db.DefaultContext, actionsUser, "not used") require.NoError(t, err) - repoInboxURL := fmt.Sprintf( - "%s/api/v1/activitypub/repository-id/%v/inbox", - srv.URL, repositoryID) - - timeNow := time.Now().UTC() + repoInboxURL := u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%d/inbox", repositoryID)).String() activity1 := []byte(fmt.Sprintf( `{"type":"Like",`+ `"startTime":"%s",`+ `"actor":"%s/api/v1/activitypub/user-id/15",`+ - `"object":"%s/api/v1/activitypub/repository-id/%v"}`, + `"object":"%s"}`, timeNow.Format(time.RFC3339), - federatedSrv.URL, srv.URL, repositoryID)) + federatedSrv.URL, u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%d", repositoryID)).String())) t.Logf("activity: %s", activity1) resp, err := c.Post(activity1, repoInboxURL) @@ -172,10 +143,10 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { `{"type":"Like",`+ `"startTime":"%s",`+ `"actor":"%s/api/v1/activitypub/user-id/30",`+ - `"object":"%s/api/v1/activitypub/repository-id/%v"}`, + `"object":"%s"}`, // Make sure this activity happens later then the one before timeNow.Add(time.Second).Format(time.RFC3339), - federatedSrv.URL, srv.URL, repositoryID)) + federatedSrv.URL, u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%d", repositoryID)).String())) t.Logf("activity: %s", activity2) resp, err = c.Post(activity2, repoInboxURL) @@ -187,17 +158,15 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { // The same user sends another like activity otherRepositoryID := 3 - otherRepoInboxURL := fmt.Sprintf( - "%s/api/v1/activitypub/repository-id/%v/inbox", - srv.URL, otherRepositoryID) + otherRepoInboxURL := u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%d/inbox", otherRepositoryID)).String() activity3 := []byte(fmt.Sprintf( `{"type":"Like",`+ `"startTime":"%s",`+ `"actor":"%s/api/v1/activitypub/user-id/30",`+ - `"object":"%s/api/v1/activitypub/repository-id/%v"}`, + `"object":"%s"}`, // Make sure this activity happens later then the ones before timeNow.Add(time.Second*2).Format(time.RFC3339), - federatedSrv.URL, srv.URL, otherRepositoryID)) + federatedSrv.URL, u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%d", otherRepositoryID)).String())) t.Logf("activity: %s", activity3) resp, err = c.Post(activity3, otherRepoInboxURL) @@ -215,32 +184,18 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { } func TestActivityPubRepositoryInboxInvalid(t *testing.T) { - setting.Federation.Enabled = true - testWebRoutes = routers.NormalRoutes() - defer func() { - setting.Federation.Enabled = false - testWebRoutes = routers.NormalRoutes() - }() + defer test.MockVariableValue(&setting.Federation.Enabled, true)() + defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())() - srv := httptest.NewServer(testWebRoutes) - defer srv.Close() - - onGiteaRun(t, func(*testing.T, *url.URL) { - appURL := setting.AppURL - setting.AppURL = srv.URL + "/" - defer func() { - setting.Database.LogSQL = false - setting.AppURL = appURL - }() + onGiteaRun(t, func(t *testing.T, u *url.URL) { actionsUser := user.NewActionsUser() repositoryID := 2 cf, err := activitypub.GetClientFactory(db.DefaultContext) require.NoError(t, err) c, err := cf.WithKeys(db.DefaultContext, actionsUser, "not used") require.NoError(t, err) - repoInboxURL := fmt.Sprintf("%s/api/v1/activitypub/repository-id/%v/inbox", - srv.URL, repositoryID) + repoInboxURL := u.JoinPath(fmt.Sprintf("/api/v1/activitypub/repository-id/%v/inbox", repositoryID)).String() activity := []byte(`{"type":"Wrong"}`) resp, err := c.Post(activity, repoInboxURL) require.NoError(t, err)