-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from toddkazakov/uri-encode
URI encode username path segment
- Loading branch information
Showing
2 changed files
with
55 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.keycloak.common.util.Encode; | ||
import org.keycloak.common.util.MultivaluedHashMap; | ||
import org.keycloak.component.ComponentModel; | ||
import org.mockito.Mock; | ||
|
@@ -118,7 +119,7 @@ void findByEmailShouldThrowWhenIOExceptionOccurs() { | |
void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatches() throws IOException { | ||
var expectedUser = createALegacyUser("someUsername", "[email protected]"); | ||
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser)); | ||
var path = String.format(URI_PATH_FORMAT, URI, expectedUser.getEmail()); | ||
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode(expectedUser.getEmail())); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
|
||
when(httpClient.get(path)).thenReturn(response); | ||
|
@@ -133,7 +134,7 @@ void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatches() throws IOExcep | |
void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatchesCaseInsensitive() throws IOException { | ||
var expectedUser = createALegacyUser("someUsername", "[email protected]"); | ||
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser)); | ||
var path = String.format(URI_PATH_FORMAT, URI, "[email protected]"); | ||
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode("[email protected]")); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
|
||
when(httpClient.get(path)).thenReturn(response); | ||
|
@@ -163,7 +164,7 @@ private LegacyUser createALegacyUser(String username, String email) { | |
@Test | ||
void findByEmailShouldReturnAnEmptyOptionalWhenUserIsNotFound() { | ||
var expectedUser = createALegacyUser("someUsername", "[email protected]"); | ||
var path = String.format(URI_PATH_FORMAT, URI, expectedUser.getEmail()); | ||
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode(expectedUser.getEmail())); | ||
var response = new HttpResponse(HttpStatus.SC_NOT_FOUND); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
when(httpClient.get(path)).thenReturn(response); | ||
|
@@ -248,6 +249,20 @@ void findByUsernameShouldReturnAUserWhenUserIsFoundAndUsernameMatchesCaseInsensi | |
assertEquals(result.get(), expectedUser); | ||
} | ||
|
||
@Test | ||
void findByUsernameShouldReturnAUserWhenUserIsFoundAndUsernameContainsSpace() throws IOException { | ||
var expectedUser = createALegacyUser("some Username", "[email protected]"); | ||
var path = String.format(URI_PATH_FORMAT, URI, "SOME+USERNAME"); | ||
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser)); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
when(httpClient.get(path)).thenReturn(response); | ||
|
||
var result = restUserService.findByUsername("SOME USERNAME"); | ||
|
||
assertTrue(result.isPresent()); | ||
assertEquals(result.get(), expectedUser); | ||
} | ||
|
||
@Test | ||
void findByUsernameShouldReturnAnEmptyOptionalWhenUserIsNotFound() { | ||
var expectedUser = createALegacyUser("someUsername", "[email protected]"); | ||
|
@@ -310,6 +325,36 @@ void isPasswordValidShouldReturnTrueWhenPasswordsMatches() throws IOException { | |
assertTrue(isPasswordValid); | ||
} | ||
|
||
@Test | ||
void isPasswordValidShouldNotThrowNullPointerExceptionWhenPasswordIsNull() throws IOException { | ||
String username = null; | ||
var password = "anyPassword"; | ||
var path = String.format(URI_PATH_FORMAT, URI, username); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
var response = new HttpResponse(HttpStatus.SC_OK); | ||
var expectedBody = objectMapper.writeValueAsString(new UserPasswordDto(password)); | ||
when(httpClient.post(path, expectedBody)).thenReturn(response); | ||
|
||
var isPasswordValid = restUserService.isPasswordValid(username, password); | ||
|
||
assertTrue(isPasswordValid); | ||
} | ||
|
||
@Test | ||
void isPasswordValidShouldReturnTrueWhenPasswordsMatchesAndUsernameContainsSpace() throws IOException { | ||
var username = "some Username"; | ||
var password = "anyPassword"; | ||
var path = String.format(URI_PATH_FORMAT, URI, "some+Username"); | ||
var restUserService = new RestUserService(model, httpClient, new ObjectMapper()); | ||
var response = new HttpResponse(HttpStatus.SC_OK); | ||
var expectedBody = objectMapper.writeValueAsString(new UserPasswordDto(password)); | ||
when(httpClient.post(path, expectedBody)).thenReturn(response); | ||
|
||
var isPasswordValid = restUserService.isPasswordValid(username, password); | ||
|
||
assertTrue(isPasswordValid); | ||
} | ||
|
||
@Test | ||
void isPasswordValidShouldReturnFalseWhenPasswordsDoNotMatch() { | ||
var username = "someUsername"; | ||
|