Search in sources :

Example 71 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project cas by apereo.

the class AbstractServiceValidateControllerTests method getModelAndViewUponServiceValidationWithSecurePgtUrl.

/*
    Helper methods.
     */
protected ModelAndView getModelAndViewUponServiceValidationWithSecurePgtUrl() throws Exception {
    final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), SERVICE);
    final TicketGrantingTicket tId = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
    final ServiceTicket sId = getCentralAuthenticationService().grantServiceTicket(tId.getId(), SERVICE, ctx);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, SERVICE.getId());
    request.addParameter(CasProtocolConstants.PARAMETER_TICKET, sId.getId());
    request.addParameter(CasProtocolConstants.PARAMETER_PROXY_GRANTING_TICKET_URL, GITHUB_URL);
    return this.serviceValidateController.handleRequestInternal(request, new MockHttpServletResponse());
}
Also used : TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 72 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project alfresco-remote-api by Alfresco.

the class LockMethodTest method testMNT_11990.

@Test
public void testMNT_11990() throws Exception {
    MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 3600);
    lockRequest.addHeader(WebDAV.HEADER_IF, "(<" + WebDAV.makeLockToken(fileNodeRef, userName) + ">)");
    lockRequest.setRequestURI("/" + TEST_FILE_NAME);
    lockMethod.setDetails(lockRequest, new MockHttpServletResponse(), davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            try {
                lockMethod.executeImpl();
                fail("Lock should not be refreshed for non-locked file.");
            } catch (WebDAVServerException e) {
                assertEquals(e.getHttpStatusCode(), HttpServletResponse.SC_BAD_REQUEST);
            }
            return null;
        }
    };
    // try to refresh lock for non-locked node
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 73 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project alfresco-remote-api by Alfresco.

the class LockMethodTest method testMNT_12425.

@Test
public void testMNT_12425() throws Exception {
    MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    MockHttpServletResponse lockResponse = new MockHttpServletResponse();
    // refresh lock set to 1 hour
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 3600);
    lockRequest.addHeader(WebDAV.HEADER_IF, "(<" + WebDAV.makeLockToken(fileNodeRef, userName) + ">)");
    // specify path to non-existing file
    lockRequest.setRequestURI("/" + TEST_NEW_FOLDER_NAME + "/" + TEST_NEW_FILE_NAME);
    lockMethod.setDetails(lockRequest, lockResponse, davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            try {
                lockMethod.executeImpl();
                fail("Refresh lock for non-exisitent resource should fail.");
            } catch (WebDAVServerException e) {
                assertEquals(HttpServletResponse.SC_FORBIDDEN, e.getHttpStatusCode());
            }
            return null;
        }
    };
    // try to lock non-existent file
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 74 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project alfresco-remote-api by Alfresco.

the class UnlockMethodTest method unlockWorkingCopy.

/**
 * Test MNT-9680: Working copies are open in read-only mode when using Webdav online edit
 *
 * @throws Exception
 */
@Test
public void unlockWorkingCopy() throws Exception {
    setUpPreconditionForCheckedOutTest();
    try {
        String workingCopyName = nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_NAME).toString();
        String lockToken = fileWorkingCopyNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + this.userName;
        String lockHeaderValue = "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">";
        final WebDAVHelper davHelper = (WebDAVHelper) appContext.getBean("webDAVHelper");
        request.addHeader(WebDAV.HEADER_LOCK_TOKEN, lockHeaderValue);
        request.setRequestURI("/" + workingCopyName);
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<d:lockinfo xmlns:d=\"DAV:\">" + "<d:lockscope><d:exclusive/></d:lockscope>" + "</d:lockinfo>";
        request.setContent(content.getBytes("UTF-8"));
        lockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        lockMethod.parseRequestHeaders();
        lockMethod.parseRequestBody();
        RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                lockMethod.executeImpl();
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
        unlockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        unlockMethod.parseRequestHeaders();
        RetryingTransactionCallback<Void> unlockExecuteImplCallBack = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                unlockMethod.executeImpl();
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(unlockExecuteImplCallBack);
        assertNull("lockType property should be deleted on unlock", nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_LOCK_TYPE));
        assertNull("lockOwner property should be deleted on unlock", nodeService.getProperty(fileWorkingCopyNodeRef, ContentModel.PROP_LOCK_OWNER));
    } finally {
        // clear context for current user
        this.authenticationComponent.clearCurrentSecurityContext();
        // delete test store as system user
        this.authenticationComponent.setSystemUserAsCurrentUser();
        RetryingTransactionCallback<Void> deleteStoreCallback = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                if (nodeService.exists(storeRef)) {
                    nodeService.deleteStore(storeRef);
                }
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(deleteStoreCallback);
    }
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 75 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project alfresco-remote-api by Alfresco.

the class UnlockMethodTest method unlockCheckedOutNode.

/**
 * Test that it is impossible to unlock a checked out node
 *
 * @throws Exception
 */
@Test
public void unlockCheckedOutNode() throws Exception {
    setUpPreconditionForCheckedOutTest();
    try {
        String lockToken = fileNodeRef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + this.userName;
        String lockHeaderValue = "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">";
        request.addHeader(WebDAV.HEADER_LOCK_TOKEN, lockHeaderValue);
        request.setRequestURI("/" + TEST_FILE_NAME);
        WebDAVHelper davHelper = (WebDAVHelper) appContext.getBean("webDAVHelper");
        unlockMethod.setDetails(request, new MockHttpServletResponse(), davHelper, folderNodeRef);
        unlockMethod.parseRequestHeaders();
        transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                unlockMethod.executeImpl();
                return null;
            }
        });
        fail("Exception should have been thrown, but wasn't.");
    } catch (AlfrescoRuntimeException e) {
        if (e.getCause() instanceof WebDAVServerException) {
            WebDAVServerException ee = (WebDAVServerException) e.getCause();
            assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, ee.getHttpStatusCode());
        } else {
            fail("Incorrect exception thrown.");
        }
    } catch (WebDAVServerException e) {
        assertEquals(HttpServletResponse.SC_PRECONDITION_FAILED, e.getHttpStatusCode());
    } finally {
        // clear context for current user
        this.authenticationComponent.clearCurrentSecurityContext();
        // delete test store as system user
        this.authenticationComponent.setSystemUserAsCurrentUser();
        RetryingTransactionCallback<Void> deleteStoreCallback = new RetryingTransactionCallback<Void>() {

            @Override
            public Void execute() throws Throwable {
                if (nodeService.exists(storeRef)) {
                    nodeService.deleteStore(storeRef);
                }
                return null;
            }
        };
        this.transactionService.getRetryingTransactionHelper().doInTransaction(deleteStoreCallback);
    }
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2338 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1986 Test (org.junit.jupiter.api.Test)1412 lombok.val (lombok.val)946 Test (org.junit.Test)558 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)239 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)184 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)106 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)65 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)64