Skip to content

Commit 9e0c0c2

Browse files
test(plugin-auth): the forced-reset OAuth test takes its token time from the routine reset, not the wall clock (#36706)
<!-- claude-projects-attribution: {"github_login":"os-warren"} --> _Requested by **Warren** · [project thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8XGffEBTNhotjbfjKWmvMXT)_ Fixes #36361 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用户无感,只改一条登录插件的测试;产品行为不变。 - 为什么做:这条测试在合并队列满载时会偶发变红,把无关的 PR 踢出队列,堵住主干。 - 出错会怎样:只影响测试本身,产品代码一行未动,可直接撤回。 - 要你做什么:无。 ## What changes One test, `an administrator's forced reset ends them too and reports the apps; with revocation off it ends none` in `packages/plugins/plugin-auth/src/password-reset-ends-added-access.test.ts`. Test-only: no product file moves. The test used to take its token's `iat` as `Math.floor(Date.now() / 1000) + 1` **before** the routine admin reset ran. Now it runs the routine reset first, reads the credential-change time that reset wrote (`readCredentialChangedAt`, the same reader the MCP door uses), and takes `iat` as the first whole second at or after it (`Math.ceil(changedAtMs / 1000)`). The forced-reset half is unchanged. The slow call is pinned: just before the routine reset the test moves the wall clock two seconds on (`vi.useFakeTimers({ toFake: ['Date'] })` + `vi.setSystemTime`, the same pattern the file's credential-change describe already uses), standing in for an admin call that takes longer than a second under load. Every run now takes the path the queue build failed on. The describe's `afterEach` restores real timers. ## Why — root cause is the test's clock, not the product `mcpConsentAdmits` places a token with no millisecond claim on a consent row at `iat * 1000` and refuses it when that is earlier than the account's credential-change time (`issuedBeforeCredentialChange`). The routine reset (`revokeSessionsAndKeys: false`) writes a new change time. The old test fixed `iat` at most one second ahead of the clock it read *before* the reset, so it assumed the admin HTTP call finished inside that second. Under full-suite load the call took longer, the change time landed after `iat * 1000`, and the door correctly refused a token that, by its own `iat`, predates the reset: `expected false to be true` at line 538 (merge-queue run 44999732595, Test Core 10/10). The product rule is right; the test's premise about timing was wrong. The two other `+ 1` sites in the file (the self-service reset case at line 468, the disabled-app case at line 983) have no credential change between taking `iat` and the assertion that needs it to stand, so they cannot hit this. ## Tests All on head `b4de067d`. - **The pin fails without the fix.** `main`'s version of the test with only the two clock lines added fails at the card's assertion: `AssertionError: expected false to be true` at the `grantStands()` right after the routine reset (the same line as merge-queue run 44999732595, shifted by the two inserted lines). The fixed test passes in the same run. The `main` copy was temporary and is not in the diff. - Before the pin existed, the same was shown with a real 1.5 s pause in place of the clock move: red on `main`, green with the fix. - The fixed test, 20 consecutive runs: 20/20 passed. - `pnpm --filter @objectstack/plugin-auth typecheck` (including `check:test-typecheck`): passed. - `pnpm --filter @objectstack/plugin-auth test`: 293 files, 6265 passed, 10 skipped. - `eslint --no-inline-config` on the changed file: clean. No skip, no narrowing, no retry wrapper, no timeout change. ## Acceptance notes None. ## Verification Two-minute check: `pnpm --filter @objectstack/plugin-auth exec vitest run --maxWorkers=2 src/password-reset-ends-added-access.test.ts -t "forced reset ends them too"` — 1 passed. Swap the two `issuedAt` lines back to `Math.floor(Date.now() / 1000) + 1` taken before the routine reset and it fails with `expected false to be true`. Files to read first: in `password-reset-ends-added-access.test.ts`, the forced-reset case (the clock move and the `iat` read back), then the describe's `afterEach` (`vi.useRealTimers()`). Changeset: none; the diff changes a test file only and publishes nothing (`skip-changeset`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JQwxbWUnHRCP5qaRA2VJEj <!-- signed: repo:objectstack#1 · os-warren · session_01JQwxbWUnHRCP5qaRA2VJEj --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6206939 commit 9e0c0c2

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

‎packages/plugins/plugin-auth/src/password-reset-ends-added-access.test.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { SqlDriver } from '@objectstack/driver-sql';
5656
import { hashApiKey, resolveApiKeyAdmission } from '@objectstack/core';
5757
import { createHash } from 'node:crypto';
5858
import { mcpConsentAdmits } from './mcp-token-revocation.js';
59+
import { readCredentialChangedAt } from './credential-change-ends-prior-access.js';
5960
import { endWhatTheAccountRegistered } from './password-reset-ends-added-access.js';
6061
import { mintScimConnectionCredential, verifyScimBearerToken } from './scim-connection-service.js';
6162
import { AuthManager } from './auth-manager.js';
@@ -413,6 +414,7 @@ describe('where the OAuth provider is mounted, a completed reset and a forced re
413414
}
414415
});
415416
afterEach(() => {
417+
vi.useRealTimers();
416418
for (const key of ENV_KEYS) {
417419
if (saved[key] === undefined) delete process.env[key];
418420
else process.env[key] = saved[key];
@@ -525,17 +527,28 @@ describe('where the OAuth provider is mounted, a completed reset and a forced re
525527
await registerClient(s);
526528
await authorize(s, s.userId, 'user');
527529
await authorize(s, s.adminUserId, 'admin');
528-
const issuedAt = Math.floor(Date.now() / 1000) + 1;
529-
const grantStands = () => mcpConsentAdmits(s.auth.adapter, { clientId: CLIENT_ID, userId: s.userId }, issuedAt, s.auth.internalAdapter);
530530

531+
// A routine reset that takes longer than a second, as an administrator's
532+
// call does under load: the wall clock moves two seconds on before it
533+
// writes, so the change it stamps lies past any second read before the call.
534+
vi.useFakeTimers({ toFake: ['Date'] });
535+
vi.setSystemTime(Date.now() + 2_000);
531536
const routine = await s.fire(
532537
'/admin/set-user-password',
533538
{ userId: s.userId, newPassword: SECOND, mustChangePassword: false, revokeSessionsAndKeys: false },
534539
s.bearer(s.adminBearer),
535540
);
536541
expect(routine.status, await routine.clone().text()).toBe(200);
542+
// A token of the grant minted after the routine reset: its `iat` (no
543+
// millisecond claim, so placed at `iat * 1000`) is the first whole second
544+
// at or after the change that reset wrote, read back from the row, so it
545+
// stands however long the reset took.
546+
const changedAtMs = await readCredentialChangedAt(s.auth.internalAdapter, s.userId);
547+
expect(changedAtMs, 'the routine reset stamps a credential change').toBeTypeOf('number');
548+
const issuedAt = Math.ceil(changedAtMs! / 1000);
549+
const grantStands = () => mcpConsentAdmits(s.auth.adapter, { clientId: CLIENT_ID, userId: s.userId }, issuedAt, s.auth.internalAdapter);
537550
expect(await grantsOf(s, s.userId), 'a routine reset ends nothing').toEqual(ALL);
538-
expect(await grantStands()).toBe(true);
551+
expect(await grantStands(), 'the MCP door admits a token of the grant minted after the routine reset').toBe(true);
539552

540553
const forced = await s.fire(
541554
'/admin/set-user-password',

0 commit comments

Comments
 (0)