diff --git a/src/puter-js/src/modules/teams/lib/shapes.js b/src/puter-js/src/modules/teams/lib/shapes.js index fec6e9b8b..e85554f0c 100644 --- a/src/puter-js/src/modules/teams/lib/shapes.js +++ b/src/puter-js/src/modules/teams/lib/shapes.js @@ -45,6 +45,8 @@ export function toMember (row) { ...(typeof row.created_at === 'string' ? { createdAt: row.created_at } : {}), // Owners only, and what billing keys a seat's plan on. ...(typeof row.uuid === 'string' ? { uuid: row.uuid } : {}), + // Owners only; true until the account sets its own password. + ...(typeof row.password_pending === 'boolean' ? { passwordPending: row.password_pending } : {}), }; } diff --git a/src/puter-js/src/modules/teams/listMembers.js b/src/puter-js/src/modules/teams/listMembers.js index 6e9579877..2f9affe96 100644 --- a/src/puter-js/src/modules/teams/listMembers.js +++ b/src/puter-js/src/modules/teams/listMembers.js @@ -28,7 +28,7 @@ import { mapListResult, toMember } from './lib/shapes.js'; * Returns the accounts belonging to a team. Any member may call it. From an * app it needs the team's directory open (else `team_not_found`), lists * active accounts only, and never carries `uuid`. The response carries no - * email, activation state or usage. + * email or usage, and only the team owner gets `passwordPending`. * * @this {import('./index.js').TeamsModule} * @param {string} uid diff --git a/src/puter-js/src/modules/teams/teams.test.js b/src/puter-js/src/modules/teams/teams.test.js index 7ff2c7250..7c2bb740b 100644 --- a/src/puter-js/src/modules/teams/teams.test.js +++ b/src/puter-js/src/modules/teams/teams.test.js @@ -133,6 +133,27 @@ describe('list forms', () => { expect('uuid' in member).toBe(false); }); + it('carries passwordPending when the row has it, true or false', async () => { + routes({ 'GET /teams/t-1/members': { items: [ + { username: 'ann', org_owned: true, created_at: 'x', uuid: 'u-1', password_pending: true }, + { username: 'bob', org_owned: false, created_at: 'x', uuid: 'u-2', password_pending: false }, + { username: 'cat', org_owned: true, created_at: 'x' }, + ] } }); + const [ann, bob, cat] = await teams.listMembers('t-1'); + expect(ann.passwordPending).toBe(true); + expect(bob.passwordPending).toBe(false); + expect('passwordPending' in cat).toBe(false); + }); + + it('drops a non-boolean password_pending rather than coercing it', async () => { + routes({ 'GET /teams/t-1/members': { items: [1, 'true', null].map((value, i) => ( + { username: `u${i}`, org_owned: true, created_at: 'x', password_pending: value } + )) } }); + const members = await teams.listMembers('t-1'); + expect(members).toHaveLength(3); + for (const m of members) expect('passwordPending' in m).toBe(false); + }); + it('keeps a false orgOwned for an account caller', async () => { routes({ 'GET /teams/t-1/members': { items: [ { username: 'ann', org_owned: false, created_at: 'x', uuid: 'u-1' }, diff --git a/src/puter-js/src/modules/teams/types.js b/src/puter-js/src/modules/teams/types.js index babba16d0..8c88afeed 100644 --- a/src/puter-js/src/modules/teams/types.js +++ b/src/puter-js/src/modules/teams/types.js @@ -51,6 +51,7 @@ * @property {string} [createdAt] When the account joined the team. Absent when an app calls. * @property {string} [uuid] Present only when the team owner calls with their own session or * API token; billing keys a seat's plan on it. + * @property {boolean} [passwordPending] True until the account sets its own password; sent to the team owner only. */ /** diff --git a/src/puter-js/tests/api/suites/teams.suite.ts b/src/puter-js/tests/api/suites/teams.suite.ts index 5c14f1398..32d1418e1 100644 --- a/src/puter-js/tests/api/suites/teams.suite.ts +++ b/src/puter-js/tests/api/suites/teams.suite.ts @@ -1,6 +1,5 @@ import { suite } from '../harness/types.ts'; import type { - SuiteTest, SuiteTestSpec, TestContext, } from '../harness/types.ts'; @@ -15,12 +14,16 @@ const CLIENT_SIDE = new Set([ ]); const needsTeams = ( - tests: Record, + tests: Record, ): Record => Object.fromEntries( - Object.entries(tests).map(([name, fn]) => [ + Object.entries(tests).map(([name, spec]) => [ name, - CLIENT_SIDE.has(name) ? fn : { requires: ['teams'], fn }, + CLIENT_SIDE.has(name) + ? spec + : typeof spec === 'function' + ? { requires: ['teams'], fn: spec } + : { ...spec, requires: ['teams', ...(spec.requires ?? [])] }, ]), ); @@ -166,6 +169,70 @@ export default suite('teams', needsTeams({ ); }, + 'listMembers tells the owner which seats still have a password to set': async (t) => { + const team = await makeTeam(t, 'pending'); + const username = `tsp${tag()}`; + await t.puter.teams.createMember(team.uid, { username }); + + const members = (await t.puter.teams.listMembers(team.uid)) as Array<{ + username: string; passwordPending?: boolean; + }>; + const seat = members.find((m) => m.username === username); + const owner = members.find((m) => m.username === t.env.users.user.username); + t.assert.equal(seat?.passwordPending, true); + t.assert.equal(owner?.passwordPending, false); + }, + + "a member's listMembers carries no passwordPending": { + // Sends the seat's session cookie by hand, which a browser page cannot. + platforms: ['node', 'workerd'], + fn: async (t) => { + const team = await makeTeam(t, 'member-view'); + const username = `tsv${tag()}`; + const { temporaryPassword } = (await t.puter.teams.createMember(team.uid, { + username, + })) as { temporaryPassword: string }; + + const login = await fetch(`${t.env.origin}/login`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', Origin: t.env.origin }, + body: JSON.stringify({ username, password: temporaryPassword }), + }); + t.assert.ok(login.ok, `seat login failed: ${login.status}`); + // The body carries a GUI token; the change-password gate wants the session cookie's. + const setCookies = login.headers.getSetCookie?.() ?? [login.headers.get('set-cookie') ?? '']; + const cookie = setCookies.find((c) => /httponly/iu.test(c))?.split(';')[0] ?? ''; + const token = cookie.slice(cookie.indexOf('=') + 1); + t.assert.ok(cookie.includes('=') && token.length > 0, 'login should set a session cookie'); + + const changed = await fetch(`${t.env.origin}/user-protected/change-password`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Origin: t.env.origin, + Authorization: `Bearer ${token}`, + Cookie: cookie, + }, + body: JSON.stringify({ password: temporaryPassword, new_pass: `seat-${tag()}-${tag()}` }), + }); + t.assert.ok(changed.ok, `password change failed: ${changed.status}`); + + t.puter.setAuthToken(token); + try { + const members = (await t.puter.teams.listMembers(team.uid)) as Array>; + t.assert.ok(members.length > 0, 'the member should see the roster'); + for (const m of members) { + // orgOwned shows the account view was served, not the username-only app view. + t.assert.equal(typeof m.orgOwned, 'boolean'); + t.assert.ok(!('uuid' in m), `${String(m.username)} should carry no uuid`); + t.assert.ok(!('passwordPending' in m), `${String(m.username)} should carry no passwordPending`); + } + } finally { + t.puter.setAuthToken(t.env.users.user.token); + } + }, + }, + 'createMember with a taken username rejects': async (t) => { const team = await makeTeam(t, 'taken'); await t.assert.rejects(