-
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
9 changed files
with
289 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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
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
45 changes: 45 additions & 0 deletions
45
springdog-project/springdog-agent/src/test/java/fixture/DashBoardResponseFixture.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 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fixture; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.easypeelsecurity.springdog.shared.dto.SystemMetricDto; | ||
import org.easypeelsecurity.springdog.shared.vo.DashboardResponse; | ||
import org.easypeelsecurity.springdog.shared.vo.DashboardResponse.DailyEndpointMetric; | ||
import org.easypeelsecurity.springdog.shared.vo.DashboardResponse.DailySlowestEndpoint; | ||
import org.easypeelsecurity.springdog.shared.vo.DashboardResponse.DailyTopFailWithRatelimitEndpoint; | ||
import org.easypeelsecurity.springdog.shared.vo.DashboardResponse.DailyTopTrafficEndpoint; | ||
|
||
@SuppressWarnings("checkstyle:HideUtilityClassConstructor") | ||
public class DashBoardResponseFixture { | ||
public static DashboardResponse get() { | ||
SystemMetricDto systemMetric = new SystemMetricDto(10.0, 20.0, 30.0, LocalDateTime.now()); | ||
var dailyEndpointMetric = new DailyEndpointMetric(1, 2, 3, LocalDate.now()); | ||
var dailyTopTrafficEndpoint = new DailyTopTrafficEndpoint("/test", "GET", 1); | ||
var dailySlowestEndpoint = new DailySlowestEndpoint("/test", "GET", 1); | ||
var dailyTopFailWithRatelimitEndpoint = new DailyTopFailWithRatelimitEndpoint("/test", "GET", 1); | ||
|
||
return new DashboardResponse(1, 2, 3, | ||
List.of(systemMetric), | ||
List.of(dailyEndpointMetric), | ||
List.of(dailyTopTrafficEndpoint), List.of(dailySlowestEndpoint), | ||
List.of(dailyTopFailWithRatelimitEndpoint)); | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
...gdog-agent/src/test/java/org/easypeelsecurity/springdog/agent/SpringdogAgentViewTest.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,170 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.easypeelsecurity.springdog.agent; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import org.easypeelsecurity.springdog.agent.security.SpringdogSecurityConfig; | ||
import org.easypeelsecurity.springdog.domain.ratelimit.EndpointService; | ||
import org.easypeelsecurity.springdog.domain.statistics.StatisticsService; | ||
import org.easypeelsecurity.springdog.shared.dto.EndpointDto; | ||
import org.easypeelsecurity.springdog.shared.enums.HttpMethod; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import fixture.DashBoardResponseFixture; | ||
|
||
@SpringBootTest(properties = "springdog.agent.base-path=springdog") | ||
@Import({TestConfig.class, SpringdogSecurityConfig.class}) | ||
class SpringdogAgentViewTest { | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
WebApplicationContext context; | ||
|
||
@MockBean | ||
StatisticsService statisticsService; | ||
|
||
@MockBean | ||
EndpointService endpointService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders | ||
.webAppContextSetup(context) | ||
.apply(springSecurity()) | ||
.build(); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"/", | ||
"/aidwjaiwjdawid", | ||
"/login", | ||
}) | ||
void invalidPage(String invalidPath) throws Exception { | ||
mockMvc.perform(get("/login")) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Inaccessible without permission") | ||
void withoutAuthentication() throws Exception { | ||
mockMvc.perform(get("/springdog/")) | ||
.andExpect(status().is3xxRedirection()) | ||
.andExpect(redirectedUrl("http://localhost/springdog/login")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = {SpringdogSecurityConfig.SPRINGDOG_AGENT_ADMIN_ROLE}) | ||
void testHomePage() throws Exception { | ||
when(endpointService.getAllChangeLogsNotResolved()).thenReturn(Collections.emptyList()); | ||
when(statisticsService.getDashboardResponse(any())).thenReturn(DashBoardResponseFixture.get()); | ||
|
||
mockMvc.perform(get("/springdog/")) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("/templates/content/main.html")) | ||
.andExpect(model().attributeExists("endpointChangeLog", "totalEndpointCount")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = {SpringdogSecurityConfig.SPRINGDOG_AGENT_ADMIN_ROLE}) | ||
void ratelimitList() throws Exception { | ||
when(endpointService.findAllEndpoints()).thenReturn(List.of()); | ||
|
||
mockMvc.perform(get("/springdog/rate-limit")) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("/templates/content/rate-limit/manage.html")) | ||
.andExpect(model().attributeExists("endpoints")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = {SpringdogSecurityConfig.SPRINGDOG_AGENT_ADMIN_ROLE}) | ||
void setRateLimitSpecific() throws Exception { | ||
long endpointId = 1L; | ||
when(endpointService.findEndpoint(endpointId)).thenReturn( | ||
new EndpointDto.Builder() | ||
.id(endpointId) | ||
.path("/test") | ||
.methodSignature("org.a.b.C.d()") | ||
.httpMethod(HttpMethod.GET) | ||
.build() | ||
); | ||
|
||
mockMvc.perform(get("/springdog/rate-limit/{endpointId}", endpointId)) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("/templates/content/rate-limit/actions/setting.html")) | ||
.andExpect(model().attributeExists("endpoint")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = {SpringdogSecurityConfig.SPRINGDOG_AGENT_ADMIN_ROLE}) | ||
void viewRatelimitEndpointAnalytics() throws Exception { | ||
long endpointId = 1L; | ||
when(endpointService.findEndpoint(endpointId)).thenReturn( | ||
new EndpointDto.Builder() | ||
.id(endpointId) | ||
.path("/test") | ||
.methodSignature("org.a.b.C.d()") | ||
.httpMethod(HttpMethod.GET) | ||
.build() | ||
); | ||
|
||
mockMvc.perform(get("/springdog/rate-limit/{endpointId}/analytics", endpointId)) | ||
.andExpect(status().isOk()) | ||
.andExpect(view().name("/templates/content/rate-limit/actions/analytics.html")) | ||
.andExpect(model().attributeExists("endpoint")); | ||
} | ||
|
||
@Test | ||
void testLogin() throws Exception { | ||
mockMvc.perform(get("/springdog/rate-limit")) | ||
.andExpect(status().is3xxRedirection()) | ||
.andExpect(redirectedUrl("http://localhost/springdog/login")); | ||
} | ||
|
||
@Test | ||
@WithMockUser(username = "admin", roles = {SpringdogSecurityConfig.SPRINGDOG_AGENT_ADMIN_ROLE}) | ||
void testLogout() throws Exception { | ||
mockMvc.perform(get("/springdog/logout")) | ||
.andExpect(status().is3xxRedirection()) | ||
.andExpect(redirectedUrl("/springdog/login?logout")); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...roject/springdog-agent/src/test/java/org/easypeelsecurity/springdog/agent/TestConfig.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,54 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.easypeelsecurity.springdog.agent; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.web.servlet.ViewResolver; | ||
|
||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
import org.thymeleaf.spring6.view.ThymeleafViewResolver; | ||
import org.thymeleaf.templatemode.TemplateMode; | ||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; | ||
|
||
/** | ||
* Test configuration for agent test. | ||
*/ | ||
@SpringBootApplication | ||
@ComponentScan(basePackages = {"org.easypeelsecurity.springdog.shared", "org.easypeelsecurity.springdog.agent"}) | ||
public class TestConfig { | ||
|
||
@Bean | ||
public ViewResolver viewResolver() { | ||
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); | ||
templateResolver.setPrefix(""); | ||
templateResolver.setSuffix(""); | ||
templateResolver.setTemplateMode(TemplateMode.HTML); | ||
|
||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); | ||
viewResolver.setTemplateEngine(templateEngine(templateResolver)); | ||
return viewResolver; | ||
} | ||
|
||
@Bean | ||
public SpringTemplateEngine templateEngine(ClassLoaderTemplateResolver templateResolver) { | ||
SpringTemplateEngine templateEngine = new SpringTemplateEngine(); | ||
templateEngine.setTemplateResolver(templateResolver); | ||
return templateEngine; | ||
} | ||
} |
Oops, something went wrong.