Files

40 lines
1.3 KiB
Go
Raw Permalink Normal View History

package workbench
import (
"context"
"errors"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
func TestJobRestoresCurrentTenantConfiguration(t *testing.T) {
s := sqliteWorkbench(t)
if err := s.db.AutoMigrate(&types.Tenant{}); err != nil {
t.Fatal(err)
}
tenant := types.Tenant{ID: 17, Name: "current workspace", Status: "active", StorageQuota: 123456}
if err := s.db.Create(&tenant).Error; err != nil {
t.Fatal(err)
}
parent := context.WithValue(context.Background(), types.TenantInfoContextKey, &types.Tenant{ID: 99})
ctx, err := s.restoreJobTenant(parent, tenant.ID)
if err != nil {
t.Fatal(err)
}
actual, ok := types.TenantInfoFromContext(ctx)
id, idOK := types.TenantIDFromContext(ctx)
if !ok || !idOK || id != tenant.ID || actual.ID != tenant.ID || actual.StorageQuota != tenant.StorageQuota {
t.Fatal("background job did not recover the current workspace configuration")
}
if err := s.db.Model(&types.Tenant{}).Where("id = ?", tenant.ID).Update("status", "suspended").Error; err != nil {
t.Fatal(err)
}
if _, err := s.restoreJobTenant(ctx, tenant.ID); !errors.Is(err, ErrForbidden) {
t.Fatalf("suspended workspace must stop queued work: %v", err)
}
if _, err := s.restoreJobTenant(parent, 999); !errors.Is(err, ErrForbidden) {
t.Fatalf("missing workspace must not reuse parent configuration: %v", err)
}
}