Skip to content

Commit

Permalink
Add test cases for OAuth feature validation (#1874)
Browse files Browse the repository at this point in the history
* test: Add test cases for OAuth feature validation

* remove old property and remove WithMockUser annotation as the logout url is public one

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
rahul-chekuri and mergify[bot] authored Mar 3, 2025
1 parent 15465c9 commit a33b053
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gate-web/gate-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ dependencies {
testImplementation "org.apache.groovy:groovy-json"
testImplementation "com.squareup.retrofit2:converter-jackson"
testImplementation "com.squareup.retrofit2:retrofit-mock"
testImplementation "org.springframework.security:spring-security-oauth2-client"


// Add each included authz provider as a runtime dependency
gradle.includedProviderProjects.each {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.netflix.spinnaker.gate.security.oauth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -32,7 +34,6 @@
@AutoConfigureMockMvc
@SpringBootTest(
properties = {
"retrofit.enabled=true",
"security.oauth2.client.clientId=Spinnaker-Client",
"security.oauth2.resource.userInfoUri=http://localhost/userinfo"
})
Expand All @@ -51,4 +52,22 @@ void shouldRedirectOnOauth2Authentication() throws Exception {

assertEquals(302, result.getResponse().getStatus());
}

/** Test: Public endpoint should be accessible without authentication */
@Test
public void whenAccessingPublicEndpointThenSuccess() throws Exception {
mockMvc.perform(get("/auth/user")).andExpect(status().isOk());
}

/** Test: Secure endpoint should be accessible with OAuth2 authentication */
@Test
public void whenAuthenticatedWithOAuth2ThenAccessGranted() throws Exception {
mockMvc.perform(get("/credentials").with(oauth2Login())).andExpect(status().isOk());
}

/** Test: Logout should redirect to home */
@Test
public void whenLoggingOutThenRedirectToHome() throws Exception {
mockMvc.perform(get("/auth/logout")).andExpect(status().is3xxRedirection());
}
}

0 comments on commit a33b053

Please sign in to comment.