package cibridge
import (
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
)
// A repository's worker token reaches its own pool's task types and no
// other repository's. The engine side is modelled as dagnats judges a
// token: the poll's group must be one of the token's groups, and each
// task type the token's prefix or a dot-child of it.
func TestAPoolPollIsTheRepositorysOwnAndNoOthers(t *testing.T) {
tokens := map[string][]string{
"PI": {"pi", "pi__c"},
"PIC": {"pi-c", "pi-c__linux"},
}
var groups []string
engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct {
Group string `json:"worker_group"`
Types []string `json:"task_types"`
}
json.NewDecoder(r.Body).Decode(&body)
own := tokens[strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")]
ok := slices.Contains(own, body.Group)
for _, tt := range body.Types {
ok = ok && slices.ContainsFunc(own, func(p string) bool { return tt == p || strings.HasPrefix(tt, p+".") })
}
if !ok {
http.Error(w, "scoped elsewhere", http.StatusForbidden)
return
}
groups = append(groups, body.Group)
w.Write([]byte(`[]`))
}))
defer engine.Close()
h, err := Handler(engine.URL, "ADMIN", func(repo string) string { return repo },
func(repo string) bool { return repo == "pi" || repo == "pi-c" }, nil, nil)
if err != nil {
t.Fatal(err)
}
front := httptest.NewServer(h)
defer front.Close()
poll := func(repo, tok, body string) int {
t.Helper()
req, _ := http.NewRequest("POST", front.URL+"/r/"+repo+"/ci/bridge/v1/tasks/poll", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+tok)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return resp.StatusCode
}
for _, c := range []struct {
name, repo, tok, body string
want int
group string
}{
{"own pool by task type", "pi", "PI", `{"task_types":["pi__c.job"]}`, 200, "pi__c"},
{"own pool by group", "pi", "PI", `{"task_types":["pi__c.job"],"worker_group":"pi__c"}`, 200, "pi__c"},
{"the default stays default", "pi", "PI", `{"task_types":["pi.job"]}`, 200, "pi"},
{"pi's pool c is not pi-c's default jobs", "pi", "PI", `{"task_types":["pi-c.job"]}`, 403, ""},
{"pi cannot name pi-c's group", "pi", "PI", `{"task_types":["pi-c.job"],"worker_group":"pi-c"}`, 400, ""},
{"pi cannot name pi-c's pool group", "pi", "PI", `{"task_types":["pi-c__linux.job"],"worker_group":"pi-c__linux"}`, 400, ""},
{"pi cannot poll pi-c's pool task", "pi", "PI", `{"task_types":["pi-c__linux.job"]}`, 400, ""},
{"pi's token on pi-c's bridge", "pi-c", "PI", `{"task_types":["pi-c.job"]}`, 403, ""},
{"pi's token on pi-c's bridge, its own pool", "pi-c", "PI", `{"task_types":["pi__c.job"]}`, 400, ""},
{"pi-c's own pool", "pi-c", "PIC", `{"task_types":["pi-c__linux.job"]}`, 200, "pi-c__linux"},
{"a malformed pool name", "pi", "PI", `{"task_types":["pi__C.job"],"worker_group":"pi__C"}`, 400, ""},
{"a nested separator", "pi", "PI", `{"task_types":["pi__c__d.job"]}`, 400, ""},
} {
groups = nil
if got := poll(c.repo, c.tok, c.body); got != c.want {
t.Errorf("FAIL: %s: status %d, want %d", c.name, got, c.want)
}
if c.group != "" && !slices.Equal(groups, []string{c.group}) {
t.Errorf("FAIL: %s: engine saw groups %v, want %q", c.name, groups, c.group)
}
}
if !t.Failed() {
t.Log("ok")
}
}