mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-05 02:37:52 +01:00
124 lines
4.2 KiB
Go
124 lines
4.2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package activitypub
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"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/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
/* ToDo: Set Up tests for http get requests
|
|
|
|
Set up an expected response for GET on api with user-id = 1:
|
|
{
|
|
"@context": [
|
|
"https://www.w3.org/ns/activitystreams",
|
|
"https://w3id.org/security/v1"
|
|
],
|
|
"id": "http://localhost:3000/api/v1/activitypub/user-id/1",
|
|
"type": "Person",
|
|
"icon": {
|
|
"type": "Image",
|
|
"mediaType": "image/png",
|
|
"url": "http://localhost:3000/avatar/3120fd0edc57d5d41230013ad88232e2"
|
|
},
|
|
"url": "http://localhost:3000/me",
|
|
"inbox": "http://localhost:3000/api/v1/activitypub/user-id/1/inbox",
|
|
"outbox": "http://localhost:3000/api/v1/activitypub/user-id/1/outbox",
|
|
"preferredUsername": "me",
|
|
"publicKey": {
|
|
"id": "http://localhost:3000/api/v1/activitypub/user-id/1#main-key",
|
|
"owner": "http://localhost:3000/api/v1/activitypub/user-id/1",
|
|
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAo1VDZGWQBDTWKhpWiPQp\n7nD94UsKkcoFwDQVuxE3bMquKEHBomB4cwUnVou922YkL3AmSOr1sX2yJQGqnCLm\nOeKS74/mCIAoYlu0d75bqY4A7kE2VrQmQLZBbmpCTfrPqDaE6Mfm/kXaX7+hsrZS\n4bVvzZCYq8sjtRxdPk+9ku2QhvznwTRlWLvwHmFSGtlQYPRu+f/XqoVM/DVRA/Is\nwDk9yiNIecV+Isus0CBq1jGQkfuVNu1GK2IvcSg9MoDm3VH/tCayAP+xWm0g7sC8\nKay6Y/khvTvE7bWEKGQsJGvi3+4wITLVLVt+GoVOuCzdbhTV2CHBzn7h30AoZD0N\nY6eyb+Q142JykoHadcRwh1a36wgoG7E496wPvV3ST8xdiClca8cDNhOzCj8woY+t\nTFCMl32U3AJ4e/cAsxKRocYLZqc95dDqdNQiIyiRMMkf5NaA/QvelY4PmFuHC0WR\nVuJ4A3mcti2QLS9j0fSwSJdlfolgW6xaPgjdvuSQsgX1AgMBAAE=\n-----END PUBLIC KEY-----\n"
|
|
}
|
|
}
|
|
|
|
Set up a user called "me" for all tests
|
|
|
|
|
|
|
|
*/
|
|
|
|
func TestNewClientReturnsClient(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
pubID := "myGpgId"
|
|
c, err := NewClient(db.DefaultContext, user, pubID)
|
|
|
|
log.Debug("Client: %v\nError: %v", c, err)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
/* TODO: bring this test to work or delete
|
|
func TestActivityPubSignedGet(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, Name: "me"})
|
|
pubID := "myGpgId"
|
|
c, err := NewClient(db.DefaultContext, user, pubID)
|
|
assert.NoError(t, err)
|
|
|
|
expected := "TestActivityPubSignedGet"
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Regexp(t, regexp.MustCompile("^"+setting.Federation.DigestAlgorithm), r.Header.Get("Digest"))
|
|
assert.Contains(t, r.Header.Get("Signature"), pubID)
|
|
assert.Equal(t, r.Header.Get("Content-Type"), ActivityStreamsContentType)
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, string(body))
|
|
fmt.Fprint(w, expected)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
r, err := c.Get(srv.URL)
|
|
assert.NoError(t, err)
|
|
defer r.Body.Close()
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, string(body))
|
|
|
|
}
|
|
*/
|
|
|
|
func TestActivityPubSignedPost(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
pubID := "https://example.com/pubID"
|
|
c, err := NewClient(db.DefaultContext, user, pubID)
|
|
assert.NoError(t, err)
|
|
|
|
expected := "BODY"
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
assert.Regexp(t, regexp.MustCompile("^"+setting.Federation.DigestAlgorithm), r.Header.Get("Digest"))
|
|
assert.Contains(t, r.Header.Get("Signature"), pubID)
|
|
assert.Equal(t, r.Header.Get("Content-Type"), ActivityStreamsContentType)
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, string(body))
|
|
fmt.Fprint(w, expected)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
r, err := c.Post([]byte(expected), srv.URL)
|
|
assert.NoError(t, err)
|
|
defer r.Body.Close()
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, string(body))
|
|
}
|