Skip to content

Commit

Permalink
test: delete jwtAccessConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
a20688392 committed Aug 8, 2023
1 parent 46539da commit 6ac5c60
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
12 changes: 8 additions & 4 deletions src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getRepositoryToken, TypeOrmModule } from "@nestjs/typeorm";
import { type Request } from "express";
import { dataSourceJest } from "src/config/data-source";
import jestConfig from "src/config/jest.config";
import { jwtAccessConfig } from "src/config/jwt.config";
import { UserEntity } from "src/user/entities/user.entity";
import type { CreateUserResponse } from "src/user/responses/create-user-response";
import { UserService } from "src/user/user.service";
Expand All @@ -26,7 +25,7 @@ describe("AuthController", () => {
let authService: AuthService;
let userRepository: Repository<UserEntity> | undefined;

const fakeAccessToken = "mocked_token";
const fakeAccessToken = "mocked_access_token";

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -37,7 +36,7 @@ describe("AuthController", () => {
}),
PassportModule,
TypeOrmModule.forRoot(dataSourceJest),
JwtModule.registerAsync(jwtAccessConfig),
JwtModule.register({}),
],
providers: [
AuthService,
Expand All @@ -51,7 +50,7 @@ describe("AuthController", () => {
provide: JwtService,
useValue: {
// 模擬JwtService中的方法
sign: jest.fn().mockReturnValue(fakeAccessToken),
sign: jest.fn(),
},
},
LocalStrategy,
Expand Down Expand Up @@ -112,6 +111,11 @@ describe("AuthController", () => {
id: 1,
} as JwtUser,
} as unknown as Request;

jest
.spyOn(authService, "generateAccessToken")
.mockImplementation(async () => fakeAccessToken);

const mockAuthService = jest.spyOn(authService, "login");

const result = await authController.login(request);
Expand Down
16 changes: 7 additions & 9 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { getRepositoryToken, TypeOrmModule } from "@nestjs/typeorm";
import { validate } from "class-validator";
import { dataSourceJest } from "src/config/data-source";
import jestConfig from "src/config/jest.config";
import { jwtAccessConfig } from "src/config/jwt.config";
import type { CreateUserDto } from "src/user/dto/create-user.dto";
import { UserEntity } from "src/user/entities/user.entity";
import { UserService } from "src/user/user.service";
Expand All @@ -24,10 +23,9 @@ import { LocalStrategy } from "./local/local.strategy";
describe("AuthService", () => {
let authService: AuthService;
let userService: UserService;
let jwtService: JwtService;
let userRepository: Repository<UserEntity> | undefined;

const fakeAccessToken = "mocked_token";
const fakeAccessToken = "mocked_access_token";

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -37,7 +35,7 @@ describe("AuthService", () => {
}),
PassportModule,
TypeOrmModule.forRoot(dataSourceJest),
JwtModule.registerAsync(jwtAccessConfig),
JwtModule.register({}),
],
providers: [
AuthService,
Expand All @@ -51,15 +49,14 @@ describe("AuthService", () => {
provide: JwtService,
useValue: {
// 模擬JwtService中的方法
sign: jest.fn().mockReturnValue(fakeAccessToken),
sign: jest.fn(),
},
},
LocalStrategy,
JwtAccessStrategy,
],
}).compile();

jwtService = module.get<JwtService>(JwtService);
userService = module.get<UserService>(UserService);
authService = module.get<AuthService>(AuthService);
userRepository = module.get<Repository<UserEntity>>(
Expand Down Expand Up @@ -164,15 +161,16 @@ describe("AuthService", () => {
email: "[email protected]",
id: 1,
};
const expectedToken = fakeAccessToken;
const expectedStatusCode = HttpStatus.CREATED;

jest.spyOn(jwtService, "sign").mockReturnValue(expectedToken);
jest
.spyOn(authService, "generateAccessToken")
.mockImplementation(async () => fakeAccessToken);

const result = await authService.login(mockUser);

expect(result).toEqual({
accessToken: expectedToken,
accessToken: fakeAccessToken,
statusCode: expectedStatusCode,
});
});
Expand Down
16 changes: 11 additions & 5 deletions src/auth/jwt/jwt-access.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { type ExecutionContext, UnauthorizedException } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { JwtModule, JwtService } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { Test } from "@nestjs/testing";
import jestConfig from "src/config/jest.config";
import { jwtAccessConfig } from "src/config/jwt.config";

import { JwtAccessGuard } from "./jwt-access.guard";
import { JwtAccessStrategy } from "./jwt-access.strategy";

describe("JwtAccessGuard", () => {
let jwtAccessGuard: JwtAccessGuard;
let jwtService: JwtService;
let configService: ConfigService;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
Expand All @@ -20,13 +20,14 @@ describe("JwtAccessGuard", () => {
load: [jestConfig],
}),
PassportModule,
JwtModule.registerAsync(jwtAccessConfig),
JwtModule.register({}),
],
providers: [JwtAccessGuard, JwtAccessStrategy, JwtService],
}).compile();

jwtAccessGuard = moduleRef.get<JwtAccessGuard>(JwtAccessGuard);
jwtService = moduleRef.get<JwtService>(JwtService);
configService = moduleRef.get<ConfigService>(ConfigService);
});

it("should be defined", () => {
Expand All @@ -35,7 +36,11 @@ describe("JwtAccessGuard", () => {

it("should return true for a valid JWT", async () => {
const payload = { email: "testuser", id: 1 };
const token = jwtService.sign(payload);
const secret: string | undefined = configService.get("jwtSecret.access");
const token = jwtService.sign(payload, {
expiresIn: "1h",
secret,
});

const response = {};
const context: ExecutionContext = {
Expand All @@ -55,7 +60,8 @@ describe("JwtAccessGuard", () => {

it("should throw an error for an expired JWT", async () => {
const payload = { email: "testuser", id: 1 };
const token = jwtService.sign(payload, { expiresIn: 0 });
const secret: string | undefined = configService.get("jwtSecret.access");
const token = jwtService.sign(payload, { expiresIn: 0, secret });

const response = {};
const context: ExecutionContext = {
Expand Down
11 changes: 0 additions & 11 deletions src/config/jwt.config.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/swagger/swagger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AuthModule } from "src/auth/auth.module";
import { JwtAccessStrategy } from "src/auth/jwt/jwt-access.strategy";
import { dataSourceJest } from "src/config/data-source";
import jestConfig from "src/config/jest.config";
import { jwtAccessConfig } from "src/config/jwt.config";
import { UserModule } from "src/user/user.module";

@Module({
Expand All @@ -21,7 +20,7 @@ import { UserModule } from "src/user/user.module";
}),
UserModule,
AuthModule,
JwtModule.registerAsync(jwtAccessConfig),
JwtModule.register({}),
],
providers: [AppService, JwtAccessStrategy],
})
Expand Down

0 comments on commit 6ac5c60

Please sign in to comment.