-
Notifications
You must be signed in to change notification settings - Fork 9
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
3 changed files
with
69 additions
and
2 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
63 changes: 63 additions & 0 deletions
63
superfly-service/src/test/java/com/payneteasy/superfly/hotp/HOTPServiceImplTest.java
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,63 @@ | ||
package com.payneteasy.superfly.hotp; | ||
|
||
import com.payneteasy.superfly.api.SsoDecryptException; | ||
import com.payneteasy.superfly.crypto.CryptoServiceImpl; | ||
import com.payneteasy.superfly.crypto.exception.EncryptException; | ||
import com.payneteasy.superfly.service.UserService; | ||
import com.payneteasy.superfly.service.impl.UserServiceImpl; | ||
import com.warrenstrange.googleauth.GoogleAuthenticatorKey; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class HOTPServiceImplTest { | ||
public static final String USERNAME = "user"; | ||
private HOTPServiceImpl service; | ||
private GoogleAuthenticatorKey credentials; | ||
|
||
@Before | ||
public void setup() { | ||
service = new HOTPServiceImpl(); | ||
credentials = service.getGoogleAuthenticator().get().createCredentials(); | ||
|
||
CryptoServiceImpl cryptoService = new CryptoServiceImpl(); | ||
cryptoService.setCryptoSalt("GOOGLE_SALT"); | ||
cryptoService.setCryptoSecret("GOOGLE_SECRET"); | ||
|
||
UserService userService = new UserServiceImpl() { | ||
@Override | ||
public String getOtpMasterKeyByUsername(String username) { | ||
if (USERNAME.equals(username)) { | ||
try { | ||
return cryptoService.encrypt(credentials.getKey()); | ||
} catch (EncryptException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return null; | ||
} | ||
}; | ||
service.setCryptoService(cryptoService); | ||
service.setUserService(userService); | ||
} | ||
|
||
@Test | ||
public void testValidateGoogleTimePassword() throws SsoDecryptException { | ||
String totpPassword = String.valueOf( | ||
service.getGoogleAuthenticator().get().getTotpPassword(credentials.getKey()) | ||
); | ||
|
||
boolean valid = service.validateGoogleTimePassword(USERNAME, totpPassword); | ||
|
||
Assert.assertTrue( "Not valid code", valid); | ||
} | ||
|
||
@Test | ||
public void testUnValidateGoogleTimePassword() throws SsoDecryptException { | ||
String totpPassword = "123123"; | ||
|
||
boolean valid = service.validateGoogleTimePassword(USERNAME, totpPassword); | ||
|
||
Assert.assertFalse( "Valid code", valid); | ||
} | ||
} |