Skip to content

Commit

Permalink
More test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaceccanti committed Oct 26, 2021
1 parent b310d4c commit c2b205b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/
package it.infn.mw.iam.test.audit;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.Collections;

import org.springframework.transaction.annotation.Transactional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
Expand All @@ -40,20 +39,19 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import it.infn.mw.iam.IamLoginService;
import it.infn.mw.iam.audit.IamAuditEventLogger;
import it.infn.mw.iam.audit.events.IamAuditApplicationEvent;
import it.infn.mw.iam.audit.events.auth.IamAuthenticationFailureEvent;
import it.infn.mw.iam.audit.events.auth.IamAuthenticationSuccessEvent;
import it.infn.mw.iam.audit.events.auth.IamAuthorizationFailureEvent;
import it.infn.mw.iam.test.util.annotation.IamMockMvcIntegrationTest;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {IamLoginService.class})
@WebAppConfiguration
@Transactional
@RunWith(SpringRunner.class)
@IamMockMvcIntegrationTest
@SpringBootTest(classes = {IamLoginService.class}, webEnvironment = WebEnvironment.MOCK)
public class AuditTests {

@Autowired
Expand All @@ -73,7 +71,8 @@ public void testAuthenticationFailureBadCredentialsEvent() throws Exception {

IamAuditApplicationEvent ev = logger.getLastEvent();

assertNotNull(ev);
assertThat(ev, notNullValue());

assertThat(ev, is(instanceOf(IamAuthenticationFailureEvent.class)));

}
Expand All @@ -88,7 +87,8 @@ public void testInteractiveAuthenticationSuccessEvent() throws Exception {

IamAuditApplicationEvent ev = logger.getLastEvent();

assertNotNull(ev);
assertThat(ev, notNullValue());

assertThat(ev, is(instanceOf(IamAuthenticationSuccessEvent.class)));

}
Expand All @@ -105,7 +105,7 @@ public void testAuthorizationFailureEvent() throws Exception {

IamAuditApplicationEvent ev = logger.getLastEvent();

assertNotNull(ev);
assertThat(ev, notNullValue());
assertThat(ev, is(instanceOf(IamAuthorizationFailureEvent.class)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.junit4.SpringRunner;

import com.google.common.collect.Lists;

import it.infn.mw.iam.IamLoginService;
import it.infn.mw.iam.api.scim.converter.ScimResourceLocationProvider;
import it.infn.mw.iam.api.scim.converter.UserConverter;
import it.infn.mw.iam.api.scim.model.ScimGroup;
Expand Down Expand Up @@ -63,11 +59,11 @@
import it.infn.mw.iam.persistence.model.IamAccount;
import it.infn.mw.iam.test.SshKeyUtils;
import it.infn.mw.iam.test.ext_authn.x509.X509TestSupport;
import it.infn.mw.iam.test.util.annotation.IamMockMvcIntegrationTest;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {IamLoginService.class})
@WebAppConfiguration
@Transactional

@RunWith(SpringRunner.class)
@IamMockMvcIntegrationTest
public class EventTests extends X509TestSupport {

private static final String USERNAME = "event_user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -34,42 +32,33 @@
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import it.infn.mw.iam.IamLoginService;
import it.infn.mw.iam.authn.ExternalAuthenticationRegistrationInfo.ExternalAuthenticationType;
import it.infn.mw.iam.test.util.WithMockOAuthUser;
import it.infn.mw.iam.test.util.WithMockOIDCUser;
import it.infn.mw.iam.test.util.annotation.IamMockMvcIntegrationTest;
import it.infn.mw.iam.test.util.oauth.MockOAuth2Filter;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {IamLoginService.class, CoreControllerTestSupport.class})
@WebAppConfiguration
public class IamCoreControllerTests {

@Autowired
protected WebApplicationContext context;
@RunWith(SpringRunner.class)
@IamMockMvcIntegrationTest
public class IamCoreControllerTests {

@Autowired
private MockOAuth2Filter mockOAuth2Filter;

@Autowired
protected MockMvc mvc;

@Value("${iam.baseUrl}")
String iamBaseUrl;

@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(context)
.apply(springSecurity())
.alwaysDo(log())
.build();
mockOAuth2Filter.cleanupSecurityContext();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;

import it.infn.mw.iam.IamLoginService;
import it.infn.mw.iam.test.scim.ScimRestUtilsMvc;
import it.infn.mw.iam.test.util.WithMockOAuthUser;
import it.infn.mw.iam.test.util.annotation.IamMockMvcIntegrationTest;
import it.infn.mw.iam.test.util.oauth.MockOAuth2Filter;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(
classes = {IamLoginService.class, CoreControllerTestSupport.class, ScimRestUtilsMvc.class})
@WebAppConfiguration

@RunWith(SpringRunner.class)
@IamMockMvcIntegrationTest
@SpringBootTest(
classes = {IamLoginService.class, CoreControllerTestSupport.class, ScimRestUtilsMvc.class},
webEnvironment = WebEnvironment.MOCK)
public class MeControllerTests {

private final static String TESTUSER_USERNAME = "test_101";
Expand All @@ -53,6 +57,12 @@ public class MeControllerTests {
@Autowired
private MockOAuth2Filter mockOAuth2Filter;


@Before
public void setup() {
mockOAuth2Filter.cleanupSecurityContext();
}

@After
public void teardown() {
mockOAuth2Filter.cleanupSecurityContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
package it.infn.mw.iam.test.scim;

import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -31,9 +29,7 @@
import org.springframework.stereotype.Component;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -44,12 +40,8 @@ public class RestUtils {
protected final ObjectMapper mapper;

@Autowired
public RestUtils(WebApplicationContext context, ObjectMapper mapper) {

mvc = MockMvcBuilders.webAppContextSetup(context)
.apply(springSecurity())
.alwaysDo(log())
.build();
public RestUtils(MockMvc mvc, ObjectMapper mapper) {
this.mvc = mvc;
this.mapper = mapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.databind.ObjectMapper;

import it.infn.mw.iam.api.scim.model.ScimPatchOperation.ScimPatchOperationType;
import it.infn.mw.iam.api.scim.model.ScimUser;
import it.infn.mw.iam.api.scim.model.ScimUserPatchRequest;
import it.infn.mw.iam.api.scim.model.ScimPatchOperation.ScimPatchOperationType;

@Component
public class ScimRestUtilsMvc extends RestUtils {

@Autowired
public ScimRestUtilsMvc(WebApplicationContext context, ObjectMapper mapper) {
super(context, mapper);
public ScimRestUtilsMvc(MockMvc mvc, ObjectMapper mapper) {
super(mvc, mapper);
}

public ScimUser postUser(ScimUser user) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class ScimUtils {

public static String SCIM_CONTENT_TYPE = "application/scim+json";
public static String SCIM_CONTENT_TYPE = "application/scim+json;charset=UTF-8";

public static final String SCIM_CLIENT_ID = "scim-client-rw";
public static final String SCIM_CLIENT_SECRET = "secret";
Expand Down

0 comments on commit c2b205b

Please sign in to comment.