-
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.
Fix unit testing for the
UsernamePasswordCredentials
class to inclu…
…de password encoding feature in the JSON format serialization
- Loading branch information
Showing
1 changed file
with
26 additions
and
20 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,50 +1,49 @@ | ||
package one.jpro.platform.auth.core.test; | ||
package one.jpro.platform.auth.core.basic; | ||
|
||
import one.jpro.platform.auth.core.authentication.CredentialValidationException; | ||
import one.jpro.platform.auth.core.basic.UsernamePasswordCredentials; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
import static one.jpro.platform.auth.core.utils.AuthUtils.BASE64_ENCODER; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static one.jpro.platform.auth.core.utils.AuthUtils.BCRYPT_PASSWORD_ENCODER; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
/** | ||
* UsernamePasswordCredentials tests. | ||
* | ||
* @author Besmir Beqiri | ||
*/ | ||
public class UsernamePasswordCredentialsTest { | ||
public class UsernamePasswordCredentialsTests { | ||
|
||
@Test | ||
public void nullUsernameShouldMakeValidationThrowsException() { | ||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(null, "password"); | ||
|
||
Exception exception = Assertions.assertThrowsExactly(CredentialValidationException.class, | ||
() -> credentials.validate(null)); | ||
assertEquals("Username cannot be null or blank", exception.getMessage()); | ||
assertThatExceptionOfType(CredentialValidationException.class) | ||
.isThrownBy(() -> credentials.validate(null)) | ||
.withMessage("Username cannot be null or blank"); | ||
} | ||
|
||
@Test | ||
public void blankUsernameShouldMakeValidationThrowsException() { | ||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(" ", "password"); | ||
|
||
Exception exception = Assertions.assertThrowsExactly(CredentialValidationException.class, | ||
() -> credentials.validate(null)); | ||
assertEquals("Username cannot be null or blank", exception.getMessage()); | ||
assertThatExceptionOfType(CredentialValidationException.class) | ||
.isThrownBy(() -> credentials.validate(null)) | ||
.withMessage("Username cannot be null or blank"); | ||
} | ||
|
||
@Test | ||
public void missingPasswordShouldMakeValidationThrowsException() { | ||
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", null); | ||
|
||
Exception exception = Assertions.assertThrowsExactly(CredentialValidationException.class, | ||
() -> credentials.validate(null)); | ||
assertEquals("Password cannot be null", exception.getMessage()); | ||
assertThatExceptionOfType(CredentialValidationException.class) | ||
.isThrownBy(() -> credentials.validate(null)) | ||
.withMessage("Password cannot be null"); | ||
} | ||
|
||
@Test | ||
|
@@ -53,14 +52,14 @@ public void credentialsWithTheSameUsernameAndPasswordShouldBeEqual() { | |
new UsernamePasswordCredentials("[email protected]", "password"); | ||
UsernamePasswordCredentials credentials2 = | ||
new UsernamePasswordCredentials("[email protected]", "password"); | ||
assertEquals(credentials1, credentials2); | ||
assertThat(credentials1).isEqualTo(credentials2); | ||
} | ||
|
||
@Test | ||
public void testHashCodeMethod() { | ||
UsernamePasswordCredentials credentials = | ||
new UsernamePasswordCredentials("some_username", "some_password"); | ||
assertEquals(Objects.hash(credentials.getUsername(), credentials.getPassword()), credentials.hashCode()); | ||
assertThat(credentials.hashCode()).isEqualTo(Objects.hash(credentials.getUsername(), credentials.getPassword())); | ||
} | ||
|
||
@Test | ||
|
@@ -70,18 +69,25 @@ public void testHttpAuthorizationMethod() { | |
|
||
String authHeader = "Basic " + BASE64_ENCODER.encodeToString((credentials.getUsername() + ":" | ||
+ credentials.getPassword()).getBytes(StandardCharsets.UTF_8)); | ||
assertEquals(authHeader, credentials.toHttpAuthorization()); | ||
assertThat(credentials.toHttpAuthorization()).isEqualTo(authHeader); | ||
} | ||
|
||
@Test | ||
public void toJSONMethodProvidesTheExpectedResult() { | ||
UsernamePasswordCredentials credentials = | ||
new UsernamePasswordCredentials("some_username", "some_password"); | ||
|
||
JSONObject credentialsJSON = credentials.toJSON(); | ||
assertThat(credentialsJSON.has("username")).isTrue(); | ||
assertThat(credentialsJSON.has("password")).isTrue(); | ||
assertThat(BCRYPT_PASSWORD_ENCODER.matches("some_password", | ||
credentialsJSON.getString("password"))).isTrue(); | ||
|
||
JSONObject json = new JSONObject(); | ||
json.put("username", credentials.getUsername()); | ||
json.put("password", credentials.getPassword()); | ||
json.put("password", BCRYPT_PASSWORD_ENCODER.encode(credentials.getPassword())); | ||
|
||
assertTrue(credentials.toJSON().similar(json)); | ||
assertThat(credentialsJSON.getString("username")).isEqualTo(json.getString("username")); | ||
assertThat(credentialsJSON.getString("password")).isNotEqualTo(json.getString("password")); | ||
} | ||
} |