This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to change their email, displayName and password (#133)
- Loading branch information
1 parent
1a2ea1c
commit 91140b5
Showing
16 changed files
with
230 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,4 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | |
|
||
# Prisma | ||
*.sqlite | ||
*.sqlite-journal |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { IsEmail, IsOptional, Length } from 'class-validator'; | ||
|
||
export class PatchUserDTO { | ||
@IsEmail() | ||
@IsOptional() | ||
email: string | undefined; | ||
|
||
@Length(12, 72) | ||
@IsOptional() | ||
password: string | undefined; | ||
|
||
@Length(2, 128) | ||
@IsOptional() | ||
displayName: string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UserController } from './user.controller'; | ||
import { User } from '@prisma/client'; | ||
import { Prisma, User } from '@prisma/client'; | ||
import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; | ||
import { UserRepository } from '../../db/repositories/user.repository'; | ||
import { AuthService } from '../../auth/auth.service'; | ||
import { TestConstants } from '../../../test/lib/constants'; | ||
import { NestRequest } from '../../types/request.type'; | ||
import { PrismaModule } from '../../db/prisma.module'; | ||
import { Response } from 'express'; | ||
import { PatchUserDTO } from './patch.user.dto'; | ||
|
||
describe('UserController', () => { | ||
let controller: UserController; | ||
let repository: DeepMockProxy<UserRepository>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [PrismaModule], | ||
controllers: [UserController], | ||
}).compile(); | ||
providers: [UserRepository, AuthService], | ||
}) | ||
.overrideProvider(UserRepository) | ||
.useValue(mockDeep<UserRepository>()) | ||
.compile(); | ||
|
||
controller = module.get<UserController>(UserController); | ||
repository = module.get(UserRepository); | ||
}); | ||
|
||
it('should be defined', () => { | ||
|
@@ -31,8 +46,59 @@ describe('UserController', () => { | |
user, | ||
}; | ||
|
||
const response = (await controller.me(req)) as any; | ||
const response = (await controller.getMe(req)) as any; | ||
|
||
expect(response?.password).toBeUndefined(); | ||
}); | ||
|
||
it('should be able to change information about a user', async () => { | ||
repository.updateUser.mockResolvedValue( | ||
TestConstants.database.users.exampleUser, | ||
); | ||
|
||
const changeQuery: Prisma.UserUpdateInput = { | ||
displayName: 'Test 1', | ||
email: '[email protected]', | ||
password: 'TEST123', | ||
}; | ||
|
||
const mockRequest = { | ||
user: { | ||
id: TestConstants.database.users.exampleUser.id, | ||
}, | ||
} as NestRequest; | ||
|
||
const mockResponse = mockDeep<Response>(); | ||
mockResponse.status.mockReturnThis(); | ||
|
||
await controller.patchMe( | ||
mockRequest, | ||
{ | ||
displayName: changeQuery.displayName as string, | ||
email: changeQuery.email as string, | ||
password: changeQuery.password as string, | ||
}, | ||
mockResponse as any, | ||
); | ||
|
||
expect(mockResponse.status).toHaveBeenCalledWith(200); | ||
expect(mockResponse.json).toHaveBeenCalledWith( | ||
TestConstants.database.users.exampleUser, | ||
); | ||
}); | ||
|
||
it('should not be able to modify a user with an empty modify request', async () => { | ||
const mockRequest = mockDeep<NestRequest>(); | ||
const mockResponse = mockDeep<Response>(); | ||
|
||
mockResponse.status.mockReturnThis(); | ||
|
||
await controller.patchMe( | ||
mockRequest as NestRequest, | ||
{} as PatchUserDTO, | ||
mockResponse as Response, | ||
); | ||
|
||
expect(mockResponse.status).toHaveBeenCalledWith(400); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Request } from 'express'; | ||
|
||
export type NestRequest = Request & { | ||
user: { id: string }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Authentication | ||
> [!WARNING] | ||
> The currently used authentication mechanism is subject to changes and only intended for development purposes. | ||
## Local Authentication (Development) | ||
During development `Basic`-Authentication is enabled to make authentication easier. You need to supply a username and a password. | ||
There are example users available, when using the database seeds. | ||
|
||
### Example Users | ||
The password for all available example users is | ||
`1234`. | ||
Please note that those users are not available in production. | ||
|
||
- `[email protected]`: Max Mustermann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Development | ||
> [!WARNING] | ||
> This section is subject to change as it's intended to document the development process. This is not intented for an external audience. |