From 574fc32c64a8b8f5a7dc06858d3ec2e27c26a681 Mon Sep 17 00:00:00 2001 From: Daniil Palagin Date: Fri, 20 Sep 2024 12:52:52 +0200 Subject: [PATCH] [kbss-cvut/record-manager-ui#202] Fix tests --- .../cz/cvut/kbss/study/model/RoleGroup.java | 4 +- .../java/cz/cvut/kbss/study/model/User.java | 2 +- .../RepositoryPatientRecordService.java | 2 +- .../environment/generator/Generator.java | 34 +++++++++++----- .../cz/cvut/kbss/study/model/UserTest.java | 4 +- .../persistence/dao/ActionHistoryDaoTest.java | 39 ++++++++++++------- .../persistence/dao/DerivableUriDaoTest.java | 9 ++++- .../persistence/dao/PatientRecordDaoTest.java | 36 ++++++++++------- .../study/persistence/dao/UserDaoTest.java | 37 ++++++++++++------ .../rest/ActionHistoryControllerTest.java | 9 +++-- .../study/rest/InstitutionControllerTest.java | 6 ++- .../rest/PatientRecordControllerTest.java | 19 ++++----- .../study/rest/StatisticsControllerTest.java | 4 +- .../kbss/study/rest/UserControllerTest.java | 25 +++++++----- .../handler/HateoasPagingListenerTest.java | 2 +- .../security/CustomSwitchUserFilterTest.java | 17 ++++---- .../study/service/BaseServiceTestRunner.java | 11 +++++- .../study/service/UserDetailsServiceTest.java | 9 ++++- .../RepositoryPatientRecordServiceTest.java | 29 +++++++------- .../service/security/SecurityUtilsTest.java | 12 +++--- 20 files changed, 199 insertions(+), 111 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java b/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java index 8a7684b3..eb7e5511 100644 --- a/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java +++ b/src/main/java/cz/cvut/kbss/study/model/RoleGroup.java @@ -19,7 +19,7 @@ public class RoleGroup implements Serializable, HasUri { private String name; @Enumerated(EnumType.OBJECT_ONE_OF) - @OWLObjectProperty(iri = Vocabulary.s_p_has_role, cascade = {CascadeType.MERGE,CascadeType.PERSIST}) + @OWLObjectProperty(iri = Vocabulary.s_p_has_role) private Set roles = new HashSet<>(); public void addRole(Role role){ @@ -52,6 +52,6 @@ public void setRoles(Set roles) { } public void generateUri() { - this.uri = URI.create(Constants.BASE_URI + name); + this.uri = URI.create(Constants.BASE_URI + "sdfsf"); } } diff --git a/src/main/java/cz/cvut/kbss/study/model/User.java b/src/main/java/cz/cvut/kbss/study/model/User.java index 3084efde..99b0ad67 100644 --- a/src/main/java/cz/cvut/kbss/study/model/User.java +++ b/src/main/java/cz/cvut/kbss/study/model/User.java @@ -62,7 +62,7 @@ public class User implements HasDerivableUri, Serializable { @OWLObjectProperty(iri = Vocabulary.s_p_is_member_of, fetch = FetchType.EAGER) private Institution institution; - @OWLObjectProperty(iri = Vocabulary.s_p_has_role_group, cascade = {CascadeType.MERGE, CascadeType.PERSIST}) + @OWLObjectProperty(iri = Vocabulary.s_p_has_role_group) private RoleGroup roleGroup; @Types diff --git a/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java b/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java index 69fa886a..d42056e6 100644 --- a/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java +++ b/src/main/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordService.java @@ -117,7 +117,7 @@ private void setImportedRecordProvenance(User currentUser, Date now, Optional roleGroupDao.persist(this.roleGroupAdmin)); + } + @Test public void findByKeyReturnsActionWithPayload() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + + User user = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action = Generator.generateActionHistory(user); transactional(() -> { @@ -53,8 +64,8 @@ public void findByKeyReturnsActionWithPayload() { @Test public void findAllWithParamsWithoutParamsReturnsAllActions() { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action1 = Generator.generateActionHistory(user1); ActionHistory action2 = Generator.generateActionHistory(user1); ActionHistory action3 = Generator.generateActionHistory(user2); @@ -73,9 +84,9 @@ public void findAllWithParamsWithoutParamsReturnsAllActions() { @Test public void findAllWithParamsWithAuthorReturnsAuthorsActions() { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); - User user3 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); + User user3 = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action1 = Generator.generateActionHistory(user1); ActionHistory action2 = Generator.generateActionHistory(user1); ActionHistory action3 = Generator.generateActionHistory(user2); @@ -98,7 +109,7 @@ public void findAllWithParamsWithAuthorReturnsAuthorsActions() { @Test public void findAllWithParamsWithTypeReturnsActionsWithExactType() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action1 = Generator.generateActionHistory(user); action1.setType(LOAD_SUCCESS); ActionHistory action2 = Generator.generateActionHistory(user); @@ -124,7 +135,7 @@ public void findAllWithParamsWithTypeReturnsActionsWithExactType() { @Test public void findAllWithParamsWithTypeReturnsActionsWithTypeContained() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action1 = Generator.generateActionHistory(user); action1.setType(LOAD_SUCCESS); ActionHistory action2 = Generator.generateActionHistory(user); @@ -145,8 +156,8 @@ public void findAllWithParamsWithTypeReturnsActionsWithTypeContained() { @Test public void findAllWithParamsReturnsMatchingActions() { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); ActionHistory action1 = Generator.generateActionHistory(user1); action1.setType(LOAD_SUCCESS); ActionHistory action2 = Generator.generateActionHistory(user1); @@ -174,7 +185,7 @@ public void findAllWithParamsReturnsMatchingActions() { @Test void findAllReturnsActionsOnMatchingPage() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); final List allActions = IntStream.range(0, 10).mapToObj(i -> Generator.generateActionHistory(user)).toList(); transactional(() -> { institutionDao.persist(institution); diff --git a/src/test/java/cz/cvut/kbss/study/persistence/dao/DerivableUriDaoTest.java b/src/test/java/cz/cvut/kbss/study/persistence/dao/DerivableUriDaoTest.java index e191e2de..767eef67 100644 --- a/src/test/java/cz/cvut/kbss/study/persistence/dao/DerivableUriDaoTest.java +++ b/src/test/java/cz/cvut/kbss/study/persistence/dao/DerivableUriDaoTest.java @@ -2,6 +2,8 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.model.Institution; +import cz.cvut.kbss.study.model.Role; +import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.persistence.BaseDaoTestRunner; import org.junit.jupiter.api.Test; @@ -17,12 +19,17 @@ public class DerivableUriDaoTest extends BaseDaoTestRunner { @Autowired private InstitutionDao institutionDao; + @Autowired + RoleGroupDao roleGroupDao; + @Test public void persistedInstanceHasGeneratedUri(){ final Institution institution = Generator.generateInstitution(); - final User user = Generator.generateUser(institution); + final RoleGroup roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + final User user = Generator.generateUser(institution, roleGroupAdmin); transactional(() -> { + roleGroupDao.persist(roleGroupAdmin); institutionDao.persist(institution); userDao.persist(user); }); diff --git a/src/test/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDaoTest.java b/src/test/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDaoTest.java index c6ec6d77..450b9f72 100644 --- a/src/test/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDaoTest.java +++ b/src/test/java/cz/cvut/kbss/study/persistence/dao/PatientRecordDaoTest.java @@ -7,16 +7,14 @@ import cz.cvut.kbss.study.dto.PatientRecordDto; import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; -import cz.cvut.kbss.study.model.Institution; -import cz.cvut.kbss.study.model.PatientRecord; -import cz.cvut.kbss.study.model.RecordPhase; -import cz.cvut.kbss.study.model.User; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.model.qam.Answer; import cz.cvut.kbss.study.persistence.BaseDaoTestRunner; import cz.cvut.kbss.study.persistence.dao.util.QuestionSaver; import cz.cvut.kbss.study.persistence.dao.util.RecordFilterParams; import cz.cvut.kbss.study.persistence.dao.util.RecordSort; import cz.cvut.kbss.study.util.IdentificationUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -57,12 +55,24 @@ public class PatientRecordDaoTest extends BaseDaoTestRunner { @Autowired private InstitutionDao institutionDao; + @Autowired + private RoleGroupDao roleGroupDao; + + RoleGroup roleGroupAdmin; + + @BeforeEach + public void setUp() { + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + transactional(() -> roleGroupDao.persist(roleGroupAdmin)); + int a =4; + } + @Test public void findByInstitutionReturnsMatchingRecords() { Institution institution = Generator.generateInstitution(); Institution institutionOther = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institutionOther); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institutionOther, this.roleGroupAdmin); PatientRecord record1 = Generator.generatePatientRecord(user1); PatientRecord record2 = Generator.generatePatientRecord(user1); PatientRecord recordOther = Generator.generatePatientRecord(user2); @@ -88,8 +98,8 @@ public void findByInstitutionReturnsMatchingRecords() { public void findAllRecordsReturnAllRecords() { Institution institution1 = Generator.generateInstitution(); Institution institution2 = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution1); - User user2 = Generator.generateUser(institution2); + User user1 = Generator.generateUser(institution1, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution2, this.roleGroupAdmin); PatientRecord record1 = Generator.generatePatientRecord(user1); PatientRecord record2 = Generator.generatePatientRecord(user1); PatientRecord record3 = Generator.generatePatientRecord(user2); @@ -112,7 +122,7 @@ public void findAllRecordsReturnAllRecords() { @Test public void getNumberOfProcessedRecords() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); PatientRecord record1 = Generator.generatePatientRecord(user); PatientRecord record2 = Generator.generatePatientRecord(user); transactional(() -> { @@ -130,8 +140,8 @@ public void getNumberOfProcessedRecords() { @Test public void findByAuthorReturnsMatchingRecords() { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); PatientRecord record1 = Generator.generatePatientRecord(user1); PatientRecord record2 = Generator.generatePatientRecord(user1); PatientRecord record3 = Generator.generatePatientRecord(user2); @@ -156,7 +166,7 @@ public void findByAuthorReturnsMatchingRecords() { void persistGeneratesIdentifierBeforeSavingRecord() { final Institution institution = Generator.generateInstitution(); institution.setKey(IdentificationUtils.generateKey()); - final User author = Generator.generateUser(institution); + final User author = Generator.generateUser(institution, this.roleGroupAdmin); author.generateUri(); transactional(() -> { em.persist(author); @@ -175,7 +185,7 @@ void persistGeneratesIdentifierBeforeSavingRecord() { private User generateAuthorWithInstitution() { final Institution institution = Generator.generateInstitution(); institution.setKey(IdentificationUtils.generateKey()); - final User author = Generator.generateUser(institution); + final User author = Generator.generateUser(institution, this.roleGroupAdmin); author.generateUri(); transactional(() -> { em.persist(author); diff --git a/src/test/java/cz/cvut/kbss/study/persistence/dao/UserDaoTest.java b/src/test/java/cz/cvut/kbss/study/persistence/dao/UserDaoTest.java index 866af0b2..24486790 100644 --- a/src/test/java/cz/cvut/kbss/study/persistence/dao/UserDaoTest.java +++ b/src/test/java/cz/cvut/kbss/study/persistence/dao/UserDaoTest.java @@ -1,11 +1,9 @@ package cz.cvut.kbss.study.persistence.dao; import cz.cvut.kbss.study.environment.generator.Generator; -import cz.cvut.kbss.study.model.Institution; -import cz.cvut.kbss.study.model.Role; -import cz.cvut.kbss.study.model.User; -import cz.cvut.kbss.study.model.Vocabulary; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.persistence.BaseDaoTestRunner; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +23,22 @@ public class UserDaoTest extends BaseDaoTestRunner { @Autowired private InstitutionDao institutionDao; + @Autowired + private RoleGroupDao roleGroupDao; + + private RoleGroup roleGroupAdmin; + private RoleGroup roleGroupUser; + + @BeforeEach + public void setUp() { + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + this.roleGroupUser = Generator.generateRoleGroupWithRoles(Role.user); + transactional(() -> { + roleGroupDao.persist(this.roleGroupAdmin); + roleGroupDao.persist(this.roleGroupUser); + }); + } + @Test public void getUserByUsername() { User user1 = getPersistedUser(); @@ -74,7 +88,7 @@ public void getUserByEmailWithNonExistingEmailReturnsNull() { @Test public void getUsersByToken() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); user.setToken("Token"); transactional(() -> { @@ -93,8 +107,8 @@ public void getUsersByToken() { public void getUsersByInstitution() { Institution institution1 = Generator.generateInstitution(); Institution institution2 = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution1); - User user2 = Generator.generateUser(institution1); + User user1 = Generator.generateUser(institution1, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution1, this.roleGroupAdmin); transactional(() -> { institutionDao.persist(institution1); @@ -118,11 +132,10 @@ public void getUsersByInstitution() { @Test public void getNumberOfInvestigators() { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); - User user3 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupUser); + User user3 = Generator.generateUser(institution, this.roleGroupUser); - user3.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.administrator)); transactional(() -> { institutionDao.persist(institution); @@ -137,7 +150,7 @@ public void getNumberOfInvestigators() { private User getPersistedUser() { Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); transactional(() -> { institutionDao.persist(institution); userDao.persist(user); diff --git a/src/test/java/cz/cvut/kbss/study/rest/ActionHistoryControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/ActionHistoryControllerTest.java index 52e3d31a..f335f016 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/ActionHistoryControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/ActionHistoryControllerTest.java @@ -4,9 +4,7 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.exception.NotFoundException; -import cz.cvut.kbss.study.model.ActionHistory; -import cz.cvut.kbss.study.model.Institution; -import cz.cvut.kbss.study.model.User; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.rest.event.PaginatedResultRetrievedEvent; import cz.cvut.kbss.study.service.ActionHistoryService; import cz.cvut.kbss.study.service.UserService; @@ -57,11 +55,14 @@ public class ActionHistoryControllerTest extends BaseControllerTestRunner { private User user; + private RoleGroup roleGroupAdmin; + @BeforeEach public void setUp() { super.setUp(controller); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); Institution institution = Generator.generateInstitution(); - this.user = Generator.generateUser(institution); + this.user = Generator.generateUser(institution, roleGroupAdmin); Environment.setCurrentUser(user); } diff --git a/src/test/java/cz/cvut/kbss/study/rest/InstitutionControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/InstitutionControllerTest.java index 03c5b2be..f9db7fbc 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/InstitutionControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/InstitutionControllerTest.java @@ -5,6 +5,8 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.model.Institution; +import cz.cvut.kbss.study.model.Role; +import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.persistence.dao.util.RecordFilterParams; import cz.cvut.kbss.study.service.InstitutionService; @@ -46,11 +48,13 @@ public class InstitutionControllerTest extends BaseControllerTestRunner { @InjectMocks private InstitutionController controller; + @BeforeEach public void setUp() { super.setUp(controller); Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + RoleGroup roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + User user = Generator.generateUser(institution, roleGroupAdmin); Environment.setCurrentUser(user); } diff --git a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java index 0917640d..2a0f5622 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java @@ -7,10 +7,7 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.exception.RecordAuthorNotFoundException; -import cz.cvut.kbss.study.model.Institution; -import cz.cvut.kbss.study.model.PatientRecord; -import cz.cvut.kbss.study.model.RecordPhase; -import cz.cvut.kbss.study.model.User; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.persistence.dao.util.RecordFilterParams; import cz.cvut.kbss.study.persistence.dao.util.RecordSort; import cz.cvut.kbss.study.rest.event.PaginatedResultRetrievedEvent; @@ -81,13 +78,17 @@ public class PatientRecordControllerTest extends BaseControllerTestRunner { private User user; + private RoleGroup roleGroupAdmin; + @BeforeEach public void setUp() { super.setUp(controller); Institution institution = Generator.generateInstitution(); institution.setKey(IdentificationUtils.generateKey()); - this.user = Generator.generateUser(institution); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + this.user = Generator.generateUser(institution, roleGroupAdmin); Environment.setCurrentUser(user); + } @Test @@ -146,8 +147,8 @@ public void getRecordsReturnsEmptyListWhenNoReportsAreFound() throws Exception { public void getRecordsReturnsAllRecords() throws Exception { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, roleGroupAdmin); + User user2 = Generator.generateUser(institution, roleGroupAdmin); List records = List.of(Generator.generatePatientRecordDto(user1), Generator.generatePatientRecordDto(user1), @@ -174,8 +175,8 @@ public void findByInstitutionReturnsRecords() throws Exception { Institution institution = Generator.generateInstitution(); institution.setKey(key); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, roleGroupAdmin); + User user2 = Generator.generateUser(institution, roleGroupAdmin); PatientRecordDto record1 = Generator.generatePatientRecordDto(user1); PatientRecordDto record2 = Generator.generatePatientRecordDto(user2); diff --git a/src/test/java/cz/cvut/kbss/study/rest/StatisticsControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/StatisticsControllerTest.java index b12645d0..7bbda82f 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/StatisticsControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/StatisticsControllerTest.java @@ -3,6 +3,7 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.model.Institution; +import cz.cvut.kbss.study.model.Role; import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.service.StatisticsService; @@ -32,7 +33,8 @@ public class StatisticsControllerTest extends BaseControllerTestRunner { public void setUp() { super.setUp(controller); Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + RoleGroup roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + User user = Generator.generateUser(institution, roleGroupAdmin); Environment.setCurrentUser(user); } diff --git a/src/test/java/cz/cvut/kbss/study/rest/UserControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/UserControllerTest.java index ba0159fe..8646cf98 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/UserControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/UserControllerTest.java @@ -5,6 +5,8 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.model.Institution; +import cz.cvut.kbss.study.model.Role; +import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.service.InstitutionService; import cz.cvut.kbss.study.service.UserService; @@ -45,11 +47,14 @@ public class UserControllerTest extends BaseControllerTestRunner { @InjectMocks private UserController controller; + private RoleGroup roleGroupAdmin; + @BeforeEach public void setUp() { super.setUp(controller); Institution institution = Generator.generateInstitution(); - User user = Generator.generateUser(institution); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + User user = Generator.generateUser(institution, this.roleGroupAdmin); user.setUsername("tom"); Environment.setCurrentUser(user); } @@ -100,9 +105,9 @@ public void getUsersReturnsEmptyListWhenNoUsersAreFound() throws Exception { public void getUsersReturnsAllUsers() throws Exception { Institution institution = Generator.generateInstitution(); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); - User user3 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); + User user3 = Generator.generateUser(institution, this.roleGroupAdmin); List users = new ArrayList<>(); users.add(user1); @@ -127,9 +132,9 @@ public void findAllUsersByInstitutionReturnsInstitutionUsers() throws Exception Institution institution = Generator.generateInstitution(); institution.setKey(key); - User user1 = Generator.generateUser(institution); - User user2 = Generator.generateUser(institution); - User user3 = Generator.generateUser(institution); + User user1 = Generator.generateUser(institution, this.roleGroupAdmin); + User user2 = Generator.generateUser(institution, this.roleGroupAdmin); + User user3 = Generator.generateUser(institution, this.roleGroupAdmin); List users = new ArrayList<>(); users.add(user1); @@ -292,7 +297,7 @@ public void changePasswordByTokenReturnsResponseStatusNoContent() throws Excepti public void sendInvitationReturnsResponseStatusBadRequest() throws Exception { final String username = "tom"; final Institution institution = Generator.generateInstitution(); - final User user = Generator.generateUser(institution); + final User user = Generator.generateUser(institution, this.roleGroupAdmin); user.setIsInvited(true); when(userServiceMock.findByUsername(username)).thenReturn(user); @@ -307,7 +312,7 @@ public void sendInvitationReturnsResponseStatusBadRequest() throws Exception { public void sendInvitationReturnsResponseStatusNoContent() throws Exception { final String username = "tom"; final Institution institution = Generator.generateInstitution(); - final User user = Generator.generateUser(institution); + final User user = Generator.generateUser(institution, this.roleGroupAdmin); user.setIsInvited(false); when(userServiceMock.findByUsername(username)).thenReturn(user); @@ -322,7 +327,7 @@ public void sendInvitationReturnsResponseStatusNoContent() throws Exception { public void sendInvitationDeleteReturnsResponseStatusNoContent() throws Exception { final String username = "tom"; final Institution institution = Generator.generateInstitution(); - final User user = Generator.generateUser(institution); + final User user = Generator.generateUser(institution, this.roleGroupAdmin); user.setIsInvited(false); when(userServiceMock.findByUsername(username)).thenReturn(user); diff --git a/src/test/java/cz/cvut/kbss/study/rest/handler/HateoasPagingListenerTest.java b/src/test/java/cz/cvut/kbss/study/rest/handler/HateoasPagingListenerTest.java index d326c106..5a5f8d38 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/handler/HateoasPagingListenerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/handler/HateoasPagingListenerTest.java @@ -42,7 +42,7 @@ public void setUp() { this.listener = new HateoasPagingListener(); this.uriBuilder = UriComponentsBuilder.newInstance().scheme("http").host("localhost").path("rest/records"); this.responseMock = new MockHttpServletResponse(); - final User author = Generator.generateUser(null); + final User author = Generator.generateUser(null, null); this.records = IntStream.range(0, 10).mapToObj(i -> Generator.generatePatientRecordDto(author)) .collect(Collectors.toList()); } diff --git a/src/test/java/cz/cvut/kbss/study/security/CustomSwitchUserFilterTest.java b/src/test/java/cz/cvut/kbss/study/security/CustomSwitchUserFilterTest.java index dbba35bb..57aae8ce 100644 --- a/src/test/java/cz/cvut/kbss/study/security/CustomSwitchUserFilterTest.java +++ b/src/test/java/cz/cvut/kbss/study/security/CustomSwitchUserFilterTest.java @@ -3,6 +3,7 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.model.Role; +import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.model.Vocabulary; import cz.cvut.kbss.study.rest.exception.BadRequestException; @@ -36,10 +37,10 @@ void setUp() { @Test void attemptSwitchUserSwitchesCurrentUserToTarget() { - final User source = Generator.generateUser(null); - source.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.administrator)); + final User source = Generator.generateUser(null, null); + source.setRoleGroup(Generator.generateRoleGroupWithRoles(Role.administrator)); Environment.setCurrentUser(source); - final User target = Generator.generateUser(null); + final User target = Generator.generateUser(null, null); when(userDetailsService.loadUserByUsername(target.getUsername())).thenReturn(new UserDetails(target)); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("username", target.getUsername()); @@ -49,11 +50,13 @@ void attemptSwitchUserSwitchesCurrentUserToTarget() { @Test void attemptSwitchUserThrowsBadRequestExceptionWhenTargetUserIsAdmin() { - final User source = Generator.generateUser(null); - source.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.administrator)); + RoleGroup roleGroup = Generator.generateRoleGroupWithRoles(Role.administrator); + final User source = Generator.generateUser(null, roleGroup); + source.setRoleGroup(Generator.generateRoleGroupWithRoles(Role.administrator)); Environment.setCurrentUser(source); - final User target = Generator.generateUser(null); - target.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.administrator)); + final User target = Generator.generateUser(null, roleGroup); + target.addType(Vocabulary.s_i_administrator); + target.setRoleGroup(Generator.generateRoleGroupWithRoles(Role.administrator)); when(userDetailsService.loadUserByUsername(target.getUsername())).thenReturn(new UserDetails(target)); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setParameter("username", target.getUsername()); diff --git a/src/test/java/cz/cvut/kbss/study/service/BaseServiceTestRunner.java b/src/test/java/cz/cvut/kbss/study/service/BaseServiceTestRunner.java index 5a6f8592..b6844f75 100644 --- a/src/test/java/cz/cvut/kbss/study/service/BaseServiceTestRunner.java +++ b/src/test/java/cz/cvut/kbss/study/service/BaseServiceTestRunner.java @@ -9,6 +9,7 @@ import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; import cz.cvut.kbss.study.persistence.dao.InstitutionDao; +import cz.cvut.kbss.study.persistence.dao.RoleGroupDao; import cz.cvut.kbss.study.persistence.dao.UserDao; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -37,6 +38,11 @@ public abstract class BaseServiceTestRunner extends TransactionalTestRunner { protected User user; + @Autowired + private RoleGroupDao roleGroupDao; + + protected RoleGroup roleGroupAdmin; + public static final String USERNAME = "halsey"; public static final String PASSWORD = "john117"; public static final String EMAIL = "john117@gmail.com"; @@ -44,9 +50,10 @@ public abstract class BaseServiceTestRunner extends TransactionalTestRunner { @BeforeEach public void setUp() throws Exception { Institution institution = Generator.generateInstitution(); - RoleGroup roleGroup = Generator.generateRoleGroupWithOneRole(Role.administrator); //TODO - user = Generator.getUser(USERNAME, PASSWORD, "John", "Grant", EMAIL, institution, roleGroup); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + user = Generator.getUser(USERNAME, PASSWORD, "John", "Grant", EMAIL, institution, this.roleGroupAdmin); transactional(() -> { + roleGroupDao.persist(this.roleGroupAdmin); institutionDao.persist(institution); if (userDao.findByUsername(user.getUsername()) == null) { user.encodePassword(passwordEncoder); diff --git a/src/test/java/cz/cvut/kbss/study/service/UserDetailsServiceTest.java b/src/test/java/cz/cvut/kbss/study/service/UserDetailsServiceTest.java index 0e58a1d6..79dda4ce 100644 --- a/src/test/java/cz/cvut/kbss/study/service/UserDetailsServiceTest.java +++ b/src/test/java/cz/cvut/kbss/study/service/UserDetailsServiceTest.java @@ -2,7 +2,10 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.model.Institution; +import cz.cvut.kbss.study.model.Role; +import cz.cvut.kbss.study.model.RoleGroup; import cz.cvut.kbss.study.model.User; +import cz.cvut.kbss.study.persistence.dao.RoleGroupDao; import cz.cvut.kbss.study.service.security.UserDetailsService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -23,12 +26,14 @@ public class UserDetailsServiceTest extends BaseServiceTestRunner { @Autowired private InstitutionService institutionService; + @Autowired + private RoleGroupService roleGroupService; + @Test public void loadUserByUsername() { Institution institution = Generator.generateInstitution(); institutionService.persist(institution); - - User user = Generator.generateUser(institution); + User user = Generator.generateUser(institution, this.roleGroupAdmin); userService.persist(user); UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername()); diff --git a/src/test/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordServiceTest.java b/src/test/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordServiceTest.java index 7c29ce7a..a7e3058a 100644 --- a/src/test/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordServiceTest.java +++ b/src/test/java/cz/cvut/kbss/study/service/repository/RepositoryPatientRecordServiceTest.java @@ -4,11 +4,7 @@ import cz.cvut.kbss.study.environment.generator.Generator; import cz.cvut.kbss.study.environment.util.Environment; import cz.cvut.kbss.study.exception.RecordAuthorNotFoundException; -import cz.cvut.kbss.study.model.PatientRecord; -import cz.cvut.kbss.study.model.RecordPhase; -import cz.cvut.kbss.study.model.Role; -import cz.cvut.kbss.study.model.User; -import cz.cvut.kbss.study.model.Vocabulary; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.persistence.dao.PatientRecordDao; import cz.cvut.kbss.study.service.UserService; import cz.cvut.kbss.study.service.security.SecurityUtils; @@ -54,16 +50,21 @@ class RepositoryPatientRecordServiceTest { private User user; + private RoleGroup roleGroupAdmin; + private RoleGroup roleGroupUser; + @BeforeEach void setUp() { - this.user = Generator.generateUser(Generator.generateInstitution()); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + this.roleGroupUser = Generator.generateRoleGroupWithRoles(Role.user); + this.user = Generator.generateUser(Generator.generateInstitution(), roleGroupUser); Environment.setCurrentUser(user); when(securityUtils.getCurrentUser()).thenReturn(user); } @Test void importRecordsSetsCurrentUserAsAuthorWhenTheyAreRegularUserAndImportsSpecifiedRecords() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupUser); final List toImport = generateRecordsToImport(originalAuthor); when(recordDao.exists(any())).thenReturn(false); @@ -96,10 +97,10 @@ private List generateRecordsToImport(User originalAuthor) { @Test void importRecordsRetainsRecordProvenanceDataWhenCurrentUserIsAdmin() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupAdmin); final List toImport = generateRecordsToImport(originalAuthor); - user.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.user)); Environment.setCurrentUser(user); + user.getRoleGroup().addRole(Role.administrator); when(userService.exists(originalAuthor.getUri())).thenReturn(true); when(recordDao.exists(any())).thenReturn(false); @@ -119,9 +120,9 @@ void importRecordsRetainsRecordProvenanceDataWhenCurrentUserIsAdmin() { @Test void importRecordsThrowsRecordAuthorNotFoundExceptionWhenAdminImportsRecordsAndRecordAuthorIsNotFound() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupAdmin); final List toImport = generateRecordsToImport(originalAuthor); - user.setRoleGroup(Generator.generateRoleGroupWithOneRole(Role.administrator)); + user.setRoleGroup(Generator.generateRoleGroupWithRoles(Role.administrator)); Environment.setCurrentUser(user); assertThrows(RecordAuthorNotFoundException.class, () -> sut.importRecords(toImport)); @@ -129,7 +130,7 @@ void importRecordsThrowsRecordAuthorNotFoundExceptionWhenAdminImportsRecordsAndR @Test void importRecordsSkipsImportingRecordsThatAlreadyExist() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupUser); final List toImport = generateRecordsToImport(originalAuthor); final PatientRecord existing = toImport.get(Generator.randomIndex(toImport)); when(recordDao.exists(any(URI.class))).thenReturn(false); @@ -144,7 +145,7 @@ void importRecordsSkipsImportingRecordsThatAlreadyExist() { @Test void importRecordsWithPhaseSetsSpecifiedPhaseToAllRecords() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupUser); final List toImport = generateRecordsToImport(originalAuthor); final RecordPhase targetPhase = RecordPhase.values()[Generator.randomInt(0, RecordPhase.values().length)]; when(recordDao.exists(any())).thenReturn(false); @@ -157,7 +158,7 @@ void importRecordsWithPhaseSetsSpecifiedPhaseToAllRecords() { @Test void importRecordsSetsRecordPhaseToOpenOnAllImportedRecordsWhenCurrentUserIsRegularUser() { - final User originalAuthor = Generator.generateUser(Generator.generateInstitution()); + final User originalAuthor = Generator.generateUser(Generator.generateInstitution(), this.roleGroupUser); final List toImport = generateRecordsToImport(originalAuthor); when(recordDao.exists(any())).thenReturn(false); diff --git a/src/test/java/cz/cvut/kbss/study/service/security/SecurityUtilsTest.java b/src/test/java/cz/cvut/kbss/study/service/security/SecurityUtilsTest.java index 6cb2e39d..d35787c5 100644 --- a/src/test/java/cz/cvut/kbss/study/service/security/SecurityUtilsTest.java +++ b/src/test/java/cz/cvut/kbss/study/service/security/SecurityUtilsTest.java @@ -60,6 +60,8 @@ public class SecurityUtilsTest { private User user; + private RoleGroup roleGroupAdmin; + public static final String USERNAME = "username"; public static final String PASSWORD = "pass" + Generator.randomInt(0, 1000); @@ -67,8 +69,8 @@ public class SecurityUtilsTest { public void setUp() { Institution institution = Generator.generateInstitution(); institution.setKey(IdentificationUtils.generateKey()); - RoleGroup roleGroup = new RoleGroup(); - this.user = Generator.getUser(USERNAME, PASSWORD, "John", "Johnie", "Johnie@gmail.com", institution, roleGroup); + this.roleGroupAdmin = Generator.generateRoleGroupWithRoles(Role.administrator); + this.user = Generator.getUser(USERNAME, PASSWORD, "John", "Johnie", "Johnie@gmail.com", institution, this.roleGroupAdmin); user.generateUri(); } @@ -125,7 +127,7 @@ public void areFromSameInstitutionReturnsTrueForUserFromSameInstitution() { Environment.setCurrentUser(user); when(userDao.findByUsername(user.getUsername())).thenReturn(user); - User userFromSameInstitution = Generator.generateUser(user.getInstitution()); + User userFromSameInstitution = Generator.generateUser(user.getInstitution(), this.roleGroupAdmin); when(userDao.findByInstitution(user.getInstitution())).thenReturn(List.of(user, userFromSameInstitution)); assertTrue(sut.areFromSameInstitution(userFromSameInstitution.getUsername())); @@ -138,7 +140,7 @@ public void areFromSameInstitutionReturnsFalseForUserFromDifferentInstitution() Institution institutionAnother = Generator.generateInstitution(); - User userFromAnotherInstitution = Generator.generateUser(institutionAnother); + User userFromAnotherInstitution = Generator.generateUser(institutionAnother, this.roleGroupAdmin); when(userDao.findByInstitution(user.getInstitution())).thenReturn(List.of(user)); assertFalse(sut.areFromSameInstitution(userFromAnotherInstitution.getUsername())); @@ -162,7 +164,7 @@ public void isRecordInUsersInstitutionReturnsFalseWhenRecordBelongsToInstitution when(userDao.findByUsername(user.getUsername())).thenReturn(user); Institution institutionAnother = Generator.generateInstitution(); institutionAnother.setKey(IdentificationUtils.generateKey()); - User userFromAnotherInstitution = Generator.generateUser(institutionAnother); + User userFromAnotherInstitution = Generator.generateUser(institutionAnother, this.roleGroupAdmin); PatientRecord record = Generator.generatePatientRecord(userFromAnotherInstitution); record.setKey(IdentificationUtils.generateKey());