Skip to content

Commit b4de41f

Browse files
committed
test(plugin-auth): carry the admin import round-trip pin on the file's existing engine double (#25078)
Claude-Session: https://claude.ai/code/session_01LmjU2x1faLZ39DVTFyVca6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 83c2dd1 commit b4de41f

4 files changed

Lines changed: 72 additions & 117 deletions

File tree

‎packages/plugins/plugin-auth/src/admin-import-users-csv-text-cell.test.ts‎

Lines changed: 0 additions & 115 deletions
This file was deleted.

‎packages/plugins/plugin-auth/src/admin-import-users.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { readFileSync } from 'node:fs';
55
import { dirname, resolve } from 'node:path';
66
import { fileURLToPath } from 'node:url';
77
import { assertEngineUpdateDispatch } from '@objectstack/objectql';
8+
import { RestServer } from '@objectstack/rest';
89
import { runAdminImportUsers, IMPORT_USERS_MAX_ROWS, type IdentityImportDeps } from './admin-import-users.js';
910
import { settleImportResetDelivery } from './account-invitation-marker.js';
1011
import type { AdminActor } from './admin-user-endpoints.js';
@@ -719,6 +720,75 @@ describe('runAdminImportUsers — CSV payloads', () => {
719720
});
720721
});
721722

723+
/**
724+
* [#25078] Export → admin user import round trip, through the CSV text-cell rule.
725+
*
726+
* `@objectstack/rest`'s CSV export writes a text cell its consumer would
727+
* interpret in a form that consumer reads as text, and its CSV import parse
728+
* (`parseCsvToRows`, reached through `prepareImportRequest`) reverses exactly
729+
* that. This door parses a `csv` body through that same `prepareImportRequest`,
730+
* so a user list exported as CSV and imported back here must hand better-auth's
731+
* `createUser` every stored name exactly.
732+
*
733+
* The file comes from the REAL export route of `@objectstack/rest`'s
734+
* `RestServer`, over a protocol double serving a two-field `sys_user` schema and
735+
* the rows below. The rule is pinned at its readers in `@objectstack/rest`
736+
* (`csv-text-cell.test.ts`), and its other CSV doors in that package's
737+
* `csv-text-cell-round-trip.test.ts`.
738+
*/
739+
describe('runAdminImportUsers — CSV export → import round trip (#25078)', () => {
740+
/** Plain words after a trigger character: marked by the export. */
741+
const MARKED = ['=Total', '+Name', '-Note', '@acme'];
742+
/** Ordinary business values, and a user-authored name that begins with the mark itself. */
743+
const UNMARKED = ['+86 138 0013 8000', '+1 555 010 0000', '-120.50', 'Acme Ltd', '120.50', "'Quoted"];
744+
const NAMES = [...MARKED, ...UNMARKED];
745+
746+
async function exportUsersCsv(rows: Array<Record<string, string>>): Promise<string> {
747+
const noop = () => {};
748+
const server = { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} };
749+
const schema = { name: 'sys_user', fields: { email: { name: 'email', type: 'email' }, name: { name: 'name', type: 'text' } } };
750+
const protocol = {
751+
getMetaItem: vi.fn(async () => ({ type: 'object', name: 'sys_user', item: schema })),
752+
findData: vi.fn(async () => ({ object: 'sys_user', records: rows, total: rows.length })),
753+
};
754+
const rest = new RestServer(server as any, protocol as any, { api: { requireAuth: false } } as any);
755+
(rest as any).resolveExecCtx = async () => ({ userId: ACTOR.id });
756+
rest.registerRoutes();
757+
const route: any = rest.getRoutes().find(
758+
(r: any) => r.method === 'GET' && r.path === '/api/v1/data/:object/export',
759+
);
760+
expect(route).toBeDefined();
761+
const chunks: string[] = [];
762+
const res: any = {
763+
write: (s: unknown) => { chunks.push(String(s)); return true; },
764+
end: () => {},
765+
header: () => res,
766+
status: () => res,
767+
json: () => res,
768+
};
769+
await route.handler({ params: { object: 'sys_user' }, query: { format: 'csv' } }, res);
770+
return chunks.join('');
771+
}
772+
773+
it('hands createUser every exported name exactly', async () => {
774+
const rows = NAMES.map((name, i) => ({ email: `user${i + 1}@example.com`, name }));
775+
const csv = await exportUsersCsv(rows);
776+
777+
// The file carried the marks, so the round trip below is not a pass-through.
778+
expect(csv.split('\r\n')[0]).toBe('email,name');
779+
for (const [i, v] of MARKED.entries()) expect(csv).toContain(`user${i + 1}@example.com,'${v}\r\n`);
780+
expect(csv).toContain(',+86 138 0013 8000\r\n');
781+
expect(csv).toContain(",'Quoted\r\n");
782+
783+
const m = makeDeps();
784+
const res = await runAdminImportUsers(m.deps, makeRequest({ passwordPolicy: 'temporary', csv }), ACTOR);
785+
expect(res.status).toBe(200);
786+
expect((res.body.data as any).summary.created).toBe(NAMES.length);
787+
const byEmail = new Map(m.createUser.mock.calls.map((c: any[]) => [c[0].body.email, c[0].body.name]));
788+
expect(rows.map((r) => byEmail.get(r.email))).toEqual(NAMES);
789+
});
790+
});
791+
722792
/**
723793
* [#17422] The IMPLEMENTOR half of #16952's contract.
724794
*

‎packages/rest/src/csv-text-cell-round-trip.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Every stored value comes back exactly. Each case also asserts that the file
2121
* it imported carried the marks, so the round trip cannot pass by the export
2222
* never having marked anything. plugin-auth's admin user import is pinned in
23-
* its own package (`admin-import-users-csv-text-cell.test.ts`).
23+
* its own package (`admin-import-users.test.ts`).
2424
*
2525
* Control characters are written as escapes in this file, never as raw bytes.
2626
*/

‎packages/rest/src/csv-text-cell.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* The round trip through each import door that parses CSV is pinned in
1919
* `csv-text-cell-round-trip.test.ts` (sync, async job, named mapping) and in
20-
* plugin-auth's `admin-import-users-csv-text-cell.test.ts` (admin user import).
20+
* plugin-auth's `admin-import-users.test.ts` (admin user import).
2121
*
2222
* Control characters are written as escapes in this file, never as raw bytes.
2323
*/

0 commit comments

Comments
 (0)