|
| 1 | +import { Test, TestingModule } from "@nestjs/testing"; |
| 2 | +import { AuthService } from "./auth.service"; |
| 3 | +import { UserRepository } from "../user/user.repository"; |
| 4 | +import { Profile } from "passport-github"; |
| 5 | + |
| 6 | +// typeorm-transactional 모킹 |
| 7 | +jest.mock("typeorm-transactional", () => ({ |
| 8 | + Transactional: () => () => ({}), |
| 9 | + runOnTransactionCommit: () => () => ({}), |
| 10 | + runOnTransactionRollback: () => () => ({}), |
| 11 | + runOnTransactionComplete: () => () => ({}), |
| 12 | + initializeTransactionalContext: () => ({}), |
| 13 | +})); |
| 14 | + |
| 15 | +describe("AuthService", () => { |
| 16 | + let authService: AuthService; |
| 17 | + let userRepository: UserRepository; |
| 18 | + |
| 19 | + // Mock GitHub 프로필 데이터 |
| 20 | + const mockGithubProfile: Profile = { |
| 21 | + id: "12345", |
| 22 | + displayName: "Test User", |
| 23 | + username: "testuser", |
| 24 | + profileUrl: "https://abcd/", |
| 25 | + photos: [], |
| 26 | + provider: "github", |
| 27 | + _raw: "", |
| 28 | + _json: {}, |
| 29 | + }; |
| 30 | + |
| 31 | + // Mock 유저 데이터 |
| 32 | + const mockUser = { |
| 33 | + id: 1, |
| 34 | + loginId: null, |
| 35 | + passwordHash: null, |
| 36 | + githubId: 12345, |
| 37 | + username: "camper_12345", |
| 38 | + }; |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + // Mock Repository 생성 |
| 42 | + const mockUserRepository = { |
| 43 | + getUserByGithubId: jest.fn(), |
| 44 | + createUser: jest.fn(), |
| 45 | + }; |
| 46 | + |
| 47 | + const module: TestingModule = await Test.createTestingModule({ |
| 48 | + providers: [ |
| 49 | + AuthService, |
| 50 | + { |
| 51 | + provide: UserRepository, |
| 52 | + useValue: mockUserRepository, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }).compile(); |
| 56 | + |
| 57 | + authService = module.get<AuthService>(AuthService); |
| 58 | + userRepository = module.get<UserRepository>(UserRepository); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("githubLogin", () => { |
| 62 | + it("기존 사용자가 있을 경우 해당 사용자를 반환해야 한다", async () => { |
| 63 | + // Given |
| 64 | + jest.spyOn(userRepository, "getUserByGithubId").mockResolvedValue( |
| 65 | + mockUser |
| 66 | + ); |
| 67 | + |
| 68 | + // When |
| 69 | + const result = await authService.githubLogin(mockGithubProfile); |
| 70 | + |
| 71 | + // Then |
| 72 | + expect(userRepository.getUserByGithubId).toHaveBeenCalledWith( |
| 73 | + parseInt(mockGithubProfile.id) |
| 74 | + ); |
| 75 | + expect(result).toEqual(mockUser); |
| 76 | + expect(userRepository.createUser).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("새로운 사용자의 경우 새 계정을 생성해야 한다", async () => { |
| 80 | + // Given |
| 81 | + jest.spyOn(userRepository, "getUserByGithubId").mockResolvedValue( |
| 82 | + null |
| 83 | + ); |
| 84 | + jest.spyOn(userRepository, "createUser").mockResolvedValue( |
| 85 | + mockUser |
| 86 | + ); |
| 87 | + |
| 88 | + // When |
| 89 | + const result = await authService.githubLogin(mockGithubProfile); |
| 90 | + |
| 91 | + // Then |
| 92 | + expect(userRepository.getUserByGithubId).toHaveBeenCalledWith( |
| 93 | + parseInt(mockGithubProfile.id) |
| 94 | + ); |
| 95 | + expect(userRepository.createUser).toHaveBeenCalledWith({ |
| 96 | + githubId: parseInt(mockGithubProfile.id), |
| 97 | + username: `camper_${mockGithubProfile.id}`, |
| 98 | + }); |
| 99 | + expect(result).toEqual(mockUser); |
| 100 | + }); |
| 101 | + |
| 102 | + it("getUserByGithubId 에러 발생 시 예외를 던져야 한다", async () => { |
| 103 | + // Given |
| 104 | + const error = new Error("Database error"); |
| 105 | + jest.spyOn(userRepository, "getUserByGithubId").mockRejectedValue( |
| 106 | + error |
| 107 | + ); |
| 108 | + |
| 109 | + // When & Then |
| 110 | + await expect( |
| 111 | + authService.githubLogin(mockGithubProfile) |
| 112 | + ).rejects.toThrow(error); |
| 113 | + }); |
| 114 | + |
| 115 | + it("createUser 에러 발생 시 예외를 던져야 한다", async () => { |
| 116 | + // Given |
| 117 | + const error = new Error("Database error"); |
| 118 | + jest.spyOn(userRepository, "getUserByGithubId").mockResolvedValue( |
| 119 | + null |
| 120 | + ); |
| 121 | + jest.spyOn(userRepository, "createUser").mockRejectedValue(error); |
| 122 | + |
| 123 | + // When & Then |
| 124 | + await expect( |
| 125 | + authService.githubLogin(mockGithubProfile) |
| 126 | + ).rejects.toThrow(error); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments