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

chore(API): Refactor authority check functions in API #189

Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import { REDIS_CLIENT } from '../../provider/redis.provider'
import { RedisClientType } from 'redis'
import { mockDeep } from 'jest-mock-extended'
import { ProviderModule } from '../../provider/provider.module'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { CommonModule } from '../../common/common.module'

describe('ApprovalController', () => {
let controller: ApprovalController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ProviderModule],
imports: [ProviderModule, CommonModule],
controllers: [ApprovalController],
providers: [
ApprovalService,
Expand All @@ -34,7 +36,8 @@ describe('ApprovalController', () => {
{
provide: MAIL_SERVICE,
useClass: MockMailService
}
},
AuthorityCheckerService
]
})
.overrideProvider(REDIS_CLIENT)
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/approval/service/approval.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { REDIS_CLIENT } from '../../provider/redis.provider'
import { RedisClientType } from 'redis'
import { mockDeep } from 'jest-mock-extended'
import { ProviderModule } from '../../provider/provider.module'
import { AuthorityCheckerService } from '../../common/authority-checker.service'
import { CommonModule } from '../../common/common.module'

describe('ApprovalService', () => {
let service: ApprovalService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ProviderModule],
imports: [ProviderModule, CommonModule],
providers: [
ApprovalService,
PrismaService,
Expand All @@ -32,7 +34,8 @@ describe('ApprovalService', () => {
{
provide: MAIL_SERVICE,
useClass: MockMailService
}
},
AuthorityCheckerService
]
})
.overrideProvider(REDIS_CLIENT)
Expand Down
29 changes: 15 additions & 14 deletions apps/api/src/approval/service/approval.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
UpdateVariableMetadata,
UpdateWorkspaceMetadata
} from '../approval.types'
import getWorkspaceWithAuthority from '../../common/get-workspace-with-authority'
import { AuthorityCheckerService } from '../../common/authority-checker.service'

@Injectable()
export class ApprovalService {
Expand All @@ -43,7 +43,8 @@ export class ApprovalService {
private readonly projectService: ProjectService,
private readonly environmentService: EnvironmentService,
private readonly secretService: SecretService,
private readonly variableService: VariableService
private readonly variableService: VariableService,
private readonly authorityCheckerService: AuthorityCheckerService
) {}

async updateApproval(user: User, reason: string, approvalId: Approval['id']) {
Expand Down Expand Up @@ -453,12 +454,12 @@ export class ApprovalService {
actions: ApprovalAction[],
statuses: ApprovalStatus[]
) {
await getWorkspaceWithAuthority(
user.id,
workspaceId,
Authority.MANAGE_APPROVALS,
this.prisma
)
await this.authorityCheckerService.checkAuthorityOverWorkspace({
userId: user.id,
entity: { id: workspaceId },
authority: Authority.MANAGE_APPROVALS,
prisma: this.prisma
})

return await this.prisma.approval.findMany({
where: {
Expand Down Expand Up @@ -493,12 +494,12 @@ export class ApprovalService {
actions: ApprovalAction[],
statuses: ApprovalStatus[]
) {
await getWorkspaceWithAuthority(
user.id,
workspaceId,
Authority.READ_WORKSPACE,
this.prisma
)
await this.authorityCheckerService.checkAuthorityOverWorkspace({
userId: user.id,
entity: { id: workspaceId },
authority: Authority.READ_WORKSPACE,
prisma: this.prisma
})

return this.prisma.approval.findMany({
where: {
Expand Down
Loading
Loading