From f9cafa1885f351efab144e226a852853bd4c4749 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Fri, 16 Aug 2019 20:31:02 +0200 Subject: [PATCH] :bug: : correct module & stack count for users without teams --- .../dashboard/controller/IndexController.java | 21 ++++++++----- .../controller/IndexControllerTest.java | 31 ++++++++++++++++--- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/codeka/gaia/dashboard/controller/IndexController.java b/src/main/java/io/codeka/gaia/dashboard/controller/IndexController.java index 8e4d49455..eaaf61c04 100644 --- a/src/main/java/io/codeka/gaia/dashboard/controller/IndexController.java +++ b/src/main/java/io/codeka/gaia/dashboard/controller/IndexController.java @@ -23,17 +23,24 @@ public IndexController(TerraformModuleRepository moduleRepository, StackReposito @GetMapping("/") public String index(Model model, User user, Team userTeam){ + long moduleCount = 0; + long runningStackCount = 0; + long toUpdateStackCount = 0; if(user.isAdmin()){ - model.addAttribute("moduleCount", this.moduleRepository.count()); - model.addAttribute("runningStackCount", this.stackRepository.countStacksByState(StackState.RUNNING)); - model.addAttribute("toUpdateStackCount", this.stackRepository.countStacksByState(StackState.TO_UPDATE)); + moduleCount = this.moduleRepository.count(); + runningStackCount = this.stackRepository.countStacksByState(StackState.RUNNING); + toUpdateStackCount = this.stackRepository.countStacksByState(StackState.TO_UPDATE); } - else{ - model.addAttribute("moduleCount", this.moduleRepository.countByAuthorizedTeamsContaining(userTeam)); - model.addAttribute("runningStackCount", stackRepository.countStacksByStateAndOwnerTeam(StackState.RUNNING, userTeam)); - model.addAttribute("toUpdateStackCount", stackRepository.countStacksByStateAndOwnerTeam(StackState.TO_UPDATE, userTeam)); + else if(userTeam != null){ + moduleCount = this.moduleRepository.countByAuthorizedTeamsContaining(userTeam); + runningStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.RUNNING, userTeam); + toUpdateStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.TO_UPDATE, userTeam); } + model.addAttribute("moduleCount", moduleCount); + model.addAttribute("runningStackCount", runningStackCount); + model.addAttribute("toUpdateStackCount", toUpdateStackCount); + return "index"; } diff --git a/src/test/java/io/codeka/gaia/dashboard/controller/IndexControllerTest.java b/src/test/java/io/codeka/gaia/dashboard/controller/IndexControllerTest.java index 7eda945ef..b18f03c72 100644 --- a/src/test/java/io/codeka/gaia/dashboard/controller/IndexControllerTest.java +++ b/src/test/java/io/codeka/gaia/dashboard/controller/IndexControllerTest.java @@ -5,6 +5,7 @@ import io.codeka.gaia.stacks.repository.StackRepository; import io.codeka.gaia.teams.bo.Team; import io.codeka.gaia.teams.bo.User; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -37,6 +38,11 @@ class IndexControllerTest { @Mock private Team team; + @BeforeEach + void setUp() { + user.setTeam(team); + } + @Test void index_shouldShowModuleCount(){ // given @@ -48,7 +54,7 @@ void index_shouldShowModuleCount(){ // then assertEquals("index", result); verify(terraformModuleRepository).countByAuthorizedTeamsContaining(team); - verify(model).addAttribute("moduleCount", 12); + verify(model).addAttribute("moduleCount", 12L); } @Test @@ -78,8 +84,9 @@ void index_shouldShowStacksCount(){ assertEquals("index", result); verify(stackRepository).countStacksByStateAndOwnerTeam(StackState.RUNNING, team); verify(stackRepository).countStacksByStateAndOwnerTeam(StackState.TO_UPDATE, team); - verify(model).addAttribute("runningStackCount", 1); - verify(model).addAttribute("toUpdateStackCount", 2); + verify(model).addAttribute("moduleCount", 0L); + verify(model).addAttribute("runningStackCount", 1L); + verify(model).addAttribute("toUpdateStackCount", 2L); } @Test @@ -95,8 +102,22 @@ void index_shouldShowStacksCount_forAdmin(){ assertEquals("index", result); verify(stackRepository).countStacksByState(StackState.RUNNING); verify(stackRepository).countStacksByState(StackState.TO_UPDATE); - verify(model).addAttribute("runningStackCount", 1); - verify(model).addAttribute("toUpdateStackCount", 2); + verify(model).addAttribute("moduleCount", 0L); + verify(model).addAttribute("runningStackCount", 1L); + verify(model).addAttribute("toUpdateStackCount", 2L); + } + + @Test + void usersWIthNoTeam_shouldNotSeeAnyModuleOrStacks(){ + // when + var result = indexController.index(model, user, null); + + // then + assertEquals("index", result); + verifyZeroInteractions(terraformModuleRepository, stackRepository); + verify(model).addAttribute("moduleCount", 0L); + verify(model).addAttribute("runningStackCount", 0L); + verify(model).addAttribute("toUpdateStackCount", 0L); } } \ No newline at end of file