From 1fbebddd7174b5a37ade4623629a184d4d401c35 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 16 May 2023 05:00:06 +0200 Subject: [PATCH] more tests for transactions and finding users by different identifiers --- backend/jest.config.js | 2 +- .../src/graphql/resolver/UserResolver.test.ts | 38 +++++++++++++++++-- .../graphql/resolver/util/validateAlias.ts | 3 +- frontend/src/validation-rules.js | 2 +- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/backend/jest.config.js b/backend/jest.config.js index 3b251916a..81ebbec55 100644 --- a/backend/jest.config.js +++ b/backend/jest.config.js @@ -7,7 +7,7 @@ module.exports = { collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'], coverageThreshold: { global: { - lines: 86, + lines: 89, }, }, setupFiles: ['/test/testSetup.ts'], diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index f46d0a9bc..25787490f 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -2358,15 +2358,21 @@ describe('UserResolver', () => { mutation: login, variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, }) + await mutate({ + mutation: updateUserInfos, + variables: { + alias: 'bibi', + }, + }) }) - describe('identifier is no gradido ID and no email', () => { + describe('identifier is no gradido ID, no email and no alias', () => { it('throws and logs "Unknown identifier type" error', async () => { await expect( query({ query: userQuery, variables: { - identifier: 'identifier', + identifier: 'identifier_is_no_valid_alias!', }, }), ).resolves.toEqual( @@ -2374,7 +2380,10 @@ describe('UserResolver', () => { errors: [new GraphQLError('Unknown identifier type')], }), ) - expect(logger.error).toBeCalledWith('Unknown identifier type', 'identifier') + expect(logger.error).toBeCalledWith( + 'Unknown identifier type', + 'identifier_is_no_valid_alias!', + ) }) }) @@ -2441,6 +2450,29 @@ describe('UserResolver', () => { ) }) }) + + describe('identifier is found via alias', () => { + it('returns user', async () => { + await expect( + query({ + query: userQuery, + variables: { + identifier: 'bibi', + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + data: { + user: { + firstName: 'Bibi', + lastName: 'Bloxberg', + }, + }, + errors: undefined, + }), + ) + }) + }) }) }) diff --git a/backend/src/graphql/resolver/util/validateAlias.ts b/backend/src/graphql/resolver/util/validateAlias.ts index 3afc9c7d0..88cb9b982 100644 --- a/backend/src/graphql/resolver/util/validateAlias.ts +++ b/backend/src/graphql/resolver/util/validateAlias.ts @@ -3,7 +3,8 @@ import { User as DbUser } from '@entity/User' import { LogError } from '@/server/LogError' -export const validAliasRegex = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9])*$/ +// eslint-disable-next-line security/detect-unsafe-regex +export const validAliasRegex = /^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/ const reservedAlias = [ 'admin', diff --git a/frontend/src/validation-rules.js b/frontend/src/validation-rules.js index 53b301676..0c30b96ee 100644 --- a/frontend/src/validation-rules.js +++ b/frontend/src/validation-rules.js @@ -141,7 +141,7 @@ export const loadAllRules = (i18nCallback, apollo) => { extend('usernameUnique', { validate(value) { - if (value.match(/^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9])*$/)) { + if (value.match(/^(?=.{3,20}$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+?)*$/)) { return apollo .query({ query: checkUsername,