-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
82 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
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 |
---|---|---|
|
@@ -37,18 +37,21 @@ describe('Test user service', () => { | |
}); | ||
|
||
describe('Test get user', () => { | ||
const requesterIdToken = { | ||
userId: requesterUserId, | ||
accessLevel: new AccessLevelEnum(AccessLevel.DEVELOPER) | ||
} as AppIdToken; | ||
|
||
beforeAll(() => { | ||
userRepository.findById = jest.fn(() => Promise.resolve(user)); | ||
}); | ||
|
||
it('should success when valid', async () => { | ||
const givenRequesterIdToken = new AppIdToken( | ||
requesterUserId, | ||
'nickname', | ||
'#TAGG', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.DEVELOPER) | ||
); | ||
const actualResult = await new UserService(userRepository).getUser( | ||
requesterIdToken, | ||
givenRequesterIdToken, | ||
requestedUserId | ||
); | ||
|
||
|
@@ -60,9 +63,16 @@ describe('Test user service', () => { | |
|
||
it('should failure when user authorization is not valid', async () => { | ||
try { | ||
requesterIdToken.accessLevel = new AccessLevelEnum(AccessLevel.USER); | ||
const givenRequesterIdToken = new AppIdToken( | ||
requesterUserId, | ||
'nickname', | ||
'#TAGG', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.USER) | ||
); | ||
await new UserService(userRepository).getUser( | ||
requesterIdToken, | ||
givenRequesterIdToken, | ||
requestedUserId | ||
); | ||
} catch (error: unknown) { | ||
|
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 |
---|---|---|
|
@@ -36,37 +36,30 @@ describe('Test jwt client', () => { | |
}); | ||
|
||
it('should verify app id token', () => { | ||
const givenToken: AppIdToken = { | ||
userId: randomUUID(), | ||
nickname: '신비로운 시네필 황금 사자', | ||
tag: '#MQ3B', | ||
idp: new IdpEnum(Idp.GOOGLE), | ||
email: '[email protected]', | ||
accessLevel: new AccessLevelEnum(AccessLevel.USER) | ||
}; | ||
const givenToken = new AppIdToken( | ||
randomUUID(), | ||
'신비로운 시네필 황금 사자', | ||
'#MQ3B', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.USER) | ||
); | ||
|
||
const tokenString = client.signAppIdToken(givenToken); | ||
const verifiedToken = client.verifyAppIdToken(tokenString); | ||
|
||
expect(verifiedToken.userId).toEqual(givenToken.userId); | ||
expect(verifiedToken.nickname).toEqual(givenToken.nickname); | ||
expect(verifiedToken.tag).toEqual(givenToken.tag); | ||
expect(verifiedToken.idp.get()).toEqual(givenToken.idp.get()); | ||
expect(verifiedToken.email).toEqual(givenToken.email); | ||
expect(verifiedToken.accessLevel.get()).toEqual( | ||
givenToken.accessLevel.get() | ||
); | ||
expect(JSON.stringify(verifiedToken)).toEqual(JSON.stringify(givenToken)); | ||
}); | ||
|
||
it('should throw error when verify malicious app id token', () => { | ||
const givenToken: AppIdToken = { | ||
userId: randomUUID(), | ||
nickname: '신비로운 시네필 황금 사자', | ||
tag: '#MQ3B', | ||
idp: new IdpEnum(Idp.GOOGLE), | ||
email: '[email protected]', | ||
accessLevel: new AccessLevelEnum(AccessLevel.USER) | ||
}; | ||
const givenToken = new AppIdToken( | ||
randomUUID(), | ||
'신비로운 시네필 황금 사자', | ||
'#MQ3B', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.USER) | ||
); | ||
|
||
const tokenString = client.signAppIdToken(givenToken); | ||
const [encodedHeader, encodedPayload, signature] = tokenString.split('.'); | ||
|
@@ -85,24 +78,28 @@ describe('Test jwt client', () => { | |
}); | ||
|
||
it('should successfully decode a token without verification', () => { | ||
const givenToken: AppIdToken = { | ||
userId: randomUUID(), | ||
nickname: '신비로운 시네필 황금 사자', | ||
tag: '#MQ3B', | ||
idp: new IdpEnum(Idp.GOOGLE), | ||
email: '[email protected]', | ||
accessLevel: new AccessLevelEnum(AccessLevel.USER) | ||
}; | ||
const givenToken = new AppIdToken( | ||
randomUUID(), | ||
'신비로운 시네필 황금 사자', | ||
'#MQ3B', | ||
new IdpEnum(Idp.GOOGLE), | ||
'[email protected]', | ||
new AccessLevelEnum(AccessLevel.USER) | ||
); | ||
|
||
const tokenString = client.signAppIdToken(givenToken); | ||
const decodedPayload = client.decodeTokenWithoutVerify(tokenString) | ||
.payload as AppPayload; | ||
|
||
expect(decodedPayload.userId).toEqual(givenToken.userId); | ||
expect(decodedPayload.nickname).toEqual(givenToken.nickname); | ||
expect(decodedPayload.tag).toEqual(givenToken.tag); | ||
expect(decodedPayload.idp).toEqual(givenToken.idp.get()); | ||
expect(decodedPayload.email).toEqual(givenToken.email); | ||
expect(decodedPayload.accessLevel).toEqual(givenToken.accessLevel.get()); | ||
expect(decodedPayload).toEqual( | ||
expect.objectContaining({ | ||
userId: givenToken.userId, | ||
nickname: givenToken.nickname, | ||
tag: givenToken.tag, | ||
idp: givenToken.idp.get(), | ||
email: givenToken.email, | ||
accessLevel: givenToken.accessLevel.get() | ||
}) | ||
); | ||
}); | ||
}); |