Extended integration test

This commit is contained in:
Clemens 2024-05-21 11:43:15 +02:00
parent 07659a5e7e
commit a62c2afcea
2 changed files with 68 additions and 7 deletions

View file

@ -74,9 +74,6 @@ func RepositoryInbox(ctx *context.APIContext) {
form := web.GetForm(ctx) form := web.GetForm(ctx)
httpStatus, title, err := federation.ProcessLikeActivity(ctx, form, repository.ID) httpStatus, title, err := federation.ProcessLikeActivity(ctx, form, repository.ID)
if err != nil { if err != nil {
log.Error("Status: %v", httpStatus)
log.Error("Title: %v", title)
log.Error("Error: %v", err)
ctx.Error(httpStatus, title, err) ctx.Error(httpStatus, title, err)
} }
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)

View file

@ -107,6 +107,22 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) {
`LXX5AQ1xQNtlssnVoUBqBrvZsX2jUUKUocvZqMGuE4hfAgMBAAE=\n-----END PUBLIC KEY-----\n"}}`) `LXX5AQ1xQNtlssnVoUBqBrvZsX2jUUKUocvZqMGuE4hfAgMBAAE=\n-----END PUBLIC KEY-----\n"}}`)
fmt.Fprint(res, responseBody) 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/3","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/3/inbox",` +
`"outbox":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/3/outbox","preferredUsername":"stargoose2",` +
`"publicKey":{"id":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/3#main-key","owner":"https://federated-repo.prod.meissa.de/api/v1/activitypub/user-id/3",` +
`"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("/", federatedRoutes.HandleFunc("/",
func(res http.ResponseWriter, req *http.Request) { func(res http.ResponseWriter, req *http.Request) {
t.Errorf("Unhandled request: %q", req.URL.EscapedPath()) t.Errorf("Unhandled request: %q", req.URL.EscapedPath())
@ -129,15 +145,17 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) {
"%s/api/v1/activitypub/repository-id/%v/inbox", "%s/api/v1/activitypub/repository-id/%v/inbox",
srv.URL, repositoryID) srv.URL, repositoryID)
activity := []byte(fmt.Sprintf( timeNow := time.Now().UTC()
activity1 := []byte(fmt.Sprintf(
`{"type":"Like",`+ `{"type":"Like",`+
`"startTime":"%s",`+ `"startTime":"%s",`+
`"actor":"%s/api/v1/activitypub/user-id/15",`+ `"actor":"%s/api/v1/activitypub/user-id/15",`+
`"object":"%s/api/v1/activitypub/repository-id/%v"}`, `"object":"%s/api/v1/activitypub/repository-id/%v"}`,
time.Now().UTC().Format(time.RFC3339), timeNow.Format(time.RFC3339),
federatedSrv.URL, srv.URL, repositoryID)) federatedSrv.URL, srv.URL, repositoryID))
t.Logf("activity: %s", activity) t.Logf("activity: %s", activity1)
resp, err := c.Post(activity, repoInboxURL) resp, err := c.Post(activity1, repoInboxURL)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Equal(t, http.StatusNoContent, resp.StatusCode)
@ -145,6 +163,52 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) {
federationHost := unittest.AssertExistsAndLoadBean(t, &forgefed.FederationHost{HostFqdn: "127.0.0.1"}) federationHost := unittest.AssertExistsAndLoadBean(t, &forgefed.FederationHost{HostFqdn: "127.0.0.1"})
federatedUser := unittest.AssertExistsAndLoadBean(t, &user.FederatedUser{ExternalID: "15", FederationHostID: federationHost.ID}) federatedUser := unittest.AssertExistsAndLoadBean(t, &user.FederatedUser{ExternalID: "15", FederationHostID: federationHost.ID})
unittest.AssertExistsAndLoadBean(t, &user.User{ID: federatedUser.UserID}) unittest.AssertExistsAndLoadBean(t, &user.User{ID: federatedUser.UserID})
// A like activity by a different user of the same federated host.
activity2 := []byte(fmt.Sprintf(
`{"type":"Like",`+
`"startTime":"%s",`+
`"actor":"%s/api/v1/activitypub/user-id/30",`+
`"object":"%s/api/v1/activitypub/repository-id/%v"}`,
// Make sure this activity happens later then the one before
timeNow.Add(time.Second).Format(time.RFC3339),
federatedSrv.URL, srv.URL, repositoryID))
t.Logf("activity: %s", activity2)
resp, err = c.Post(activity2, repoInboxURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
federatedUser = unittest.AssertExistsAndLoadBean(t, &user.FederatedUser{ExternalID: "30", FederationHostID: federationHost.ID})
unittest.AssertExistsAndLoadBean(t, &user.User{ID: federatedUser.UserID})
// The same user sends another like activity
otherRepositoryID := 3
otherRepoInboxURL := fmt.Sprintf(
"%s/api/v1/activitypub/repository-id/%v/inbox",
srv.URL, otherRepositoryID)
activity3 := []byte(fmt.Sprintf(
`{"type":"Like",`+
`"startTime":"%s",`+
`"actor":"%s/api/v1/activitypub/user-id/30",`+
`"object":"%s/api/v1/activitypub/repository-id/%v"}`,
// Make sure this activity happens later then the ones before
timeNow.Add(time.Second*2).Format(time.RFC3339),
federatedSrv.URL, srv.URL, otherRepositoryID))
t.Logf("activity: %s", activity3)
resp, err = c.Post(activity3, otherRepoInboxURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusNoContent, resp.StatusCode)
federatedUser = unittest.AssertExistsAndLoadBean(t, &user.FederatedUser{ExternalID: "30", FederationHostID: federationHost.ID})
unittest.AssertExistsAndLoadBean(t, &user.User{ID: federatedUser.UserID})
// Replay activity2.
resp, err = c.Post(activity2, repoInboxURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusNotAcceptable, resp.StatusCode)
// TODO: Check for server api context error saying "Activity already processed"
}) })
} }