From 1ad7bdf98367c9dc448f5299f81b18e78d3b344c Mon Sep 17 00:00:00 2001 From: Clemens Date: Thu, 28 Mar 2024 09:36:08 +0100 Subject: [PATCH] fixed tests part1, one test still failing --- models/forgefed/activity_test.go | 10 +++++----- models/forgefed/actor_test.go | 10 +++++----- models/forgefed/nodeinfo_test.go | 2 +- models/repo/repo_test.go | 9 +++++++++ models/repo/star.go | 13 +++++++------ models/user/user.go | 2 +- models/user/user_test.go | 9 +++++++++ modules/forgefed/federation_service.go | 2 +- modules/validation/validatable.go | 2 +- routers/api/v1/user/star.go | 4 ++-- routers/web/repo/repo.go | 4 ++-- 11 files changed, 43 insertions(+), 24 deletions(-) diff --git a/models/forgefed/activity_test.go b/models/forgefed/activity_test.go index 4819b5fa6c..3cfe230d62 100644 --- a/models/forgefed/activity_test.go +++ b/models/forgefed/activity_test.go @@ -110,7 +110,7 @@ func Test_StarUnmarshalJSON(t *testing.T) { } } -func TestAcivityValidation(t *testing.T) { +func TestActivityValidation(t *testing.T) { sut := new(ForgeLike) sut.UnmarshalJSON([]byte(`{"type":"Like", "actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1", @@ -123,16 +123,16 @@ func TestAcivityValidation(t *testing.T) { sut.UnmarshalJSON([]byte(`{"actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1", "object":"https://codeberg.org/api/activitypub/repository-id/1", "startTime": "2014-12-31T23:00:00-08:00"}`)) - if sut.Validate()[0] != "Field type may not be empty" { - t.Errorf("validation error expected but was: %v\n", sut.Validate()) + if sut.Validate()[0] != "Field type should not be empty" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) } sut.UnmarshalJSON([]byte(`{"type":"bad-type", "actor":"https://repo.prod.meissa.de/api/activitypub/user-id/1", "object":"https://codeberg.org/api/activitypub/repository-id/1", "startTime": "2014-12-31T23:00:00-08:00"}`)) - if sut.Validate()[0] != "Value bad-type is not contained in allowed values [[Like]]" { - t.Errorf("validation error expected but was: %v\n", sut.Validate()) + if sut.Validate()[0] != "Value bad-type is not contained in allowed values [Like]" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) } sut.UnmarshalJSON([]byte(`{"type":"Like", diff --git a/models/forgefed/actor_test.go b/models/forgefed/actor_test.go index 0995a48c00..40e2a25f4d 100644 --- a/models/forgefed/actor_test.go +++ b/models/forgefed/actor_test.go @@ -66,7 +66,7 @@ func TestActorIdValidation(t *testing.T) { sut.Host = "an.other.host" sut.Port = "" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/" - if sut.Validate()[0] != "Field userId may not be empty" { + if sut.Validate()[0] != "Field userId should not be empty" { t.Errorf("validation error expected but was: %v\n", sut.Validate()) } @@ -78,8 +78,8 @@ func TestActorIdValidation(t *testing.T) { sut.Host = "an.other.host" sut.Port = "" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1?illegal=action" - if sut.Validate()[0] != "not all input: \"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" was parsed: \"https://an.other.host/api/v1/activitypub/user-id/1\"" { - t.Errorf("validation error expected but was: %v\n", sut.Validate()) + if sut.Validate()[0] != "not all input was parsed, \nUnvalidated Input:\"https://an.other.host/api/v1/activitypub/user-id/1?illegal=action\" \nParsed URI: \"https://an.other.host/api/v1/activitypub/user-id/1\"" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) } } @@ -104,8 +104,8 @@ func TestPersonIdValidation(t *testing.T) { sut.Host = "an.other.host" sut.Port = "" sut.UnvalidatedInput = "https://an.other.host/api/v1/activitypub/user-id/1" - if sut.Validate()[0] != "Value forgejox is not contained in allowed values [[forgejo gitea]]" { - t.Errorf("validation error expected but was: %v\n", sut.Validate()) + if sut.Validate()[0] != "Value forgejox is not contained in allowed values [forgejo gitea]" { + t.Errorf("validation error expected but was: %v\n", sut.Validate()[0]) } } diff --git a/models/forgefed/nodeinfo_test.go b/models/forgefed/nodeinfo_test.go index be31ace1cc..04384fe4f3 100644 --- a/models/forgefed/nodeinfo_test.go +++ b/models/forgefed/nodeinfo_test.go @@ -52,7 +52,7 @@ func Test_NodeInfoWellKnownValidate(t *testing.T) { } sut = NodeInfoWellKnown{Href: "./federated-repo.prod.meissa.de/api/v1/nodeinfo"} - if _, err := validation.IsValid(sut); err.Error() != "Href has to be absolute\nValue is not contained in allowed values [[http https]]" { + if _, err := validation.IsValid(sut); err.Error() != "Href has to be absolute\nValue is not contained in allowed values [http https]" { t.Errorf("validation error expected but was: %v\n", err) } diff --git a/models/repo/repo_test.go b/models/repo/repo_test.go index ca9209d751..e4b9618e35 100644 --- a/models/repo/repo_test.go +++ b/models/repo/repo_test.go @@ -217,3 +217,12 @@ func TestComposeSSHCloneURL(t *testing.T) { setting.SSH.Port = 123 assert.Equal(t, "ssh://git@[::1]:123/user/repo.git", repo_model.ComposeSSHCloneURL("user", "repo")) } + +func TestAPAPIURL(t *testing.T) { + repo := repo_model.Repository{ID: 1} + url := repo.APAPIURL() + expected := "https://try.gitea.io/api/v1/activitypub/repository-id/1" + if url != expected { + t.Errorf("unexpected APAPIURL, expected: %q, actual: %q", expected, url) + } +} diff --git a/models/repo/star.go b/models/repo/star.go index c120eb9882..61df584173 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -11,7 +11,8 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/forgefed" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/activitypub" + + //"code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -94,10 +95,10 @@ func sendLikeActivities(ctx context.Context, doer user_model.User, repoID int64) return err } - apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL()) + /*apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL()) if err != nil { return err - } + }*/ for _, federatedRepo := range federatedRepos { target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo @@ -107,16 +108,16 @@ func sendLikeActivities(ctx context.Context, doer user_model.User, repoID int64) return err } log.Info("Like Activity: %v", likeActivity) - json, err := likeActivity.MarshalJSON() + /*json, err := likeActivity.MarshalJSON() if err != nil { return err - } + }*/ // TODO: decouple loading & creating activities from sending them - use two loops. // TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories // TODO: Check if we need to respect rate limits // ToDo: Change this to the standalone table of FederatedRepos - apclient.Post([]byte(json), target) + //apclient.Post([]byte(json), target) } return nil diff --git a/models/user/user.go b/models/user/user.go index 3e47bfccc8..1c305fff8e 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -303,7 +303,7 @@ func (u *User) HTMLURL() string { // APAPIURL returns the IRI to the api endpoint of the user func (u *User) APAPIURL() string { - return setting.AppURL + url.PathEscape("api/v1/activitypub/user-id/") + url.PathEscape(string(u.ID)) + return fmt.Sprintf("%vapi/v1/activitypub/user-id/%v", setting.AppURL, url.PathEscape(fmt.Sprintf("%v", u.ID))) } // OrganisationLink returns the organization sub page link. diff --git a/models/user/user_test.go b/models/user/user_test.go index 411d1ab830..5105845394 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -60,6 +60,15 @@ func TestCanCreateOrganization(t *testing.T) { assert.False(t, user.CanCreateOrganization()) } +func TestAPAPIURL(t *testing.T) { + user := user_model.User{ID: 1} + url := user.APAPIURL() + expected := "https://try.gitea.io/api/v1/activitypub/user-id/1" + if url != expected { + t.Errorf("unexpected APAPIURL, expected: %q, actual: %q", expected, url) + } +} + func TestSearchUsers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(opts *user_model.SearchUserOptions, expectedUserOrOrgIDs []int64) { diff --git a/modules/forgefed/federation_service.go b/modules/forgefed/federation_service.go index 843a3f7cbd..1751d6958e 100644 --- a/modules/forgefed/federation_service.go +++ b/modules/forgefed/federation_service.go @@ -82,7 +82,7 @@ func LikeActivity(ctx context.Context, form any, repositoryID int64) (int, strin // execute the activity if the repo was not stared already alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID) if !alreadyStared { - err = repo.StarRepo(ctx, user, repositoryID, true) + err = repo.StarRepo(ctx, *user, repositoryID, true) if err != nil { return http.StatusNotAcceptable, "Error staring", err } diff --git a/modules/validation/validatable.go b/modules/validation/validatable.go index 405450aa1c..1e9de3929f 100644 --- a/modules/validation/validatable.go +++ b/modules/validation/validatable.go @@ -62,7 +62,7 @@ func ValidateOneOf(value any, allowed []any) []string { return []string{} } } - return []string{fmt.Sprintf("Value %v is not contained in allowed values [%v]", value, allowed)} + return []string{fmt.Sprintf("Value %v is not contained in allowed values %v", value, allowed)} } func ValidateSuffix(str, suffix string) bool { diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index d5b36d8131..e0131dfe28 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -157,7 +157,7 @@ func Star(ctx *context.APIContext) { // "$ref": "#/responses/notFound" // TODO: why is this *context.APIContext passed, where a context.Context is expected? - err := repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository.ID, true) + err := repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return @@ -188,7 +188,7 @@ func Unstar(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) + err := repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index bede21be17..fbc267a3cb 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -316,9 +316,9 @@ func Action(ctx *context.Context) { case "unwatch": err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) case "star": - err = repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true) + err = repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) case "unstar": - err = repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) + err = repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) case "accept_transfer": err = acceptOrRejectRepoTransfer(ctx, true) case "reject_transfer":