Search in sources :

Example 1 with IAuthorizationPolicy

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

the class UserSettingServiceTest method testDeleteUserSettingsByName.

@Test
public void testDeleteUserSettingsByName() throws Exception {
    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);
    final RepositoryFile repositoryFile = mock(RepositoryFile.class);
    when(repositoryFile.getId()).thenReturn(USER_FOLDER_ID);
    when(repository.getFile(anyString())).thenReturn(repositoryFile);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final Map<String, Serializable> settings = (Map<String, Serializable>) invocation.getArguments()[1];
            assertNotNull(settings);
            assertEquals(2, settings.size());
            final Iterator<String> iterator = settings.keySet().iterator();
            assertFalse(iterator.next().startsWith(UserSettingService.SETTING_PREFIX));
            assertFalse(iterator.next().startsWith(UserSettingService.SETTING_PREFIX));
            return null;
        }
    }).when(repository).setFileMetadata(eq(USER_FOLDER_ID), anyMap());
    userSettingService.deleteUserSettings("test");
}
Also used : Answer(org.mockito.stubbing.Answer) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Serializable(java.io.Serializable) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Iterator(java.util.Iterator) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with IAuthorizationPolicy

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

the class UserSettingServiceTest method testGetUserSettingByName.

@Test
public void testGetUserSettingByName() throws Exception {
    final String settingName = USER_SETTING_NAME_3;
    final String defaultValue = "defaultValue";
    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);
    // try to get existing setting
    final IUserSetting userSetting = userSettingService.getUserSetting("test", settingName, defaultValue);
    assertEquals(settingName, userSetting.getSettingName());
    assertEquals(USER_SETTING_VALUE_3, userSetting.getSettingValue());
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) Test(org.junit.Test)

Example 3 with IAuthorizationPolicy

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

the class FileServiceTest method testDoCanAdministerException.

@Test
public void testDoCanAdministerException() throws Exception {
    IAuthorizationPolicy authorizationPolicy = mock(IAuthorizationPolicy.class);
    doReturn(authorizationPolicy).when(fileService).getPolicy();
    doThrow(new InternalError()).when(authorizationPolicy).isAllowed(RepositoryReadAction.NAME);
    doReturn(true).when(authorizationPolicy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(false).when(authorizationPolicy).isAllowed(AdministerSecurityAction.NAME);
    try {
        assertFalse(fileService.doCanAdminister());
        fail();
    } catch (InternalError e) {
    }
    doReturn(false).when(authorizationPolicy).isAllowed(RepositoryReadAction.NAME);
    doThrow(new InternalError()).when(authorizationPolicy).isAllowed(RepositoryCreateAction.NAME);
    doReturn(true).when(authorizationPolicy).isAllowed(AdministerSecurityAction.NAME);
    try {
        assertFalse(fileService.doCanAdminister());
    } catch (InternalError e) {
        // the first comparison fail and the result should be false and no exception returned
        fail();
    }
    doReturn(true).when(authorizationPolicy).isAllowed(RepositoryReadAction.NAME);
    doReturn(false).when(authorizationPolicy).isAllowed(RepositoryCreateAction.NAME);
    doThrow(new InternalError()).when(authorizationPolicy).isAllowed(AdministerSecurityAction.NAME);
    try {
        assertFalse(fileService.doCanAdminister());
    } catch (InternalError e) {
        // the second comparison fail and the result should be false and no exception returned
        fail();
    }
}
Also used : IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Test(org.junit.Test)

Example 4 with IAuthorizationPolicy

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

the class PasswordResourceTest method testEncodePassword.

@Test
public void testEncodePassword() throws Exception {
    final IAuthorizationPolicy policy = Mockito.mock(IAuthorizationPolicy.class);
    PasswordResource resource = getPasswordResource(policy);
    Mockito.when(policy.isAllowed(AdministerSecurityAction.NAME)).thenReturn(true);
    Response response = resource.encryptPassword("password");
    Assert.assertTrue(response.getEntity().toString().contains("ENC:Encrypted 2be98afc86aa7f2e4bb18bd63c99dbdde"));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(200, resource.encryptionForm().getStatus());
}
Also used : Response(javax.ws.rs.core.Response) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) Test(org.junit.Test)

Example 5 with IAuthorizationPolicy

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

the class ActionRunnerTest method testCallWithStreamProvider.

@Test
public void testCallWithStreamProvider() throws Exception {
    Map<String, Serializable> paramsMap = createMapWithUserLocale();
    IAction actionBeanSpy = Mockito.spy(new TestAction());
    IBackgroundExecutionStreamProvider mockStreamProvider = Mockito.mock(IBackgroundExecutionStreamProvider.class);
    InputStream mockInputStream = Mockito.mock(InputStream.class);
    OutputStream mockOutputStream = Mockito.mock(OutputStream.class);
    when(mockStreamProvider.getInputStream()).thenReturn(mockInputStream);
    String mockOutputPath = "/someUser/someOutput";
    when(mockStreamProvider.getOutputPath()).thenReturn(mockOutputPath);
    when(mockStreamProvider.getOutputStream()).thenReturn(mockOutputStream);
    ISecurityHelper mockSecurityHelper = Mockito.mock(ISecurityHelper.class);
    SecurityHelper.setMockInstance(mockSecurityHelper);
    when(mockSecurityHelper.runAsUser(Mockito.anyString(), Mockito.any())).thenReturn(mockOutputPath);
    PowerMockito.mockStatic(PentahoSystem.class);
    IUnifiedRepository mockRepository = Mockito.mock(IUnifiedRepository.class);
    when(PentahoSystem.get(isA(IUnifiedRepository.class.getClass()), Mockito.any())).thenReturn(mockRepository);
    IAuthorizationPolicy mockAuthorizationPolicy = Mockito.mock(IAuthorizationPolicy.class);
    when(PentahoSystem.get(isA(IAuthorizationPolicy.class.getClass()), Mockito.any())).thenReturn(mockAuthorizationPolicy);
    when(mockAuthorizationPolicy.isAllowed(SchedulerOutputPathResolver.SCHEDULER_ACTION_NAME)).thenReturn(true);
    String repoId = "SOME_REPO_ID";
    Map<String, Serializable> dummyMetaData = new HashMap<>();
    dummyMetaData.put(RepositoryFile.SCHEDULABLE_KEY, true);
    when(mockRepository.getFileMetadata(repoId)).thenReturn(dummyMetaData);
    RepositoryFile mockRepoFile = Mockito.mock(RepositoryFile.class);
    when(mockRepoFile.isFolder()).thenReturn(true);
    when(mockRepoFile.getId()).thenReturn(repoId);
    ActionRunner actionRunner = new ActionRunner(actionBeanSpy, "actionUser", paramsMap, mockStreamProvider);
    actionRunner.call();
    Mockito.verify(actionBeanSpy).execute();
}
Also used : IBackgroundExecutionStreamProvider(org.pentaho.platform.api.scheduler2.IBackgroundExecutionStreamProvider) Serializable(java.io.Serializable) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) IAction(org.pentaho.platform.api.action.IAction) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TestAction(org.pentaho.platform.util.bean.TestAction) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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