code.gitea.io/gitea@v1.21.7/tests/integration/api_activitypub_person_test.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "context" 8 "fmt" 9 "net/http" 10 "net/http/httptest" 11 "net/url" 12 "testing" 13 14 "code.gitea.io/gitea/models/db" 15 user_model "code.gitea.io/gitea/models/user" 16 "code.gitea.io/gitea/modules/activitypub" 17 "code.gitea.io/gitea/modules/setting" 18 "code.gitea.io/gitea/routers" 19 20 ap "github.com/go-ap/activitypub" 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestActivityPubPerson(t *testing.T) { 25 setting.Federation.Enabled = true 26 testWebRoutes = routers.NormalRoutes() 27 defer func() { 28 setting.Federation.Enabled = false 29 testWebRoutes = routers.NormalRoutes() 30 }() 31 32 onGiteaRun(t, func(*testing.T, *url.URL) { 33 userID := 2 34 username := "user2" 35 req := NewRequestf(t, "GET", fmt.Sprintf("/api/v1/activitypub/user-id/%v", userID)) 36 resp := MakeRequest(t, req, http.StatusOK) 37 body := resp.Body.Bytes() 38 assert.Contains(t, string(body), "@context") 39 40 var person ap.Person 41 err := person.UnmarshalJSON(body) 42 assert.NoError(t, err) 43 44 assert.Equal(t, ap.PersonType, person.Type) 45 assert.Equal(t, username, person.PreferredUsername.String()) 46 keyID := person.GetID().String() 47 assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v$", userID), keyID) 48 assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/outbox$", userID), person.Outbox.GetID().String()) 49 assert.Regexp(t, fmt.Sprintf("activitypub/user-id/%v/inbox$", userID), person.Inbox.GetID().String()) 50 51 pubKey := person.PublicKey 52 assert.NotNil(t, pubKey) 53 publicKeyID := keyID + "#main-key" 54 assert.Equal(t, pubKey.ID.String(), publicKeyID) 55 56 pubKeyPem := pubKey.PublicKeyPem 57 assert.NotNil(t, pubKeyPem) 58 assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) 59 }) 60 } 61 62 func TestActivityPubMissingPerson(t *testing.T) { 63 setting.Federation.Enabled = true 64 testWebRoutes = routers.NormalRoutes() 65 defer func() { 66 setting.Federation.Enabled = false 67 testWebRoutes = routers.NormalRoutes() 68 }() 69 70 onGiteaRun(t, func(*testing.T, *url.URL) { 71 req := NewRequestf(t, "GET", "/api/v1/activitypub/user-id/999999999") 72 resp := MakeRequest(t, req, http.StatusNotFound) 73 assert.Contains(t, resp.Body.String(), "user does not exist") 74 }) 75 } 76 77 func TestActivityPubPersonInbox(t *testing.T) { 78 setting.Federation.Enabled = true 79 testWebRoutes = routers.NormalRoutes() 80 defer func() { 81 setting.Federation.Enabled = false 82 testWebRoutes = routers.NormalRoutes() 83 }() 84 85 srv := httptest.NewServer(testWebRoutes) 86 defer srv.Close() 87 88 onGiteaRun(t, func(*testing.T, *url.URL) { 89 appURL := setting.AppURL 90 setting.AppURL = srv.URL + "/" 91 defer func() { 92 setting.Database.LogSQL = false 93 setting.AppURL = appURL 94 }() 95 username1 := "user1" 96 ctx := context.Background() 97 user1, err := user_model.GetUserByName(ctx, username1) 98 assert.NoError(t, err) 99 user1url := fmt.Sprintf("%s/api/v1/activitypub/user-id/1#main-key", srv.URL) 100 c, err := activitypub.NewClient(db.DefaultContext, user1, user1url) 101 assert.NoError(t, err) 102 user2inboxurl := fmt.Sprintf("%s/api/v1/activitypub/user-id/2/inbox", srv.URL) 103 104 // Signed request succeeds 105 resp, err := c.Post([]byte{}, user2inboxurl) 106 assert.NoError(t, err) 107 assert.Equal(t, http.StatusNoContent, resp.StatusCode) 108 109 // Unsigned request fails 110 req := NewRequest(t, "POST", user2inboxurl) 111 MakeRequest(t, req, http.StatusInternalServerError) 112 }) 113 }