1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-24 08:57:03 -05:00

Note ToDos for setting up useful tests

This commit is contained in:
erik 2023-11-28 15:17:59 +01:00 committed by Michael Jerger
parent e60d89cb67
commit 7529b9da91

View file

@ -21,6 +21,38 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
/* 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) { func TestNewClientReturnsClient(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
@ -32,6 +64,35 @@ func TestNewClientReturnsClient(t *testing.T) {
} }
func TestActivityPubSignedGet(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, Name: "me"})
pubID := "https://repo.prod.meissa.de/api/v1/activitypub/user-id/1/"
c, err := NewClient(db.DefaultContext, user, pubID)
assert.NoError(t, err)
expected := "BODY" // We want a person response
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([]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))
}
func TestActivityPubSignedPost(t *testing.T) { func TestActivityPubSignedPost(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})