-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev-be' of github.com:boostcampwm-2024/web17-boostproje…
…ct into dev-be
- Loading branch information
Showing
10 changed files
with
178 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GoogleAuthController } from '@/auth/googleAuth.controller'; | ||
import { GoogleStrategy } from '@/auth/google.strategy'; | ||
import { GoogleAuthController } from '@/auth/googleAuth.controller'; | ||
import { GoogleAuthService } from '@/auth/googleAuth.service'; | ||
import { UserModule } from '@/user/user.module'; | ||
|
||
@Module({ | ||
imports: [UserModule], | ||
controllers: [GoogleAuthController], | ||
providers: [GoogleStrategy], | ||
providers: [GoogleStrategy, GoogleAuthService], | ||
}) | ||
export class AuthModule {} | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { OauthUserInfo } from '@/auth/google.strategy'; | ||
import { GoogleAuthService } from '@/auth/googleAuth.service'; | ||
import { OauthType } from '@/user/domain/ouathType'; | ||
import { Role } from '@/user/domain/role'; | ||
import { User } from '@/user/domain/user.entity'; | ||
import { UserService } from '@/user/user.service'; | ||
|
||
describe('GoogleAuthService 테스트', () => { | ||
const userInfo: OauthUserInfo = { | ||
type: OauthType.GOOGLE, | ||
givenName: 'Homer Jay', | ||
familyName: 'Simpson', | ||
email: '[email protected]', | ||
oauthId: '12345678910', | ||
}; | ||
|
||
test('oauthId와 type에 맞는 유저가 있으면 해당 객체를 반환한다.', async () => { | ||
const user: User = { | ||
id: 1, | ||
role: Role.USER, | ||
type: OauthType.GOOGLE, | ||
isLight: true, | ||
}; | ||
const userService: Partial<UserService> = { | ||
findUserByOauthIdAndType: jest.fn().mockResolvedValue(user), | ||
}; | ||
const authService = new GoogleAuthService(userService as UserService); | ||
|
||
const authUser = await authService.attemptAuthentication(userInfo); | ||
|
||
expect(authUser).toBe(user); | ||
}); | ||
|
||
test('자동 회원가입시 이메일이 없으면 예외가 발생한다.', async () => { | ||
const userInfo: OauthUserInfo = { | ||
type: OauthType.GOOGLE, | ||
givenName: 'Homer Jay', | ||
familyName: 'Simpson', | ||
oauthId: '12345678910', | ||
}; | ||
const userService: Partial<UserService> = { | ||
findUserByOauthIdAndType: jest.fn().mockResolvedValue(null), | ||
}; | ||
const authService = new GoogleAuthService(userService as UserService); | ||
|
||
await expect(() => | ||
authService.attemptAuthentication(userInfo), | ||
).rejects.toThrow('email is required'); | ||
}); | ||
|
||
test('자동 회원가입시 givenName과 FamilyName이 없으면 예외가 발생한다.', async () => { | ||
const userInfo: OauthUserInfo = { | ||
type: OauthType.GOOGLE, | ||
email: '[email protected]', | ||
oauthId: '12345678910', | ||
}; | ||
const userService: Partial<UserService> = { | ||
findUserByOauthIdAndType: jest.fn().mockResolvedValue(null), | ||
}; | ||
const authService = new GoogleAuthService(userService as UserService); | ||
|
||
await expect(() => | ||
authService.attemptAuthentication(userInfo), | ||
).rejects.toThrow('name is required'); | ||
}); | ||
|
||
test.each([ | ||
['Homer Jay', 'Simpson', 'Homer Jay Simpson'], | ||
['Homer Jay', undefined, 'Homer Jay'], | ||
[undefined, 'Simpson', 'Simpson'], | ||
])( | ||
'이름을 자동 생성 후 가입한다.', | ||
async (givenName, familyName, expected) => { | ||
const userInfo: OauthUserInfo = { | ||
type: OauthType.GOOGLE, | ||
givenName, | ||
familyName, | ||
email: '[email protected]', | ||
oauthId: '12345678910', | ||
}; | ||
const userService: Partial<UserService> = { | ||
findUserByOauthIdAndType: jest.fn().mockResolvedValue(null), | ||
register: jest.fn(), | ||
}; | ||
const authService = new GoogleAuthService(userService as UserService); | ||
|
||
await authService.attemptAuthentication(userInfo); | ||
|
||
expect(userService.register).toHaveBeenCalledWith({ | ||
type: userInfo.type, | ||
nickname: expected, | ||
email: userInfo.email, | ||
oauthId: userInfo.oauthId, | ||
}); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { OauthUserInfo } from '@/auth/google.strategy'; | ||
import { UserService } from '@/user/user.service'; | ||
|
||
@Injectable() | ||
export class GoogleAuthService { | ||
constructor(private readonly userService: UserService) {} | ||
|
||
async attemptAuthentication(userInfo: OauthUserInfo) { | ||
const { email, givenName, familyName, oauthId, type } = userInfo; | ||
const user = await this.userService.findUserByOauthIdAndType(oauthId, type); | ||
if (user) { | ||
return user; | ||
} | ||
if (!email) { | ||
throw new UnauthorizedException('email is required'); | ||
} | ||
if (!givenName && !familyName) { | ||
throw new UnauthorizedException('name is required'); | ||
} | ||
return await this.userService.register({ | ||
type, | ||
nickname: this.createName(givenName, familyName), | ||
email: email as string, | ||
oauthId, | ||
}); | ||
} | ||
|
||
private createName(givenName?: string, familyName?: string) { | ||
return `${givenName ? `${givenName} ` : ''}${familyName ? familyName : ''}`.trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { CreateDateColumn, UpdateDateColumn } from 'typeorm'; | ||
|
||
export class DateEmbedded { | ||
@CreateDateColumn({ name: 'created_at' }) | ||
@CreateDateColumn({ type: 'timestamp', name: 'created_at' }) | ||
createdAt?: Date; | ||
|
||
@UpdateDateColumn({ name: 'updated_at' }) | ||
@UpdateDateColumn({ type: 'timestamp', name: 'updated_at' }) | ||
updatedAt?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ describe('UserService 테스트', () => { | |
email: '[email protected]', | ||
type: OauthType.GOOGLE, | ||
nickname: 'test', | ||
oauthId: 1, | ||
oauthId: '123123231242141', | ||
}; | ||
|
||
test('유저를 생성한다', async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters