+
WC-API Example
+
+
+
Hello!
+
When you click the login button below, you will be redirected to the login page on auth0.com. After you authenticate, you will be returned to this application.
+
+
+
+
Welcome home, !
+
![User Avatar]()
+
Visit the My Profile page in this application to view the information retrieved with your OAuth Access Token.
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/templates/menu.html b/src/main/resources/templates/menu.html
new file mode 100644
index 0000000..0f47da3
--- /dev/null
+++ b/src/main/resources/templates/menu.html
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
My Profile
+
Hello, . Below is the information that was read with your Access Token.
+
+
This route is protected with the annotation @PreAuthorize("hasAuthority('SCOPE_profile')"), which will ensure that this page cannot be accessed until you have authenticated, and have the scope profile.
+
+
+
+
+
+ | Claim |
+ Value |
+
+
+
+
+ | Key |
+ Value |
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/java/com/wiredcraft/wcapi/MatrixTest.java b/src/test/java/com/wiredcraft/wcapi/MatrixTest.java
new file mode 100644
index 0000000..858be5b
--- /dev/null
+++ b/src/test/java/com/wiredcraft/wcapi/MatrixTest.java
@@ -0,0 +1,40 @@
+package com.wiredcraft.wcapi;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class MatrixTest {
+
+ public boolean testMatrix(int[][] mat, int target) {
+ int m = mat[0].length;
+ int i = 0, j = m - 1;
+
+ while (true) {
+ int val = mat[i][j];
+ if (val == target) {
+ return true;
+ } else if (val < target && i < m - 1) {
+ i++;
+ } else if (val > target && j > 0) {
+ j--;
+ } else {
+ return false;
+ }
+ }
+ }
+
+ @Test
+ public void testMat() {
+ //
+ MatrixTest t = new MatrixTest();
+ int[][] mat = {
+ {1, 4, 7, 11, 15},
+ {2, 5, 8, 12, 19},
+ {3, 6, 9, 16, 22},
+ {10, 13, 14, 17, 24},
+ {18, 21, 23, 26, 30}};
+
+ Assertions.assertFalse(t.testMatrix(mat, 20));
+ Assertions.assertTrue(t.testMatrix(mat, 5));
+ }
+}
diff --git a/src/test/java/com/wiredcraft/wcapi/WcApiApplicationTests.java b/src/test/java/com/wiredcraft/wcapi/WcApiApplicationTests.java
new file mode 100644
index 0000000..cbdfb86
--- /dev/null
+++ b/src/test/java/com/wiredcraft/wcapi/WcApiApplicationTests.java
@@ -0,0 +1,25 @@
+package com.wiredcraft.wcapi;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+@SpringBootTest
+@ActiveProfiles("test")
+class WcApiApplicationTests {
+
+ @Test
+ void contextLoads() {
+ }
+
+ @Test
+ void mainMethodShouldStartApplication() {
+ // Test that the main method can be called without throwing an exception
+ // This is a basic smoke test to ensure the application can start
+ // We don't actually call main here as it would start the full application
+ // Instead, we just verify the context can load, which is done by contextLoads()
+ }
+
+}
diff --git a/src/test/java/com/wiredcraft/wcapi/config/SecurityConfigTest.java b/src/test/java/com/wiredcraft/wcapi/config/SecurityConfigTest.java
new file mode 100644
index 0000000..a5e6ff6
--- /dev/null
+++ b/src/test/java/com/wiredcraft/wcapi/config/SecurityConfigTest.java
@@ -0,0 +1,149 @@
+package com.wiredcraft.wcapi.config;
+
+import com.wiredcraft.wcapi.controller.LogoutController;
+import jakarta.servlet.http.HttpServletRequest;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.BDDMockito.given;
+
+@ExtendWith(MockitoExtension.class)
+@ActiveProfiles("test")
+public class SecurityConfigTest {
+
+ private SecurityConfig securityConfig;
+
+ @Mock
+ private HttpServletRequest request;
+
+ @BeforeEach
+ void setUp() {
+ securityConfig = new SecurityConfig();
+
+ // Set up test values using reflection
+ ReflectionTestUtils.setField(securityConfig, "domain", "https://dev-wc-1.jp.auth0.com/");
+ ReflectionTestUtils.setField(securityConfig, "clientId", "test-client-id");
+ ReflectionTestUtils.setField(securityConfig, "clientSecret", "test-client-secret");
+ ReflectionTestUtils.setField(securityConfig, "managementApiClientId", "test-mgmt-client-id");
+ ReflectionTestUtils.setField(securityConfig, "managementApiClientSecret", "test-mgmt-client-secret");
+ ReflectionTestUtils.setField(securityConfig, "grantType", "client_credentials");
+ }
+
+ @Test
+ void shouldCreateLogoutSuccessHandler() {
+ LogoutSuccessHandler handler = securityConfig.logoutSuccessHandler();
+
+ assertThat(handler).isNotNull();
+ assertThat(handler).isInstanceOf(LogoutController.class);
+ }
+
+ @Test
+ void shouldGetContextPath() {
+ given(request.getScheme()).willReturn("https");
+ given(request.getServerName()).willReturn("localhost");
+ given(request.getServerPort()).willReturn(8080);
+
+ String contextPath = securityConfig.getContextPath(request);
+
+ assertThat(contextPath).isEqualTo("https://localhost:8080");
+ }
+
+ @Test
+ void shouldGetContextPathWithHttpAndPort80() {
+ given(request.getScheme()).willReturn("http");
+ given(request.getServerName()).willReturn("example.com");
+ given(request.getServerPort()).willReturn(80);
+
+ String contextPath = securityConfig.getContextPath(request);
+
+ assertThat(contextPath).isEqualTo("http://example.com:80");
+ }
+
+ @Test
+ void shouldGetUserInfoUrl() {
+ String userInfoUrl = securityConfig.getUserInfoUrl();
+
+ assertThat(userInfoUrl).isEqualTo("https://dev-wc-1.jp.auth0.com/userinfo");
+ }
+
+ @Test
+ void shouldGetUsersUrl() {
+ String usersUrl = securityConfig.getUsersUrl();
+
+ assertThat(usersUrl).isEqualTo("https://dev-wc-1.jp.auth0.com/api/v2/users");
+ }
+
+ @Test
+ void shouldGetUsersByEmailUrl() {
+ String usersByEmailUrl = securityConfig.getUsersByEmailUrl();
+
+ assertThat(usersByEmailUrl).isEqualTo("https://dev-wc-1.jp.auth0.com/api/v2/users-by-email?email=");
+ }
+
+ @Test
+ void shouldGetLogoutUrl() {
+ String logoutUrl = securityConfig.getLogoutUrl();
+
+ assertThat(logoutUrl).isEqualTo("https://dev-wc-1.jp.auth0.com/v2/logout");
+ }
+
+ @Test
+ void shouldGetDomain() {
+ String domain = securityConfig.getDomain();
+
+ assertThat(domain).isEqualTo("https://dev-wc-1.jp.auth0.com/");
+ }
+
+ @Test
+ void shouldGetClientId() {
+ String clientId = securityConfig.getClientId();
+
+ assertThat(clientId).isEqualTo("test-client-id");
+ }
+
+ @Test
+ void shouldGetClientSecret() {
+ String clientSecret = securityConfig.getClientSecret();
+
+ assertThat(clientSecret).isEqualTo("test-client-secret");
+ }
+
+ @Test
+ void shouldGetManagementApiClientId() {
+ String managementApiClientId = securityConfig.getManagementApiClientId();
+
+ assertThat(managementApiClientId).isEqualTo("test-mgmt-client-id");
+ }
+
+ @Test
+ void shouldGetManagementApiClientSecret() {
+ String managementApiClientSecret = securityConfig.getManagementApiClientSecret();
+
+ assertThat(managementApiClientSecret).isEqualTo("test-mgmt-client-secret");
+ }
+
+ @Test
+ void shouldGetGrantType() {
+ String grantType = securityConfig.getGrantType();
+
+ assertThat(grantType).isEqualTo("client_credentials");
+ }
+
+ @Test
+ void shouldHandleNullDomain() {
+ ReflectionTestUtils.setField(securityConfig, "domain", null);
+
+ String userInfoUrl = securityConfig.getUserInfoUrl();
+ assertThat(userInfoUrl).isEqualTo("nulluserinfo");
+
+ String usersUrl = securityConfig.getUsersUrl();
+ assertThat(usersUrl).isEqualTo("nullapi/v2/users");
+ }
+}
diff --git a/src/test/java/com/wiredcraft/wcapi/controller/AuthControllerTest.java b/src/test/java/com/wiredcraft/wcapi/controller/AuthControllerTest.java
new file mode 100644
index 0000000..0e9928a
--- /dev/null
+++ b/src/test/java/com/wiredcraft/wcapi/controller/AuthControllerTest.java
@@ -0,0 +1,110 @@
+package com.wiredcraft.wcapi.controller;
+
+import com.wiredcraft.wcapi.service.UserService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.security.oauth2.client.registration.ClientRegistration;
+import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
+import org.springframework.security.oauth2.core.AuthorizationGrantType;
+import org.springframework.security.oauth2.core.user.OAuth2User;
+import org.springframework.test.context.ActiveProfiles;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.*;
+
+@ActiveProfiles("test")
+public class AuthControllerTest {
+
+ private ClientRegistrationRepository clientRegistrationRepository;
+ private TestSecurityConfig securityConfig;
+ private UserService userService;
+
+ private OAuth2User oauth2User;
+ private ClientRegistration clientRegistration;
+
+ @BeforeEach
+ void setUp() {
+ // Create mocks
+ clientRegistrationRepository = mock(ClientRegistrationRepository.class);
+ securityConfig = new TestSecurityConfig();
+ userService = mock(UserService.class);
+
+ oauth2User = mock(OAuth2User.class);
+
+ clientRegistration = ClientRegistration.withRegistrationId("okta")
+ .clientId("test-client-id")
+ .clientSecret("test-client-secret")
+ .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
+ .redirectUri("http://localhost:8080/login/oauth2/code/okta")
+ .authorizationUri("https://dev-example.okta.com/oauth2/v1/authorize")
+ .tokenUri("https://dev-example.okta.com/oauth2/v1/token")
+ .userInfoUri("https://dev-example.okta.com/oauth2/v1/userinfo")
+ .userNameAttributeName("sub")
+ .build();
+
+ given(clientRegistrationRepository.findByRegistrationId("okta")).willReturn(clientRegistration);
+ }
+
+ @Test
+ void shouldReturnHomePageWhenUserIsNotAuthenticated() throws Exception {
+ // Note: This test may require authentication to be disabled for proper testing
+ // For now, we're testing the controller method directly
+ AuthController controller = new AuthController(clientRegistrationRepository, securityConfig, userService);
+
+ String result = controller.home(null);
+
+ // Verify the view name is returned correctly
+ assert "home".equals(result);
+
+ // Verify that syncAuth0User is not called when user is null
+ verify(userService, never()).syncAuth0User(any());
+ }
+
+ @Test
+ void shouldReturnHomePageAndSyncUserWhenAuthenticated() throws Exception {
+ // Mock OAuth2User
+ Map