Search in sources :

Example 86 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class PrePostSecurityAnnotationsTest method authorityNoAuthorityCantAccessApps.

@Test
public void authorityNoAuthorityCantAccessApps() throws Exception {
    MockHttpSession session = getSession("NO_AUTHORITY");
    mvc.perform(put("/apps").session(session)).andExpect(status().isForbidden());
}
Also used : MockHttpSession(org.springframework.mock.web.MockHttpSession) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.Test)

Example 87 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class TranslationWebApiTest method testOK.

@Test
public void testOK() throws Exception {
    Locale locale = Locale.FRENCH;
    MockHttpSession session = getSession("ALL");
    DataElement dataElementA = createDataElement('A');
    identifiableObjectManager.save(dataElementA);
    String valueToCheck = "frenchTranslated";
    dataElementA.getTranslations().add(new ObjectTranslation(locale.getLanguage(), TranslationProperty.NAME, valueToCheck));
    mvc.perform(put("/dataElements/" + dataElementA.getUid() + "/translations").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(dataElementA))).andExpect(status().is(HttpStatus.SC_NO_CONTENT));
    MvcResult result = mvc.perform(get("/dataElements/" + dataElementA.getUid() + "?locale=" + locale.getLanguage()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andReturn();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(result.getResponse().getContentAsString());
    assertEquals(valueToCheck, node.get("displayName").asText());
}
Also used : Locale(java.util.Locale) DataElement(org.hisp.dhis.dataelement.DataElement) MockHttpSession(org.springframework.mock.web.MockHttpSession) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) JsonNode(org.codehaus.jackson.JsonNode) MvcResult(org.springframework.test.web.servlet.MvcResult) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest)

Example 88 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class AttributeControllerDocumentation method testUpdate.

@Override
public void testUpdate() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    MvcResult postResult = mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andReturn();
    String uid = TestUtils.getCreatedUid(postResult.getResponse().getContentAsString());
    InputStream inputUpdate = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    mvc.perform(put(schema.getRelativeApiEndpoint() + "/" + uid).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(inputUpdate))).andExpect(status().is(updateStatus)).andDo(documentPrettyPrint(schema.getPlural() + "/update"));
}
Also used : InputStream(java.io.InputStream) MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 89 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class AttributeControllerDocumentation method testGetAll.

@Override
public void testGetAll() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andReturn();
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath(schema.getPlural()).description(schema.getPlural()));
    mvc.perform(get(schema.getRelativeApiEndpoint()).session(session).accept(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$." + schema.getPlural()).isArray()).andExpect(jsonPath("$." + schema.getPlural() + ".length()").value(1)).andDo(documentPrettyPrint(schema.getPlural() + "/all", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) MockHttpSession(org.springframework.mock.web.MockHttpSession) ClassPathResource(org.springframework.core.io.ClassPathResource) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Example 90 with MockHttpSession

use of org.springframework.mock.web.MockHttpSession in project dhis2-core by dhis2.

the class AttributeControllerDocumentation method testGetByIdOk.

@Override
public void testGetByIdOk() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    MvcResult postResult = mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andReturn();
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    String uid = TestUtils.getCreatedUid(postResult.getResponse().getContentAsString());
    mvc.perform(get(schema.getRelativeApiEndpoint() + "/{id}", uid).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.name").value("sqlViewAttribute")).andDo(documentPrettyPrint(schema.getPlural() + "/id", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : InputStream(java.io.InputStream) MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) ClassPathResource(org.springframework.core.io.ClassPathResource) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Aggregations

MockHttpSession (org.springframework.mock.web.MockHttpSession)106 Test (org.junit.Test)84 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)34 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)23 HashMap (java.util.HashMap)13 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)13 MockFilterChain (org.springframework.mock.web.MockFilterChain)12 FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)11 ModelAndView (org.springframework.web.servlet.ModelAndView)11 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)9 FilterChain (javax.servlet.FilterChain)8 MockServletContext (org.springframework.mock.web.MockServletContext)8 SessionRegistry (org.springframework.security.core.session.SessionRegistry)8 ConcurrentSessionFilter (org.springframework.security.web.session.ConcurrentSessionFilter)8 Principal (org.apereo.cas.authentication.principal.Principal)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 CasProfile (org.pac4j.cas.profile.CasProfile)7 InputStream (java.io.InputStream)6 Before (org.junit.Before)6