Skip to content

Commit

Permalink
🐛 : show owned modules on dashboard when user has no team
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Nov 20, 2019
1 parent 48368e3 commit b5d0f45
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public String index(Model model, User user, Team userTeam){
toUpdateStackCount = this.stackRepository.countStacksByState(StackState.TO_UPDATE);
}
else if(userTeam != null){
moduleCount = this.moduleRepository.countByAuthorizedTeamsContaining(userTeam);
moduleCount = this.moduleRepository.countByAuthorizedTeamsContainingOrCreatedBy(userTeam, user);
runningStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.RUNNING, userTeam);
toUpdateStackCount = stackRepository.countStacksByStateAndOwnerTeam(StackState.TO_UPDATE, userTeam);
}
else {
moduleCount = this.moduleRepository.countByCreatedBy(user);
runningStackCount = stackRepository.countStacksByStateAndCreatedBy(StackState.RUNNING, user);
toUpdateStackCount = stackRepository.countStacksByStateAndCreatedBy(StackState.TO_UPDATE, user);
}

model.addAttribute("moduleCount", moduleCount);
model.addAttribute("runningStackCount", runningStackCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
@Repository
public interface TerraformModuleRepository extends MongoRepository<TerraformModule, String> {

int countByCreatedBy(User user);

int countByAuthorizedTeamsContaining(Team team);

List<TerraformModule> findAllByAuthorizedTeamsContaining(Team team);
int countByAuthorizedTeamsContainingOrCreatedBy(Team team, User user);

List<TerraformModule> findAllByAuthorizedTeamsContainingOrCreatedBy(Team team, User user);

Optional<TerraformModule> findByIdAndAuthorizedTeamsContaining(String id, Team team);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.codeka.gaia.stacks.bo.Stack;
import io.codeka.gaia.stacks.bo.StackState;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -17,6 +18,8 @@ public interface StackRepository extends MongoRepository<Stack, String>{

int countStacksByStateAndOwnerTeam(StackState state, Team team);

int countStacksByStateAndCreatedBy(StackState state, User user);

int countStacksByState(StackState state);

List<Stack> findByOwnerTeam(Team team);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class IndexControllerTest {
@Test
void index_shouldShowModuleCount(){
// given
when(terraformModuleRepository.countByAuthorizedTeamsContaining(team)).thenReturn(12);
when(terraformModuleRepository.countByAuthorizedTeamsContainingOrCreatedBy(team, user)).thenReturn(12);

// when
var result = indexController.index(model, user, team);

// then
assertEquals("index", result);
verify(terraformModuleRepository).countByAuthorizedTeamsContaining(team);
verify(terraformModuleRepository).countByAuthorizedTeamsContainingOrCreatedBy(team, user);
verify(model).addAttribute("moduleCount", 12L);
}

Expand Down Expand Up @@ -101,16 +101,20 @@ void index_shouldShowStacksCount_forAdmin(){
}

@Test
void usersWIthNoTeam_shouldNotSeeAnyModuleOrStacks(){
void usersWIthNoTeam_shouldSeeTheirCreatedModuleOrStacks(){
// given
doReturn(3).when(terraformModuleRepository).countByCreatedBy(user);
doReturn(1).when(stackRepository).countStacksByStateAndCreatedBy(StackState.RUNNING, user);
doReturn(2).when(stackRepository).countStacksByStateAndCreatedBy(StackState.TO_UPDATE, user);

// 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);
verify(model).addAttribute("moduleCount", 3L);
verify(model).addAttribute("runningStackCount", 1L);
verify(model).addAttribute("toUpdateStackCount", 2L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ class ModuleRestControllerTest {

private User bob;

private User john;

private Team bobsTeam;

@BeforeEach
void setUp() {
admin = new User("admin", null);

john = new User("John Dorian", null);

bobsTeam = new Team("bobsTeam");
bob = new User("Bob Kelso", bobsTeam);
}
Expand All @@ -51,7 +55,7 @@ void findAll_shouldReturnAllModules_forAdmin(){
}

@Test
void findAll_shouldReturnAuthorizedModules_forUserTeam(){
void findAll_shouldReturnAuthorizedModules_forUserTeam_andOwnedModules(){
// given
when(moduleRepository.findAllByCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam)).thenReturn(List.of(new TerraformModule()));

Expand All @@ -63,6 +67,15 @@ void findAll_shouldReturnAuthorizedModules_forUserTeam(){
verify(moduleRepository).findAllByCreatedByOrAuthorizedTeamsContaining(bob, bobsTeam);
}

@Test
void findAll_shouldReturnOwnedModules_forUserWithNoTeam(){
// when
var modules = moduleRestController.findAllModules(john);

// then
verify(moduleRepository).findAllByCreatedBy(john);
}

@Test
void findById_shouldReturnModule_forAdmin(){
// given
Expand All @@ -75,6 +88,20 @@ void findById_shouldReturnModule_forAdmin(){
verify(moduleRepository).findById("12");
}

@Test
void findById_shouldReturnOwnedModule_forUserWithNoTeam(){
// given
var ownedModule = new TerraformModule();
ownedModule.setCreatedBy(john);
when(moduleRepository.findById("12")).thenReturn(Optional.of(ownedModule));

// when
moduleRestController.findModule("12", john);

// then
verify(moduleRepository).findById("12");
}

@Test
void findById_shouldReturnAuthorizedModule_forUserTeam(){
// given
Expand All @@ -89,13 +116,15 @@ void findById_shouldReturnAuthorizedModule_forUserTeam(){
verify(moduleRepository).findById("12");
}


@Test
void findById_shouldThrowModuleNotFound_forNonExistingModules(){
// given
when(moduleRepository.findById("12")).thenReturn(Optional.empty());

// when
assertThrows(ModuleNotFoundException.class, () -> moduleRestController.findModule("12", admin));
assertThrows(ModuleNotFoundException.class, () -> moduleRestController.findModule("12", john));
assertThrows(ModuleNotFoundException.class, () -> moduleRestController.findModule("12", bob));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.codeka.gaia.modules.bo.TerraformModule;
import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import io.codeka.gaia.test.MongoContainer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -30,6 +31,8 @@ class TerraformModuleRepositoryIT {

private Team team2;

private User bob;

private TerraformModule module1;

private TerraformModule module2;
Expand All @@ -40,29 +43,34 @@ void setUp() {
team1 = new Team("team1");
team2 = new Team("team2");

// sample owners
bob = new User("Bob", null);

// saving sample modules
module1 = new TerraformModule();
module1.setId("Module 1");
module1.setAuthorizedTeams(List.of(team1));
module1.setCreatedBy(bob);

module2 = new TerraformModule();
module2.setId("Module 2");
module2.setAuthorizedTeams(List.of(team1, team2));
module2.setCreatedBy(bob);

terraformModuleRepository.deleteAll();
terraformModuleRepository.saveAll(List.of(module1, module2));
}

@Test
void team1Users_shouldHaveAccessToModule1And2(){
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContaining(team1);
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrCreatedBy(team1, null);

assertThat(modules).hasSize(2);
}

@Test
void team2Users_shouldHaveAccessToModule2(){
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContaining(team2);
var modules = terraformModuleRepository.findAllByAuthorizedTeamsContainingOrCreatedBy(team2, null);

assertThat(modules).hasSize(1);
}
Expand Down

0 comments on commit b5d0f45

Please sign in to comment.