mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
11 changed files with 201 additions and 1 deletions
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { ApiTokensController } from './api-tokens.controller'; |
||||
import { ApiTokensService } from './api-tokens.service'; |
||||
|
||||
describe('ApiTokensController', () => { |
||||
let controller: ApiTokensController; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
controllers: [ApiTokensController], |
||||
providers: [ApiTokensService], |
||||
}).compile(); |
||||
|
||||
controller = module.get<ApiTokensController>(ApiTokensController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,49 @@
|
||||
import { |
||||
Controller, |
||||
Delete, |
||||
Get, |
||||
Post, |
||||
UseGuards, |
||||
Request, |
||||
Body, |
||||
Param, |
||||
} from '@nestjs/common'; |
||||
import { PagedResponseImpl } from '../../helpers/PagedResponse'; |
||||
import { |
||||
Acl, |
||||
ExtractProjectIdMiddleware, |
||||
} from '../../middlewares/extract-project-id/extract-project-id.middleware'; |
||||
import { ApiTokensService } from './api-tokens.service'; |
||||
import { AuthGuard } from '@nestjs/passport'; |
||||
|
||||
@Controller() |
||||
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) |
||||
export class ApiTokensController { |
||||
constructor(private readonly apiTokensService: ApiTokensService) {} |
||||
|
||||
@Get('/api/v1/db/meta/projects/:projectId/api-tokens') |
||||
@Acl('apiTokenList') |
||||
async apiTokenList(@Request() req) { |
||||
return new PagedResponseImpl( |
||||
await this.apiTokensService.apiTokenList({ userId: req['user'].id }), |
||||
); |
||||
} |
||||
|
||||
@Post('/api/v1/db/meta/projects/:projectId/api-tokens') |
||||
@Acl('apiTokenCreate') |
||||
async apiTokenCreate(@Request() req, @Body() body) { |
||||
return await this.apiTokensService.apiTokenCreate({ |
||||
tokenBody: body, |
||||
userId: req['user'].id, |
||||
}); |
||||
} |
||||
|
||||
@Delete('/api/v1/db/meta/projects/:projectId/api-tokens/:token') |
||||
@Acl('apiTokenDelete') |
||||
async apiTokenDelete(@Request() req, @Param('token') token: string) { |
||||
return await this.apiTokensService.apiTokenDelete({ |
||||
token, |
||||
user: req['user'], |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'; |
||||
import { ApiTokensService } from './api-tokens.service'; |
||||
import { ApiTokensController } from './api-tokens.controller'; |
||||
|
||||
@Module({ |
||||
controllers: [ApiTokensController], |
||||
providers: [ApiTokensService] |
||||
}) |
||||
export class ApiTokensModule {} |
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { ApiTokensService } from './api-tokens.service'; |
||||
|
||||
describe('ApiTokensService', () => { |
||||
let service: ApiTokensService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [ApiTokensService], |
||||
}).compile(); |
||||
|
||||
service = module.get<ApiTokensService>(ApiTokensService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common'; |
||||
import { T } from 'nc-help'; |
||||
import { ApiTokenReqType, OrgUserRoles } from 'nocodb-sdk' |
||||
import { validatePayload } from '../../helpers' |
||||
import { NcError } from '../../helpers/catchError' |
||||
import { ApiToken, User } from '../../models' |
||||
|
||||
@Injectable() |
||||
export class ApiTokensService { |
||||
async apiTokenList(param: { userId: string }) { |
||||
return await ApiToken.list(param.userId); |
||||
} |
||||
async apiTokenCreate(param: { |
||||
userId: string; |
||||
tokenBody: ApiTokenReqType; |
||||
}) { |
||||
validatePayload( |
||||
'swagger.json#/components/schemas/ApiTokenReq', |
||||
param.tokenBody |
||||
); |
||||
|
||||
T.emit('evt', { evt_type: 'apiToken:created' }); |
||||
return await ApiToken.insert({ |
||||
...param.tokenBody, |
||||
fk_user_id: param.userId, |
||||
}); |
||||
} |
||||
|
||||
async apiTokenDelete(param: { token; user: User }) { |
||||
const apiToken = await ApiToken.getByToken(param.token); |
||||
if ( |
||||
!param.user.roles.includes(OrgUserRoles.SUPER_ADMIN) && |
||||
apiToken.fk_user_id !== param.user.id |
||||
) { |
||||
NcError.notFound('Token not found'); |
||||
} |
||||
T.emit('evt', { evt_type: 'apiToken:deleted' }); |
||||
|
||||
// todo: verify token belongs to the user
|
||||
return await ApiToken.delete(param.token); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { HookFiltersController } from './hook-filters.controller'; |
||||
import { HookFiltersService } from './hook-filters.service'; |
||||
|
||||
describe('HookFiltersController', () => { |
||||
let controller: HookFiltersController; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
controllers: [HookFiltersController], |
||||
providers: [HookFiltersService], |
||||
}).compile(); |
||||
|
||||
controller = module.get<HookFiltersController>(HookFiltersController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,8 @@
|
||||
import { Controller } from '@nestjs/common'; |
||||
import { HookFiltersService } from './hook-filters.service'; |
||||
|
||||
@Controller('hook-filters') |
||||
export class HookFiltersController { |
||||
constructor(private readonly hookFiltersService: HookFiltersService) {} |
||||
|
||||
} |
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'; |
||||
import { HookFiltersService } from './hook-filters.service'; |
||||
import { HookFiltersController } from './hook-filters.controller'; |
||||
|
||||
@Module({ |
||||
controllers: [HookFiltersController], |
||||
providers: [HookFiltersService] |
||||
}) |
||||
export class HookFiltersModule {} |
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { HookFiltersService } from './hook-filters.service'; |
||||
|
||||
describe('HookFiltersService', () => { |
||||
let service: HookFiltersService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [HookFiltersService], |
||||
}).compile(); |
||||
|
||||
service = module.get<HookFiltersService>(HookFiltersService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
Loading…
Reference in new issue