Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /users tests for user management backend #2790

Merged
merged 71 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
5ae6b3f
:zap: Refactor users namespace
ivov Feb 9, 2022
eadbec0
:zap: Adjust fillout endpoint
ivov Feb 9, 2022
ba9a7ad
:zap: Refactor initTestServer arg
ivov Feb 9, 2022
4d17eb8
:pencil2: Specify agent type
ivov Feb 9, 2022
b251fe0
:pencil2: Specify role type
ivov Feb 9, 2022
a7b74db
:zap: Tighten `/users/:id` check
ivov Feb 9, 2022
ccea112
:sparkles: Add initial tests
ivov Feb 9, 2022
381f111
:truck: Reposition init server map
ivov Feb 9, 2022
27d2393
:zap: Set constants in `validatePassword()`
ivov Feb 10, 2022
4d613e7
:zap: Tighten `/users/:id` check
ivov Feb 10, 2022
9b55b2e
:zap: Improve checks in `/users/:id`
ivov Feb 10, 2022
3e65857
:sparkles: Add tests for `/users/:id`
ivov Feb 10, 2022
03cba0e
:package: Update package-lock.json
ivov Feb 10, 2022
9d6a47e
:zap: Simplify expectation
ivov Feb 10, 2022
1e0b94f
:zap: Reuse util for authless agent
ivov Feb 10, 2022
218f085
:truck: Make role names consistent
ivov Feb 10, 2022
ce93c66
:blue_book: Tighten namespaces map type
ivov Feb 10, 2022
0894f6e
:fire: Remove unneeded default arg
ivov Feb 10, 2022
2279efd
:sparkles: Add tests for `POST /users`
ivov Feb 10, 2022
d99c05a
:blue_book: Create test SMTP account type
ivov Feb 10, 2022
b857097
:pencil2: Improve wording
ivov Feb 10, 2022
673ee5d
:art: Formatting
ivov Feb 10, 2022
858ae42
:fire: Remove temp fix
ivov Feb 11, 2022
0adc929
:zap: Replace helper with config call
ivov Feb 11, 2022
9fbc5bb
:zap: Fix failing tests
ivov Feb 11, 2022
5667a34
:twisted_rightwards_arrows: Merge parent branch
ivov Feb 11, 2022
e021618
:fire: Remove outdated test
ivov Feb 11, 2022
0c904eb
:fire: Remove unused helper
ivov Feb 11, 2022
1ae3ebe
:zap: Increase readability of domain fetcher
ivov Feb 14, 2022
5c657e1
:zap: Refactor payload validation
ivov Feb 14, 2022
3639cda
:fire: Remove repetition
ivov Feb 14, 2022
14000a4
:rewind: Restore logging
ivov Feb 14, 2022
84d1ee0
:zap: Initialize logger in tests
ivov Feb 14, 2022
93c3e02
:fire: Remove redundancy from check
ivov Feb 14, 2022
b116981
:truck: Move `globalOwnerRole` fetching to global scope
ivov Feb 14, 2022
bc1c428
:fire: Remove unused imports
ivov Feb 14, 2022
8c96a51
:truck: Move random utils to own module
ivov Feb 14, 2022
ba2de0e
:truck: Move test types to own module
ivov Feb 14, 2022
af20438
:pencil2: Add dividers to utils
ivov Feb 14, 2022
403f198
:pencil2: Reorder `initTestServer` param docstring
ivov Feb 14, 2022
6169636
:pencil2: Add TODO comment
ivov Feb 14, 2022
273a36b
:zap: Dry up member creation
ivov Feb 14, 2022
771464b
:zap: Tighten search criteria
ivov Feb 14, 2022
fff3504
:test_tube: Add expectation to `GET /users`
ivov Feb 14, 2022
d21d63d
:zap: Create role fetcher utils
ivov Feb 14, 2022
ca46df3
:zap: Create one more role fetch util
ivov Feb 14, 2022
657a4aa
:fire: Remove unneeded DB query
ivov Feb 14, 2022
4e58847
:test_tube: Add expectation to `POST /users`
ivov Feb 14, 2022
3519c0f
:test_tube: Add expectation to `DELETE /users/:id`
ivov Feb 14, 2022
7c55e03
:test_tube: Add another expectation to `DELETE /users/:id`
ivov Feb 14, 2022
371e9ae
:test_tube: Add expectations to `DELETE /users/:id`
ivov Feb 14, 2022
856a96c
:test_tube: Adjust expectations in `POST /users/:id`
ivov Feb 14, 2022
e5fc785
:test_tube: Add expectations to `DELETE /users/:id`
ivov Feb 14, 2022
ed075ec
:twisted_rightwards_arrows: Merge parent branch
ivov Feb 14, 2022
cb460f7
:shirt: Fix build
ivov Feb 14, 2022
f4820af
:zap: Update method
ivov Feb 14, 2022
7273280
:twisted_rightwards_arrows: Merge parent branch
ivov Feb 14, 2022
df7b4ca
:blue_book: Fix `userToDelete` type
ivov Feb 15, 2022
c32423f
:zap: Refactor `createAgent()`
ivov Feb 15, 2022
2dc4838
:zap: Make role fetching global
ivov Feb 15, 2022
2c4579b
:zap: Optimize roles fetching
ivov Feb 15, 2022
9e69323
:zap: Centralize member creation
ivov Feb 15, 2022
ded6663
:zap: Refactor truncation helper
ivov Feb 15, 2022
9136d67
:test_tube: Add teardown to `DELETE /users/:id`
ivov Feb 15, 2022
b17e9d1
:test_tube: Add DB expectations to users tests
ivov Feb 15, 2022
aab2cd9
:fire: Remove pass validation due to hash
ivov Feb 15, 2022
45bfd12
:pencil2: Improve pass validation error message
ivov Feb 15, 2022
44e0fdc
:zap: Improve owner pass validation
ivov Feb 15, 2022
ab7753f
:zap: Create logger initialization helper
ivov Feb 15, 2022
c60226e
:zap: Optimize helpers
ivov Feb 15, 2022
89cabd5
:zap: Restructure `getAllRoles` helper
ivov Feb 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/cli/src/UserManagement/UserManagementHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable import/no-cycle */
import { IsNull, Not } from 'typeorm';
import { Db, ResponseHelper } from '..';
import { Db, GenericHelpers, ResponseHelper } from '..';
import config = require('../../config');
import { User } from '../databases/entities/User';
import { MAX_PASSWORD_LENGTH, MIN_PASSWORD_LENGTH, User } from '../databases/entities/User';
import { PublicUser } from './Interfaces';

export function isEmailSetup(): boolean {
const emailMode = config.get('userManagement.emails.mode') as string;
return !!emailMode;
export const isEmailSetUp = Boolean(config.get('userManagement.emails.mode'));

export function getInstanceDomain(): string {
ivov marked this conversation as resolved.
Show resolved Hide resolved
let domain = GenericHelpers.getBaseUrl();
if (domain.endsWith('/')) {
domain = domain.slice(0, domain.length - 1);
}

return domain;
}

export async function isInstanceOwnerSetup(): Promise<boolean> {
const users = await Db.collections.User!.find({ email: Not(IsNull()) });
return users.length !== 0;
}

// TODO: Enforce at model level
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be a bigger task? out of scope of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashing changes length so pass cannot be validated at model level unless we hash there as well - probably worth discussing. Since the helper is used by multiple namespaces, I decided to leave it untouched.

export function validatePassword(password?: string): string {
if (!password) {
throw new ResponseHelper.ResponseError('Password is mandatory', undefined, 400);
}

if (password.length < 8 || password.length > 64) {
if (password.length < MIN_PASSWORD_LENGTH || password.length > MAX_PASSWORD_LENGTH) {
throw new ResponseHelper.ResponseError(
'Password must be 8 to 64 characters long',
undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/UserManagement/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function addRoutes(this: N8nApp, ignoredEndpoints: string[], restEndpoint
req.url.startsWith('/fonts/') ||
req.url.startsWith(`/${restEndpoint}/settings`) ||
req.url.startsWith(`/${restEndpoint}/resolve-signup-token`) ||
req.url === `/${restEndpoint}/user`
(req.method === 'POST' && new RegExp(`/${restEndpoint}/users/[\\w\\d-]*`).test(req.url))
BHesseldieck marked this conversation as resolved.
Show resolved Hide resolved
) {
return next();
}
Expand Down
Loading