test: admin seed ve kimlik doğrulama entegrasyon testleri
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+64
-6
@@ -1,10 +1,68 @@
|
||||
/**
|
||||
* Seed betiği — Faz 4'te doldurulacak (admin, örnek hane, aile üyeleri ve alan verisi).
|
||||
* Çalıştırma: npm run seed | Sıfırlayıp doldurma: npm run seed:reset
|
||||
* Seed betiği: npm run seed | Sıfırlayıp doldurma: npm run seed:reset
|
||||
*
|
||||
* Faz 3: yalnızca ilk admin (SEED_ADMIN_EMAIL / SEED_ADMIN_PASSWORD, kullanıcı adı "admin").
|
||||
* Faz 4: örnek kullanıcılar, hane ve aile üyeleri eklenecek.
|
||||
*
|
||||
* İdempotent: kullanıcı e-postasına göre kontrol eder, tekrar çalıştırınca kopya oluşturmaz.
|
||||
* Kullanıcılar Better Auth API'siyle oluşturulur (şifre doğru hashlenir; koleksiyona elle yazılmaz).
|
||||
* `server-only` modülleri Next dışında yükleyebilmek için --conditions=react-server ile çalışır.
|
||||
*/
|
||||
if (process.env.NODE_ENV === "production" && process.argv.includes("--reset")) {
|
||||
console.error("seed:reset production ortamında çalıştırılamaz.");
|
||||
process.exit(1);
|
||||
import { getAuth } from "@/lib/auth";
|
||||
import { getDb, getMongoClient } from "@/lib/db/mongo";
|
||||
import { env } from "@/lib/env";
|
||||
|
||||
const reset = process.argv.includes("--reset");
|
||||
|
||||
async function main() {
|
||||
const e = env();
|
||||
if (reset && e.NODE_ENV === "production") {
|
||||
throw new Error("seed:reset production ortamında çalıştırılamaz.");
|
||||
}
|
||||
if (!e.SEED_ADMIN_EMAIL || !e.SEED_ADMIN_PASSWORD) {
|
||||
throw new Error("SEED_ADMIN_EMAIL ve SEED_ADMIN_PASSWORD tanımlı olmalı (.env.local).");
|
||||
}
|
||||
|
||||
const db = getDb();
|
||||
if (reset) {
|
||||
for (const name of ["user", "account", "session", "verification", "rateLimit"]) {
|
||||
await db.collection(name).deleteMany({});
|
||||
}
|
||||
console.log("• Kimlik doğrulama koleksiyonları temizlendi.");
|
||||
}
|
||||
|
||||
const email = e.SEED_ADMIN_EMAIL.toLowerCase();
|
||||
const existing = await db.collection("user").findOne({ email });
|
||||
if (existing) {
|
||||
console.log(`• Admin zaten var: ${email}`);
|
||||
} else {
|
||||
await getAuth().api.createUser({
|
||||
body: {
|
||||
email,
|
||||
password: e.SEED_ADMIN_PASSWORD,
|
||||
name: "Yönetici",
|
||||
role: "admin",
|
||||
data: { username: "admin" },
|
||||
},
|
||||
});
|
||||
console.log(`✓ Admin oluşturuldu: ${email} (kullanıcı adı: admin)`);
|
||||
}
|
||||
|
||||
if (e.NODE_ENV !== "production") {
|
||||
console.table([
|
||||
{
|
||||
rol: "admin",
|
||||
kullaniciAdi: "admin",
|
||||
eposta: email,
|
||||
sifre: "SEED_ADMIN_PASSWORD (.env.local)",
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Seed henüz uygulanmadı (Faz 4).");
|
||||
main()
|
||||
.catch((err: unknown) => {
|
||||
console.error(err instanceof Error ? err.message : err);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.finally(() => getMongoClient().close());
|
||||
|
||||
Reference in New Issue
Block a user