-
Notifications
You must be signed in to change notification settings - Fork 13
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
1 parent
be1b9f9
commit 895eb5d
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...tore-webapp/src/test/java/com/example/retailstore/webapp/services/SecurityHelperTest.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,45 @@ | ||
package com.example.retailstore.webapp.services; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; | ||
|
||
class SecurityHelperTest { | ||
|
||
@Test | ||
void testGetLoggedInUserEmail_Success() { | ||
// Setup | ||
SecurityHelper securityHelper = | ||
new SecurityHelper(null); // OAuth2AuthorizedClientService is not used in this method | ||
OAuth2AuthenticationToken oauthToken = mock(OAuth2AuthenticationToken.class); | ||
DefaultOidcUser user = mock(DefaultOidcUser.class); | ||
given(user.getEmail()).willReturn("[email protected]"); | ||
given(oauthToken.getPrincipal()).willReturn(user); | ||
SecurityContextHolder.getContext().setAuthentication(oauthToken); | ||
|
||
// Execute | ||
String email = securityHelper.getLoggedInUserEmail(); | ||
|
||
// Verify | ||
assertThat(email).isEqualTo("[email protected]"); | ||
} | ||
|
||
@Test | ||
void testGetLoggedInUserEmail_Failure() { | ||
// Setup | ||
SecurityHelper securityHelper = | ||
new SecurityHelper(null); // OAuth2AuthorizedClientService is not used in this method | ||
SecurityContextHolder.getContext().setAuthentication(null); | ||
|
||
// Execute | ||
String email = securityHelper.getLoggedInUserEmail(); | ||
|
||
// Verify | ||
assertThat(email).isNull(); | ||
} | ||
} |