From 508b4deac8e4e974bc96678fd5b8e45ac2255d4d Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 22 Mar 2024 08:37:06 +0100 Subject: [PATCH] add test & fix compile --- models/repo/federated_repo.go | 8 ++++---- models/repo/federated_repo_test.go | 29 +++++++++++++++++++++++++++++ models/repo/repo.go | 3 ++- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 models/repo/federated_repo_test.go diff --git a/models/repo/federated_repo.go b/models/repo/federated_repo.go index 854c1915b6..d2f25d2a17 100644 --- a/models/repo/federated_repo.go +++ b/models/repo/federated_repo.go @@ -10,14 +10,14 @@ import ( // FederatedRepo represents a federated Repository Actor connected with a local Repo type FederatedRepo struct { ID int64 `xorm:"pk autoincr"` - RepositoryID int64 `xorm:"NOT NULL"` + RepoID int64 `xorm:"NOT NULL"` ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"` } -func NewFederatedRepo(repositoryID int64, externalID string, federationHostID int64) (FederatedRepo, error) { +func NewFederatedRepo(repoID int64, externalID string, federationHostID int64) (FederatedRepo, error) { result := FederatedRepo{ - RepositoryID: repositoryID, + RepoID: repoID, ExternalID: externalID, FederationHostID: federationHostID, } @@ -29,7 +29,7 @@ func NewFederatedRepo(repositoryID int64, externalID string, federationHostID in func (user FederatedRepo) Validate() []string { var result []string - result = append(result, validation.ValidateNotEmpty(user.RepositoryID, "UserID")...) + result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...) result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...) return result diff --git a/models/repo/federated_repo_test.go b/models/repo/federated_repo_test.go new file mode 100644 index 0000000000..075061044a --- /dev/null +++ b/models/repo/federated_repo_test.go @@ -0,0 +1,29 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "testing" + + "code.gitea.io/gitea/modules/validation" +) + +func Test_FederatedRepoValidation(t *testing.T) { + sut := FederatedRepo{ + RepoID: 12, + ExternalID: "12", + FederationHostID: 1, + } + if res, err := validation.IsValid(sut); !res { + t.Errorf("sut should be valid but was %q", err) + } + + sut = FederatedRepo{ + ExternalID: "12", + FederationHostID: 1, + } + if res, _ := validation.IsValid(sut); res { + t.Errorf("sut should be invalid") + } +} diff --git a/models/repo/repo.go b/models/repo/repo.go index da10b315f1..037a79d504 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -350,7 +350,8 @@ func (repo *Repository) APIURL() string { // TODO: At least camel case? // TODO: Mv federation related stuff to federated_repo func (repo *Repository) APAPIURL() string { - return setting.AppURL + "api/v1/activitypub/repository-id/" + url.PathEscape(string(repo.ID)) + // TODO: use spintf instead of concat - might mitigate injections + return fmt.Sprintf("%vapi/v1/activitypub/repository-id/%v", setting.AppURL, url.PathEscape(fmt.Sprint(repo.ID))) } // GetCommitsCountCacheKey returns cache key used for commits count caching.