Skip to content

Commit

Permalink
Write backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Jun 13, 2024
1 parent 45e29a7 commit 7b57228
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ values (4, 1, '', '2023-07-25 08:17:51.309958', 66, 'Build a company culture tha
null, '2023-07-25 08:39:45.772126', false),
(8,1, '', '2023-07-25 08:39:28.175703', 40,
'consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua',
1, 2, 6, 'ONGOING', null, '2023-07-25 08:39:28.175703', false);
1, 2, 6, 'ONGOING', null, '2023-07-25 08:39:28.175703', false),
(998,1, '', '2023-07-25 08:39:28.175703', 40,
'consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua',
1, 6, 6, 'ONGOING', null, '2023-07-25 08:39:28.175703', true);

insert into key_result (id, version, baseline, description, modified_on, stretch_goal, title, created_by_id,
objective_id, owner_id, key_result_type, created_on, unit, commit_zone, target_zone, stretch_zone)
Expand Down
7 changes: 7 additions & 0 deletions backend/src/test/java/ch/puzzle/okr/KeyResultTestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ public class KeyResultTestHelpers {
.withUsername("bkaufmann").withEmail("[email protected]").build();
public static final Objective objective = Objective.Builder.builder().withId(5L).withTitle("Objective 1")
.withArchived(false).build();
public static final Objective archivedObjective = Objective.Builder.builder().withId(12L).withTitle("Objective 12")
.withArchived(true).build();

public static final KeyResult metricKeyResult = KeyResultMetric.Builder.builder().withId(5L).withTitle(TITLE)
.withObjective(objective).build();
public static final KeyResult archivedKeyResult = KeyResultMetric.Builder.builder().withId(1L).withTitle(TITLE)
.withObjective(archivedObjective).build();
public static final CheckIn checkIn1 = CheckInMetric.Builder.builder().withValue(23D).withId(1L)
.withKeyResult(metricKeyResult).withCreatedBy(user).withCreatedOn(LocalDateTime.MAX)
.withChangeInfo(CHANGE_INFO_1).withInitiatives(INITIATIVES_1).build();
public static final CheckIn archivedCheckin = CheckInMetric.Builder.builder().withValue(23D).withId(13L)
.withKeyResult(archivedKeyResult).withCreatedBy(user).withCreatedOn(LocalDateTime.MAX)
.withChangeInfo(CHANGE_INFO_1).withInitiatives(INITIATIVES_1).build();
public static final CheckIn checkIn2 = CheckInMetric.Builder.builder().withValue(12D).withId(4L)
.withKeyResult(metricKeyResult).withCreatedBy(user).withCreatedOn(LocalDateTime.MAX)
.withChangeInfo(CHANGE_INFO_2).withInitiatives(INITIATIVES_2).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

import java.util.List;

import static ch.puzzle.okr.KeyResultTestHelpers.checkIn1;
import static ch.puzzle.okr.KeyResultTestHelpers.metricKeyResult;
import static ch.puzzle.okr.KeyResultTestHelpers.*;
import static ch.puzzle.okr.TestHelper.defaultAuthorizationUser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

Expand All @@ -37,133 +36,207 @@ class KeyResultAuthorizationServiceTest {

@Test
void createEntityShouldReturnKeyResultWhenAuthorized() {
// arrange
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.createEntity(metricKeyResult, authorizationUser)).thenReturn(metricKeyResult);

// act
KeyResult keyResult = keyResultAuthorizationService.createEntity(metricKeyResult);

// assert
assertEquals(metricKeyResult, keyResult);
}

@Test
void createEntityShouldThrowExceptionWhenNotAuthorized() {
// arrange
String reason = "junit test reason";
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason)).when(authorizationService)
.hasRoleCreateOrUpdate(metricKeyResult, authorizationUser);

// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> keyResultAuthorizationService.createEntity(metricKeyResult));

// assert
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@Test
void getEntityByIdShouldReturnKeyResultWhenAuthorized() {
// arrange
Long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.getEntityById(id)).thenReturn(metricKeyResult);

// act
KeyResult keyResult = keyResultAuthorizationService.getEntityById(id);

// assert
assertEquals(metricKeyResult, keyResult);
}

@Test
void getEntityByIdShouldReturnKeyResultWritableWhenAuthorized() {
// arrange
Long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(authorizationService.isWriteable(metricKeyResult, authorizationUser)).thenReturn(true);
when(keyResultBusinessService.getEntityById(id)).thenReturn(metricKeyResult);

// act
KeyResult keyResult = keyResultAuthorizationService.getEntityById(id);

// assert
assertTrue(keyResult.isWriteable());
}

@Test
void getEntityByIdShouldReturnKeyResultNotWritableWhenArchived() {
// arrange
Long id = 1L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.getEntityById(id)).thenReturn(archivedKeyResult);

// act
KeyResult keyResult = keyResultAuthorizationService.getEntityById(id);

// assert
assertFalse(keyResult.isWriteable());
}

@Test
void getEntityByIdShouldThrowExceptionWhenNotAuthorized() {
// arrange
Long id = 13L;
String reason = "junit test reason";
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason)).when(authorizationService)
.hasRoleReadByKeyResultId(id, authorizationUser);

// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> keyResultAuthorizationService.getEntityById(id));

// assert
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@Test
void updateEntitiesShouldReturnUpdatedKeyResultWhenAuthorized() {
// arrange
Long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.updateEntities(id, metricKeyResult, List.of()))
.thenReturn(new KeyResultWithActionList(metricKeyResult, List.of()));

// act
KeyResultWithActionList KeyResult = keyResultAuthorizationService.updateEntities(id, metricKeyResult,
List.of());

// assert
assertEquals(metricKeyResult, KeyResult.keyResult());
}

@Test
void updateEntitiesShouldThrowExceptionWhenNotAuthorized() {
// arrange
Long id = 13L;
String reason = "junit test reason";
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason)).when(authorizationService)
.hasRoleCreateOrUpdate(metricKeyResult, authorizationUser);

// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> keyResultAuthorizationService.updateEntities(id, metricKeyResult, List.of()));

// assert
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@Test
void updateEntityShouldThrowException() {
// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> keyResultAuthorizationService.updateEntity(1L, metricKeyResult));

// assert
assertEquals(BAD_REQUEST, exception.getStatusCode());
assertEquals("unsupported method in class " + KeyResultAuthorizationService.class.getSimpleName()
+ ", use updateEntities() instead", exception.getReason());
}

@Test
void deleteEntityByIdShouldPassThroughWhenAuthorized() {
// arrange
Long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);

// act
keyResultAuthorizationService.deleteEntityById(id);
}

@Test
void deleteEntityByIdShouldThrowExceptionWhenNotAuthorized() {
// arrange
Long id = 13L;
String reason = "junit test reason";
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason)).when(authorizationService)
.hasRoleDeleteByKeyResultId(id, authorizationUser);

// act
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> keyResultAuthorizationService.deleteEntityById(id));

// assert
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@Test
void getAllCheckInsByKeyResultShouldReturnListOfCheckInsWhenAuthorized() {
// arrange
long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.getAllCheckInsByKeyResult(id)).thenReturn(List.of(checkIn1, checkIn1));

// act
List<CheckIn> checkIns = keyResultAuthorizationService.getAllCheckInsByKeyResult(id);

// assert
verify(authorizationService, times(1)).isWriteable(checkIn1, authorizationUser);
assertThat(List.of(checkIn1, checkIn1)).hasSameElementsAs(checkIns);
}

@Test
void getAllCheckInsByKeyResultShouldReturnListOfCheckInsNotWritableWhenArchived() {
// arrange
Long id = 13L;
when(authorizationService.getAuthorizationUser()).thenReturn(authorizationUser);
when(keyResultBusinessService.getAllCheckInsByKeyResult(id)).thenReturn(List.of(archivedCheckin));

// act
List<CheckIn> checkIns = keyResultAuthorizationService.getAllCheckInsByKeyResult(id);

// assert
verify(authorizationService, times(0)).isWriteable(archivedCheckin, authorizationUser);
assertThat(List.of(archivedCheckin)).hasSameElementsAs(checkIns);
assertFalse(checkIns.get(0).isWriteable());
}

@Test
void isImUsedShouldReturnTrueWhenImUsed() {
// arrange
Long id = 13L;
when(keyResultBusinessService.isImUsed(id, metricKeyResult)).thenReturn(true);

// assert
assertTrue(keyResultAuthorizationService.isImUsed(id, metricKeyResult));
}
}
Loading

0 comments on commit 7b57228

Please sign in to comment.