Search in sources :

Example 26 with IAuthorizationPolicy

use of org.pentaho.platform.api.engine.IAuthorizationPolicy in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileOrDirAsDownloadException.

@Test
public void testDoGetFileOrDirAsDownloadException() {
    // Test 1
    IAuthorizationPolicy mockAuthPolicy = mock(IAuthorizationPolicy.class);
    doReturn(false).when(mockAuthPolicy).isAllowed(anyString());
    doReturn(mockAuthPolicy).when(fileService).getPolicy();
    try {
        fileService.doGetFileOrDirAsDownload("", "mock:path:fileName", "true");
        fail();
    } catch (PentahoAccessControlException e) {
    // Expected
    } catch (Throwable t) {
        fail();
    }
    // Test 2
    doReturn(true).when(mockAuthPolicy).isAllowed(anyString());
    try {
        fileService.doGetFileOrDirAsDownload("", "", "true");
        fail();
    } catch (InvalidParameterException e) {
    // Expected
    } catch (Throwable e) {
        fail();
    }
    // Test 3
    doReturn(false).when(fileService).isPathValid(anyString());
    try {
        fileService.doGetFileOrDirAsDownload("", "mock:path:fileName", "true");
        fail();
    } catch (IllegalSelectorException e) {
    // Expected
    } catch (Throwable t) {
        fail();
    }
    /*
     * Test 4
     */
    doReturn(true).when(fileService).isPathValid(anyString());
    doReturn(null).when(fileService.repository).getFile(anyString());
    try {
        fileService.doGetFileOrDirAsDownload("", "mock:path:fileName", "true");
        fail();
    } catch (FileNotFoundException e) {
    // Expected
    } catch (Throwable t) {
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) FileNotFoundException(java.io.FileNotFoundException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) Test(org.junit.Test)

Example 27 with IAuthorizationPolicy

use of org.pentaho.platform.api.engine.IAuthorizationPolicy in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileAsInlineException.

@Test
public void testDoGetFileAsInlineException() {
    /*
     * TEST 1
     */
    doReturn(true).when(fileService).isPath(anyString());
    doReturn(false).when(fileService).isPathValid(anyString());
    try {
        fileService.doGetFileAsInline("test");
        fail();
    } catch (IllegalArgumentException e) {
    // Excpected
    } catch (FileNotFoundException e) {
        fail();
    }
    /*
     * TEST 2
     */
    doReturn(true).when(fileService).isPathValid(anyString());
    doReturn(null).when(fileService.repository).getFile(anyString());
    try {
        fileService.doGetFileAsInline("test");
        fail();
    } catch (FileNotFoundException e) {
    // Expected
    }
    /*
     * TEST 3
     */
    RepositoryFile mockFile = mock(RepositoryFile.class);
    doReturn(mockFile).when(fileService.repository).getFile(anyString());
    RepositoryDownloadWhitelist mockWhiteList = mock(RepositoryDownloadWhitelist.class);
    doReturn(mockWhiteList).when(fileService).getWhitelist();
    doReturn(false).when(mockWhiteList).accept(anyString());
    IAuthorizationPolicy mockPolicy = mock(IAuthorizationPolicy.class);
    doReturn(mockPolicy).when(fileService).getPolicy();
    doReturn(false).when(mockPolicy).isAllowed(anyString());
    try {
        fileService.doGetFileAsInline("test");
        fail();
    } catch (IllegalArgumentException e) {
    // Excpected
    } catch (FileNotFoundException e) {
        fail();
    }
    /*
     * TEST 4
     */
    doReturn(true).when(mockWhiteList).accept(anyString());
    doThrow(new InternalError()).when(fileService.repository).getDataForRead(any(Serializable.class), any(Class.class));
    try {
        fileService.doGetFileAsInline("test");
        fail();
    } catch (InternalError e) {
    // Excpected
    } catch (FileNotFoundException e) {
        fail();
    }
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Serializable(java.io.Serializable) RepositoryDownloadWhitelist(org.pentaho.platform.repository.RepositoryDownloadWhitelist) FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Test(org.junit.Test)

Example 28 with IAuthorizationPolicy

use of org.pentaho.platform.api.engine.IAuthorizationPolicy in project pentaho-platform by pentaho.

the class FileServiceTest method assertDoGetFileOrDirAsDownload.

/**
 * @param fileName
 * @param withManifest
 * @param expectedEncodedFileName
 * @param expectedFileName
 * @throws Throwable
 */
public void assertDoGetFileOrDirAsDownload(final String fileName, final String withManifest, final String expectedEncodedFileName, final String expectedFileName) throws Throwable {
    IAuthorizationPolicy mockAuthPolicy = mock(IAuthorizationPolicy.class);
    when(mockAuthPolicy.isAllowed(anyString())).thenReturn(true);
    BaseExportProcessor mockExportProcessor = mock(BaseExportProcessor.class);
    File mockExportFile = mock(File.class);
    ExportHandler mockExportHandler = mock(ExportHandler.class);
    StreamingOutput mockStream = mock(StreamingOutput.class);
    RepositoryFile mockRepoFile = mock(RepositoryFile.class);
    doReturn(fileName).when(mockRepoFile).getName();
    doReturn(mockExportFile).when(mockExportProcessor).performExport(mockRepoFile);
    doReturn(mockRepoFile).when(fileService.repository).getFile(anyString());
    doReturn(mockAuthPolicy).when(fileService).getPolicy();
    doReturn(mockExportProcessor).when(fileService).getDownloadExportProcessor(anyString(), anyBoolean(), anyBoolean());
    doReturn(mockExportHandler).when(fileService).getDownloadExportHandler();
    doReturn(mockStream).when(fileService).getDownloadStream(mockRepoFile, mockExportProcessor);
    FileService.DownloadFileWrapper wrapper = fileService.doGetFileOrDirAsDownload("", "mock:path:" + fileName, withManifest);
    verify(fileService.repository, times(1)).getFile(anyString());
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(mockAuthPolicy, times(3)).isAllowed(captor.capture());
    assertTrue(captor.getAllValues().contains(RepositoryReadAction.NAME));
    assertTrue(captor.getAllValues().contains(RepositoryCreateAction.NAME));
    assertEquals(mockStream, wrapper.getOutputStream());
    assertEquals(expectedEncodedFileName, wrapper.getEncodedFileName());
    assertEquals("attachment; filename=\"" + expectedFileName + "\"", wrapper.getAttachment());
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) ExportHandler(org.pentaho.platform.plugin.services.importexport.ExportHandler) StreamingOutput(javax.ws.rs.core.StreamingOutput) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) BaseExportProcessor(org.pentaho.platform.plugin.services.importexport.BaseExportProcessor)

Example 29 with IAuthorizationPolicy

use of org.pentaho.platform.api.engine.IAuthorizationPolicy in project pentaho-platform by pentaho.

the class FileServiceTest method testDoGetFileOrDirAsDownloadNonAdminUserHomeFolder.

@Test
public void testDoGetFileOrDirAsDownloadNonAdminUserHomeFolder() throws Throwable {
    IAuthorizationPolicy mockAuthPolicy = mock(IAuthorizationPolicy.class);
    doReturn(true).when(mockAuthPolicy).isAllowed(RepositoryReadAction.NAME);
    /* user has 'Read Content' */
    doReturn(true).when(mockAuthPolicy).isAllowed(RepositoryCreateAction.NAME);
    /* user has 'Create Content' */
    /* non-admin user */
    doReturn(false).when(mockAuthPolicy).isAllowed(AdministerSecurityAction.NAME);
    doReturn(mockAuthPolicy).when(fileService).getPolicy();
    // Test 1: in the home-folder
    try {
        fileService.doGetFileOrDirAsDownload("", "home:testUser:test_file", "true");
        fail();
    } catch (FileNotFoundException ex) {
    /* expected; this is a mock test, we don't actually have a 'test_file' to download :) */
    } catch (Throwable t) {
        fail();
    }
    // Test 2: in some home-folder sub-folders
    try {
        fileService.doGetFileOrDirAsDownload("", "home:testUser:subFolder1:subFolder2:test_file", "true");
        fail();
    } catch (FileNotFoundException ex) {
    /* expected; this is a mock test, we don't actually have a 'test_file' to download :) */
    } catch (Throwable t) {
        fail();
    }
    // Test 3: while still being on the user's home folder, user loses 'Read Content' permission
    try {
        doReturn(false).when(mockAuthPolicy).isAllowed(RepositoryReadAction.NAME);
        fileService.doGetFileOrDirAsDownload("", "home:testUser:test_file", "true");
        fail();
    } catch (PentahoAccessControlException e) {
    /* expected */
    } catch (Throwable t) {
        fail();
    } finally {
        doReturn(true).when(mockAuthPolicy).isAllowed(RepositoryReadAction.NAME);
    }
    // Test 4: while still being on the user's home folder, user loses 'Create Content' permission
    try {
        doReturn(false).when(mockAuthPolicy).isAllowed(RepositoryCreateAction.NAME);
        fileService.doGetFileOrDirAsDownload("", "home:testUser:test_file", "true");
        fail();
    } catch (PentahoAccessControlException e) {
    /* expected */
    } catch (Throwable t) {
        fail();
    } finally {
        doReturn(true).when(mockAuthPolicy).isAllowed(RepositoryCreateAction.NAME);
    }
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) FileNotFoundException(java.io.FileNotFoundException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Test(org.junit.Test)

Example 30 with IAuthorizationPolicy

use of org.pentaho.platform.api.engine.IAuthorizationPolicy in project pentaho-platform by pentaho.

the class PasswordResourceTest method testMustHaveAdminAccess.

@Test
public void testMustHaveAdminAccess() throws Exception {
    final IAuthorizationPolicy policy = Mockito.mock(IAuthorizationPolicy.class);
    PasswordResource resource = getPasswordResource(policy);
    Mockito.when(policy.isAllowed(AdministerSecurityAction.NAME)).thenReturn(false);
    Response response = resource.encryptPassword("password");
    Assert.assertEquals(401, response.getStatus());
    Assert.assertEquals(401, resource.encryptionForm().getStatus());
}
Also used : Response(javax.ws.rs.core.Response) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Test(org.junit.Test)

Aggregations

IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)40 Test (org.junit.Test)18 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)11 MicroPlatform (org.pentaho.test.platform.engine.core.MicroPlatform)7 Serializable (java.io.Serializable)6 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)6 File (java.io.File)5 Before (org.junit.Before)5 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)5 FileNotFoundException (java.io.FileNotFoundException)4 HashMap (java.util.HashMap)4 IPluginResourceLoader (org.pentaho.platform.api.engine.IPluginResourceLoader)4 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)4 PluginClassLoader (org.pentaho.platform.plugin.services.pluginmgr.PluginClassLoader)4 PluginResourceLoader (org.pentaho.platform.plugin.services.pluginmgr.PluginResourceLoader)4 InputStream (java.io.InputStream)3 Matchers.anyString (org.mockito.Matchers.anyString)3 SystemSettings (org.pentaho.platform.engine.core.system.SystemSettings)3 MockSecurityHelper (org.pentaho.test.platform.engine.security.MockSecurityHelper)3 OutputStream (java.io.OutputStream)2