diff options
Diffstat (limited to 'integrations/api_activitypub_repository_test.go')
-rw-r--r-- | integrations/api_activitypub_repository_test.go | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/integrations/api_activitypub_repository_test.go b/integrations/api_activitypub_repository_test.go new file mode 100644 index 000000000..37aa995ba --- /dev/null +++ b/integrations/api_activitypub_repository_test.go @@ -0,0 +1,102 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "sort" + "testing" + + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/services/migrations" + "github.com/go-fed/activity/streams" + "github.com/go-fed/activity/streams/vocab" + + "github.com/stretchr/testify/assert" +) + +func TestActivityPubRepository(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + AllowedDomains := setting.Migrations.AllowedDomains + setting.Migrations.AllowedDomains = "loopback" + AllowLocalNetworks := setting.Migrations.AllowLocalNetworks + setting.Migrations.AllowLocalNetworks = true + setting.Federation.Enabled = true + setting.Database.LogSQL = true + AppVer := setting.AppVer + setting.AppVer = "1.15" + defer func() { + setting.Migrations.AllowedDomains = AllowedDomains + setting.Migrations.AllowLocalNetworks = AllowLocalNetworks + setting.Federation.Enabled = false + setting.Database.LogSQL = false + setting.AppVer = AppVer + }() + + assert.NoError(t, migrations.Init()) + + username := "user2" + reponame := "repo1" + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + assert.False(t, repo1.HasProjectBase()) + + req := NewRequestf(t, "GET", fmt.Sprintf("/api/v1/activitypub/repos/%s/%s", username, reponame)) + resp := MakeRequest(t, req, http.StatusOK) + assert.Contains(t, resp.Body.String(), "@context") + var m map[string]interface{} + _ = json.Unmarshal(resp.Body.Bytes(), &m) + + var repository vocab.ForgeFedRepository + resolver, _ := streams.NewJSONResolver(func(c context.Context, r vocab.ForgeFedRepository) error { + repository = r + return nil + }) + ctx := context.Background() + err := resolver.Resolve(ctx, m) + assert.NoError(t, err) + assert.Equal(t, "Repository", repository.GetTypeName()) + assert.Equal(t, reponame, repository.GetActivityStreamsName().Begin().GetXMLSchemaString()) + keyID := repository.GetJSONLDId().GetIRI().String() + assert.Regexp(t, fmt.Sprintf("activitypub/repos/%s/%s$", username, reponame), keyID) + urlIter := repository.GetActivityStreamsUrl().Begin() + assert.Regexp(t, fmt.Sprintf("^ssh:.*/%s/%s.projectbase.git", username, reponame), urlIter.GetIRI().String()) + urlIter = urlIter.Next() + assert.Regexp(t, fmt.Sprintf("^http.*/%s/%s.projectbase.git", username, reponame), urlIter.GetIRI().String()) + + assert.True(t, repo1.HasProjectBase()) + gitRepo, err := git.OpenRepository(repo1.ProjectBasePath()) + fmt.Println(repo1.ProjectBasePath()) + assert.NoError(t, err) + defer gitRepo.Close() + files := []string{"repo.yml", "issue.yml", "comments"} + filelist, err := gitRepo.LsTree("HEAD", files...) + assert.NoError(t, err) + sort.Strings(filelist) + expected := files + expected = append(expected, "") + sort.Strings(expected) + assert.Equal(t, expected, filelist) + }) +} + +func TestActivityPubMissingRepository(t *testing.T) { + onGiteaRun(t, func(*testing.T, *url.URL) { + setting.Federation.Enabled = true + defer func() { + setting.Federation.Enabled = false + }() + + req := NewRequestf(t, "GET", "/api/v1/activitypub/repos/nonexistentuser/nonexistentrepo") + resp := MakeRequest(t, req, http.StatusNotFound) + assert.Contains(t, resp.Body.String(), "GetUserByName") + }) +} |