From e013a72ddd6691a0da8072caf1b5b099c2ec88dd Mon Sep 17 00:00:00 2001 From: olav Date: Wed, 22 Jun 2022 15:37:26 +0200 Subject: [PATCH] refactor: add missing tokenUserSchema fields (#1742) --- .../__snapshots__/token-user-schema.test.ts.snap | 6 +++--- src/lib/openapi/spec/token-user-schema.test.ts | 7 ++++--- src/lib/openapi/spec/token-user-schema.ts | 14 ++++++++++++-- src/lib/services/user-service.ts | 16 ++-------------- .../__snapshots__/openapi.e2e.test.ts.snap | 15 ++++++++++++++- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap b/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap index 1ffcc3b78b..c2147b9a73 100644 --- a/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap +++ b/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`tokenUserSchema empty 1`] = ` +exports[`tokenUserSchema 1`] = ` Object { "data": Object {}, "errors": Array [ Object { "instancePath": "", "keyword": "required", - "message": "must have required property 'createdBy'", + "message": "must have required property 'id'", "params": Object { - "missingProperty": "createdBy", + "missingProperty": "id", }, "schemaPath": "#/required", }, diff --git a/src/lib/openapi/spec/token-user-schema.test.ts b/src/lib/openapi/spec/token-user-schema.test.ts index 2cd3fae92e..c0bbe6256b 100644 --- a/src/lib/openapi/spec/token-user-schema.test.ts +++ b/src/lib/openapi/spec/token-user-schema.test.ts @@ -3,10 +3,13 @@ import { TokenUserSchema } from './token-user-schema'; test('tokenUserSchema', () => { const data: TokenUserSchema = { + id: 0, + name: '', + email: '', createdBy: '', token: '', role: { - id: 1, + id: 0, description: '', name: '', type: '', @@ -16,9 +19,7 @@ test('tokenUserSchema', () => { expect( validateSchema('#/components/schemas/tokenUserSchema', data), ).toBeUndefined(); -}); -test('tokenUserSchema empty', () => { expect( validateSchema('#/components/schemas/tokenUserSchema', {}), ).toMatchSnapshot(); diff --git a/src/lib/openapi/spec/token-user-schema.ts b/src/lib/openapi/spec/token-user-schema.ts index efafc3c8e3..50fa38a122 100644 --- a/src/lib/openapi/spec/token-user-schema.ts +++ b/src/lib/openapi/spec/token-user-schema.ts @@ -5,14 +5,24 @@ export const tokenUserSchema = { $id: '#/components/schemas/tokenUserSchema', type: 'object', additionalProperties: false, - required: ['createdBy', 'token', 'role'], + required: ['id', 'name', 'email', 'token', 'createdBy', 'role'], properties: { - createdBy: { + id: { + type: 'number', + }, + name: { + type: 'string', + }, + email: { type: 'string', }, token: { type: 'string', }, + createdBy: { + type: 'string', + nullable: true, + }, role: { $ref: '#/components/schemas/roleSchema', }, diff --git a/src/lib/services/user-service.ts b/src/lib/services/user-service.ts index 19c3a83c21..a983abb546 100644 --- a/src/lib/services/user-service.ts +++ b/src/lib/services/user-service.ts @@ -27,6 +27,7 @@ import DisabledError from '../error/disabled-error'; import PasswordMismatch from '../error/password-mismatch'; import BadDataError from '../error/bad-data-error'; import { isDefined } from '../util/isDefined'; +import { TokenUserSchema } from '../openapi/spec/token-user-schema'; const systemUser = new User({ id: -1, username: 'system' }); @@ -56,19 +57,6 @@ interface IUserWithRole extends IUser { rootRole: number; } -interface IRoleDescription { - id: number; - description: string; - name: string; - type: string; -} - -interface ITokenUser extends IUpdateUser { - createdBy: string; - token: string; - role: IRoleDescription; -} - const saltRounds = 10; class UserService { @@ -362,7 +350,7 @@ class UserService { return this.store.setPasswordHash(userId, passwordHash); } - async getUserForToken(token: string): Promise { + async getUserForToken(token: string): Promise { const { createdBy, userId } = await this.resetTokenService.isValid( token, ); diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index 9ff7ce4e7a..9705a96a96 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -1502,6 +1502,16 @@ Object { "additionalProperties": false, "properties": Object { "createdBy": Object { + "nullable": true, + "type": "string", + }, + "email": Object { + "type": "string", + }, + "id": Object { + "type": "number", + }, + "name": Object { "type": "string", }, "role": Object { @@ -1512,8 +1522,11 @@ Object { }, }, "required": Array [ - "createdBy", + "id", + "name", + "email", "token", + "createdBy", "role", ], "type": "object",