Skip to content

Commit

Permalink
🐛 : correct module & stack count for users without teams
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Aug 16, 2019
1 parent 522543b commit f9cafa1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +38,11 @@ class IndexControllerTest {
@Mock
private Team team;

@BeforeEach
void setUp() {
user.setTeam(team);
}

@Test
void index_shouldShowModuleCount(){
// given
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

}

0 comments on commit f9cafa1

Please sign in to comment.