-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api)!: update workspace role mechanism and added functionality t…
…o create custom roles
- Loading branch information
Showing
54 changed files
with
2,749 additions
and
1,355 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
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,158 @@ | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication | ||
} from '@nestjs/platform-fastify' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
import { ApiKeyModule } from './api-key.module' | ||
import { MAIL_SERVICE } from '../mail/services/interface.service' | ||
import { MockMailService } from '../mail/services/mock.service' | ||
import { AppModule } from '../app/app.module' | ||
import { Test } from '@nestjs/testing' | ||
import { ApiKey, User } from '@prisma/client' | ||
|
||
describe('Api Key Role Controller Tests', () => { | ||
let app: NestFastifyApplication | ||
let prisma: PrismaService | ||
let user: User | ||
let apiKey: ApiKey | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppModule, ApiKeyModule] | ||
}) | ||
.overrideProvider(MAIL_SERVICE) | ||
.useClass(MockMailService) | ||
.compile() | ||
app = moduleRef.createNestApplication<NestFastifyApplication>( | ||
new FastifyAdapter() | ||
) | ||
prisma = moduleRef.get(PrismaService) | ||
|
||
await app.init() | ||
await app.getHttpAdapter().getInstance().ready() | ||
|
||
await prisma.apiKey.deleteMany() | ||
await prisma.user.deleteMany() | ||
|
||
user = await prisma.user.create({ | ||
data: { | ||
email: '[email protected]', | ||
name: 'John', | ||
isActive: true, | ||
isAdmin: false, | ||
isOnboardingFinished: true | ||
} | ||
}) | ||
}) | ||
|
||
it('should be able to create api key', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/api-key', | ||
payload: { | ||
name: 'Test Key', | ||
expiresAfter: '24' | ||
}, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(response.statusCode).toBe(201) | ||
expect(response.json()).toEqual({ | ||
id: expect.any(String), | ||
name: 'Test Key', | ||
value: expect.stringMatching(/^ks_*/), | ||
expiresAt: expect.any(String), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String) | ||
}) | ||
|
||
apiKey = response.json() | ||
}) | ||
|
||
it('should be able to update the api key', async () => { | ||
const response = await app.inject({ | ||
method: 'PUT', | ||
url: `/api-key/${apiKey.id}`, | ||
payload: { | ||
name: 'Updated Test Key', | ||
expiresAfter: '168' | ||
}, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(response.statusCode).toBe(200) | ||
expect(response.json()).toEqual({ | ||
id: apiKey.id, | ||
name: 'Updated Test Key', | ||
expiresAt: expect.any(String), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String) | ||
}) | ||
|
||
apiKey = response.json() | ||
}) | ||
|
||
it('should be able to get the api key', async () => { | ||
const response = await app.inject({ | ||
method: 'GET', | ||
url: `/api-key/${apiKey.id}`, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(response.statusCode).toBe(200) | ||
expect(response.json()).toEqual({ | ||
id: apiKey.id, | ||
name: 'Updated Test Key', | ||
expiresAt: expect.any(String), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String) | ||
}) | ||
}) | ||
|
||
it('should be able to get all the api keys of the user', async () => { | ||
const response = await app.inject({ | ||
method: 'GET', | ||
url: '/api-key/all/as-user', | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(response.statusCode).toBe(200) | ||
expect(response.json()).toEqual([ | ||
{ | ||
id: apiKey.id, | ||
name: 'Updated Test Key', | ||
expiresAt: expect.any(String), | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String) | ||
} | ||
]) | ||
}) | ||
|
||
it('should be able to delete the api key', async () => { | ||
const response = await app.inject({ | ||
method: 'DELETE', | ||
url: `/api-key/${apiKey.id}`, | ||
headers: { | ||
'x-e2e-user-email': user.email | ||
} | ||
}) | ||
|
||
expect(response.statusCode).toBe(200) | ||
}) | ||
|
||
afterAll(async () => { | ||
await prisma.apiKey.deleteMany() | ||
await prisma.user.deleteMany() | ||
|
||
await prisma.$disconnect() | ||
await app.close() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
import { ApiKey, ApiKeyGeneralRole, ApiKeyWorkspaceScope } from '@prisma/client' | ||
import { IsArray, IsOptional, IsString } from 'class-validator' | ||
import { ApiKey } from '@prisma/client' | ||
import { IsString } from 'class-validator' | ||
|
||
export class CreateApiKey { | ||
@IsString() | ||
name: ApiKey['name'] | ||
|
||
@IsString() | ||
expiresAfter: '24' | '168' | '720' | '8760' | 'never' = 'never' | ||
|
||
@IsArray() | ||
generalRoles: ApiKeyGeneralRole[] | ||
|
||
@IsArray() | ||
@IsOptional() | ||
scopes: ApiKeyWorkspaceScope[] | ||
} |
Oops, something went wrong.