Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ async function handleAdmin(request: Request, env: StartupAPIEnv, usersPath: stri
const userId = parts[1];
if (request.method === 'GET') return Response.json(await systemStub.getUser(userId));
if (request.method === 'DELETE') return Response.json(await systemStub.deleteUser(userId));
if (request.method === 'PATCH' || request.method === 'PUT') {
const data = (await request.json()) as any;
return Response.json(await systemStub.updateUser(userId, data));
}
}
if (parts.length === 3 && parts[2] === 'memberships' && request.method === 'GET') {
const userId = parts[1];
Expand Down
44 changes: 44 additions & 0 deletions test/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,50 @@ describe('Admin Administration', () => {
expect(Object.keys(profile).length).toBe(0);
});

it('should update user profile via admin API', async () => {
// 1. Get an admin user
const adminId = env.USER.idFromName('admin');
const adminStub = env.USER.get(adminId);
const adminIdStr = adminId.toString();

const { sessionId } = await adminStub.createSession();
const cookieHeader = `session_id=${await cookieManager.encrypt(`${sessionId}:${adminIdStr}`)}`;

// 2. Create a user to update
const userId = env.USER.newUniqueId();
const userIdStr = userId.toString();
const systemStub = env.SYSTEM.get(env.SYSTEM.idFromName('global'));
await systemStub.registerUser({
id: userIdStr,
name: 'Original Name',
email: 'original@example.com',
});
const userStub = env.USER.get(userId);
await userStub.updateProfile({ name: 'Original Name', email: 'original@example.com' });

// 3. Update the user
const updatedName = 'Updated Name Admin';
const res = await SELF.fetch(`http://example.com/users/admin/api/users/${userIdStr}`, {
method: 'PATCH',
headers: {
Cookie: cookieHeader,
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: updatedName }),
});

expect(res.status).toBe(200);

// 4. Verify in SystemDO index
const users = await systemStub.listUsers();
const updatedUser = users.find((u: any) => u.id === userIdStr);
expect(updatedUser.name).toBe(updatedName);

// 5. Verify in UserDO profile
const profile = await userStub.getProfile();
expect(profile.name).toBe(updatedName);
});

it('should remove memberships from users when account is deleted', async () => {
// 1. Get an admin user
const adminId = env.USER.idFromName('admin');
Expand Down