<commit_before># coding: UTF-8

require_relative '../spec_helper'
require_relative '../../services/named-maps-api-wrapper/lib/named_maps_wrapper'

include CartoDB

# Specs for /services/named-maps-api-wrapper
# NOTES:
# - Does not check responses from the windshaft API endpoint, stubs them
# - Typhoeus allows stubbing, but no easy way of knowing how many times (if any) you called it
describe CartoDB::NamedMapsWrapper::NamedMaps do

  SPEC_NAME = 'named_maps_spec'

  before(:all) do
    CartoDB::Varnish.any_instance.stubs(:send_command).returns(true)
    # Hook new backend to Sequel current connection
    Visualization.repository = DataRepository::Backend::Sequel.new(Rails::Sequel.connection, :visualizations)

    puts "\n[rspec][#{SPEC_NAME}] Creating test user database..."
    @user = create_user( :quota_in_bytes => 524288000, :table_quota => 100 )

    puts "[rspec][#{SPEC_NAME}] Running..."
  end

  before(:each) do
    Typhoeus::Expectation.clear()
    CartoDB::Varnish.any_instance.stubs(:send_command).returns(true)
  end

  describe 'password_protected_visualization' do
    it 'tests visualization auth capabilities for password restricted type' do

      auth_token = '<KEY>'

      template_data = {
        template: {
          version: '0.0.1',
          name: '@@PLACEHOLDER@@',
          auth: {
            method: 'token',
            valid_tokens: [ auth_token ]
          },
          placeholders: {
            layer0: {
              type: "number",
              default: 1
            }
          },
          layergroup: {
            layers: [
              {
                type: "cartodb",
                options: {
                  sql: "WITH wrapped_query AS (select * from test1) SELECT * from wrapped_query where <%= layer0 %>=1",
                  layer_name: "test1",
                  cartocss: "/** */",
                  cartocss_version: "2.1.1",<commit_msg>CDB-1692 Tests stabilization<commit_after># coding: UTF-8

require_relative '../spec_helper'
require_relative '../../services/named-maps-api-wrapper/lib/named_maps_wrapper'

include CartoDB

# Specs for /services/named-maps-api-wrapper
# NOTES:
# - Does not check responses from the windshaft API endpoint, stubs them
# - Typhoeus allows stubbing, but no easy way of knowing how many times (if any) you called it
describe CartoDB::NamedMapsWrapper::NamedMaps do

  SPEC_NAME = 'named_maps_spec'

  before(:all) do
    CartoDB::Varnish.any_instance.stubs(:send_command).returns(true)
    # Hook new backend to Sequel current connection
    Visualization.repository = DataRepository::Backend::Sequel.new(Rails::Sequel.connection, :visualizations)

    puts "\n[rspec][#{SPEC_NAME}] Creating test user database..."
    @user = create_user( :quota_in_bytes => 524288000, :table_quota => 100 )

    puts "[rspec][#{SPEC_NAME}] Running..."
  end

  before(:each) do
    Typhoeus::Expectation.clear()
    CartoDB::Varnish.any_instance.stubs(:send_command).returns(true)

    Typhoeus.stub( %r{http:\/\/[a-z0-9]+\.localhost\.lan:8181\/tiles\/template\/[a-zA-Z0-9_]+\?api_key=.*},
      { method: :get}  )
            .and_return(
              Typhoeus::Response.new(code: 404, body: "")
            )
  end

  describe '#normalize_name' do
    it 'tests normalization of names' do
      name_1 = '08fee512-97cf-11e3-a775-30f9edfe5da6'
      expected_name_1 = CartoDB::NamedMapsWrapper::NamedMap::NAME_PREFIX + '08fee512_97cf_11e3_a775_30f9edfe5da6'

      name_2 = '!&32 += 01Z'
      expected_name_2 = CartoDB::NamedMapsWrapper::NamedMap::NAME_PREFIX + '3201Z'

      normalized_name_1 = CartoDB::NamedMapsWrapper::NamedMap.normalize_name(name_1)
      normalized_name_1.should eq expected_name_1

      normalized_name_2 = CartoDB::NamedMapsWrapper::NamedMap.normalize_name(name_2)
      normalized_name_2.should eq expected_name_2
    end
  end #normalize_name

  describe 'password_protected_visualization' do
    it 'tests visualization auth capabilities for password restricted type' do

      auth_token = '1<PASSWORD>'

      template_data = {
        template: {
          version: '0.0.1',
          name: '@@PLACEHOLDER@@',
          auth: {
            method: 'token',
            valid_tokens: [ auth_token ]
          },
          placeholders: {
            layer0: {
              type: "number",
              default: 1
            }
          },
          layergroup: {
            layers: [
              {
                type: "cartodb",
                options: {
                  sql: "WITH wrapped_query AS (select * from test1) SELECT * from wrapped_query where <%= layer0 %>=1",
                  layer_name: "test1",
                  cartocss: "/** */",
                  cartocss_version: "2.1.1",
{'fraction_non_alphanumeric': 0.11890179514255544, 'fraction_numerical': 0.02576557550158395, 'mean_word_length': 2.529061102831595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2738055', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1462, 'n_words': 312}