1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

Extend integration test for staring federated repo

This commit is contained in:
Clemens 2024-06-04 12:37:18 +02:00
parent 98939c4745
commit c0b9ab2060

View file

@ -5,8 +5,10 @@ package integration
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
@ -16,8 +18,10 @@ import (
unit_model "code.gitea.io/gitea/models/unit" unit_model "code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
fm "code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/validation"
gitea_context "code.gitea.io/gitea/services/context" gitea_context "code.gitea.io/gitea/services/context"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
user_service "code.gitea.io/gitea/services/user" user_service "code.gitea.io/gitea/services/user"
@ -291,6 +295,36 @@ func TestRepoFollowing(t *testing.T) {
`"openRegistrations":true,"usage":{"users":{"total":14,"activeHalfyear":2}},"metadata":{}}`) `"openRegistrations":true,"usage":{"users":{"total":14,"activeHalfyear":2}},"metadata":{}}`)
fmt.Fprint(res, responseBody) fmt.Fprint(res, responseBody)
}) })
repo1InboxReceivedLike := false
federatedRoutes.HandleFunc("/api/v1/activitypub/repository-id/1/inbox/",
func(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
t.Errorf("Unhandled request: %q", req.URL.EscapedPath())
}
buf := new(strings.Builder)
_, err := io.Copy(buf, req.Body)
if err != nil {
t.Errorf("Error reading body: %q", err)
}
like := fm.ForgeLike{}
err = like.UnmarshalJSON([]byte(buf.String()))
if err != nil {
t.Errorf("Error unmarshalling ForgeLike: %q", err)
}
if isValid, err := validation.IsValid(like); !isValid {
t.Errorf("ForgeLike is not valid: %q", err)
}
activityType := like.Type
object := like.Object.GetLink().String()
isLikeType := activityType == "Like"
isCorrectObject := strings.HasSuffix(object, "/api/v1/activitypub/repository-id/1")
if !isLikeType || !isCorrectObject {
t.Errorf("Activity is not a like for this repo")
}
repo1InboxReceivedLike = true
})
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())
@ -320,4 +354,16 @@ func TestRepoFollowing(t *testing.T) {
FederationHostID: federationHost.ID, FederationHostID: federationHost.ID,
}) })
}) })
t.Run("Star a repo having a following repo", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
repoLink := fmt.Sprintf("/%s", repo.FullName())
link := fmt.Sprintf("%s/action/star", repoLink)
req := NewRequestWithValues(t, "POST", link, map[string]string{
"_csrf": GetCSRF(t, session, repoLink),
})
assert.False(t, repo1InboxReceivedLike)
session.MakeRequest(t, req, http.StatusOK)
assert.True(t, repo1InboxReceivedLike)
})
} }