2024-01-01 19:05:20 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2024-01-06 13:47:47 +00:00
|
|
|
"errors"
|
2024-02-03 14:18:37 +00:00
|
|
|
"strings"
|
2024-01-01 19:05:20 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
"git.polynom.me/rio/internal/context"
|
2024-02-03 14:18:37 +00:00
|
|
|
"git.polynom.me/rio/internal/gitea"
|
2024-02-03 14:51:23 +00:00
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-01-01 19:05:20 +00:00
|
|
|
)
|
|
|
|
|
2024-02-03 14:18:37 +00:00
|
|
|
func TestHeaderFilter(t *testing.T) {
|
|
|
|
map1 := filterHeaders(
|
2024-02-03 15:42:08 +00:00
|
|
|
map[string]interface{}{
|
2024-02-03 14:18:37 +00:00
|
|
|
"Content-Type": "hallo",
|
|
|
|
"content-Type": "welt",
|
|
|
|
"content-type": "uwu",
|
|
|
|
"CONTENT-TYPE": "lol",
|
|
|
|
"Content-Security-Policy": "none",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(map1) != 1 {
|
|
|
|
t.Fatalf("filterHeaders allowed %d != 1 headers", len(map1))
|
|
|
|
}
|
|
|
|
|
|
|
|
for key := range map1 {
|
|
|
|
if strings.ToLower(key) == "content-type" {
|
|
|
|
t.Fatalf("filterHeaders allowed Content-Type")
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickingCorrectRepositoryDefault(t *testing.T) {
|
|
|
|
// Test that we default to the <username>.<pages domain> repository, if we have only
|
|
|
|
// one path component.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username != "example-user" {
|
|
|
|
t.Fatalf("Called with unknown user %s", username)
|
|
|
|
}
|
|
|
|
if repositoryName != "example-user.pages.example.org" {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
|
|
|
}
|
|
|
|
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
|
|
|
t.Fatal("getFile called")
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupCNAME called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupRepoTXT called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "index.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occured: %v", err)
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
t.Fatal("Result is nil")
|
|
|
|
}
|
|
|
|
if path != "index.html" {
|
|
|
|
t.Fatalf("Returned path is invalid: %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickingCorrectRepositoryDefaultSubdirectory(t *testing.T) {
|
|
|
|
// Test that we return the default repository when the first path component does
|
|
|
|
// not correspong to an existing repository.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username != "example-user" {
|
|
|
|
t.Fatalf("Called with unknown user %s", username)
|
|
|
|
}
|
|
|
|
if repositoryName == "assets" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, errors.New("gitea.Repository does not exist")
|
2024-01-06 13:47:47 +00:00
|
|
|
} else if repositoryName == "example-user.pages.example.org" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
} else {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
|
|
|
t.Fatal("getFile called")
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupCNAME called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupRepoTXT called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "assets/index.css", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occured: %v", err)
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
t.Fatal("Result is nil")
|
|
|
|
}
|
|
|
|
if path != "assets/index.css" {
|
|
|
|
t.Fatalf("Returned path is invalid: %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickingCorrectRepositorySubdirectoryNoPagesBranch(t *testing.T) {
|
|
|
|
// Test that we're picking the correct repository when the first path component
|
|
|
|
// returns a repository without a pages branch.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username != "example-user" {
|
|
|
|
t.Fatalf("Called with unknown user %s", username)
|
|
|
|
}
|
|
|
|
if repositoryName == "blog" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 13:47:47 +00:00
|
|
|
Name: "blog",
|
|
|
|
}, nil
|
|
|
|
} else if repositoryName == "example-user.pages.example.org" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 13:47:47 +00:00
|
|
|
Name: "example-user.pages.example.org",
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
|
|
|
t.Fatal("getFile called")
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupCNAME called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
|
|
|
t.Fatal("LookupRepoTXT called")
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
res, path, err := RepoFromPath("example-user", "example-user.pages.example.org", "", "blog/post1.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occured: %v", err)
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
t.Fatal("Result is nil")
|
|
|
|
}
|
|
|
|
if res.Name != "example-user.pages.example.org" {
|
|
|
|
t.Fatalf("Invalid repository selected: %s", res.Name)
|
|
|
|
}
|
|
|
|
if path != "blog/post1.html" {
|
|
|
|
t.Fatalf("Returned path is invalid: %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickingNoRepositoryInvalidCNAME(t *testing.T) {
|
|
|
|
// Test that we're not picking a repository if the CNAME validation fails.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 13:47:47 +00:00
|
|
|
Name: "example-user.pages.example.org",
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
2024-01-01 19:05:20 +00:00
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
2024-02-03 14:51:23 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"some-other-domain.local\"}"), true, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
_, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err == nil {
|
2024-02-03 14:18:37 +00:00
|
|
|
t.Fatal("gitea.Repository returned even though CNAME validation should fail")
|
2024-01-01 19:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
func TestPickingRepositoryValidCNAME(t *testing.T) {
|
|
|
|
// Test that we're picking a repository, given a CNAME, if the CNAME validation succeeds.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 20:30:44 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.local" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 20:30:44 +00:00
|
|
|
Name: "example-user.local",
|
2024-01-06 13:47:47 +00:00
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 20:30:44 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.local" && branchName == "pages" {
|
2024-01-06 13:47:47 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
2024-02-03 14:51:23 +00:00
|
|
|
if username == "example-user" && repositoryName == "example-user.local" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error returned: %v", err)
|
|
|
|
}
|
2024-01-06 20:30:44 +00:00
|
|
|
if repo.Name != "example-user.local" {
|
2024-01-06 13:47:47 +00:00
|
|
|
t.Fatalf("Invalid repository name returned: %s", repo.Name)
|
2024-01-01 19:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
func TestPickingRepositoryValidCNAMEWithTXTLookup(t *testing.T) {
|
|
|
|
// Test that we're picking a repository, given a CNAME, if the CNAME validation succeeds
|
|
|
|
// and the TXT lookup returns something different.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 13:47:47 +00:00
|
|
|
Name: "some-different-repository",
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Called with unknown repository %s", repositoryName)
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
2024-02-03 14:51:23 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if domain == "example-user.local" {
|
|
|
|
return "some-different-repository", nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error returned: %v", err)
|
|
|
|
}
|
|
|
|
if repo.Name != "some-different-repository" {
|
|
|
|
t.Fatalf("Invalid repository name returned: %s", repo.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPickingRepositoryValidCNAMEWithTXTLookupAndSubdirectory(t *testing.T) {
|
|
|
|
// Test that we're picking a repository, given a CNAME, if the CNAME validation succeeds
|
|
|
|
// and the TXT lookup returns something different. Additionally, we now have a subdirectory
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2024-02-03 14:18:37 +00:00
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" {
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{
|
2024-01-06 13:47:47 +00:00
|
|
|
Name: "some-different-repository",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-03 14:18:37 +00:00
|
|
|
return gitea.Repository{}, errors.New("Unknown repository")
|
2024-01-06 13:47:47 +00:00
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
2024-01-06 13:47:47 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
2024-02-03 14:51:23 +00:00
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil
|
2024-01-06 13:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
2024-02-03 14:18:37 +00:00
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
2024-01-06 13:47:47 +00:00
|
|
|
if domain == "example-user.local" {
|
|
|
|
return "some-different-repository", nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
2024-02-03 14:51:23 +00:00
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2024-02-03 14:51:23 +00:00
|
|
|
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "blog/index.html", ctx)
|
2024-01-06 13:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error returned: %v", err)
|
|
|
|
}
|
|
|
|
if repo.Name != "some-different-repository" {
|
|
|
|
t.Fatalf("Invalid repository name returned: %s", repo.Name)
|
2024-01-01 19:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-03 15:42:08 +00:00
|
|
|
|
|
|
|
func TestHeaderParsingEmpty(t *testing.T) {
|
|
|
|
// Test that we are correctly handling a repository with no headers.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" {
|
|
|
|
return gitea.Repository{
|
|
|
|
Name: "some-different-repository",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gitea.Repository{}, errors.New("Unknown repository")
|
|
|
|
},
|
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"example-user.local\"}"), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
|
|
|
if domain == "example-user.local" {
|
|
|
|
return "some-different-repository", nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
info := GetRepositoryInformation("example-user", "some-different-repository", ctx)
|
|
|
|
if info == nil {
|
|
|
|
t.Fatalf("No repository information returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.Headers) > 0 {
|
|
|
|
t.Fatalf("Headers returned: %v", info.Headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeaderParsing(t *testing.T) {
|
|
|
|
// Test that we are correctly handling a repository with no headers.
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
client := gitea.GiteaClient{
|
|
|
|
GetRepository: func(username, repositoryName string) (gitea.Repository, error) {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" {
|
|
|
|
return gitea.Repository{
|
|
|
|
Name: "some-different-repository",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gitea.Repository{}, errors.New("Unknown repository")
|
|
|
|
},
|
|
|
|
HasBranch: func(username, repositoryName, branchName string) bool {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branchName == "pages" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
|
|
|
|
if username == "example-user" && repositoryName == "some-different-repository" && branch == "pages" && path == "rio.json" {
|
|
|
|
return []byte("{\"CNAME\": \"example-user.local\", \"headers\": {\"X-Cool-Header\": \"Very nice!\"}}"), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatalf("Invalid file requested: %s/%s@%s:%s", username, repositoryName, branch, path)
|
|
|
|
return []byte{}, true, nil
|
|
|
|
},
|
|
|
|
LookupCNAME: func(domain string) (string, error) {
|
|
|
|
return "", errors.New("No CNAME")
|
|
|
|
},
|
|
|
|
LookupRepoTXT: func(domain string) (string, error) {
|
|
|
|
if domain == "example-user.local" {
|
|
|
|
return "some-different-repository", nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx := &context.GlobalContext{
|
|
|
|
Gitea: &client,
|
|
|
|
Cache: &context.CacheContext{
|
|
|
|
RepositoryInformationCache: context.MakeRepoInfoCache(),
|
|
|
|
RepositoryPathCache: context.MakeRepoPathCache(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
info := GetRepositoryInformation("example-user", "some-different-repository", ctx)
|
|
|
|
if info == nil {
|
|
|
|
t.Fatalf("No repository information returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(info.Headers) != 1 {
|
|
|
|
t.Fatalf("len(info.Headers) != 1: %v", info.Headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
header, found := info.Headers["X-Cool-Header"]
|
|
|
|
if !found {
|
|
|
|
t.Fatal("Header X-Cool-Header not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if header != "Very nice!" {
|
|
|
|
t.Fatalf("Invalid header value for X-Cool-Header: \"%s\"", header)
|
|
|
|
}
|
|
|
|
}
|