use of com.enonic.xp.service.ServiceDescriptor in project xp by enonic.
the class ServiceHandlerTest method setup.
@BeforeEach
public final void setup() throws Exception {
this.request = new PortalRequest();
final ControllerScriptFactory controllerScriptFactory = Mockito.mock(ControllerScriptFactory.class);
this.controllerScript = Mockito.mock(ControllerScript.class);
Mockito.when(controllerScriptFactory.fromDir(Mockito.any())).thenReturn(this.controllerScript);
final PortalResponse portalResponse = PortalResponse.create().build();
Mockito.when(this.controllerScript.execute(Mockito.any())).thenReturn(portalResponse);
this.resourceService = Mockito.mock(ResourceService.class);
final Resource resourceNotFound = Mockito.mock(Resource.class);
Mockito.when(resourceNotFound.exists()).thenReturn(false);
final Resource resource = Mockito.mock(Resource.class);
Mockito.when(resource.exists()).thenReturn(true);
Mockito.when(this.resourceService.getResource(ResourceKey.from("demo:/services/test"))).thenReturn(resourceNotFound);
this.serviceDescriptorService = Mockito.mock(ServiceDescriptorService.class);
final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).build();
Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
this.contentService = Mockito.mock(ContentService.class);
this.handler = new ServiceHandler();
this.handler.setControllerScriptFactory(controllerScriptFactory);
this.handler.setContentService(this.contentService);
this.handler.setResourceService(this.resourceService);
this.handler.setServiceDescriptorService(this.serviceDescriptorService);
this.request.setMethod(HttpMethod.GET);
this.request.setContentPath(ContentPath.from("/site/somepath/content"));
this.request.setEndpointPath("/_/service/demo/myservice");
this.request.setRawPath("/site/draft/site/somepath/content/_/service/demo/myservice");
}
use of com.enonic.xp.service.ServiceDescriptor in project xp by enonic.
the class ServiceHandlerTest method testForbiddenService.
@Test
public void testForbiddenService() throws Exception {
final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
final Set<PrincipalKey> allowedPrincipals = Collections.singleton(PrincipalKey.from("role:system.admin"));
final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).setAllowedPrincipals(allowedPrincipals).build();
Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
this.request.setEndpointPath("/_/service/demo/test");
boolean forbiddenErrorThrown = false;
try {
this.handler.handle(this.request, PortalResponse.create().build(), null);
} catch (WebException e) {
if (HttpStatus.UNAUTHORIZED == e.getStatus()) {
forbiddenErrorThrown = true;
}
}
assertTrue(forbiddenErrorThrown);
}
Aggregations