Search in sources :

Example 36 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class EndpointAccessCheckerTest method should_notShowHelpfulMessage_When_accessDeniedInProductionMode.

@Test
public void should_notShowHelpfulMessage_When_accessDeniedInProductionMode() throws Exception {
    VaadinService mockService = Mockito.mock(VaadinService.class);
    DeploymentConfiguration mockDeploymentConfiguration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(mockService.getDeploymentConfiguration()).thenReturn(mockDeploymentConfiguration);
    Mockito.when(mockDeploymentConfiguration.isProductionMode()).thenReturn(true);
    CurrentInstance.set(VaadinService.class, mockService);
    try {
        class Test {

            public void test() {
            }
        }
        Method method = Test.class.getMethod("test");
        String accessDeniedMessage = checker.check(method, requestMock);
        assertEquals(EndpointAccessChecker.ACCESS_DENIED_MSG, accessDeniedMessage);
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) Method(java.lang.reflect.Method) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 37 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class EndpointAccessCheckerTest method should_showHelpfulMessage_When_accessDeniedInDevMode.

@Test
public void should_showHelpfulMessage_When_accessDeniedInDevMode() throws Exception {
    VaadinService mockService = Mockito.mock(VaadinService.class);
    DeploymentConfiguration mockDeploymentConfiguration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(mockService.getDeploymentConfiguration()).thenReturn(mockDeploymentConfiguration);
    Mockito.when(mockDeploymentConfiguration.isProductionMode()).thenReturn(false);
    CurrentInstance.set(VaadinService.class, mockService);
    try {
        class Test {

            public void test() {
            }
        }
        Method method = Test.class.getMethod("test");
        String accessDeniedMessage = checker.check(method, requestMock);
        assertEquals(EndpointAccessChecker.ACCESS_DENIED_MSG_DEV_MODE, accessDeniedMessage);
        assertTrue(accessDeniedMessage.contains(PermitAll.class.getSimpleName()));
        assertTrue(accessDeniedMessage.contains(RolesAllowed.class.getSimpleName()));
        assertTrue(accessDeniedMessage.contains(AnonymousAllowed.class.getSimpleName()));
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) Method(java.lang.reflect.Method) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 38 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class LitTemplateParserImpl method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends LitTemplate> clazz, String tag, VaadinService service) {
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, JsModule.class).stream().map(jsModule -> new Dependency(Dependency.Type.JS_MODULE, jsModule.value(), // load mode doesn't
    LoadMode.EAGER)).collect(Collectors.toList());
    for (DependencyFilter filter : service.getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), service);
    }
    Pair<Dependency, String> chosenDep = null;
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Dependency.Type.JS_MODULE) {
            continue;
        }
        String url = dependency.getUrl();
        String source = getSourcesFromTemplate(service, tag, url);
        if (source == null) {
            continue;
        }
        if (chosenDep == null) {
            chosenDep = new Pair<>(dependency, source);
        }
        if (dependencyHasTagName(dependency, tag)) {
            chosenDep = new Pair<>(dependency, source);
            break;
        }
    }
    Element templateElement = null;
    if (chosenDep != null) {
        templateElement = BundleLitParser.parseLitTemplateElement(chosenDep.getFirst().getUrl(), chosenDep.getSecond());
    }
    if (templateElement != null) {
        // Template needs to be wrapped in an element with id, to look
        // like a P2 template
        Element parent = new Element(tag);
        parent.attr("id", tag);
        templateElement.appendTo(parent);
        return new TemplateData(chosenDep.getFirst().getUrl(), templateElement);
    }
    getLogger().info("Couldn't find the " + "definition of the element with tag '{}' " + "in any lit template file declared using '@{}' annotations. " + "In a Spring Boot project, please ensure that the template's " + "groupId is added to the vaadin.whitelisted-packages " + "property. Otherwise, please Check the availability of the " + "template files in your WAR file or provide alternative " + "implementation of the method " + "LitTemplateParser.getTemplateContent() which should return " + "an element representing the content of the template file", tag, JsModule.class.getSimpleName());
    return null;
}
Also used : LitTemplateParser(com.vaadin.flow.component.littemplate.LitTemplateParser) URL(java.net.URL) BundleLitParser(com.vaadin.flow.component.littemplate.BundleLitParser) LoggerFactory(org.slf4j.LoggerFactory) Dependency(com.vaadin.flow.shared.ui.Dependency) FrontendUtils(com.vaadin.flow.server.frontend.FrontendUtils) ArrayList(java.util.ArrayList) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Lookup(com.vaadin.flow.di.Lookup) Constants(com.vaadin.flow.server.Constants) Logger(org.slf4j.Logger) Pair(com.vaadin.flow.internal.Pair) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) IOException(java.io.IOException) ResourceProvider(com.vaadin.flow.di.ResourceProvider) LitTemplate(com.vaadin.flow.component.littemplate.LitTemplate) Collectors(java.util.stream.Collectors) List(java.util.List) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) JsModule(com.vaadin.flow.component.dependency.JsModule) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) JsModule(com.vaadin.flow.component.dependency.JsModule) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 39 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class CurrentInstance method setCurrent.

/**
 * Sets current instances for the {@link VaadinSession} and all related
 * classes. The previously defined values can be restored by passing the
 * returned map to {@link #restoreInstances(Map)}.
 *
 * @param session
 *            The VaadinSession
 * @return A map containing the old values of the instances this method
 *         updated.
 */
public static Map<Class<?>, CurrentInstance> setCurrent(VaadinSession session) {
    Map<Class<?>, CurrentInstance> old = new HashMap<>();
    old.put(VaadinSession.class, doSet(VaadinSession.class, session));
    VaadinService service = null;
    if (session != null) {
        service = session.getService();
    }
    old.put(VaadinService.class, doSet(VaadinService.class, service));
    return old;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) HashMap(java.util.HashMap) VaadinService(com.vaadin.flow.server.VaadinService)

Example 40 with VaadinService

use of com.vaadin.flow.server.VaadinService in project flow by vaadin.

the class RouteConfigurationTest method init.

@Before
public void init() {
    servletContext = new MockServletContext();
    vaadinContext = new MockVaadinContext(servletContext);
    registry = ApplicationRouteRegistry.getInstance(vaadinContext);
    vaadinService = Mockito.mock(MockService.class);
    Mockito.when(vaadinService.getRouteRegistry()).thenReturn(registry);
    Mockito.when(vaadinService.getContext()).thenReturn(vaadinContext);
    VaadinService.setCurrent(vaadinService);
    session = new MockVaadinSession(vaadinService) {

        @Override
        public VaadinService getService() {
            return vaadinService;
        }
    };
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) MockServletContext(com.vaadin.flow.server.MockServletContext) Before(org.junit.Before)

Aggregations

VaadinService (com.vaadin.flow.server.VaadinService)86 Test (org.junit.Test)39 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)19 VaadinSession (com.vaadin.flow.server.VaadinSession)18 Properties (java.util.Properties)15 VaadinContext (com.vaadin.flow.server.VaadinContext)12 Before (org.junit.Before)11 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)10 UI (com.vaadin.flow.component.UI)9 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)8 Lookup (com.vaadin.flow.di.Lookup)7 VaadinResponse (com.vaadin.flow.server.VaadinResponse)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ServletContext (javax.servlet.ServletContext)6 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 Instantiator (com.vaadin.flow.di.Instantiator)4 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)4 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4