- handleAuthLogin now authenticates via store.FindUserByPhone instead of FindUserByEmail; the login/bootstrap-super-admin request payload field is now `phone`. Old email addresses are no longer valid login credentials. - bootstrap-super-admin now validates phone format (isValidChinaMobilePhone) instead of net/mail.ParseAddress; net/mail is fully removed from internal/api. - Same-class gap fix: handleAdminOrganizationUsersCreate (the legacy per-organization admin endpoint, not touched by ticket 02) still created users by email only, which would have left them unable to log in under the new phone-only login. Switched it to phone validation/uniqueness too, and removed the now-dead normalizeUserInput/validateUserEmailAvailable helpers. - Frontend: authApi.login, UserContext's login signature, and UserBootstrapPanel's submit handler now send `phone` instead of `email` (the form/labels were already phone-based from ticket 04). - Updated ~20 Go test files whose fixtures/helpers logged in via user.Email; adminCreateUser/loginSession/bootstrapSuperAdminSession now work in terms of phone (deterministically derived from existing email-shaped test identities via testPhoneFromSeed, to avoid rewriting every call site). Added internal/api/auth_phone_login_test.go covering phone login success/failure and old-email rejection. go test ./internal/api/... and npm run test:frontend match their pre-ticket baselines exactly (75 Go failures / 599 frontend tests with 586 pass, 13 fail) by failing test name, not just count.
229 lines
10 KiB
Go
229 lines
10 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type progressiveMoveTestBrowser struct {
|
|
*fakeObjectStorageBrowser
|
|
started chan struct{}
|
|
proceed chan struct{}
|
|
}
|
|
|
|
func (b *progressiveMoveTestBrowser) MovePrefixWithProgress(ctx context.Context, bucket, prefix, target string, progress func(objectStorageMoveProgress)) (domain.ObjectStorageObjectSummary, error) {
|
|
if prefix == "source/first" {
|
|
close(b.started)
|
|
<-b.proceed
|
|
}
|
|
progress(objectStorageMoveProgress{TotalObjects: 3, CopiedObjects: 2, SkippedObjects: 1, DeletedObjects: 3})
|
|
return domain.ObjectStorageObjectSummary{Bucket: bucket, Key: target}, nil
|
|
}
|
|
|
|
func TestMoveJobRetainsBrowserAndAggregatesObjectProgress(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
org, _ := dataStore.EnsureDefaultOrganization(ctx)
|
|
hash, _ := hashPassword("move-test-password")
|
|
admin, err := dataStore.CreateUser(ctx, domain.CreateUserInput{OrganizationID: org.ID, Name: "Move Admin", Phone: "13800007002", Role: domain.UserRoleAdmin, PasswordHash: hash})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{Name: "BOS", Provider: domain.StorageProviderBOS, Endpoint: "https://bos.example.test", Bucket: "datastorage", AccessKeyID: "ak", SecretAccessKey: "sk"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
browser := &progressiveMoveTestBrowser{newFakeObjectStorageBrowser(), make(chan struct{}), make(chan struct{})}
|
|
var cleaned atomic.Bool
|
|
original := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return browser, func() { cleaned.Store(true) }, nil
|
|
}
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = original })
|
|
handler := NewHandler(dataStore, nil)
|
|
cookie := loginSession(t, handler, admin.Phone, "move-test-password")
|
|
response := serveJSONWithCookie(handler, http.MethodPost, "/api/object-storage/move-jobs?connectionId="+connection.ID, []byte(`{"bucket":"datastorage","destinationPrefix":"test","items":[{"key":"source/first","kind":"directory"},{"key":"source/second","kind":"directory"}]}`), cookie)
|
|
if response.Code != http.StatusAccepted {
|
|
t.Fatalf("%d: %s", response.Code, response.Body.String())
|
|
}
|
|
var accepted struct {
|
|
MoveID string `json:"moveId"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &accepted); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
<-browser.started
|
|
closedEarly := cleaned.Load()
|
|
close(browser.proceed)
|
|
if closedEarly {
|
|
t.Fatal("HTTP response closed the browser while move was running")
|
|
}
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for !cleaned.Load() && time.Now().Before(deadline) {
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
if !cleaned.Load() {
|
|
t.Fatal("browser was not cleaned up after completion")
|
|
}
|
|
snapshot := objectStorageMoveJobs.get(accepted.MoveID).snapshot()
|
|
for key, want := range map[string]any{"status": "completed", "totalObjects": 6, "copiedObjects": 4, "skippedObjects": 2, "deletedObjects": 6} {
|
|
if snapshot[key] != want {
|
|
t.Fatalf("%s=%v, want %v", key, snapshot[key], want)
|
|
}
|
|
}
|
|
result := snapshot["result"].(domain.ObjectStorageObjectSummary)
|
|
if result.Key != "test/second" {
|
|
t.Fatalf("result key=%q", result.Key)
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageMovePrecheckIssues(t *testing.T) {
|
|
items := []domain.ObjectStorageObjectMoveItem{
|
|
{Key: "data/session", Kind: "directory", Recursive: true},
|
|
}
|
|
if issues := objectStorageMovePrecheckIssues(items, "data"); len(issues) == 0 || issues[0].Code != "move_destination_same_as_source" {
|
|
t.Fatalf("expected move_destination_same_as_source issue for a no-op move, got %#v", issues)
|
|
}
|
|
if issues := objectStorageMovePrecheckIssues(items, "data/session/nested"); len(issues) == 0 || issues[0].Code != "move_destination_inside_source" {
|
|
t.Fatalf("expected move_destination_inside_source issue for a move into its own subtree, got %#v", issues)
|
|
}
|
|
overlapping := []domain.ObjectStorageObjectMoveItem{
|
|
{Key: "data/session", Kind: "directory", Recursive: true},
|
|
{Key: "data/session/file.csv", Kind: "file"},
|
|
}
|
|
if issues := objectStorageMovePrecheckIssues(overlapping, "archive"); len(issues) == 0 || issues[0].Code != "move_items_overlap" {
|
|
t.Fatalf("expected move_items_overlap issue for parent+child selection, got %#v", issues)
|
|
}
|
|
if issues := objectStorageMovePrecheckIssues([]domain.ObjectStorageObjectMoveItem{{Key: "data/session", Kind: "directory", Recursive: true}}, "archive"); len(issues) != 0 {
|
|
t.Fatalf("expected no issues for a valid move, got %#v", issues)
|
|
}
|
|
}
|
|
|
|
func TestObjectStoragePrefixOverlaps(t *testing.T) {
|
|
if !objectStoragePrefixOverlaps("test2", "test2/a") {
|
|
t.Fatal("expected capture prefix test2 to overlap with key test2/a")
|
|
}
|
|
if !objectStoragePrefixOverlaps("test2/a", "test2") {
|
|
t.Fatal("expected capture prefix test2/a to overlap with destination test2")
|
|
}
|
|
if objectStoragePrefixOverlaps("test2", "test3") {
|
|
t.Fatal("did not expect test2 and test3 to overlap")
|
|
}
|
|
if objectStoragePrefixOverlaps("", "test2/a") {
|
|
t.Fatal("empty capture prefix must not be reported as a conflict")
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageMoveJobRejectsNoOpDestination(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
passwordHash, err := hashPassword("move-admin-password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin, err := dataStore.CreateUser(ctx, domain.CreateUserInput{OrganizationID: organization.ID, Name: "Move Admin", Phone: "13800007003", Role: domain.UserRoleAdmin, PasswordHash: passwordHash})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{Name: "BOS", Provider: domain.StorageProviderBOS, Endpoint: "https://bos.example.test", Bucket: "datastorage", AccessKeyID: "ak", SecretAccessKey: "sk"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fakeBrowser := newFakeObjectStorageBrowser()
|
|
original := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return fakeBrowser, func() {}, nil
|
|
}
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = original })
|
|
handler := NewHandler(dataStore, nil)
|
|
cookie := loginSession(t, handler, admin.Phone, "move-admin-password")
|
|
|
|
body := `{"bucket":"datastorage","destinationPrefix":"test2","items":[{"key":"test2/session","kind":"directory","recursive":true}]}`
|
|
response := serveJSONWithCookie(handler, http.MethodPatch, "/api/object-storage/objects?connectionId="+connection.ID, []byte(body), cookie)
|
|
if response.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
if !strings.Contains(response.Body.String(), "move_destination_same_as_source") {
|
|
t.Fatalf("expected move_destination_same_as_source issue, got %s", response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestObjectStorageMoveJobCapturesCaptureAutomationConflict(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
organization, err := dataStore.EnsureDefaultOrganization(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
passwordHash, err := hashPassword("move-admin-password")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin, err := dataStore.CreateUser(ctx, domain.CreateUserInput{OrganizationID: organization.ID, Name: "Move Admin", Phone: "13800007004", Role: domain.UserRoleAdmin, PasswordHash: passwordHash})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{Name: "BOS", Provider: domain.StorageProviderBOS, Endpoint: "https://bos.example.test", Bucket: "datastorage", AccessKeyID: "ak", SecretAccessKey: "sk"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: organization.ID,
|
|
ProjectID: "project-move",
|
|
Name: "capture watcher",
|
|
Type: domain.WorkflowTypeBatch,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enabled := true
|
|
workflow.CaptureAutomation = &domain.CaptureAutomationConfig{Enabled: enabled, ConnectionID: connection.ID, Bucket: "datastorage", Prefix: "test2"}
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fakeBrowser := newFakeObjectStorageBrowser()
|
|
original := storageConnectionBrowserResolver
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return fakeBrowser, func() {}, nil
|
|
}
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = original })
|
|
handler := NewHandler(dataStore, nil)
|
|
cookie := loginSession(t, handler, admin.Phone, "move-admin-password")
|
|
|
|
body := `{"bucket":"datastorage","destinationPrefix":"archive","items":[{"key":"test2/session","kind":"directory","recursive":true}]}`
|
|
conflicted := serveJSONWithCookie(handler, http.MethodPost, "/api/object-storage/move-jobs?connectionId="+connection.ID, []byte(body), cookie)
|
|
if conflicted.Code != http.StatusConflict {
|
|
t.Fatalf("conflict status=%d body=%s", conflicted.Code, conflicted.Body.String())
|
|
}
|
|
if !strings.Contains(conflicted.Body.String(), "capture_automation_conflict") {
|
|
t.Fatalf("expected capture_automation_conflict code, got %s", conflicted.Body.String())
|
|
}
|
|
|
|
forced := serveJSONWithCookie(handler, http.MethodPost, "/api/object-storage/move-jobs?connectionId="+connection.ID+"&force=true", []byte(body), cookie)
|
|
if forced.Code != http.StatusAccepted {
|
|
t.Fatalf("forced move status=%d body=%s", forced.Code, forced.Body.String())
|
|
}
|
|
var accepted struct {
|
|
MoveID string `json:"moveId"`
|
|
}
|
|
if err := json.Unmarshal(forced.Body.Bytes(), &accepted); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jobResponse := serveJSONWithCookie(handler, http.MethodGet, "/api/object-storage/move-jobs/"+accepted.MoveID+"?connectionId="+connection.ID, nil, cookie)
|
|
if jobResponse.Code != http.StatusOK {
|
|
t.Fatalf("job status=%d body=%s", jobResponse.Code, jobResponse.Body.String())
|
|
}
|
|
}
|