Skip to content

Commit

Permalink
feat : Auth service 테스트 코드 구현 #35
Browse files Browse the repository at this point in the history
- 소셜로그인을 통해 로그인 요청한 유저에 대한 테스트 진행
- 리프레시 토큰 재발급 테스트 진행
  • Loading branch information
LeeTH916 committed Nov 29, 2023
1 parent 8a3eb7a commit 72b3ce0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 10 deletions.
14 changes: 14 additions & 0 deletions be/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.8'
services:
postgres:
image: postgis/postgis
environment:
POSTGRES_DB: testdb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5433:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
8 changes: 6 additions & 2 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
"typescript": "^5.1.3"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/$1"
},
"moduleFileExtensions": [
"js",
"json",
Expand All @@ -83,7 +88,6 @@
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"coverageDirectory": "../coverage"
}
}
107 changes: 99 additions & 8 deletions be/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,109 @@
import { Test, TestingModule } from "@nestjs/testing";
import { AuthService } from "./auth.service";
import { DataSource } from "typeorm";
import { User } from "src/user/entities/user.entity";
import { TypeOrmModule } from "@nestjs/typeorm";
import { FollowEntity } from "src/user/entities/user.followList.entity";
import { RestaurantInfoEntity } from "src/restaurant/entities/restaurant.entity";
import { UserRestaurantListEntity } from "src/user/entities/user.restaurantlist.entity";
import { ReviewInfoEntity } from "src/review/entities/review.entity";
import { JwtService } from "@nestjs/jwt";
import { UserModule } from "src/user/user.module";
import { RestaurantModule } from "src/restaurant/restaurant.module";
import { ReviewModule } from "src/review/review.module";
import { HttpException } from '@nestjs/common';
import { UserInfoDto } from "src/user/dto/userInfo.dto";
import { UserService } from "src/user/user.service";
import { AuthModule } from "./auth.module";

describe("AuthService", () => {
let service: AuthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
let authService: AuthService;
let dataSource: DataSource;
let jwtService: JwtService;
let userService: UserService;

beforeAll(async () => {
const testModule = await Test.createTestingModule({
imports: [
AuthModule,
UserModule,
RestaurantModule,
ReviewModule,
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5433,
username: 'user',
password: 'password',
database: 'testdb',
entities: [User, FollowEntity, RestaurantInfoEntity, UserRestaurantListEntity, ReviewInfoEntity],
synchronize: true,
}),
],
}).compile();

service = module.get<AuthService>(AuthService);
authService = testModule.get<AuthService>(AuthService);
jwtService = testModule.get<JwtService>(JwtService);
dataSource = testModule.get<DataSource>(DataSource);
userService = testModule.get<UserService>(UserService);
});

it("should be defined", () => {
expect(service).toBeDefined();


beforeEach(async () => {
const repository = dataSource.getRepository(User);
await repository.query('DELETE FROM public.user');
});
});

it("회원가입 된 유저가 로그인 요청을 한 경우", async () => {

const userInfoDto: UserInfoDto = {
email: "[email protected]",
password: "",
provider: "naver",
nickName: "test",
region: "인천",
birthdate: "1999/10/13",
isMale: true,
};

await userService.signup(userInfoDto);

const loginRequestUser = {
email: "[email protected]"
}

const result = await authService.signin(loginRequestUser);
expect(result).toBeDefined();
expect(jwtService.verify(result.accessToken)).toBeTruthy();
expect(jwtService.verify(result.refreshToken)).toBeTruthy();
})

it("회원가입이 안된 유저가 로그인 요청을 한 경우",async () => {
const loginRequestUser = {
email: "[email protected]"
}

await expect(authService.signin(loginRequestUser))
.rejects
.toThrow(HttpException);
})

it("정상적인 리프레쉬 토큰을 받은 경우", async () => {
const refreshToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwiaWQiOi0xLCJpYXQiOjIwMTYyMzkwMjJ9.zOd1UtNxKjPqNUYGapfDgqY78M5iBEj2Ike386HTDOA"

const result = await authService.checkRefreshToken(refreshToken);

expect(result).toBeDefined();
expect(jwtService.verify(result.accessToken)).toBeTruthy();
})

it("비정상적인 리프레쉬 토큰을 받은 경우", async () => {
const refreshToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwiaWQiOjEsImlhdCI6MjAxNjIzOTAyMn0.3nRp6Qdxf5xj2C0M7-0lOURWx-dAKEjl9eS9dRoQTsA"

await expect(authService.checkRefreshToken(refreshToken))
.rejects
.toThrow(HttpException);
})
});

0 comments on commit 72b3ce0

Please sign in to comment.