Repository navigation
refactor(user-tokens): migrate from server #2279
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
Open
+762
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
60 changes: 60 additions & 0 deletions
60
apps/server-nestjs/src/modules/user-tokens/user-tokens-queries.utils.spec.ts
This file contains hidden or 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,60 @@ | ||
| import type { Prisma } from '@prisma/client' | ||
| import type { DeepMockProxy } from 'vitest-mock-extended' | ||
| import { faker } from '@faker-js/faker' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { mockDeep } from 'vitest-mock-extended' | ||
| import { | ||
| createUserToken, | ||
| deleteUserToken, | ||
| listUserTokens, | ||
| userTokenSelect, | ||
| } from './user-tokens-queries.utils' | ||
|
|
||
| describe('user-tokens-queries.utils', () => { | ||
| let tx: DeepMockProxy<Prisma.TransactionClient> | ||
|
|
||
| beforeEach(() => { | ||
| tx = mockDeep<Prisma.TransactionClient>() | ||
| }) | ||
|
|
||
| describe('listUserTokens', () => { | ||
| it('scopes by userId and uses the shared select', async () => { | ||
| const userId = faker.string.uuid() | ||
| tx.personalAccessToken.findMany.mockResolvedValue([]) | ||
|
|
||
| await listUserTokens(tx, userId) | ||
|
|
||
| expect(tx.personalAccessToken.findMany).toHaveBeenCalledWith( | ||
| expect.objectContaining({ where: { userId }, select: userTokenSelect }), | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('createUserToken', () => { | ||
| it('creates a personal access token with the provided fields', async () => { | ||
| const data = { | ||
| name: faker.word.noun(), | ||
| expirationDate: faker.date.future(), | ||
| hash: faker.string.alphanumeric(64), | ||
| userId: faker.string.uuid(), | ||
| } | ||
|
|
||
| await createUserToken(tx, data) | ||
|
|
||
| expect(tx.personalAccessToken.create).toHaveBeenCalledWith({ data, select: userTokenSelect }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('deleteUserToken', () => { | ||
| it('scopes the delete to both id and userId', async () => { | ||
| const id = faker.string.uuid() | ||
| const userId = faker.string.uuid() | ||
| tx.personalAccessToken.deleteMany.mockResolvedValue({ count: 1 }) | ||
|
|
||
| const result = await deleteUserToken(tx, { id, userId }) | ||
|
|
||
| expect(result).toEqual({ count: 1 }) | ||
| expect(tx.personalAccessToken.deleteMany).toHaveBeenCalledWith({ where: { id, userId } }) | ||
| }) | ||
| }) | ||
| }) |
58 changes: 58 additions & 0 deletions
58
apps/server-nestjs/src/modules/user-tokens/user-tokens-queries.utils.ts
This file contains hidden or 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,58 @@ | ||
| import type { Prisma } from '@prisma/client' | ||
|
|
||
| export const userTokenOwnerSelect = { | ||
| id: true, | ||
| email: true, | ||
| firstName: true, | ||
| lastName: true, | ||
| type: true, | ||
| } satisfies Prisma.UserSelect | ||
|
|
||
| export const userTokenSelect = { | ||
| id: true, | ||
| name: true, | ||
| lastUse: true, | ||
| expirationDate: true, | ||
| status: true, | ||
| createdAt: true, | ||
| userId: true, | ||
| owner: { | ||
| select: userTokenOwnerSelect, | ||
| }, | ||
| } satisfies Prisma.PersonalAccessTokenSelect | ||
|
|
||
| export type UserToken = Prisma.PersonalAccessTokenGetPayload<{ | ||
| select: typeof userTokenSelect | ||
| }> | ||
|
|
||
| export function listUserTokens(tx: Prisma.TransactionClient, userId: string) { | ||
| return tx.personalAccessToken.findMany({ | ||
| where: { userId }, | ||
| orderBy: [{ status: 'asc' }, { createdAt: 'asc' }], | ||
| select: userTokenSelect, | ||
| }) | ||
| } | ||
|
|
||
| export function createUserToken(tx: Prisma.TransactionClient, data: { | ||
| name: string | ||
| expirationDate: Date | ||
| hash: string | ||
| userId: string | ||
| }) { | ||
| return tx.personalAccessToken.create({ | ||
| data: { | ||
| name: data.name, | ||
| hash: data.hash, | ||
| expirationDate: data.expirationDate, | ||
| userId: data.userId, | ||
| }, | ||
| select: userTokenSelect, | ||
| }) | ||
| } | ||
|
|
||
| export function deleteUserToken(tx: Prisma.TransactionClient, where: { | ||
| id: string | ||
| userId: string | ||
| }) { | ||
| return tx.personalAccessToken.deleteMany({ where }) | ||
|
shikanime marked this conversation as resolved.
|
||
| } | ||
113 changes: 113 additions & 0 deletions
113
apps/server-nestjs/src/modules/user-tokens/user-tokens.controller.spec.ts
This file contains hidden or 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,113 @@ | ||
| import type { TestingModule } from '@nestjs/testing' | ||
| import type { DeepMockProxy } from 'vitest-mock-extended' | ||
| import type { UserContext } from '../infrastructure/auth/auth-user.decorator' | ||
| import { endOfToday } from '@cpn-console/shared' | ||
| import { faker } from '@faker-js/faker' | ||
| import { Test } from '@nestjs/testing' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { mockDeep } from 'vitest-mock-extended' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { UserTokensController } from './user-tokens.controller' | ||
| import { UserTokensService } from './user-tokens.service' | ||
|
|
||
| describe('userTokensController', () => { | ||
| let module: TestingModule | ||
| let controller: UserTokensController | ||
| let service: DeepMockProxy<UserTokensService> | ||
|
|
||
| const user: UserContext = { userId: faker.string.uuid() } | ||
|
|
||
| beforeEach(async () => { | ||
| service = mockDeep<UserTokensService>() | ||
|
|
||
| module = await Test.createTestingModule({ | ||
| controllers: [UserTokensController], | ||
| providers: [{ provide: UserTokensService, useValue: service }], | ||
| }) | ||
| .overrideGuard(UserGuard) | ||
| .useValue({ canActivate: () => true }) | ||
| .compile() | ||
|
|
||
| controller = module.get<UserTokensController>(UserTokensController) | ||
| }) | ||
|
|
||
| it('delegates creation to the service with the body and the caller id', async () => { | ||
| const expirationDate = faker.date.soon({ days: 1, refDate: endOfToday().getTime() }) | ||
| service.create.mockResolvedValueOnce({ | ||
| token: { | ||
| id: faker.string.uuid(), | ||
| name: 'my-token', | ||
| lastUse: null, | ||
| expirationDate, | ||
| status: 'active' as const, | ||
| createdAt: new Date(), | ||
| userId: user.userId, | ||
| owner: { id: user.userId, email: faker.internet.email(), firstName: 'a', lastName: 'b', type: 'human' as const }, | ||
| }, | ||
| password: faker.string.alphanumeric(48), | ||
| }) | ||
|
|
||
| await controller.create({ name: 'my-token', expirationDate }, user) | ||
|
|
||
| expect(service.create).toHaveBeenCalledWith({ name: 'my-token', expirationDate }, user.userId) | ||
| }) | ||
|
|
||
| it('maps creation result to the exposed contract shape', async () => { | ||
| const expirationDate = faker.date.soon({ days: 1, refDate: endOfToday().getTime() }) | ||
| const createdAt = faker.date.past() | ||
| const password = faker.string.alphanumeric(48) | ||
| service.create.mockResolvedValueOnce({ | ||
| token: { | ||
| id: faker.string.uuid(), | ||
| name: 'my-token', | ||
| lastUse: null, | ||
| expirationDate, | ||
| status: 'active' as const, | ||
| createdAt, | ||
| userId: user.userId, | ||
| owner: { id: user.userId, email: faker.internet.email(), firstName: 'a', lastName: 'b', type: 'human' as const }, | ||
| }, | ||
| password, | ||
| }) | ||
|
|
||
| const result = await controller.create({ name: 'my-token', expirationDate }, user) | ||
|
|
||
| expect(result).toEqual({ | ||
| id: expect.any(String), | ||
| name: 'my-token', | ||
| lastUse: null, | ||
| expirationDate: expirationDate.toISOString(), | ||
| status: 'active', | ||
| createdAt: createdAt.toISOString(), | ||
| owner: expect.any(Object), | ||
| password, | ||
| }) | ||
| expect('userId' in result).toBe(false) | ||
| }) | ||
|
|
||
| it('maps listed records to the contract shape', async () => { | ||
| const record = { | ||
| id: faker.string.uuid(), | ||
| name: 'my-token', | ||
| lastUse: null, | ||
| expirationDate: faker.date.soon({ days: 1, refDate: endOfToday().getTime() }), | ||
| status: 'active' as const, | ||
| createdAt: faker.date.past(), | ||
| userId: user.userId, | ||
| owner: { id: user.userId, email: faker.internet.email(), firstName: 'a', lastName: 'b', type: 'human' as const }, | ||
| } | ||
| service.list.mockResolvedValueOnce([record]) | ||
|
|
||
| const result = await controller.list(user) | ||
|
|
||
| expect(result).toEqual([{ | ||
| id: record.id, | ||
| name: record.name, | ||
| lastUse: null, | ||
| expirationDate: record.expirationDate.toISOString(), | ||
| status: record.status, | ||
| createdAt: record.createdAt.toISOString(), | ||
| owner: record.owner, | ||
| }]) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.