forked from baron/baron-sso
test: add unit and e2e tests for bulk user creation and schema validation
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
import { ScrollArea } from "../../../components/ui/scroll-area";
|
||||
import { bulkCreateUsers, type BulkUserItem, type BulkUserResult } from "../../../lib/adminApi";
|
||||
import { t } from "../../../lib/i18n";
|
||||
import { parseUserCSV } from "../utils/csvParser";
|
||||
|
||||
interface UserBulkUploadModalProps {
|
||||
onSuccess?: () => void;
|
||||
@@ -48,40 +49,7 @@ export function UserBulkUploadModal({ onSuccess }: UserBulkUploadModalProps) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
const text = e.target?.result as string;
|
||||
const lines = text.split(/\r?\n/);
|
||||
if (lines.length < 2) {
|
||||
setParsing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const headers = lines[0].split(",").map(h => h.trim().toLowerCase());
|
||||
const data: BulkUserItem[] = [];
|
||||
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (!lines[i].trim()) continue;
|
||||
|
||||
// Simple CSV split (doesn't handle commas in quotes, but enough for basic template)
|
||||
const values = lines[i].split(",").map(v => v.trim());
|
||||
const item: any = { metadata: {} };
|
||||
|
||||
headers.forEach((header, index) => {
|
||||
const value = values[index];
|
||||
if (!value) return;
|
||||
|
||||
if (["email", "name", "phone", "role", "companycode", "department"].includes(header)) {
|
||||
const key = header === "companycode" ? "companyCode" : header;
|
||||
item[key] = value;
|
||||
} else {
|
||||
item.metadata[header] = value;
|
||||
}
|
||||
});
|
||||
|
||||
if (item.email && item.name) {
|
||||
data.push(item as BulkUserItem);
|
||||
}
|
||||
}
|
||||
|
||||
const data = parseUserCSV(text);
|
||||
setPreviewData(data);
|
||||
setParsing(false);
|
||||
};
|
||||
|
||||
46
adminfront/src/features/users/utils/csvParser.test.ts
Normal file
46
adminfront/src/features/users/utils/csvParser.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseUserCSV } from "./csvParser";
|
||||
|
||||
describe("parseUserCSV", () => {
|
||||
it("should parse valid CSV correctly", () => {
|
||||
const csv = `email,name,phone,role,companyCode,department,emp_id
|
||||
user1@test.com,Hong Gil Dong,010-1111-2222,user,baron,HR,E001
|
||||
user2@test.com,Kim Cheol Su,,admin,baron,IT,E002`;
|
||||
|
||||
const result = parseUserCSV(csv);
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]).toEqual({
|
||||
email: "user1@test.com",
|
||||
name: "Hong Gil Dong",
|
||||
phone: "010-1111-2222",
|
||||
role: "user",
|
||||
companyCode: "baron",
|
||||
department: "HR",
|
||||
metadata: {
|
||||
emp_id: "E001",
|
||||
},
|
||||
});
|
||||
expect(result[1].email).toBe("user2@test.com");
|
||||
expect(result[1].metadata.emp_id).toBe("E002");
|
||||
});
|
||||
|
||||
it("should return empty array for empty input", () => {
|
||||
expect(parseUserCSV("")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should skip rows without email or name", () => {
|
||||
const csv = `email,name
|
||||
,Only Name
|
||||
no-name@test.com,`;
|
||||
expect(parseUserCSV(csv)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should handle mixed case headers", () => {
|
||||
const csv = `EMAIL,Name,CompanyCode
|
||||
test@test.com,Test,baron`;
|
||||
const result = parseUserCSV(csv);
|
||||
expect(result[0].email).toBe("test@test.com");
|
||||
expect(result[0].companyCode).toBe("baron");
|
||||
});
|
||||
});
|
||||
40
adminfront/src/features/users/utils/csvParser.ts
Normal file
40
adminfront/src/features/users/utils/csvParser.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { type BulkUserItem } from "../../../lib/adminApi";
|
||||
|
||||
export function parseUserCSV(text: string): BulkUserItem[] {
|
||||
const lines = text.split(/\r?\n/);
|
||||
if (lines.length < 2) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const headers = lines[0].split(",").map((h) => h.trim().toLowerCase());
|
||||
const data: BulkUserItem[] = [];
|
||||
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (!lines[i].trim()) continue;
|
||||
|
||||
const values = lines[i].split(",").map((v) => v.trim());
|
||||
const item: any = { metadata: {} };
|
||||
|
||||
headers.forEach((header, index) => {
|
||||
const value = values[index];
|
||||
if (value === undefined || value === "") return;
|
||||
|
||||
if (
|
||||
["email", "name", "phone", "role", "companycode", "department"].includes(
|
||||
header,
|
||||
)
|
||||
) {
|
||||
const key = header === "companycode" ? "companyCode" : header;
|
||||
item[key] = value;
|
||||
} else {
|
||||
item.metadata[header] = value;
|
||||
}
|
||||
});
|
||||
|
||||
if (item.email && item.name) {
|
||||
data.push(item as BulkUserItem);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user