package org.baeldung.um.security; import static com.jayway.restassured.RestAssured.given; import static org.baeldung.common.spring.util.Profiles.CLIENT; import static org.baeldung.common.spring.util.Profiles.TEST; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import org.baeldung.test.common.client.security.ITestAuthenticator; import org.baeldung.um.client.template.PrivilegeRestClient; import org.baeldung.um.model.PrivilegeDtoOpsImpl; import org.baeldung.um.spring.CommonTestConfig; import org.baeldung.um.spring.UmClientConfig; import org.baeldung.um.spring.UmLiveTestConfig; import org.baeldung.um.util.Um; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import com.jayway.restassured.response.Response; import com.jayway.restassured.specification.RequestSpecification; @ActiveProfiles({ CLIENT, TEST }) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { UmLiveTestConfig.class, UmClientConfig.class, CommonTestConfig.class }, loader = AnnotationConfigContextLoader.class) public class SecurityRestLiveTest { @Autowired private PrivilegeRestClient resourceClient; @Autowired private PrivilegeDtoOpsImpl resourceOps; @Autowired private ITestAuthenticator auth; // tests // Unauthenticated @Test public final void givenUnauthenticated_whenAResourceIsDeleted_then401IsReceived() { // Given final String uriOfExistingResource = resourceClient.createAsUri(resourceOps.createNewResource()); // When final Response response = given().delete(uriOfExistingResource); // Then assertThat(response.getStatusCode(), is(401)); } // Authenticated @Test public final void givenAuthenticatedByBasicAuth_whenResourceIsCreated_then201IsReceived() { // Given // When final Response response = givenAuthenticated().contentType(resourceClient.getMarshaller().getMime()).body(resourceOps.createNewResource()).post(resourceClient.getUri()); // Then assertThat(response.getStatusCode(), is(201)); } // util protected final RequestSpecification givenAuthenticated() { return auth.givenBasicAuthenticated(Um.ADMIN_EMAIL, Um.ADMIN_PASS); } }