Skip to content

Commit

Permalink
Fix unit testing for the UsernamePasswordCredentials class to inclu…
Browse files Browse the repository at this point in the history
…de password encoding feature in the JSON format serialization
  • Loading branch information
besidev committed Jan 3, 2024
1 parent 227354e commit 6d15708
Showing 1 changed file with 26 additions and 20 deletions.
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
Expand All @@ -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
Expand All @@ -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"));
}
}

0 comments on commit 6d15708

Please sign in to comment.