Files
urapt/server/restapi/repos_test.go
T
owen c5e64cf63f
CI / Test (linux/amd64) (push) Waiting to run
CI / Lint (push) Waiting to run
CI / Cross-build (darwin/amd64) (push) Waiting to run
CI / Cross-build (linux/amd64) (push) Waiting to run
CI / Cross-build (darwin/arm64) (push) Waiting to run
CI / Cross-build (linux/arm64) (push) Waiting to run
Fix lint errors and bump golangci-lint to v2/action v9
- Migrate .golangci.yml to golangci-lint v2 config format
- Bump golangci-lint-action from v6 (Node 20, deprecated) to v9 (Node 24)
- Fix errcheck: check fmt.Fscanln and fmt.Sscanf return values
- Fix ineffassign: replace unused vars with _ in test files
- Fix revive unused-parameter: rename unused cmd/args/r/c params to _
- Fix revive empty-block: invert if condition in push command
- Fix revive redefines-builtin-id: rename 'clear' to 'clearsigned'
- Fix revive blank-imports: add comment on modernc.org/sqlite import
- Fix unused: remove unused 'suite' and 'gen' fields from cache entry
2026-06-28 17:07:50 -05:00

123 lines
4.3 KiB
Go

package restapi
import (
"encoding/json"
"testing"
)
func TestRepoCreateAndPermissions(t *testing.T) {
h := newHarness(t)
// alice (owner/admin), bob (non-admin)
_, body := h.do("POST", "/api/v1/auth/register", "", map[string]string{"username": "alice", "password": "supersecret"})
var alice struct {
User map[string]any `json:"user"`
Token string `json:"token"`
}
json.Unmarshal(body, &alice)
_, body = h.do("POST", "/api/v1/auth/register", "", map[string]string{"username": "bob", "password": "supersecret"})
var bob struct {
User map[string]any `json:"user"`
Token string `json:"token"`
}
json.Unmarshal(body, &bob)
// alice creates a private repo
code, body := h.do("POST", "/api/v1/repositories", alice.Token, map[string]any{
"name": "myrepo", "visibility": "private", "description": "test",
})
if code != 201 {
t.Fatalf("create repo status %d body %s", code, body)
}
// bob cannot see it (private, not a member)
code, _ = h.do("GET", "/api/v1/repositories/myrepo", bob.Token, nil)
if code != 403 {
t.Fatalf("bob should be forbidden from private repo, got %d", code)
}
// alice grants bob read access
code, _ = h.do("POST", "/api/v1/repositories/myrepo/members", alice.Token, map[string]string{
"username": "bob", "access": "read",
})
if code != 201 {
t.Fatalf("add member status %d", code)
}
// bob can now read
code, _ = h.do("GET", "/api/v1/repositories/myrepo", bob.Token, nil)
if code != 200 {
t.Fatalf("bob should read after grant, got %d", code)
}
// bob cannot write (read only)
code, _ = h.do("POST", "/api/v1/repositories/myrepo/distributions", bob.Token, map[string]string{"name": "stable"})
if code != 403 {
t.Fatalf("bob read-only should not write, got %d", code)
}
// alice upgrades bob to write
h.do("PATCH", "/api/v1/repositories/myrepo/members/bob", alice.Token, map[string]string{"access": "write"})
code, _ = h.do("POST", "/api/v1/repositories/myrepo/distributions", bob.Token, map[string]string{"name": "stable"})
if code != 201 {
t.Fatalf("bob with write should create distro, got %d", code)
}
// add component and arch
h.do("POST", "/api/v1/repositories/myrepo/distributions/stable/components", alice.Token, map[string]string{"name": "main"})
code, _ = h.do("POST", "/api/v1/repositories/myrepo/distributions/stable/architectures", alice.Token, map[string]string{"name": "amd64"})
if code != 201 {
t.Fatalf("add arch status %d", code)
}
// 'all' arch rejected
code, _ = h.do("POST", "/api/v1/repositories/myrepo/distributions/stable/architectures", alice.Token, map[string]string{"name": "all"})
if code != 400 {
t.Fatalf("all arch should be rejected, got %d", code)
}
// list distros/components/arches
code, _ = h.do("GET", "/api/v1/repositories/myrepo/distributions", alice.Token, nil)
if code != 200 {
t.Fatalf("list distros status %d", code)
}
code, _ = h.do("GET", "/api/v1/repositories/myrepo/distributions/stable/components", alice.Token, nil)
if code != 200 {
t.Fatalf("list components status %d", code)
}
// remove member
code, _ = h.do("DELETE", "/api/v1/repositories/myrepo/members/bob", alice.Token, nil)
if code != 204 {
t.Fatalf("remove member status %d", code)
}
code, _ = h.do("GET", "/api/v1/repositories/myrepo", bob.Token, nil)
if code != 403 {
t.Fatalf("bob should be forbidden after removal, got %d", code)
}
}
func TestPublicRepoReadableByAll(t *testing.T) {
h := newHarness(t)
_, body := h.do("POST", "/api/v1/auth/register", "", map[string]string{"username": "owner", "password": "supersecret"})
var owner struct {
Token string `json:"token"`
}
json.Unmarshal(body, &owner)
_, body = h.do("POST", "/api/v1/auth/register", "", map[string]string{"username": "stranger", "password": "supersecret"})
var stranger struct {
Token string `json:"token"`
}
json.Unmarshal(body, &stranger)
h.do("POST", "/api/v1/repositories", owner.Token, map[string]any{"name": "pubrepo", "visibility": "public"})
code, _ := h.do("GET", "/api/v1/repositories/pubrepo", stranger.Token, nil)
if code != 200 {
t.Fatalf("stranger should read public repo, got %d", code)
}
// but stranger cannot write
code, _ = h.do("POST", "/api/v1/repositories/pubrepo/distributions", stranger.Token, map[string]string{"name": "x"})
if code != 403 {
t.Fatalf("stranger should not write public repo, got %d", code)
}
}