Skip to content

Commit

Permalink
feat : adds Security Test (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Jun 26, 2024
1 parent be1b9f9 commit 895eb5d
Showing 1 changed file with 45 additions and 0 deletions.
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();
}
}

0 comments on commit 895eb5d

Please sign in to comment.