From 33648f2a4c86c45e38d69cd1cf3a65256ec1e5cd Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 10 May 2024 17:30:34 +0200 Subject: [PATCH] add second federated instance to integration test --- .../api_activitypub_repository_test.go | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/tests/integration/api_activitypub_repository_test.go b/tests/integration/api_activitypub_repository_test.go index 19bf1cf094..e237ffeb1e 100644 --- a/tests/integration/api_activitypub_repository_test.go +++ b/tests/integration/api_activitypub_repository_test.go @@ -9,8 +9,11 @@ import ( "net/http/httptest" "net/url" "testing" + "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/forgefed" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" forgefed_modules "code.gitea.io/gitea/modules/forgefed" @@ -70,6 +73,31 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { srv := httptest.NewServer(testWebRoutes) defer srv.Close() + federatedRoutes := http.NewServeMux() + federatedRoutes.HandleFunc("/.well-known/nodeinfo", + func(res http.ResponseWriter, req *http.Request) { + // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/.well-known/nodeinfo + responseBody := fmt.Sprintf(`{"links":[{"href":"http://%s/api/v1/nodeinfo","rel":"http://nodeinfo.diaspora.software/ns/schema/2.1"}]}`, req.Host) + t.Logf("response: %s", responseBody) + // TODO: as soon as content-type will become important: content-type: application/json;charset=utf-8 + fmt.Fprint(res, responseBody) + }) + federatedRoutes.HandleFunc("/api/v1/nodeinfo", + func(res http.ResponseWriter, req *http.Request) { + // curl -H "Accept: application/json" https://federated-repo.prod.meissa.de/api/v1/nodeinfo + responseBody := fmt.Sprintf(`{"version":"2.1","software":{"name":"forgejo","version":"1.20.0+dev-3183-g976d79044",` + + `"repository":"https://codeberg.org/forgejo/forgejo.git","homepage":"https://forgejo.org/"},` + + `"protocols":["activitypub"],"services":{"inbound":[],"outbound":["rss2.0"]},` + + `"openRegistrations":true,"usage":{"users":{"total":14,"activeHalfyear":2}},"metadata":{}}`) + fmt.Fprint(res, responseBody) + }) + federatedRoutes.HandleFunc("/", + func(res http.ResponseWriter, req *http.Request) { + t.Errorf("Unhandled request: %q", req.URL.EscapedPath()) + }) + federatedSrv := httptest.NewServer(federatedRoutes) + defer federatedSrv.Close() + onGiteaRun(t, func(*testing.T, *url.URL) { appURL := setting.AppURL setting.AppURL = srv.URL + "/" @@ -81,14 +109,26 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { repositoryID := 2 c, err := activitypub.NewClient(db.DefaultContext, actionsUser, "not used") assert.NoError(t, err) - repoInboxURL := fmt.Sprintf("%s/api/v1/activitypub/repository-id/%v/inbox", + repoInboxURL := fmt.Sprintf( + "%s/api/v1/activitypub/repository-id/%v/inbox", srv.URL, repositoryID) - activity := []byte(fmt.Sprintf(`{"type":"Like","startTime":"2024-03-27T00:00:00Z","actor":"%s/api/v1/activitypub/user-id/2","object":"%s/api/v1/activitypub/repository-id/%v"}`, - srv.URL, srv.URL, repositoryID)) + activity := []byte(fmt.Sprintf( + `{"type":"Like",`+ + `"startTime":"%s",`+ + `"actor":"%s/api/v1/activitypub/user-id/2",`+ + `"object":"%s/api/v1/activitypub/repository-id/%v"}`, + time.Now().UTC().Format(time.RFC3339), + federatedSrv.URL, srv.URL, repositoryID)) + t.Logf("activity: %s", activity) resp, err := c.Post(activity, repoInboxURL) + assert.NoError(t, err) assert.Equal(t, http.StatusNoContent, resp.StatusCode) + + federationHost := unittest.AssertExistsAndLoadBean(t, &forgefed.FederationHost{ID: 1}) + assert.Equal(t, "127.0.0.1", federationHost.HostFqdn) + }) }