Search in sources :

Example 6 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerTest method shouldFailWhenAppNamespaceNameIsInvalid.

@Test
public void shouldFailWhenAppNamespaceNameIsInvalid() {
    Assert.assertTrue(consumerPermissionValidator.hasCreateNamespacePermission(null, null));
    OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId("appId");
    dto.setName("invalid name");
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    try {
        restTemplate.postForEntity(url("/openapi/v1/apps/{appId}/appnamespaces"), dto, OpenAppNamespaceDTO.class, dto.getAppId());
        Assert.fail("should throw");
    } catch (HttpClientErrorException e) {
        String result = e.getResponseBodyAsString();
        Assert.assertThat(result, containsString(InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
        Assert.assertThat(result, containsString(InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with OpenAppNamespaceDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO in project apollo by ctripcorp.

the class NamespaceControllerWithAuthorizationTest method testCreateAppNamespaceWithoutAuthority.

/**
 * test method {@link NamespaceController#createAppNamespace(String, OpenAppNamespaceDTO)} without
 * authority.
 */
@Test
@Sql(scripts = "/sql/openapi/NamespaceControllerTest.testCreateAppNamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testCreateAppNamespaceWithoutAuthority() {
    final OpenAppNamespaceDTO dto = new OpenAppNamespaceDTO();
    dto.setAppId("consumer-test-app-id-1");
    dto.setName("create-app-namespace-fail");
    dto.setFormat(ConfigFileFormat.Properties.getValue());
    dto.setDataChangeCreatedBy("apollo");
    try {
        restTemplate.exchange(this.url("/openapi/v1/apps/{appId}/appnamespaces"), HttpMethod.POST, new HttpEntity<>(dto, HTTP_HEADERS_WITH_TOKEN), OpenAppNamespaceDTO.class, dto.getAppId());
        fail("should throw");
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.FORBIDDEN, e.getStatusCode());
        String result = e.getResponseBodyAsString();
        assertTrue(result.contains("org.springframework.security.access.AccessDeniedException"));
    }
    // random app id
    dto.setAppId(UUID.randomUUID().toString());
    try {
        restTemplate.exchange(this.url("/openapi/v1/apps/{appId}/appnamespaces"), HttpMethod.POST, new HttpEntity<>(dto, HTTP_HEADERS_WITH_TOKEN), OpenAppNamespaceDTO.class, dto.getAppId());
        fail("should throw");
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.FORBIDDEN, e.getStatusCode());
        String result = e.getResponseBodyAsString();
        assertTrue(result.contains("org.springframework.security.access.AccessDeniedException"));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) OpenAppNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Aggregations

OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)7 Test (org.junit.Test)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)5 Sql (org.springframework.test.context.jdbc.Sql)4 InputValidator (com.ctrip.framework.apollo.common.utils.InputValidator)1 ConfigFileFormat (com.ctrip.framework.apollo.core.enums.ConfigFileFormat)1 OpenNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)1 Arrays (java.util.Arrays)1 UUID (java.util.UUID)1 HttpPost (org.apache.http.client.methods.HttpPost)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 Ignore (org.junit.Ignore)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1