Skip to content

Commit 0b60402

Browse files
authored
Merge pull request #655 from bettercodepaul/#2077
#2077
2 parents 6a5d1ca + e7492f1 commit 0b60402

2 files changed

Lines changed: 121 additions & 9 deletions

File tree

bogenliga/bogenliga-application/src/main/java/de/bogenliga/application/services/v1/dsbmitglied/service/DsbMitgliedService.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,22 @@ public List<DsbMitgliedDTO> findBySearch(@PathVariable("searchstring") final Str
176176
* @return list of {@link DsbMitgliedDTO} as JSON
177177
*/
178178
@GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
179-
@RequiresPermission(UserPermission.CAN_READ_DSBMITGLIEDER)
180-
public DsbMitgliedDTO findById(@PathVariable("id") final long id) {
179+
@RequiresOnePermissions(perm = {UserPermission.CAN_READ_DSBMITGLIEDER, UserPermission.CAN_READ_MY_VEREIN})
180+
public DsbMitgliedDTO findById(@PathVariable("id") final long id) throws NoPermissionException {
181181
Preconditions.checkArgument(id >= 0, PRECONDITION_MSG_ID_NEGATIVE);
182+
final DsbMitgliedDO dsbMitgliedDO = dsbMitgliedComponent.findById(id);
182183

183-
LOG.debug("Receive 'findByDsbMitgliedId' request with ID '{}'", id);
184+
if (this.requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_DSBMITGLIEDER)
185+
|| this.requiresOnePermissionAspect.hasSpecificPermissionSportleiter(UserPermission.CAN_READ_MY_VEREIN, dsbMitgliedDO.getVereinsId()))
186+
{
187+
LOG.debug("Receive 'findByDsbMitgliedId' request with ID '{}'", id);
184188

185-
final DsbMitgliedDO dsbMitgliedDO = dsbMitgliedComponent.findById(id);
186-
return DsbMitgliedDTOMapper.toDTO.apply(dsbMitgliedDO);
189+
return DsbMitgliedDTOMapper.toDTO.apply(dsbMitgliedDO);
190+
}
191+
else
192+
{
193+
throw new NoPermissionException();
194+
}
187195
}
188196

189197

bogenliga/bogenliga-application/src/test/java/de/bogenliga/application/services/v1/dsbmitglied/service/DsbMitgliedServiceTest.java

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,67 @@ public void findAllNotInTeamId() {
307307
}
308308

309309

310+
@Test
311+
public void findByIdSportleiter() {
312+
// prepare test data
313+
final DsbMitgliedDO dsbMitgliedDO = getDsbMitgliedDO();
314+
315+
// configure mocks similar to findAllInVerein: user belongs to the same verein
316+
final UserDO userDO = new UserDO();
317+
userDO.setId(USER);
318+
userDO.setDsbMitgliedId(ID);
319+
320+
when(userComponent.findById(USER)).thenReturn(userDO);
321+
when(dsbMitgliedComponent.findById(ID)).thenReturn(dsbMitgliedDO);
322+
323+
// simulate that the user DOES NOT have CAN_READ_DSBMITGLIEDER and CAN_READ_MY_VEREIN
324+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_DSBMITGLIEDER)).thenReturn(false);
325+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_MY_VEREIN)).thenReturn(true);
326+
when(requiresOnePermissionAspect.hasSpecificPermissionSportleiter(UserPermission.CAN_READ_MY_VEREIN, dsbMitgliedDO.getVereinsId())).thenReturn(true);
327+
328+
329+
DsbMitgliedDTO actual;
330+
// call test method
331+
try {
332+
actual = underTest.findById(ID);
333+
}
334+
catch (NoPermissionException e) {
335+
actual = null;
336+
}
337+
338+
// assert result
339+
assertThat(actual).isNotNull();
340+
assertThat(actual.getId()).isEqualTo(dsbMitgliedDO.getId());
341+
assertThat(actual.getVorname()).isEqualTo(dsbMitgliedDO.getVorname());
342+
343+
// verify invocations
344+
verify(dsbMitgliedComponent).findById(ID);
345+
}
346+
310347
@Test
311348
public void findById() {
312349
// prepare test data
313350
final DsbMitgliedDO dsbMitgliedDO = getDsbMitgliedDO();
314351

315-
// configure mocks
316-
when(dsbMitgliedComponent.findById(anyLong())).thenReturn(dsbMitgliedDO);
352+
// configure mocks similar to findAllInVerein: user belongs to the same verein
353+
final UserDO userDO = new UserDO();
354+
userDO.setId(USER);
355+
userDO.setDsbMitgliedId(ID);
356+
357+
when(userComponent.findById(USER)).thenReturn(userDO);
358+
when(dsbMitgliedComponent.findById(ID)).thenReturn(dsbMitgliedDO);
317359

360+
// simulate that the user has CAN_READ_DSBMITGLIEDER
361+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_DSBMITGLIEDER)).thenReturn(true);
362+
363+
DsbMitgliedDTO actual;
318364
// call test method
319-
final DsbMitgliedDTO actual = underTest.findById(ID);
365+
try {
366+
actual = underTest.findById(ID);
367+
}
368+
catch (NoPermissionException e) {
369+
actual = null;
370+
}
320371

321372
// assert result
322373
assertThat(actual).isNotNull();
@@ -327,6 +378,60 @@ public void findById() {
327378
verify(dsbMitgliedComponent).findById(ID);
328379
}
329380

381+
@Test
382+
public void findByIdNoPermission(){
383+
// prepare test data
384+
final DsbMitgliedDO dsbMitgliedDO = getDsbMitgliedDO();
385+
386+
// configure mocks similar to findAllInVerein: user belongs to the same verein
387+
final UserDO userDO = new UserDO();
388+
userDO.setId(USER);
389+
userDO.setDsbMitgliedId(ID);
390+
391+
when(userComponent.findById(USER)).thenReturn(userDO);
392+
when(dsbMitgliedComponent.findById(ID)).thenReturn(dsbMitgliedDO);
393+
394+
// simulate that the user DOES NOT have CAN_READ_DSBMITGLIEDER and CAN_READ_MY_VEREIN
395+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_DSBMITGLIEDER)).thenReturn(false);
396+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_MY_VEREIN)).thenReturn(false);
397+
when(requiresOnePermissionAspect.hasSpecificPermissionSportleiter(UserPermission.CAN_READ_MY_VEREIN, dsbMitgliedDO.getVereinsId())).thenReturn(false);
398+
399+
// assert that NoPermissionException is thrown
400+
assertThatExceptionOfType(NoPermissionException.class)
401+
.isThrownBy(() -> underTest.findById(ID));
402+
403+
// verify invocations
404+
verify(dsbMitgliedComponent).findById(ID);
405+
}
406+
407+
@Test
408+
public void findByIdWrongClubSportleiter(){
409+
// prepare test data
410+
final DsbMitgliedDO dsbMitgliedDO = getDsbMitgliedDO();
411+
412+
// configure mocks similar to findAllInVerein: user belongs to the same verein
413+
final UserDO userDO = new UserDO();
414+
userDO.setId(USER);
415+
userDO.setDsbMitgliedId(ID);
416+
417+
when(userComponent.findById(USER)).thenReturn(userDO);
418+
when(dsbMitgliedComponent.findById(ID)).thenReturn(dsbMitgliedDO);
419+
420+
// simulate that the user DOES NOT have CAN_READ_DSBMITGLIEDER but has CAN_READ_MY_VEREIN but tries to find dsbmitglied not in his club
421+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_DSBMITGLIEDER)).thenReturn(false);
422+
when(requiresOnePermissionAspect.hasPermission(UserPermission.CAN_READ_MY_VEREIN)).thenReturn(true);
423+
when(requiresOnePermissionAspect.hasSpecificPermissionSportleiter(UserPermission.CAN_READ_MY_VEREIN, dsbMitgliedDO.getVereinsId())).thenReturn(false);
424+
425+
// assert that NoPermissionException is thrown
426+
assertThatExceptionOfType(NoPermissionException.class)
427+
.isThrownBy(() -> underTest.findById(ID));
428+
429+
// verify invocations
430+
verify(dsbMitgliedComponent).findById(ID);
431+
432+
// verify invocations
433+
verify(dsbMitgliedComponent).findById(ID);
434+
}
330435

331436
@Test
332437
public void findBySearch() {
@@ -352,7 +457,6 @@ public void findBySearch() {
352457
verify(dsbMitgliedComponent).findBySearch(dsbMitgliedDO.getVorname());
353458
}
354459

355-
356460
@Test
357461
public void insertUserId() {
358462
// prepare test data

0 commit comments

Comments
 (0)