<commit_before>/*!
 * Copyright 2002 - 2016 Webdetails, a Pentaho company. All rights reserved.
 *
 * This software was developed by Webdetails and is provided under the terms
 * of the Mozilla Public License, Version 2.0, or any later version. You may not use
 * this file except in compliance with the license. If you need a copy of the license,
 * please go to http://mozilla.org/MPL/2.0/. The Initial Developer is Webdetails.
 *
 * Software distributed under the Mozilla Public License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
 * the license for the specific language governing your rights and limitations.
 */

package org.pentaho.cdf.storage;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;

import org.junit.Assert;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.ISecurityHelper;
import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
import org.pentaho.platform.engine.security.SecurityHelper;

import com.google.gwt.editor.client.Editor.Ignore;

import pt.webdetails.cpf.messaging.MockHttpServletRequest;
import pt.webdetails.cpf.messaging.MockHttpServletResponse;
import pt.webdetails.cpf.utils.CharsetHelper;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

public class StorageApiTest {
  private static final String STORAGE_VALUE = "fake";
  private static final String USER = "fake";

  private static final JSONObject dummy = mock( JSONObject.class );

  private StorageApi storageApi;
  private MockHttpServletRequest servletRequest;
  private MockHttpServletResponse servletResponse;

  private StorageApi testee;
  StorageEngineInterface se;

  @Before
  public void setUp() throws Exception {
    storageApi = spy( new StorageApiForTests() );
    servletRequest = new MockHttpServletRequest( "/pentaho-cdf/api/storage", new HashMap<String, String[]>() );
    servletResponse = new MockHttpServletResponse( new ObjectOutputStream( new ByteArrayOutputStream() ) );
    servletResponse.setContentType( null );
    servletResponse.setCharacterEncoding( null );

    se = mock( StorageEngineInterface.class );
    when( se.delete( anyString() ) ).thenReturn( dummy );
    when( se.read( anyString() ) ).thenReturn( dummy );
    when( se.store( anyString(), anyString() ) ).thenReturn( dummy );

    StorageEngine.setMockInstance( se );
    testee = spy( new StorageApi() );
    doNothing().when( testee ).setCorsHeaders( anyObject(), anyObject() );
  }

  @After
  public void tearDown() {
    storageApi = null;
    servletRequest = null;
    servletResponse = null;
    StorageEngine.setMockInstance( null );
  }

  @Test
  @Ignore
  public void storeTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.store( STORAGE_VALUE, USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).store( STORAGE_VALUE, USER );
  }

  @Test
  @Ignore
  public void readTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.read( USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).read( USER );
  }

  @Test
  @Ignore
  public void deleteTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.delete( USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).delete( USER );
  }

  @Test
  public void testImpersonationSameUser() throws Exception {
    IPentahoSession thisUserSession = mock( IPentahoSession.class );
    when( thisUserSession.getName() ).thenReturn( "user" );<commit_msg>[CDF-896] - CDF Storage: any user is able to change the storage of another user<commit_after>/*!
 * Copyright 2002 - 2016 Webdetails, a Pentaho company. All rights reserved.
 *
 * This software was developed by Webdetails and is provided under the terms
 * of the Mozilla Public License, Version 2.0, or any later version. You may not use
 * this file except in compliance with the license. If you need a copy of the license,
 * please go to http://mozilla.org/MPL/2.0/. The Initial Developer is Webdetails.
 *
 * Software distributed under the Mozilla Public License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
 * the license for the specific language governing your rights and limitations.
 */

package org.pentaho.cdf.storage;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;

import org.junit.Assert;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.ISecurityHelper;
import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
import org.pentaho.platform.engine.security.SecurityHelper;

import pt.webdetails.cpf.messaging.MockHttpServletRequest;
import pt.webdetails.cpf.messaging.MockHttpServletResponse;
import pt.webdetails.cpf.utils.CharsetHelper;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

public class StorageApiTest {
  private static final String STORAGE_VALUE = "fake";
  private static final String USER = "fake";

  private static final JSONObject dummy = mock( JSONObject.class );

  private StorageApi storageApi;
  private MockHttpServletRequest servletRequest;
  private MockHttpServletResponse servletResponse;

  private StorageApi testee;
  StorageEngineInterface se;

  @Before
  public void setUp() throws Exception {
    storageApi = spy( new StorageApiForTests() );
    servletRequest = new MockHttpServletRequest( "/pentaho-cdf/api/storage", new HashMap<String, String[]>() );
    servletResponse = new MockHttpServletResponse( new ObjectOutputStream( new ByteArrayOutputStream() ) );
    servletResponse.setContentType( null );
    servletResponse.setCharacterEncoding( null );

    se = mock( StorageEngineInterface.class );
    when( se.delete( anyString() ) ).thenReturn( dummy );
    when( se.read( anyString() ) ).thenReturn( dummy );
    when( se.store( anyString(), anyString() ) ).thenReturn( dummy );

    StorageEngine.setMockInstance( se );
    testee = spy( new StorageApi() );
    doNothing().when( testee ).setCorsHeaders( anyObject(), anyObject() );
  }

  @After
  public void tearDown() {
    storageApi = null;
    servletRequest = null;
    servletResponse = null;
    StorageEngine.setMockInstance( null );
  }

  @Test
  public void storeTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.store( STORAGE_VALUE, USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).store( STORAGE_VALUE, USER );
  }

  @Test
  public void readTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.read( USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).read( USER );
  }

  @Test
  public void deleteTest() throws Exception {
    Assert.assertEquals( servletResponse.getContentType(), null );
    Assert.assertEquals( servletResponse.getCharacterEncoding(), null );

    storageApi.delete( USER, servletRequest, servletResponse );

    Assert.assertTrue( servletResponse.getContentType().equals( APPLICATION_JSON ) );
    Assert.assertTrue( servletResponse.getCharacterEncoding().equals( CharsetHelper.getEncoding() ) );
    verify( storageApi, times( 1 ) ).delete( USER );
  }

  @Test
  public void testImpersonationSameUser() throws Exception {
    IPentahoSession thisUserSession = mock( IPentahoSession.class );
    when( thisUserSession.getName() ).thenReturn( "user" );
{'fraction_non_alphanumeric': 0.10154241645244216, 'fraction_numerical': 0.0032628040340122602, 'mean_word_length': 4.809879379666858, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035824', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2792, 'n_words': 762}