explicitly mock user+role in *IntTest, set global role HOSTMASTER only in dev profile

This commit is contained in:
Michael Hoennig 2019-04-27 11:10:36 +02:00
parent 5d843f4725
commit b9a7c33c6a
6 changed files with 28 additions and 4 deletions

View File

@ -42,9 +42,6 @@ public class HsadminNgApp {
@PostConstruct
public void initApplication() {
// TODO: remove this hack once proper user roles are implemented
SecurityUtils.addUserRole(null, null, Role.HOSTMASTER);
Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) && activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_PRODUCTION)) {
log.error("You have misconfigured your application! It should not run " +
@ -54,6 +51,14 @@ public class HsadminNgApp {
log.error("You have misconfigured your application! It should not " +
"run with both the 'dev' and 'cloud' profiles at the same time.");
}
// TODO: remove this hack once proper user roles are implemented
if (activeProfiles.contains(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) {
// For some strange reasons, HsadminNgApp is created in locally running tests,
// but not on Jenkins, therefore the login user had no rights and many tests
// failed.
SecurityUtils.addUserRole(null, null, Role.HOSTMASTER);
}
}
/**

View File

@ -20,7 +20,7 @@ public class MockSecurityContext {
public static void givenUserHavingRole(final Class<?> onClass, final Long onId, final Role role) {
if ((onClass == null || onId == null) && !role.isIndependent()) {
throw new IllegalArgumentException("dependent roles like " + role + " missing DtoClass and ID");
throw new IllegalArgumentException("dependent role " + role + " needs DtoClass and ID");
}
SecurityUtils.addUserRole(onClass, onId, role);
}

View File

@ -7,6 +7,8 @@ import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
import org.hostsharing.hsadminng.repository.AssetRepository;
import org.hostsharing.hsadminng.service.AssetQueryService;
import org.hostsharing.hsadminng.service.AssetService;
import org.hostsharing.hsadminng.service.accessfilter.MockSecurityContext;
import org.hostsharing.hsadminng.service.accessfilter.Role;
import org.hostsharing.hsadminng.service.dto.AssetDTO;
import org.hostsharing.hsadminng.service.mapper.AssetMapper;
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
@ -94,6 +96,9 @@ public class AssetResourceIntTest {
@Before
public void setup() {
MockSecurityContext.givenAuthenticatedUser();
MockSecurityContext.givenUserHavingRole(Role.ADMIN);
MockitoAnnotations.initMocks(this);
final AssetResource assetResource = new AssetResource(assetService, assetQueryService);
this.restAssetMockMvc = MockMvcBuilders.standaloneSetup(assetResource)

View File

@ -9,6 +9,7 @@ import org.hostsharing.hsadminng.domain.enumeration.VatRegion;
import org.hostsharing.hsadminng.repository.CustomerRepository;
import org.hostsharing.hsadminng.service.CustomerQueryService;
import org.hostsharing.hsadminng.service.CustomerService;
import org.hostsharing.hsadminng.service.accessfilter.MockSecurityContext;
import org.hostsharing.hsadminng.service.accessfilter.Role;
import org.hostsharing.hsadminng.service.dto.CustomerDTO;
import org.hostsharing.hsadminng.service.mapper.CustomerMapper;
@ -136,6 +137,9 @@ public class CustomerResourceIntTest {
@Before
public void setup() {
MockSecurityContext.givenAuthenticatedUser();
MockSecurityContext.givenUserHavingRole(Role.ADMIN);
MockitoAnnotations.initMocks(this);
givenAuthenticatedUser();

View File

@ -8,6 +8,8 @@ import org.hostsharing.hsadminng.domain.Share;
import org.hostsharing.hsadminng.repository.MembershipRepository;
import org.hostsharing.hsadminng.service.MembershipQueryService;
import org.hostsharing.hsadminng.service.MembershipService;
import org.hostsharing.hsadminng.service.accessfilter.MockSecurityContext;
import org.hostsharing.hsadminng.service.accessfilter.Role;
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
import org.hostsharing.hsadminng.service.mapper.MembershipMapper;
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
@ -97,6 +99,9 @@ public class MembershipResourceIntTest {
@Before
public void setup() {
MockSecurityContext.givenAuthenticatedUser();
MockSecurityContext.givenUserHavingRole(Role.ADMIN);
MockitoAnnotations.initMocks(this);
final MembershipResource membershipResource = new MembershipResource(membershipService, membershipQueryService);
this.restMembershipMockMvc = MockMvcBuilders.standaloneSetup(membershipResource)

View File

@ -7,6 +7,8 @@ import org.hostsharing.hsadminng.domain.enumeration.ShareAction;
import org.hostsharing.hsadminng.repository.ShareRepository;
import org.hostsharing.hsadminng.service.ShareQueryService;
import org.hostsharing.hsadminng.service.ShareService;
import org.hostsharing.hsadminng.service.accessfilter.MockSecurityContext;
import org.hostsharing.hsadminng.service.accessfilter.Role;
import org.hostsharing.hsadminng.service.dto.ShareDTO;
import org.hostsharing.hsadminng.service.mapper.ShareMapper;
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
@ -92,6 +94,9 @@ public class ShareResourceIntTest {
@Before
public void setup() {
MockSecurityContext.givenAuthenticatedUser();
MockSecurityContext.givenUserHavingRole(Role.ADMIN);
MockitoAnnotations.initMocks(this);
final ShareResource shareResource = new ShareResource(shareService, shareQueryService);
this.restShareMockMvc = MockMvcBuilders.standaloneSetup(shareResource)