Search in sources :

Example 6 with WebException

use of com.enonic.xp.web.WebException in project xp by enonic.

the class PageHandlerTest method getContentExistsButNeedsAuthentication.

@Test
public void getContentExistsButNeedsAuthentication() {
    final ContentPath path = ContentPath.from("/site/somepath/content");
    when(this.contentService.getByPath(path)).thenThrow(new ContentNotFoundException(path, Branch.from("draft")));
    when(this.contentService.contentExists(path)).thenReturn(true);
    this.request.setContentPath(path);
    final WebException e = assertThrows(WebException.class, () -> this.handler.handle(this.request, PortalResponse.create().build(), null));
    assertEquals(HttpStatus.UNAUTHORIZED, e.getStatus());
    assertEquals("You don't have permission to access [/site/somepath/content]", e.getMessage());
}
Also used : WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) Test(org.junit.jupiter.api.Test)

Example 7 with WebException

use of com.enonic.xp.web.WebException in project xp by enonic.

the class PageHandlerTest method getContentExistsButInsufficientRights.

@Test
public void getContentExistsButInsufficientRights() {
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(User.ANONYMOUS).build();
    final Context authenticatedContext = ContextBuilder.from(ContextAccessor.current()).authInfo(authenticationInfo).build();
    final ContentPath path = ContentPath.from("/site/somepath/content");
    when(this.contentService.getByPath(path)).thenThrow(new ContentNotFoundException(path, Branch.from("draft")));
    when(this.contentService.contentExists(path)).thenReturn(true);
    this.request.setContentPath(path);
    final WebException e = assertThrows(WebException.class, () -> authenticatedContext.callWith(() -> this.handler.handle(this.request, PortalResponse.create().build(), null)));
    assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
    assertEquals("You don't have permission to access [/site/somepath/content]", e.getMessage());
}
Also used : Context(com.enonic.xp.context.Context) WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 8 with WebException

use of com.enonic.xp.web.WebException in project xp by enonic.

the class PageHandlerTest method getContentNotFound.

@Test
public void getContentNotFound() {
    final ContentPath path = ContentPath.from("/site/somepath/content");
    when(this.contentService.getByPath(path)).thenThrow(new ContentNotFoundException(path, Branch.from("draft")));
    this.request.setContentPath(path);
    final WebException e = assertThrows(WebException.class, () -> this.handler.handle(this.request, PortalResponse.create().build(), null));
    assertEquals(HttpStatus.NOT_FOUND, e.getStatus());
    assertEquals("Page [/site/somepath/content] not found", e.getMessage());
}
Also used : WebException(com.enonic.xp.web.WebException) ContentNotFoundException(com.enonic.xp.content.ContentNotFoundException) ContentPath(com.enonic.xp.content.ContentPath) Test(org.junit.jupiter.api.Test)

Example 9 with WebException

use of com.enonic.xp.web.WebException in project xp by enonic.

the class AssetHandlerTest method testSiteResourceNotFound.

@Test
public void testSiteResourceNotFound() throws Exception {
    addResource("demo:/site/assets/css/main.css");
    final WebException ex = assertThrows(WebException.class, () -> {
        this.handler.handle(this.request, PortalResponse.create().build(), null);
    });
    assertEquals("Resource [demo:/assets/css/main.css] not found", ex.getMessage());
}
Also used : WebException(com.enonic.xp.web.WebException) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 10 with WebException

use of com.enonic.xp.web.WebException in project xp by enonic.

the class MappingHandlerTest method methodNotAllowed.

@Test
public void methodNotAllowed() {
    final PortalResponse response = PortalResponse.create().build();
    this.request.setBaseUri("/admin/site");
    this.request.setContentPath(ContentPath.from("/site/content"));
    this.request.setMethod(HttpMethod.LOCK);
    final WebException webException = assertThrows(WebException.class, () -> this.handler.handle(this.request, response, null));
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED, webException.getStatus());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebException(com.enonic.xp.web.WebException) Test(org.junit.jupiter.api.Test)

Aggregations

WebException (com.enonic.xp.web.WebException)47 Test (org.junit.jupiter.api.Test)39 PortalResponse (com.enonic.xp.portal.PortalResponse)14 ContentPath (com.enonic.xp.content.ContentPath)12 Site (com.enonic.xp.site.Site)10 PortalRequest (com.enonic.xp.portal.PortalRequest)8 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)7 ContentNotFoundException (com.enonic.xp.content.ContentNotFoundException)6 HttpStatus (com.enonic.xp.web.HttpStatus)5 MediaType (com.google.common.net.MediaType)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ApplicationKey (com.enonic.xp.app.ApplicationKey)4 Branch (com.enonic.xp.branch.Branch)4 Content (com.enonic.xp.content.Content)4 ContentId (com.enonic.xp.content.ContentId)4 ContentService (com.enonic.xp.content.ContentService)4 PropertyTree (com.enonic.xp.data.PropertyTree)4 RenderMode (com.enonic.xp.portal.RenderMode)4 ErrorHandlerScript (com.enonic.xp.portal.impl.error.ErrorHandlerScript)4 ErrorHandlerScriptFactory (com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory)4