Skip to content

Commit 009fee4

Browse files
committed
MixedGuard added
1 parent 48a38c7 commit 009fee4

File tree

9 files changed

+57
-11
lines changed

9 files changed

+57
-11
lines changed

src/auth/auth.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { JwtStrategy } from './jwt.strategy';
66
import { ApiGuard } from './guards/api.guard';
77
import { ConfigService } from '@nestjs/config';
88
import { PrismaService } from '../prisma/prisma.service';
9+
import { MixedGuard } from './guards/mixed.guard';
10+
import { JwtAuthGuard } from './guards/auth.guard';
911

1012
@Module({
1113
imports: [
@@ -20,7 +22,7 @@ import { PrismaService } from '../prisma/prisma.service';
2022
inject: [ConfigService],
2123
}),
2224
],
23-
providers: [AuthService, JwtStrategy, ApiGuard, PrismaService],
24-
exports: [AuthService],
25+
providers: [AuthService, JwtStrategy, ApiGuard, JwtAuthGuard, PrismaService],
26+
exports: [AuthService, ApiGuard, JwtAuthGuard],
2527
})
2628
export class AuthModule {}

src/auth/guards/mixed.guard.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ExecutionContext, Injectable, CanActivate } from '@nestjs/common';
2+
import { ApiGuard } from './api.guard';
3+
import { JwtAuthGuard } from './auth.guard';
4+
5+
@Injectable()
6+
export class MixedGuard implements CanActivate {
7+
constructor(private readonly apiGuard: ApiGuard, private readonly authGuard: JwtAuthGuard) {}
8+
9+
async canActivate(context: ExecutionContext): Promise<boolean> {
10+
let jwtAuth = false;
11+
try {
12+
jwtAuth = (await this.authGuard.canActivate(context)) as boolean;
13+
} catch (err) {}
14+
15+
let apiAuth = false;
16+
try {
17+
apiAuth = await this.apiGuard.canActivate(context);
18+
} catch (err) {}
19+
return jwtAuth || apiAuth;
20+
}
21+
}

src/builds/builds.controller.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import { Test, TestingModule } from '@nestjs/testing';
22
import { BuildsController } from './builds.controller';
33
import { BuildsService } from './builds.service';
44
import { PrismaService } from '../prisma/prisma.service';
5+
import { ApiGuard } from '../auth/guards/api.guard';
6+
import { JwtAuthGuard } from '../auth/guards/auth.guard';
57

68
describe('Builds Controller', () => {
79
let controller: BuildsController;
810

911
beforeEach(async () => {
1012
const module: TestingModule = await Test.createTestingModule({
1113
controllers: [BuildsController],
12-
providers: [{ provide: BuildsService, useValue: {} },
13-
{ provide: PrismaService, useValue: {} }],
14+
providers: [
15+
{ provide: BuildsService, useValue: {} },
16+
{ provide: PrismaService, useValue: {} },
17+
{ provide: ApiGuard, useValue: {} },
18+
{ provide: JwtAuthGuard, useValue: {} },
19+
],
1420
}).compile();
1521

1622
controller = module.get<BuildsController>(BuildsController);

src/builds/builds.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CreateBuildDto } from './dto/build-create.dto';
66
import { ApiGuard } from '../auth/guards/api.guard';
77
import { Build } from '@prisma/client';
88
import { BuildDto } from './dto/build.dto';
9+
import { MixedGuard } from '../auth/guards/mixed.guard';
910

1011
@Controller('builds')
1112
@ApiTags('builds')
@@ -38,7 +39,8 @@ export class BuildsController {
3839
@Patch(':id')
3940
@ApiResponse({ type: BuildDto })
4041
@ApiSecurity('api_key')
41-
@UseGuards(ApiGuard)
42+
@ApiBearerAuth()
43+
@UseGuards(MixedGuard)
4244
stop(@Param('id', new ParseUUIDPipe()) id: string): Promise<BuildDto> {
4345
return this.buildsService.stop(id);
4446
}

src/builds/builds.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { UsersModule } from '../users/users.module';
55
import { PrismaService } from '../prisma/prisma.service';
66
import { TestRunsModule } from '../test-runs/test-runs.module';
77
import { SharedModule } from '../shared/shared.module';
8+
import { AuthModule } from '../auth/auth.module';
89

910
@Module({
10-
imports: [SharedModule, UsersModule, TestRunsModule],
11+
imports: [SharedModule, UsersModule, TestRunsModule, AuthModule],
1112
providers: [BuildsService, PrismaService],
1213
controllers: [BuildsController],
1314
exports: [BuildsService],

src/builds/builds.service.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ describe('BuildsService', () => {
257257

258258
expect(buildUpdateMock).toHaveBeenCalledWith({
259259
where: { id },
260+
include: {
261+
testRuns: true,
262+
},
260263
data: {
261264
isRunning: false,
262265
},

src/builds/builds.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export class BuildsService {
6565
async stop(id: string): Promise<BuildDto> {
6666
const build = await this.prismaService.build.update({
6767
where: { id },
68+
include: {
69+
testRuns: true,
70+
},
6871
data: {
6972
isRunning: false,
7073
},

test/builds.e2e-spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,30 @@ describe('Builds (e2e)', () => {
127127
});
128128

129129
describe('PATCH /', () => {
130-
it('200', async () => {
130+
it('200 jwt', async () => {
131+
const build = await buildsService.create({ project: project.id, branchName: 'develop' });
132+
133+
return requestWithAuth(app, 'patch', `/builds/${build.id}`, {}, user.token)
134+
.expect(200)
135+
.expect((res) => {
136+
expect(res.body.isRunning).toBe(false);
137+
});
138+
});
139+
140+
it('200 api', async () => {
131141
const build = await buildsService.create({ project: project.id, branchName: 'develop' });
132142

133143
return requestWithApiKey(app, 'patch', `/builds/${build.id}`, {}, user.apiKey)
134144
.expect(200)
135145
.expect((res) => {
136-
expect(res.body.projectId).toBe(project.id);
137146
expect(res.body.isRunning).toBe(false);
138147
});
139148
});
140149

141-
it('401', async () => {
150+
it('403', async () => {
142151
const build = await buildsService.create({ project: project.id, branchName: 'develop' });
143152

144-
return requestWithAuth(app, 'patch', `/builds/${build.id}`, {}, '').expect(401);
153+
return requestWithAuth(app, 'patch', `/builds/${build.id}`, {}, '').expect(403);
145154
});
146155
});
147156
});

test/projects.e2e-spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ describe('Projects (e2e)', () => {
3434

3535
app = moduleFixture.createNestApplication();
3636
usersService = moduleFixture.get<UsersService>(UsersService);
37-
usersService = moduleFixture.get<UsersService>(UsersService);
3837

3938
await app.init();
4039
});

0 commit comments

Comments
 (0)