From 0663a70c97b7a689c904c89d3158b2e157580f44 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 16 Feb 2025 15:54:34 -0500 Subject: [PATCH] Force the password-reset state to Unauthorized on most auth-required routes --- visions/client/Taskfile.yml | 2 +- visions/client/jest.config.js | 3 ++- visions/client/src/client.test.ts | 19 +++++++++---------- visions/client/src/client.ts | 19 +++---------------- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/visions/client/Taskfile.yml b/visions/client/Taskfile.yml index fc9eefd..c9afb69 100644 --- a/visions/client/Taskfile.yml +++ b/visions/client/Taskfile.yml @@ -13,4 +13,4 @@ tasks: test: cmds: - - npx jest src/ + - npx jest diff --git a/visions/client/jest.config.js b/visions/client/jest.config.js index f5d30d1..73259d6 100644 --- a/visions/client/jest.config.js +++ b/visions/client/jest.config.js @@ -1,7 +1,8 @@ /** @type {import('ts-jest').JestConfigWithTsJest} **/ module.exports = { testEnvironment: "node", + testMatch: [ "**/*.test.ts" ], transform: { "^.+.tsx?$": ["ts-jest",{}], }, -}; \ No newline at end of file +}; diff --git a/visions/client/src/client.test.ts b/visions/client/src/client.test.ts index 2279b29..ea3f7e7 100644 --- a/visions/client/src/client.test.ts +++ b/visions/client/src/client.test.ts @@ -6,7 +6,7 @@ describe('what happens in an authentication', () => { let response = await client.auth('vakarian', 'aoeu') expect(response).toEqual({ status: 'ok', - content: 'vakarian-session-id', + content: { type: 'success', content: 'vakarian-session-id' }, }) }) @@ -28,8 +28,8 @@ describe('what happens in an authentication', () => { { let response = await client.auth('shephard', 'aoeu') expect(response).toEqual({ - status: 'password-reset', - content: 'shephard-session-id', + status: 'ok', + content: { type: 'password-reset', content: 'shephard-session-id' }, }) } { @@ -42,8 +42,8 @@ describe('what happens in an authentication', () => { let client = new Connection(new URL('http://127.0.0.1:8001')) { let authResponse = await client.auth('vakarian', 'aoeu') - if (authResponse.status === 'ok') { - let sessionId = authResponse.content + if (authResponse.status === 'ok' && authResponse.content.type === 'success') { + let sessionId = authResponse.content.content let response = await client.listUsers(sessionId) expect(response).toEqual({ status: 'ok', @@ -74,12 +74,11 @@ describe('what happens in an authentication', () => { } { let authResponse = await client.auth('shephard', 'aoeu') - if (authResponse.status === 'password-reset') { - let sessionId = authResponse.content - let response = await client.listUsers(sessionId) - expect(response).toEqual({ status: 'unauthorized' }) + if (authResponse.status === 'ok' && authResponse.content.type === 'password-reset') { + let sessionId = authResponse.content.content + expect(await client.listUsers(sessionId)).toEqual({ status: 'unauthorized' }) } else { - throw new Error('authorization should have been password-reset') + throw new Error('Authorization shuld have been password-reset') } } /* diff --git a/visions/client/src/client.ts b/visions/client/src/client.ts index fcf2a3f..85c7781 100644 --- a/visions/client/src/client.ts +++ b/visions/client/src/client.ts @@ -1,17 +1,16 @@ -import { VResponse, SessionId, UserOverview } from '../gen/types' +import { AuthResponse, SessionId, UserOverview } from '../gen/types' export interface Client { auth: ( username: string, password: string, - ) => Promise> + ) => Promise>> listUsers: (sessionId: SessionId) => Promise> } export type ClientResponse = | { status: 'ok'; content: A } - | { status: 'password-reset'; content: SessionId } | { status: 'unauthorized' } | { status: 'unexpected'; code: number } @@ -25,7 +24,7 @@ export class Connection implements Client { async auth( username: string, password: string, - ): Promise> { + ): Promise>> { const url = new URL(this.base) url.pathname = `/api/test/auth` const response = await fetch(url, { @@ -35,12 +34,6 @@ export class Connection implements Client { }) if (response.ok) { let resp = await response.json() - switch (resp.type) { - case 'success': - return { status: 'ok', content: resp.content } - case 'password-reset': - return { status: 'password-reset', content: resp.content } - } return { status: 'ok', content: resp } } else if (response.status == 401) { return { status: 'unauthorized' } @@ -60,12 +53,6 @@ export class Connection implements Client { }) if (response.ok) { let resp = await response.json() - switch (resp.type) { - case 'success': - return { status: 'ok', content: resp.content } - case 'password-reset': - return { status: 'password-reset', content: resp.content } - } return { status: 'ok', content: resp } } else if (response.status == 401) { return { status: 'unauthorized' }