<commit_before># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '../../../factories/users_helper'
require_relative '../../api/json/visualizations_controller_shared_examples'
require_relative '../../../../app/controllers/carto/api/visualizations_controller'
require_relative '../../../factories/visualization_creation_helpers'

# TODO: Remove once Carto::Visualization is complete enough
require_relative '../../../../app/models/visualization/member'
require_relative '../../../../app/helpers/bounding_box_helper'

describe Carto::Api::VisualizationsController do
  it_behaves_like 'visualization controllers' do
  end

  TEST_UUID = '00000000-0000-0000-0000-000000000000'

  DATE_ATTRIBUTES = %w{ created_at updated_at }
  NORMALIZED_ASSOCIATION_ATTRIBUTES = {
    attributes: DATE_ATTRIBUTES,
    associations: {
      'permission' => {
        attributes: DATE_ATTRIBUTES,
        associations: {}
      },
      'table' => {
        attributes: DATE_ATTRIBUTES,
        associations: {}
      }
    }
  }

  NEW_ATTRIBUTES = {
    attributes: [],
    associations: {
      'table' => {
        attributes: [],
        associations: {
          'permission' => {
            attributes: [],
            associations: {
              'owner' => {
                attributes: [ 'email', 'quota_in_bytes', 'db_size_in_bytes', 'public_visualization_count',
                'all_visualization_count', 'table_count' ],
                associations: {}
              }
            }
          }
        }
      },
      'permission' => {
        attributes: [],
        associations: {
          'owner' => {
            attributes: [ 'email', 'quota_in_bytes', 'db_size_in_bytes', 'public_visualization_count',
            'all_visualization_count', 'table_count' ],
            associations: {}
          }
        }
      }
    }
  }

  BBOX_GEOM = '{"type":"MultiPolygon","coordinates":[[[[-75.234375,54.57206166],[4.921875,54.36775852],[7.03125,-0.35156029],[-71.71875,1.75753681],[-75.234375,54.57206166]]]]}'
  OUTSIDE_BBOX_GEOM = '{"type":"MultiPolygon","coordinates":[[[[-149.4140625,79.74993208],[-139.921875,79.74993208],[-136.0546875,78.13449318],[-148.7109375,78.06198919],[-149.4140625,79.74993208]]]]}'

  describe 'static_map' do
    include_context 'visualization creation helpers'
    include_context 'users helper'

    before(:each) do
      CartoDB::NamedMapsWrapper::NamedMaps.any_instance.stubs(:get => nil, :create => true, :update => true)

      @user_1 = FactoryGirl.create(:valid_user, private_tables_enabled: false)
      @user_2 = FactoryGirl.create(:valid_user)

      @headers = {'CONTENT_TYPE'  => 'application/json'}
      host! "#{@user_1.subdomain}.localhost.lan"
    end

    after(:each) do
      @user_1.destroy
      @user_2.destroy
    end

    it 'tests with non-existing cdn config, which uses maps_api_template url' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                .stubs(:get_cdn_config)
                                .returns(nil)
      ApplicationHelper.stubs(:maps_api_template)
                       .returns("http://#{@user_1.username}.localhost.lan:8181")

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               id: table1.table_visualization.id,
                                               width: width,
                                               height: height),
          @headers
      last_response.status.should == 302

      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(table1.table_visualization.id)
      last_response.location.should == "http://#{@user_1.username}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests with existing cdn config' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                .stubs(:get_cdn_config)
                                .returns("http" => "cdn.local.lan")

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               id: table1.table_visualization.id,
                                               width: width,
                                               height: height),
          @headers
      last_response.status.should == 302

      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(table1.table_visualization.id)
      last_response.location.should == "http://cdn.local.lan/#{@user_1.username}/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests privacy of static_maps calls' do
      # As privacy is equal to other visualizations controller methods, no need to test every option, just generally

      width = 123
      height = 456

      public_table = create_random_table(@user_1)

      # By default no private tables so all are created public
      @user_1.private_tables_enabled = true
      @user_1.save

      private_table = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                     .stubs(:get_cdn_config)
                                     .returns(nil)
      ApplicationHelper.stubs(:maps_api_template)
                       .returns("http://#{@user_1.username}.localhost.lan:8181")

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               id: public_table.table_visualization.id,
                                               width: width,
                                               height: height),
          @headers
      last_response.status.should == 302
      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(public_table.table_visualization.id)
      last_response.location.should == "http://#{@user_1.username}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               id: private_table.table_visualization.id,
                                               width: width,
                                               height: height),
          @headers
      last_response.status.should == 403

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               api_key: @user_1.api_key,
                                               id: private_table.table_visualization.id,
                                               width: width,
                                               height: height),
        @headers
      last_response.status.should == 302
      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(private_table.table_visualization.id)
      last_response.location.should == "http://#{@user_1.username}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests varnish keys' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                     .stubs(:get_cdn_config)
                                     .returns("http" => "cdn.local.lan")

      get api_v2_visualizations_static_map_url(user_domain: @user_1.username,
                                               id: table1.table_visualization.id,
                                               width: width,
                                               height: height),
          @headers<commit_msg>Undo Hound refactor for spec/requests/carto/api/visualizations_controller_spec.rb<commit_after># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '../../../factories/users_helper'
require_relative '../../api/json/visualizations_controller_shared_examples'
require_relative '../../../../app/controllers/carto/api/visualizations_controller'
require_relative '../../../factories/visualization_creation_helpers'

# TODO: Remove once Carto::Visualization is complete enough
require_relative '../../../../app/models/visualization/member'
require_relative '../../../../app/helpers/bounding_box_helper'

describe Carto::Api::VisualizationsController do
  it_behaves_like 'visualization controllers' do
  end

  TEST_UUID = '00000000-0000-0000-0000-000000000000'

  DATE_ATTRIBUTES = %w{ created_at updated_at }
  NORMALIZED_ASSOCIATION_ATTRIBUTES = {
    attributes: DATE_ATTRIBUTES,
    associations: {
      'permission' => {
        attributes: DATE_ATTRIBUTES,
        associations: {}
      },
      'table' => {
        attributes: DATE_ATTRIBUTES,
        associations: {}
      }
    }
  }

  NEW_ATTRIBUTES = {
    attributes: [],
    associations: {
      'table' => {
        attributes: [],
        associations: {
          'permission' => {
            attributes: [],
            associations: {
              'owner' => {
                attributes: [ 'email', 'quota_in_bytes', 'db_size_in_bytes', 'public_visualization_count',
                'all_visualization_count', 'table_count' ],
                associations: {}
              }
            }
          }
        }
      },
      'permission' => {
        attributes: [],
        associations: {
          'owner' => {
            attributes: [ 'email', 'quota_in_bytes', 'db_size_in_bytes', 'public_visualization_count',
            'all_visualization_count', 'table_count' ],
            associations: {}
          }
        }
      }
    }
  }

  BBOX_GEOM = '{"type":"MultiPolygon","coordinates":[[[[-75.234375,54.57206166],[4.921875,54.36775852],[7.03125,-0.35156029],[-71.71875,1.75753681],[-75.234375,54.57206166]]]]}'
  OUTSIDE_BBOX_GEOM = '{"type":"MultiPolygon","coordinates":[[[[-149.4140625,79.74993208],[-139.921875,79.74993208],[-136.0546875,78.13449318],[-148.7109375,78.06198919],[-149.4140625,79.74993208]]]]}'

  describe 'static_map' do
    include_context 'visualization creation helpers'
    include_context 'users helper'

    before(:each) do
      CartoDB::NamedMapsWrapper::NamedMaps.any_instance.stubs(:get => nil, :create => true, :update => true)

      @user_1 = FactoryGirl.create(:valid_user, private_tables_enabled: false)
      @user_2 = FactoryGirl.create(:valid_user)

      @headers = {'CONTENT_TYPE'  => 'application/json'}
      host! "#{@user_1.subdomain}.localhost.lan"
    end

    after(:each) do
      @user_1.destroy
      @user_2.destroy
    end

    it 'tests with non-existing cdn config, which uses maps_api_template url' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                .stubs(:get_cdn_config)
                                .returns(nil)
      ApplicationHelper.stubs(:maps_api_template)
                       .returns("http://#{@user_1.username}.localhost.lan:8181")

      get api_v2_visualizations_static_map_url({
          user_domain: @user_1.username,
          id: table1.table_visualization.id,
          width: width,
          height: height
        }),
        @headers
      last_response.status.should == 302

      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(table1.table_visualization.id)
      last_response.location.should == "http://#{@user_1.<EMAIL>}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests with existing cdn config' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                .stubs(:get_cdn_config)
                                .returns("http" => "cdn.local.lan")

      get api_v2_visualizations_static_map_url(
          user_domain: @user_1.username,
          id: table1.table_visualization.id,
          width: width,
          height: height
        ),
        @headers
      last_response.status.should == 302

      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(table1.table_visualization.id)
      last_response.location.should == "http://cdn.local.lan/#{@user_1.username}/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests privacy of static_maps calls' do
      # As privacy is equal to other visualizations controller methods, no need to test every option, just generally

      width = 123
      height = 456

      public_table = create_random_table(@user_1)

      # By default no private tables so all are created public
      @user_1.private_tables_enabled = true
      @user_1.save

      private_table = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                     .stubs(:get_cdn_config)
                                     .returns(nil)
      ApplicationHelper.stubs(:maps_api_template)
                       .returns("http://#{@user_1.username}.localhost.lan:8181")

      get api_v2_visualizations_static_map_url({
          user_domain: @user_1.username,
          id: public_table.table_visualization.id,
          width: width,
          height: height
        }),
        @headers
      last_response.status.should == 302
      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(public_table.table_visualization.id)
      last_response.location.should == "http://#{@user_<EMAIL>}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"

      get api_v2_visualizations_static_map_url({
          user_domain: @user_1.username,
          id: private_table.table_visualization.id,
          width: width,
          height: height
        }),
        @headers
      last_response.status.should == 403

      get api_v2_visualizations_static_map_url({
          user_domain: @user_1.username,
          api_key: @user_1.api_key,
          id: private_table.table_visualization.id,
          width: width,
          height: height
        }),
        @headers
      last_response.status.should == 302
      tpl_id = CartoDB::NamedMapsWrapper::NamedMap.template_name(private_table.table_visualization.id)
      last_response.location.should == "http://#{@user_1.username}.localhost.lan:8181/api/v1/map/static/named/#{tpl_id}/#{width}/#{height}.png"
    end

    it 'tests varnish keys' do
      width = 123
      height = 456

      table1 = create_random_table(@user_1)

      Carto::StaticMapsURLHelper.any_instance
                                     .stubs(:get_cdn_config)
                                     .returns("http" => "cdn.local.lan")

      get api_v2_visualizations_static_map_url({
          user_domain: @user_1.username,
          #api_key: @user_1.api_key,
          id: table1.table_visualization.id,
          width: width,
          height: height
        }),
        @headers
{'fraction_non_alphanumeric': 0.11936392914653784, 'fraction_numerical': 0.0426731078904992, 'mean_word_length': 2.1019771071800206, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '528641', 'n_tokens_mistral': 4795, 'n_tokens_neox': 4325, 'n_words': 639}