Files
mcr/internal/grpcserver/registry_test.go
Kyle Isom d5580f01f2 Migrate module path from kyle/ to mc/ org
All import paths updated to git.wntrmute.dev/mc/. Bumps mcdsl to v1.2.0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 02:05:59 -07:00

201 lines
5.2 KiB
Go

package grpcserver
import (
"context"
"net/http/httptest"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
mcdslauth "git.wntrmute.dev/mc/mcdsl/auth"
pb "git.wntrmute.dev/mc/mcr/gen/mcr/v1"
)
// testMCIAS is a package-level variable set by adminDeps for reuse.
var testMCIASSrv *httptest.Server
// adminDeps returns Deps with an admin-capable authenticator and a fresh DB.
func adminDeps(t *testing.T) Deps {
t.Helper()
mcias := mockMCIAS(t)
testMCIASSrv = mcias
auth := testAuthenticator(t, mcias.URL)
return Deps{
DB: openTestDB(t),
Authenticator: auth,
}
}
// adminCtx returns a context with an admin bearer token.
func adminCtx() context.Context {
return withAuth(context.Background(), "admin-token")
}
// userCtx returns a context with a regular user bearer token.
func userCtx() context.Context {
return withAuth(context.Background(), "user-token")
}
// adminDepsWithAuthenticator returns Deps using the given authenticator.
func adminDepsWithAuthenticator(t *testing.T, auth *mcdslauth.Authenticator) Deps {
t.Helper()
return Deps{
DB: openTestDB(t),
Authenticator: auth,
}
}
func TestListRepositoriesEmpty(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
resp, err := client.ListRepositories(adminCtx(), &pb.ListRepositoriesRequest{})
if err != nil {
t.Fatalf("ListRepositories: %v", err)
}
if len(resp.GetRepositories()) != 0 {
t.Fatalf("expected 0 repos, got %d", len(resp.Repositories))
}
}
func TestGetRepositoryNotFound(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
_, err := client.GetRepository(adminCtx(), &pb.GetRepositoryRequest{Name: "nonexistent"})
if err == nil {
t.Fatal("expected error for nonexistent repo")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("expected gRPC status, got %v", err)
}
if st.Code() != codes.NotFound {
t.Fatalf("code: got %v, want NotFound", st.Code())
}
}
func TestGetRepositoryEmptyName(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
_, err := client.GetRepository(adminCtx(), &pb.GetRepositoryRequest{})
if err == nil {
t.Fatal("expected error for empty name")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("expected gRPC status, got %v", err)
}
if st.Code() != codes.InvalidArgument {
t.Fatalf("code: got %v, want InvalidArgument", st.Code())
}
}
func TestDeleteRepositoryNotFound(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
_, err := client.DeleteRepository(adminCtx(), &pb.DeleteRepositoryRequest{Name: "nonexistent"})
if err == nil {
t.Fatal("expected error for nonexistent repo")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("expected gRPC status, got %v", err)
}
if st.Code() != codes.NotFound {
t.Fatalf("code: got %v, want NotFound", st.Code())
}
}
func TestDeleteRepositoryEmptyName(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
_, err := client.DeleteRepository(adminCtx(), &pb.DeleteRepositoryRequest{})
if err == nil {
t.Fatal("expected error for empty name")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("expected gRPC status, got %v", err)
}
if st.Code() != codes.InvalidArgument {
t.Fatalf("code: got %v, want InvalidArgument", st.Code())
}
}
func TestGCStatusInitial(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
resp, err := client.GetGCStatus(adminCtx(), &pb.GetGCStatusRequest{})
if err != nil {
t.Fatalf("GetGCStatus: %v", err)
}
if resp.Running {
t.Fatal("expected running=false on startup")
}
if resp.LastRun != nil {
t.Fatal("expected no last_run on startup")
}
}
func TestGarbageCollectTrigger(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
// Trigger GC without a collector (no-op but should return an ID).
resp, err := client.GarbageCollect(adminCtx(), &pb.GarbageCollectRequest{})
if err != nil {
t.Fatalf("GarbageCollect: %v", err)
}
if resp.GetId() == "" {
t.Fatal("expected non-empty GC ID")
}
}
func TestListRepositoriesRequiresAuth(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
// No auth token -- should be unauthenticated.
_, err := client.ListRepositories(context.Background(), &pb.ListRepositoriesRequest{})
if err == nil {
t.Fatal("expected error for unauthenticated request")
}
st, ok := status.FromError(err)
if !ok {
t.Fatalf("expected gRPC status, got %v", err)
}
if st.Code() != codes.Unauthenticated {
t.Fatalf("code: got %v, want Unauthenticated", st.Code())
}
}
func TestListRepositoriesAllowsRegularUser(t *testing.T) {
deps := adminDeps(t)
cc := startTestServer(t, deps)
client := pb.NewRegistryServiceClient(cc)
// Regular user should be able to list repositories (auth-required, not admin-required).
resp, err := client.ListRepositories(userCtx(), &pb.ListRepositoriesRequest{})
if err != nil {
t.Fatalf("ListRepositories: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
}