-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 0 additions & 4 deletions
4
src/main/java/org/example/commerce_site/common/util/PasswordUtil.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
src/test/java/org/example/commerce_site/application/auth/KeycloakAuthServiceTest.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,67 @@ | ||
package org.example.commerce_site.application.auth; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import org.example.commerce_site.application.auth.dto.OAuthAccessTokenResponse; | ||
import org.example.commerce_site.config.KeycloakProperties; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.admin.client.resource.ClientsResource; | ||
import org.keycloak.admin.client.resource.RealmResource; | ||
import org.keycloak.admin.client.resource.UserResource; | ||
import org.keycloak.admin.client.resource.UsersResource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class KeycloakAuthServiceTest { | ||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@Mock | ||
private Keycloak keycloak; | ||
|
||
@Mock | ||
private KeycloakProperties keycloakProperties; | ||
|
||
@Mock | ||
private KeycloakProperties.Credentials credentials; | ||
|
||
@Mock | ||
private KeycloakProperties.Uri uri; | ||
|
||
@Test | ||
void getAccessToken_ShouldReturnAccessToken() { | ||
String code = "validCode"; | ||
OAuthAccessTokenResponse.Keycloak expectedToken = new OAuthAccessTokenResponse.Keycloak(); | ||
expectedToken.setAccessToken("sampleToken"); | ||
expectedToken.setRefreshToken("sampleRefreshToken"); | ||
expectedToken.setIdToken("sampleIdToken"); | ||
|
||
when(credentials.getClient()).thenReturn("clientId"); | ||
when(credentials.getSecret()).thenReturn("clientSecret"); | ||
|
||
when(uri.getRedirect()).thenReturn("redirectUri"); | ||
when(uri.getToken()).thenReturn("tokenUri"); | ||
|
||
when(keycloakProperties.getCredentials()).thenReturn(credentials); | ||
when(keycloakProperties.getUri()).thenReturn(uri); | ||
|
||
when(restTemplate.postForEntity(eq("tokenUri"), any(), eq(OAuthAccessTokenResponse.Keycloak.class))) | ||
.thenReturn(ResponseEntity.ok(expectedToken)); | ||
|
||
KeycloakAuthService keycloakAuthService = new KeycloakAuthService(keycloak, keycloakProperties, restTemplate); | ||
|
||
OAuthAccessTokenResponse.Keycloak result = keycloakAuthService.getAccessToken(code); | ||
|
||
assertNotNull(result); | ||
assertEquals(expectedToken.getAccessToken(), result.getAccessToken()); | ||
assertEquals(expectedToken.getRefreshToken(), result.getRefreshToken()); | ||
assertEquals(expectedToken.getIdToken(), result.getIdToken()); | ||
} | ||
} |