package demoseed_test import ( "context" "os" "testing" "time" "Rezina-Backend/internal/demoseed" "Rezina-Backend/internal/repository" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "golang.org/x/crypto/bcrypt" ) // Opt in with a migrated disposable test database. Never uses application DB_URL. func TestDemoSeedAndPublicCatalogIntegration(t *testing.T) { url := os.Getenv("DEMO_TEST_DB_URL") if url == "" { t.Skip("set DEMO_TEST_DB_URL to a disposable migrated PostgreSQL database") } ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() conn, err := pgx.Connect(ctx, url) if err != nil { t.Fatal(err) } defer conn.Close(ctx) for i := 0; i < 2; i++ { if err := demoseed.Run(ctx, conn, demoseed.DefaultPassword, ""); err != nil { t.Fatal(err) } } var courses, lessons, quizzes int if err = conn.QueryRow(ctx, `SELECT COUNT(*) FILTER(WHERE kind='course'),COUNT(*) FILTER(WHERE kind='lesson'),COUNT(*) FILTER(WHERE kind='quiz') FROM demo_seed_entities`).Scan(&courses, &lessons, &quizzes); err != nil { t.Fatal(err) } if courses != 4 || lessons != 18 || quizzes != 6 { t.Fatalf("duplicate/missing fixtures after rerun: courses=%d lessons=%d quizzes=%d", courses, lessons, quizzes) } for _, a := range demoseed.Accounts { table := "users" if a.Audience == "staff" { table = "team_members" } var hash []byte if err = conn.QueryRow(ctx, `SELECT password FROM `+table+` WHERE email=$1`, a.Email).Scan(&hash); err != nil { t.Fatal(err) } if err = bcrypt.CompareHashAndPassword(hash, []byte(demoseed.DefaultPassword)); err != nil { t.Errorf("login hash invalid for %s", a.Email) } } pool, err := pgxpool.New(ctx, url) if err != nil { t.Fatal(err) } defer pool.Close() store := repository.NewStore(pool) var id, draftID int64 if err = conn.QueryRow(ctx, `SELECT entity_id FROM demo_seed_entities WHERE kind='course' AND seed_key='everyday-english'`).Scan(&id); err != nil { t.Fatal(err) } if err = conn.QueryRow(ctx, `SELECT entity_id FROM demo_seed_entities WHERE kind='course' AND seed_key='draft'`).Scan(&draftID); err != nil { t.Fatal(err) } outline, err := store.PublicCatalogCourse(ctx, id) if err != nil { t.Fatal(err) } if outline.ChapterCount != 2 || outline.LessonCount != 6 || outline.QuizCount != 2 || len(outline.Chapters) != 2 { t.Fatalf("outline counts=%+v", outline.PublicCourse) } if len(outline.Chapters[0].Lessons) != 3 || len(outline.Chapters[0].Quizzes) != 1 || outline.Chapters[0].Quizzes[0].QuestionCount != 3 { t.Fatalf("chapter=%+v", outline.Chapters[0]) } if _, err = store.PublicCatalogCourse(ctx, draftID); err != pgx.ErrNoRows { t.Fatalf("draft course accessible: %v", err) } page, err := store.PublicCatalogCourses(ctx, "[Demo draft]", 20, 0) if err != nil { t.Fatal(err) } if page.TotalCount != 0 || len(page.Courses) != 0 { t.Fatal("draft course leaked into search") } // A hidden chapter and all descendants must disappear from the public outline. chapterID := outline.Chapters[0].ID if _, err = conn.Exec(ctx, `UPDATE exam_prep.units SET publish_status='DRAFT' WHERE id=$1`, chapterID); err != nil { t.Fatal(err) } defer conn.Exec(context.Background(), `UPDATE exam_prep.units SET publish_status='PUBLISHED' WHERE id=$1`, chapterID) outline, err = store.PublicCatalogCourse(ctx, id) if err != nil { t.Fatal(err) } if len(outline.Chapters) != 1 || outline.LessonCount != 3 || outline.QuizCount != 1 { t.Fatal("hidden chapter descendants leaked into preview") } }