test(backend): move tests to *_test pkg

This commit is contained in:
2026-01-03 16:07:38 +00:00
parent ceb4c9f23c
commit 0002affaff
9 changed files with 96 additions and 75 deletions

View File

@@ -1,4 +1,6 @@
.PHONY: build build-docs build-all run run-docs docs install-tools fmt clean .PHONY: build build-docs build-all run run-docs docs install-tools fmt test clean
ROOT_DIR := $(abspath $(CURDIR)/../..)
# Build the API server # Build the API server
build: build:
@@ -39,6 +41,10 @@ fmt:
go fmt ./... go fmt ./...
swag fmt swag fmt
test:
@mkdir -p $(ROOT_DIR)/.gocache $(ROOT_DIR)/.gomodcache
@GOCACHE=$(ROOT_DIR)/.gocache GOMODCACHE=$(ROOT_DIR)/.gomodcache go test ./...
# Clean build artifacts # Clean build artifacts
clean: clean:
rm -rf bin/ rm -rf bin/

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package drexa package drexa_test
import ( import (
"bytes" "bytes"
@@ -15,6 +15,7 @@ import (
"time" "time"
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/drexa"
"github.com/get-drexa/drexa/internal/organization" "github.com/get-drexa/drexa/internal/organization"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/google/uuid" "github.com/google/uuid"
@@ -42,19 +43,26 @@ func TestRegistrationFlow(t *testing.T) {
} }
t.Cleanup(func() { _ = os.RemoveAll(blobRoot) }) t.Cleanup(func() { _ = os.RemoveAll(blobRoot) })
s, err := NewServer(Config{ db := database.NewFromPostgres(postgresURL)
Server: ServerConfig{Port: 8080}, t.Cleanup(func() { _ = db.Close() })
Database: DatabaseConfig{
if err := database.RunMigrations(ctx, db); err != nil {
t.Fatalf("RunMigrations: %v", err)
}
s, err := drexa.NewServer(drexa.Config{
Server: drexa.ServerConfig{Port: 8080},
Database: drexa.DatabaseConfig{
PostgresURL: postgresURL, PostgresURL: postgresURL,
}, },
JWT: JWTConfig{ JWT: drexa.JWTConfig{
Issuer: "drexa-test", Issuer: "drexa-test",
Audience: "drexa-test", Audience: "drexa-test",
SecretKey: []byte("drexa-test-secret"), SecretKey: []byte("drexa-test-secret"),
}, },
Storage: StorageConfig{ Storage: drexa.StorageConfig{
Mode: StorageModeFlat, Mode: drexa.StorageModeFlat,
Backend: StorageBackendFS, Backend: drexa.StorageBackendFS,
RootPath: blobRoot, RootPath: blobRoot,
}, },
}) })
@@ -62,10 +70,6 @@ func TestRegistrationFlow(t *testing.T) {
t.Fatalf("NewServer: %v", err) t.Fatalf("NewServer: %v", err)
} }
if err := database.RunMigrations(ctx, s.db); err != nil {
t.Fatalf("RunMigrations: %v", err)
}
type registerResponse struct { type registerResponse struct {
Account struct { Account struct {
ID string `json:"id"` ID string `json:"id"`
@@ -94,7 +98,7 @@ func TestRegistrationFlow(t *testing.T) {
} }
var reg registerResponse var reg registerResponse
doJSON(t, s.app, http.MethodPost, "/api/accounts", "", registerBody, http.StatusOK, &reg) doJSON(t, s.App(), http.MethodPost, "/api/accounts", "", registerBody, http.StatusOK, &reg)
if reg.AccessToken == "" { if reg.AccessToken == "" {
t.Fatalf("expected access token in registration response") t.Fatalf("expected access token in registration response")
} }
@@ -129,7 +133,7 @@ func TestRegistrationFlow(t *testing.T) {
} }
var login loginResponse var login loginResponse
doJSON(t, s.app, http.MethodPost, "/api/auth/login", "", loginBody, http.StatusOK, &login) doJSON(t, s.App(), http.MethodPost, "/api/auth/login", "", loginBody, http.StatusOK, &login)
if login.AccessToken == "" { if login.AccessToken == "" {
t.Fatalf("expected access token in login response") t.Fatalf("expected access token in login response")
} }
@@ -151,7 +155,7 @@ func TestRegistrationFlow(t *testing.T) {
var drives []struct { var drives []struct {
ID string `json:"id"` ID string `json:"id"`
} }
doJSON(t, s.app, http.MethodGet, "/api/my/drives", reg.AccessToken, nil, http.StatusOK, &drives) doJSON(t, s.App(), http.MethodGet, "/api/my/drives", reg.AccessToken, nil, http.StatusOK, &drives)
if len(drives) != 1 { if len(drives) != 1 {
t.Fatalf("expected 1 drive, got %d", len(drives)) t.Fatalf("expected 1 drive, got %d", len(drives))
} }
@@ -166,7 +170,7 @@ func TestRegistrationFlow(t *testing.T) {
DisplayName string `json:"displayName"` DisplayName string `json:"displayName"`
Email string `json:"email"` Email string `json:"email"`
} }
doJSON(t, s.app, http.MethodGet, "/api/users/me", reg.AccessToken, nil, http.StatusOK, &me) doJSON(t, s.App(), http.MethodGet, "/api/users/me", reg.AccessToken, nil, http.StatusOK, &me)
if me.ID != reg.User.ID { if me.ID != reg.User.ID {
t.Fatalf("unexpected user id: got %q want %q", me.ID, reg.User.ID) t.Fatalf("unexpected user id: got %q want %q", me.ID, reg.User.ID)
} }
@@ -182,7 +186,7 @@ func TestRegistrationFlow(t *testing.T) {
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
} }
doJSON(t, s.app, http.MethodGet, "/api/users/me/organizations", reg.AccessToken, nil, http.StatusOK, &orgs) doJSON(t, s.App(), http.MethodGet, "/api/users/me/organizations", reg.AccessToken, nil, http.StatusOK, &orgs)
if len(orgs) != 1 { if len(orgs) != 1 {
t.Fatalf("expected 1 organization, got %d", len(orgs)) t.Fatalf("expected 1 organization, got %d", len(orgs))
} }
@@ -201,7 +205,7 @@ func TestRegistrationFlow(t *testing.T) {
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
} }
doJSON(t, s.app, http.MethodGet, "/api/organizations/my", reg.AccessToken, nil, http.StatusOK, &myOrg) doJSON(t, s.App(), http.MethodGet, "/api/organizations/my", reg.AccessToken, nil, http.StatusOK, &myOrg)
if myOrg.Kind != string(organization.KindPersonal) { if myOrg.Kind != string(organization.KindPersonal) {
t.Fatalf("unexpected personal org kind: %q", myOrg.Kind) t.Fatalf("unexpected personal org kind: %q", myOrg.Kind)
} }
@@ -218,7 +222,7 @@ func TestRegistrationFlow(t *testing.T) {
t.Fatalf("uuid: %v", err) t.Fatalf("uuid: %v", err)
} }
_, err = s.db.ExecContext(ctx, _, err = db.ExecContext(ctx,
`INSERT INTO organizations (id, kind, name, slug) VALUES (?, ?, ?, ?)`, `INSERT INTO organizations (id, kind, name, slug) VALUES (?, ?, ?, ?)`,
orgID, organization.KindTeam, "Acme", "acme", orgID, organization.KindTeam, "Acme", "acme",
) )
@@ -226,7 +230,7 @@ func TestRegistrationFlow(t *testing.T) {
t.Fatalf("insert org: %v", err) t.Fatalf("insert org: %v", err)
} }
_, err = s.db.ExecContext(ctx, _, err = db.ExecContext(ctx,
`INSERT INTO accounts (id, org_id, user_id, role, status) VALUES (?, ?, ?, ?, ?)`, `INSERT INTO accounts (id, org_id, user_id, role, status) VALUES (?, ?, ?, ?, ?)`,
accountID, orgID, reg.User.ID, "member", "active", accountID, orgID, reg.User.ID, "member", "active",
) )
@@ -240,7 +244,7 @@ func TestRegistrationFlow(t *testing.T) {
Name string `json:"name"` Name string `json:"name"`
Slug string `json:"slug"` Slug string `json:"slug"`
} }
doJSON(t, s.app, http.MethodGet, "/api/organizations/acme", reg.AccessToken, nil, http.StatusOK, &got) doJSON(t, s.App(), http.MethodGet, "/api/organizations/acme", reg.AccessToken, nil, http.StatusOK, &got)
if got.Slug != "acme" { if got.Slug != "acme" {
t.Fatalf("unexpected org slug: %q", got.Slug) t.Fatalf("unexpected org slug: %q", got.Slug)
} }
@@ -257,7 +261,7 @@ func TestRegistrationFlow(t *testing.T) {
Role string `json:"role"` Role string `json:"role"`
Status string `json:"status"` Status string `json:"status"`
} }
doJSON(t, s.app, http.MethodGet, fmt.Sprintf("/api/accounts/%s", reg.Account.ID), reg.AccessToken, nil, http.StatusOK, &got) doJSON(t, s.App(), http.MethodGet, fmt.Sprintf("/api/accounts/%s", reg.Account.ID), reg.AccessToken, nil, http.StatusOK, &got)
if got.ID != reg.Account.ID { if got.ID != reg.Account.ID {
t.Fatalf("unexpected account id: got %q want %q", got.ID, reg.Account.ID) t.Fatalf("unexpected account id: got %q want %q", got.ID, reg.Account.ID)
} }
@@ -272,7 +276,7 @@ func TestRegistrationFlow(t *testing.T) {
} }
doJSON( doJSON(
t, t,
s.app, s.App(),
http.MethodGet, http.MethodGet,
fmt.Sprintf("/api/my/drives/%s/directories/root/content?limit=100", reg.Drive.ID), fmt.Sprintf("/api/my/drives/%s/directories/root/content?limit=100", reg.Drive.ID),
reg.AccessToken, reg.AccessToken,

View File

@@ -163,3 +163,7 @@ func NewServer(c Config) (*Server, error) {
func (s *Server) Start() error { func (s *Server) Start() error {
return s.app.Listen(fmt.Sprintf(":%d", s.config.Server.Port)) return s.app.Listen(fmt.Sprintf(":%d", s.config.Server.Port))
} }
func (s *Server) App() *fiber.App {
return s.app
}

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package drive package drive_test
import ( import (
"context" "context"
@@ -10,6 +10,7 @@ import (
"github.com/get-drexa/drexa/internal/account" "github.com/get-drexa/drexa/internal/account"
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/drive"
"github.com/get-drexa/drexa/internal/organization" "github.com/get-drexa/drexa/internal/organization"
"github.com/get-drexa/drexa/internal/password" "github.com/get-drexa/drexa/internal/password"
"github.com/get-drexa/drexa/internal/user" "github.com/get-drexa/drexa/internal/user"
@@ -46,7 +47,7 @@ func TestService_DriveAccess(t *testing.T) {
userSvc := user.NewService() userSvc := user.NewService()
orgSvc := organization.NewService() orgSvc := organization.NewService()
accSvc := account.NewService() accSvc := account.NewService()
driveSvc := NewService() driveSvc := drive.NewService()
testUser, err := userSvc.RegisterUser(ctx, db, user.UserRegistrationOptions{ testUser, err := userSvc.RegisterUser(ctx, db, user.UserRegistrationOptions{
Email: "drive@example.com", Email: "drive@example.com",
@@ -75,7 +76,7 @@ func TestService_DriveAccess(t *testing.T) {
t.Fatalf("CreateAccount(org2): %v", err) t.Fatalf("CreateAccount(org2): %v", err)
} }
drive1, err := driveSvc.CreateDrive(ctx, db, CreateDriveOptions{ drive1, err := driveSvc.CreateDrive(ctx, db, drive.CreateDriveOptions{
OrgID: org1.ID, OrgID: org1.ID,
OwnerAccountID: &acc1.ID, OwnerAccountID: &acc1.ID,
Name: "Drive One", Name: "Drive One",
@@ -84,7 +85,7 @@ func TestService_DriveAccess(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("CreateDrive(org1): %v", err) t.Fatalf("CreateDrive(org1): %v", err)
} }
drive2, err := driveSvc.CreateDrive(ctx, db, CreateDriveOptions{ drive2, err := driveSvc.CreateDrive(ctx, db, drive.CreateDriveOptions{
OrgID: org2.ID, OrgID: org2.ID,
OwnerAccountID: &acc2.ID, OwnerAccountID: &acc2.ID,
Name: "Drive Two", Name: "Drive Two",

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package organization package organization_test
import ( import (
"context" "context"
@@ -10,6 +10,7 @@ import (
"github.com/get-drexa/drexa/internal/account" "github.com/get-drexa/drexa/internal/account"
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/organization"
"github.com/get-drexa/drexa/internal/password" "github.com/get-drexa/drexa/internal/password"
"github.com/get-drexa/drexa/internal/user" "github.com/get-drexa/drexa/internal/user"
"github.com/google/uuid" "github.com/google/uuid"
@@ -38,14 +39,14 @@ func TestService_CreatePersonalOrganization(t *testing.T) {
t.Fatalf("RunMigrations: %v", err) t.Fatalf("RunMigrations: %v", err)
} }
svc := NewService() svc := organization.NewService()
org, err := svc.CreatePersonalOrganization(ctx, db, "Personal Org") org, err := svc.CreatePersonalOrganization(ctx, db, "Personal Org")
if err != nil { if err != nil {
t.Fatalf("CreatePersonalOrganization: %v", err) t.Fatalf("CreatePersonalOrganization: %v", err)
} }
if org.Kind != KindPersonal { if org.Kind != organization.KindPersonal {
t.Fatalf("unexpected org kind: got %q want %q", org.Kind, KindPersonal) t.Fatalf("unexpected org kind: got %q want %q", org.Kind, organization.KindPersonal)
} }
if org.Name != "Personal Org" { if org.Name != "Personal Org" {
t.Fatalf("unexpected org name: got %q want %q", org.Name, "Personal Org") t.Fatalf("unexpected org name: got %q want %q", org.Name, "Personal Org")
@@ -59,8 +60,8 @@ func TestService_CreatePersonalOrganization(t *testing.T) {
if got.ID != org.ID { if got.ID != org.ID {
t.Fatalf("unexpected org id: got %q want %q", got.ID, org.ID) t.Fatalf("unexpected org id: got %q want %q", got.ID, org.ID)
} }
if got.Kind != KindPersonal { if got.Kind != organization.KindPersonal {
t.Fatalf("unexpected org kind: got %q want %q", got.Kind, KindPersonal) t.Fatalf("unexpected org kind: got %q want %q", got.Kind, organization.KindPersonal)
} }
if got.Name != "Personal Org" { if got.Name != "Personal Org" {
t.Fatalf("unexpected org name: got %q want %q", got.Name, "Personal Org") t.Fatalf("unexpected org name: got %q want %q", got.Name, "Personal Org")
@@ -69,9 +70,9 @@ func TestService_CreatePersonalOrganization(t *testing.T) {
t.Run("organization by slug", func(t *testing.T) { t.Run("organization by slug", func(t *testing.T) {
slug := "test-org" slug := "test-org"
orgWithSlug := &Organization{ orgWithSlug := &organization.Organization{
ID: uuid.Must(uuid.NewV7()), ID: uuid.Must(uuid.NewV7()),
Kind: KindTeam, Kind: organization.KindTeam,
Name: "Team Org", Name: "Team Org",
Slug: &slug, Slug: &slug,
} }

View File

@@ -1,8 +1,10 @@
package organization package organization_test
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/get-drexa/drexa/internal/organization"
) )
func TestNormalizeSlug(t *testing.T) { func TestNormalizeSlug(t *testing.T) {
@@ -61,7 +63,7 @@ func TestNormalizeSlug(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := NormalizeSlug(tt.input) got, err := organization.NormalizeSlug(tt.input)
if tt.wantErr { if tt.wantErr {
if err == nil { if err == nil {
t.Fatalf("expected error, got none (slug=%q)", got) t.Fatalf("expected error, got none (slug=%q)", got)

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package registration package registration_test
import ( import (
"context" "context"
@@ -14,6 +14,7 @@ import (
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/drive" "github.com/get-drexa/drexa/internal/drive"
"github.com/get-drexa/drexa/internal/organization" "github.com/get-drexa/drexa/internal/organization"
"github.com/get-drexa/drexa/internal/registration"
"github.com/get-drexa/drexa/internal/user" "github.com/get-drexa/drexa/internal/user"
"github.com/get-drexa/drexa/internal/virtualfs" "github.com/get-drexa/drexa/internal/virtualfs"
"github.com/testcontainers/testcontainers-go/modules/postgres" "github.com/testcontainers/testcontainers-go/modules/postgres"
@@ -62,9 +63,9 @@ func TestService_Register(t *testing.T) {
accSvc := account.NewService() accSvc := account.NewService()
driveSvc := drive.NewService() driveSvc := drive.NewService()
regSvc := NewService(userSvc, orgSvc, accSvc, driveSvc, vfs) regSvc := registration.NewService(userSvc, orgSvc, accSvc, driveSvc, vfs)
result, err := regSvc.Register(ctx, db, RegisterOptions{ result, err := regSvc.Register(ctx, db, registration.RegisterOptions{
Email: "reg@example.com", Email: "reg@example.com",
Password: "password123", Password: "password123",
DisplayName: "Reg User", DisplayName: "Reg User",

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package sharing package sharing_test
import ( import (
"context" "context"
@@ -16,6 +16,7 @@ import (
"github.com/get-drexa/drexa/internal/drive" "github.com/get-drexa/drexa/internal/drive"
"github.com/get-drexa/drexa/internal/organization" "github.com/get-drexa/drexa/internal/organization"
"github.com/get-drexa/drexa/internal/password" "github.com/get-drexa/drexa/internal/password"
"github.com/get-drexa/drexa/internal/sharing"
"github.com/get-drexa/drexa/internal/user" "github.com/get-drexa/drexa/internal/user"
"github.com/get-drexa/drexa/internal/virtualfs" "github.com/get-drexa/drexa/internal/virtualfs"
"github.com/testcontainers/testcontainers-go/modules/postgres" "github.com/testcontainers/testcontainers-go/modules/postgres"
@@ -128,36 +129,36 @@ func TestService_SharingScopes(t *testing.T) {
t.Fatalf("WriteFile: %v", err) t.Fatalf("WriteFile: %v", err)
} }
shareSvc, err := NewService(vfs) shareSvc, err := sharing.NewService(vfs)
if err != nil { if err != nil {
t.Fatalf("NewService: %v", err) t.Fatalf("NewService: %v", err)
} }
t.Run("create share validation", func(t *testing.T) { t.Run("create share validation", func(t *testing.T) {
if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{}); err != ErrShareNoItems { if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{}); err != sharing.ErrShareNoItems {
t.Fatalf("expected ErrShareNoItems, got %v", err) t.Fatalf("expected ErrShareNoItems, got %v", err)
} }
if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{root}, Items: []*virtualfs.Node{root},
}); err != ErrCannotShareRoot { }); err != sharing.ErrCannotShareRoot {
t.Fatalf("expected ErrCannotShareRoot, got %v", err) t.Fatalf("expected ErrCannotShareRoot, got %v", err)
} }
if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ if _, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{dirNode, fileNode}, Items: []*virtualfs.Node{dirNode, fileNode},
}); err != ErrNotSameParent { }); err != sharing.ErrNotSameParent {
t.Fatalf("expected ErrNotSameParent, got %v", err) t.Fatalf("expected ErrNotSameParent, got %v", err)
} }
}) })
dirShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ dirShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{dirNode}, Items: []*virtualfs.Node{dirNode},
}) })
if err != nil { if err != nil {
t.Fatalf("CreateShare(dir): %v", err) t.Fatalf("CreateShare(dir): %v", err)
} }
fileShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ fileShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
}) })
if err != nil { if err != nil {
@@ -212,7 +213,7 @@ func TestService_SharingScopes(t *testing.T) {
}) })
t.Run("list shares includes expired", func(t *testing.T) { t.Run("list shares includes expired", func(t *testing.T) {
expiredShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ expiredShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
ExpiresAt: time.Now().Add(-1 * time.Hour), ExpiresAt: time.Now().Add(-1 * time.Hour),
}) })
@@ -220,7 +221,7 @@ func TestService_SharingScopes(t *testing.T) {
t.Fatalf("CreateShare(expired list): %v", err) t.Fatalf("CreateShare(expired list): %v", err)
} }
activeShares, err := shareSvc.ListShares(ctx, db, drv.ID, ListSharesOptions{IncludesExpired: false}) activeShares, err := shareSvc.ListShares(ctx, db, drv.ID, sharing.ListSharesOptions{IncludesExpired: false})
if err != nil { if err != nil {
t.Fatalf("ListShares(active): %v", err) t.Fatalf("ListShares(active): %v", err)
} }
@@ -235,7 +236,7 @@ func TestService_SharingScopes(t *testing.T) {
t.Fatalf("expected active shares to be listed") t.Fatalf("expected active shares to be listed")
} }
allShares, err := shareSvc.ListShares(ctx, db, drv.ID, ListSharesOptions{IncludesExpired: true}) allShares, err := shareSvc.ListShares(ctx, db, drv.ID, sharing.ListSharesOptions{IncludesExpired: true})
if err != nil { if err != nil {
t.Fatalf("ListShares(all): %v", err) t.Fatalf("ListShares(all): %v", err)
} }
@@ -252,26 +253,26 @@ func TestService_SharingScopes(t *testing.T) {
}) })
t.Run("find share not found", func(t *testing.T) { t.Run("find share not found", func(t *testing.T) {
if _, err := shareSvc.FindShareByPublicID(ctx, db, "missing-share"); err != ErrShareNotFound { if _, err := shareSvc.FindShareByPublicID(ctx, db, "missing-share"); err != sharing.ErrShareNotFound {
t.Fatalf("expected ErrShareNotFound, got %v", err) t.Fatalf("expected ErrShareNotFound, got %v", err)
} }
}) })
t.Run("expired share", func(t *testing.T) { t.Run("expired share", func(t *testing.T) {
expiredShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ expiredShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
ExpiresAt: time.Now().Add(-1 * time.Hour), ExpiresAt: time.Now().Add(-1 * time.Hour),
}) })
if err != nil { if err != nil {
t.Fatalf("CreateShare(expired): %v", err) t.Fatalf("CreateShare(expired): %v", err)
} }
if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, expiredShare); err != ErrShareExpired { if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, expiredShare); err != sharing.ErrShareExpired {
t.Fatalf("expected ErrShareExpired, got %v", err) t.Fatalf("expected ErrShareExpired, got %v", err)
} }
}) })
t.Run("revoked share", func(t *testing.T) { t.Run("revoked share", func(t *testing.T) {
revokedShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ revokedShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
}) })
if err != nil { if err != nil {
@@ -285,31 +286,31 @@ func TestService_SharingScopes(t *testing.T) {
t.Fatalf("update revoked share: %v", err) t.Fatalf("update revoked share: %v", err)
} }
revokedShare.RevokedAt = &now revokedShare.RevokedAt = &now
if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, revokedShare); err != ErrShareRevoked { if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, revokedShare); err != sharing.ErrShareRevoked {
t.Fatalf("expected ErrShareRevoked, got %v", err) t.Fatalf("expected ErrShareRevoked, got %v", err)
} }
}) })
t.Run("no permissions", func(t *testing.T) { t.Run("no permissions", func(t *testing.T) {
noPermShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ noPermShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
}) })
if err != nil { if err != nil {
t.Fatalf("CreateShare(no permissions): %v", err) t.Fatalf("CreateShare(no permissions): %v", err)
} }
if _, err := db.NewDelete(). if _, err := db.NewDelete().
Model(&SharePermission{}). Model(&sharing.SharePermission{}).
Where("share_id = ?", noPermShare.ID). Where("share_id = ?", noPermShare.ID).
Exec(ctx); err != nil { Exec(ctx); err != nil {
t.Fatalf("delete permissions: %v", err) t.Fatalf("delete permissions: %v", err)
} }
if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, noPermShare); err != ErrNoPermissions { if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, noPermShare); err != sharing.ErrNoPermissions {
t.Fatalf("expected ErrNoPermissions, got %v", err) t.Fatalf("expected ErrNoPermissions, got %v", err)
} }
}) })
t.Run("permission expired", func(t *testing.T) { t.Run("permission expired", func(t *testing.T) {
expiredPermShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ expiredPermShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
}) })
if err != nil { if err != nil {
@@ -317,31 +318,31 @@ func TestService_SharingScopes(t *testing.T) {
} }
expiredAt := time.Now().Add(-1 * time.Hour) expiredAt := time.Now().Add(-1 * time.Hour)
if _, err := db.NewUpdate(). if _, err := db.NewUpdate().
Model(&SharePermission{}). Model(&sharing.SharePermission{}).
Set("expires_at = ?", expiredAt). Set("expires_at = ?", expiredAt).
Where("share_id = ?", expiredPermShare.ID). Where("share_id = ?", expiredPermShare.ID).
Exec(ctx); err != nil { Exec(ctx); err != nil {
t.Fatalf("update permission expiry: %v", err) t.Fatalf("update permission expiry: %v", err)
} }
if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, expiredPermShare); err != ErrShareExpired { if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, expiredPermShare); err != sharing.ErrShareExpired {
t.Fatalf("expected ErrShareExpired, got %v", err) t.Fatalf("expected ErrShareExpired, got %v", err)
} }
}) })
t.Run("no items", func(t *testing.T) { t.Run("no items", func(t *testing.T) {
noItemsShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, CreateShareOptions{ noItemsShare, err := shareSvc.CreateShare(ctx, db, drv.ID, acc.ID, sharing.CreateShareOptions{
Items: []*virtualfs.Node{fileNode}, Items: []*virtualfs.Node{fileNode},
}) })
if err != nil { if err != nil {
t.Fatalf("CreateShare(no items): %v", err) t.Fatalf("CreateShare(no items): %v", err)
} }
if _, err := db.NewDelete(). if _, err := db.NewDelete().
Model(&ShareItem{}). Model(&sharing.ShareItem{}).
Where("share_id = ?", noItemsShare.ID). Where("share_id = ?", noItemsShare.ID).
Exec(ctx); err != nil { Exec(ctx); err != nil {
t.Fatalf("delete share items: %v", err) t.Fatalf("delete share items: %v", err)
} }
if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, noItemsShare); err != ErrShareNoItems { if _, err := shareSvc.ResolveScopeForShare(ctx, db, nil, noItemsShare); err != sharing.ErrShareNoItems {
t.Fatalf("expected ErrShareNoItems, got %v", err) t.Fatalf("expected ErrShareNoItems, got %v", err)
} }
}) })

View File

@@ -1,6 +1,6 @@
//go:build integration //go:build integration
package user package user_test
import ( import (
"context" "context"
@@ -11,6 +11,7 @@ import (
"github.com/get-drexa/drexa/internal/database" "github.com/get-drexa/drexa/internal/database"
"github.com/get-drexa/drexa/internal/password" "github.com/get-drexa/drexa/internal/password"
"github.com/get-drexa/drexa/internal/user"
"github.com/testcontainers/testcontainers-go/modules/postgres" "github.com/testcontainers/testcontainers-go/modules/postgres"
) )
@@ -45,9 +46,9 @@ func TestService_UserQueries(t *testing.T) {
t.Fatalf("HashString: %v", err) t.Fatalf("HashString: %v", err)
} }
svc := NewService() svc := user.NewService()
user1, err := svc.RegisterUser(ctx, db, UserRegistrationOptions{ user1, err := svc.RegisterUser(ctx, db, user.UserRegistrationOptions{
Email: "alice@example.com", Email: "alice@example.com",
DisplayName: "Alice", DisplayName: "Alice",
Password: pw1, Password: pw1,
@@ -55,7 +56,7 @@ func TestService_UserQueries(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("RegisterUser(user1): %v", err) t.Fatalf("RegisterUser(user1): %v", err)
} }
user2, err := svc.RegisterUser(ctx, db, UserRegistrationOptions{ user2, err := svc.RegisterUser(ctx, db, user.UserRegistrationOptions{
Email: "bob@example.com", Email: "bob@example.com",
DisplayName: "Bob", DisplayName: "Bob",
Password: pw2, Password: pw2,
@@ -134,9 +135,9 @@ func TestService_RegisterUserConflict(t *testing.T) {
t.Fatalf("HashString: %v", err) t.Fatalf("HashString: %v", err)
} }
svc := NewService() svc := user.NewService()
_, err = svc.RegisterUser(ctx, db, UserRegistrationOptions{ _, err = svc.RegisterUser(ctx, db, user.UserRegistrationOptions{
Email: "conflict@example.com", Email: "conflict@example.com",
DisplayName: "Conflict", DisplayName: "Conflict",
Password: pw, Password: pw,
@@ -145,7 +146,7 @@ func TestService_RegisterUserConflict(t *testing.T) {
t.Fatalf("RegisterUser(first): %v", err) t.Fatalf("RegisterUser(first): %v", err)
} }
_, err = svc.RegisterUser(ctx, db, UserRegistrationOptions{ _, err = svc.RegisterUser(ctx, db, user.UserRegistrationOptions{
Email: "conflict@example.com", Email: "conflict@example.com",
DisplayName: "Conflict 2", DisplayName: "Conflict 2",
Password: pw, Password: pw,
@@ -153,7 +154,7 @@ func TestService_RegisterUserConflict(t *testing.T) {
if err == nil { if err == nil {
t.Fatalf("expected conflict error, got nil") t.Fatalf("expected conflict error, got nil")
} }
var existsErr *AlreadyExistsError var existsErr *user.AlreadyExistsError
if !errors.As(err, &existsErr) { if !errors.As(err, &existsErr) {
t.Fatalf("expected AlreadyExistsError, got %T: %v", err, err) t.Fatalf("expected AlreadyExistsError, got %T: %v", err, err)
} }