DefaultUserAuthService implements UserAuthService { @Override public boolean userIsEffectivelyAManager(User user) { return (getAuthenticatedUser().equals(user) && authenticatedUserIsAdmin()) || (userService.userIsManager(user)); } DefaultUserAuthService(UserService userService, AuthenticationInfoRetriever retriever); @Override User getAuthenticatedUser(); @Override String getAuthenticatedUserId(); @Override boolean authenticatedUserIsAdmin(); @Override boolean userIsEffectivelyAManager(User user);  }
@Test public void userIsEffectivelyAManagerReturnsFalseWhenUserIsNoManagerAndNoAdmin() { given(retrieverMock.getUserId()).willReturn(USER_ID); given(userServiceMock.userIsManager(userMock)).willReturn(false); given(userServiceMock.getUserById(USER_ID)).willReturn(userMock); boolean actualHasAccess = userAuthService.userIsEffectivelyAManager(userMock); assertThat(actualHasAccess).isFalse(); }
@Test public void userIsEffectivelyAManagerReturnsTrueWhenUserIsManager() { given(retrieverMock.getUserId()).willReturn(USER_ID); given(userServiceMock.userIsManager(userMock)).willReturn(true); given(userServiceMock.getUserById(USER_ID)).willReturn(userMock); boolean actualHasAccess = userAuthService.userIsEffectivelyAManager(userMock); assertThat(actualHasAccess).isTrue(); }
@Test public void userIsEffectivelyAManagerReturnsFalseWhenAuthenticatedUserIsAdminButUserDoesNotEqual() { given(retrieverMock.getUserId()).willReturn(USER_ID); given(userServiceMock.getUserById(USER_ID)).willReturn(userMock); boolean actualHasAccess = userAuthService.userIsEffectivelyAManager(otherUserMock); assertThat(actualHasAccess).isFalse(); }