package test import ( "RenewCMS/internal/domain/author" "RenewCMS/internal/infrastructure/useCases" "context" "errors" "testing" ) func TestAccountDeleteReservedAuthorSlug(t *testing.T) { t.Run("The deleted-user prefix is not a valid author slug", func(t *testing.T) { for slug, valid := range map[string]bool{"deleted-user-5": false, "deleted-user-": false, "deleted-user": true, "deleted-users-5": true, "marta": true} { if author.IsValidSlug(slug) != valid { t.Errorf("%q: expected valid=%v", slug, valid) } } }) t.Run("The reserved prefix is detected exactly", func(t *testing.T) { for slug, reserved := range map[string]bool{"deleted-user-": true, "deleted-user-7": true, "deleted-user": false, "": false, "deleted-users": false} { if author.IsReservedSlug(slug) != reserved { t.Errorf("%q: expected reserved=%v", slug, reserved) } } }) t.Run("A generated slug never lands on the reserved prefix", func(t *testing.T) { if got := author.GenerateSlug("Deleted User 5"); author.IsReservedSlug(got) || got != "author-deleted-user-5" { t.Errorf("unexpected slug %q", got) } if got := author.SlugWithSuffix("deleted-user", 2); author.IsReservedSlug(got) { t.Errorf("suffixed slug %q is reserved", got) } }) t.Run("Creating an author with a reserved slug is refused", func(t *testing.T) { authors := plainAuthors() uc := useCases.NewCreateAuthorUseCase(authors, newMockUserRepo(), newMockImageRepo(), discardRecorder) _, err := uc.Create(context.Background(), useCases.CreateAuthorCommand{Name: "Marta", Slug: "deleted-user-9"}) if !errors.Is(err, author.ErrInvalidSlug) || len(authors.authors) != 0 { t.Errorf("err %v created %d", err, len(authors.authors)) } }) t.Run("Creating an author named like the reserved prefix gets a safe slug", func(t *testing.T) { authors := plainAuthors() uc := useCases.NewCreateAuthorUseCase(authors, newMockUserRepo(), newMockImageRepo(), discardRecorder) created, err := uc.Create(context.Background(), useCases.CreateAuthorCommand{Name: "Deleted User 9"}) if err != nil || created.Slug != "author-deleted-user-9" { t.Errorf("slug %q err %v", created.Slug, err) } }) t.Run("Changing an author slug to a reserved one is refused", func(t *testing.T) { authors := plainAuthors() authors.seed(2, "B", "b") uc := useCases.NewUpdateAuthorUseCase(authors, newMockUserRepo(), newMockImageRepo(), discardRecorder) reserved := "deleted-user-2" _, err := uc.Update(context.Background(), 2, useCases.AuthorCaller{UserID: 9, Grants: authorsEditAllGrants}, author.Patch{Slug: &reserved}) if !errors.Is(err, author.ErrInvalidSlug) { t.Errorf("err = %v", err) } }) }