summaryrefslogtreecommitdiffstats
path: root/tests/integration/api_activitypub_person_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /tests/integration/api_activitypub_person_test.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'tests/integration/api_activitypub_person_test.go')
-rw-r--r--tests/integration/api_activitypub_person_test.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/integration/api_activitypub_person_test.go b/tests/integration/api_activitypub_person_test.go
new file mode 100644
index 0000000..55935e4
--- /dev/null
+++ b/tests/integration/api_activitypub_person_test.go
@@ -0,0 +1,116 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/activitypub"
+ "code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/routers"
+
+ ap "github.com/go-ap/activitypub"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestActivityPubPerson(t *testing.T) {
+ setting.Federation.Enabled = true
+ testWebRoutes = routers.NormalRoutes()
+ defer func() {
+ setting.Federation.Enabled = false
+ testWebRoutes = routers.NormalRoutes()
+ }()
+
+ 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")
+
+ var person ap.Person
+ err := person.UnmarshalJSON(body)
+ 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())
+
+ 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)
+ })
+}
+
+func TestActivityPubMissingPerson(t *testing.T) {
+ setting.Federation.Enabled = true
+ testWebRoutes = routers.NormalRoutes()
+ defer func() {
+ setting.Federation.Enabled = false
+ testWebRoutes = routers.NormalRoutes()
+ }()
+
+ 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")
+ })
+}
+
+func TestActivityPubPersonInbox(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()
+
+ 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)
+ 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)
+
+ // Signed request succeeds
+ resp, err := c.Post([]byte{}, user2inboxurl)
+ require.NoError(t, err)
+ assert.Equal(t, http.StatusNoContent, resp.StatusCode)
+
+ // Unsigned request fails
+ req := NewRequest(t, "POST", user2inboxurl)
+ MakeRequest(t, req, http.StatusBadRequest)
+ })
+}