Files
urapt/server/store/store_test.go
T
owen 981587e83d Initial release: self-hostable APT repository server and CLI
urapt is a self-hostable APT repository server with a companion CLI for
pushing and managing Debian .deb packages.

Server (urapt-server):
- REST API + APT endpoint, SQLite storage (pure-Go modernc driver, no CGO)
- .deb files stored content-addressed on disk, reference-counted for dedup
- Server-managed RSA-4096 OpenPGP signing key (ProtonMail/go-crypto)
- APT indices (Release/InRelease/Packages[.gz/.xz]) generated on demand
  from the DB, cached in memory, signed with the server key
- Full APT model: repositories -> distributions -> components -> architectures
- Bearer-token auth for REST; HTTP Basic auth for private-repo APT reads
- First registrant becomes admin; repo-scoped permissions
  (read/write/read-write/admin) plus owner and server-admin roles
- Multipart package push with control-field extraction, list/show/delete,
  pool serving, blob ref-count cleanup
- Audit log

CLI (urapt):
- register/login/logout/whoami, token management
- repo/distro/component/arch CRUD, member management
- push/pull/ls/show/rm for packages
- apt-config helper that emits apt setup commands (key, sources.list,
  auth.conf for private repos)

Packaging & docs:
- Dockerfile (multi-stage distroless), docker-compose.yml, sample config
- README quick start, architecture overview, config reference, security notes
- PLAN.md design blueprint, CHANGELOG.md, GPL-3.0 LICENSE
- GitHub Actions CI (test, lint, cross-build for linux/darwin amd64/arm64)
- Makefile release target producing static binaries + tarballs + checksums

Tests cover the data-access layer, auth/permission checks, APT index
generation, .deb parsing, GPG signing, the REST API, and the typed API
client. Verified end-to-end on a Raspberry Pi (arm64) pushing and installing
a real package.
2026-06-28 16:57:34 -05:00

88 lines
2.6 KiB
Go

package store
import (
"context"
"path/filepath"
"testing"
"urapt/shared/crypto"
"urapt/shared/db"
"urapt/shared/models"
)
// newTestStore opens a fresh migrated SQLite database in a per-test temp
// directory and returns a Store over it. The database is closed automatically
// when the test finishes.
func newTestStore(t *testing.T) *Store {
t.Helper()
dir := t.TempDir()
database, err := db.Open(filepath.Join(dir, "test.db"))
if err != nil {
t.Fatalf("db open: %v", err)
}
t.Cleanup(func() { database.Close() })
return New(database)
}
// createUser is a test helper that inserts a user with a bcrypt-hashed
// password and returns it. The password is hashed for realism so that
// password-verification paths can be exercised if needed.
func (s *Store) createUser(t *testing.T, ctx context.Context, username, password string) *models.User {
t.Helper()
hash, err := crypto.HashPassword(password)
if err != nil {
t.Fatalf("hash password: %v", err)
}
u, admin, err := s.CreateUser(ctx, username, hash)
if err != nil {
t.Fatalf("create user %q: %v", username, err)
}
t.Logf("created user %q (admin=%v)", u.Username, admin)
return u
}
// createToken is a test helper that mints a real API token (with hash and
// prefix) for userID and returns the plaintext token plus the stored row.
func (s *Store) createToken(t *testing.T, ctx context.Context, userID, name string) (string, *models.APIToken) {
t.Helper()
tok, hash, prefix, err := crypto.GenerateToken()
if err != nil {
t.Fatalf("generate token: %v", err)
}
stored, err := s.CreateToken(ctx, userID, name, prefix, hash)
if err != nil {
t.Fatalf("create token: %v", err)
}
return tok, stored
}
// createRepo is a test helper that creates a repository owned by userID.
func (s *Store) createRepo(t *testing.T, ctx context.Context, name, ownerID string, vis models.Visibility) *models.Repository {
t.Helper()
r, err := s.CreateRepository(ctx, name, ownerID, vis, "")
if err != nil {
t.Fatalf("create repo %q: %v", name, err)
}
return r
}
// createDistro is a test helper that creates a distribution within repoID.
func (s *Store) createDistro(t *testing.T, ctx context.Context, repoID, name string) *models.Distribution {
t.Helper()
d, err := s.CreateDistribution(ctx, repoID, name)
if err != nil {
t.Fatalf("create distro %q: %v", name, err)
}
return d
}
// createComponent is a test helper that creates a component within distroID.
func (s *Store) createComponent(t *testing.T, ctx context.Context, distroID, name string) *models.Component {
t.Helper()
c, err := s.CreateComponent(ctx, distroID, name)
if err != nil {
t.Fatalf("create component %q: %v", name, err)
}
return c
}