<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'

  def tiler_port
    Cartodb.config[:tiler]['internal']['port']
  end

  def public_tiler_port
    Cartodb.config[:tiler]['public']['port']
  end

  def tiler_regex
    if (tiler_port == '8888')
      %r{http:\/\/127\.0\.0\.1:8888\/api\/v1\/map\/named\/[a-zA-Z0-9_]+\?api_key=.*}
    else
      %r{http:\/\/127\.0\.0\.1:8181\/api\/v1\/map\/named\/[a-zA-Z0-9_]+\?api_key=.*}
    end
  end

  def named_maps_url(user_api_key)
    "http://127.0.0.1:#{tiler_port}/api/v1/map/named?api_key=#{user_api_key}"
  end

  def named_map_url(template_id, user_api_key)
    "http://127.0.0.1:#{tiler_port}/api/v1/map/named/#{template_id}?api_key=#{user_api_key}"
  end

  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, :private_tables_enabled => true )

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

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

    Typhoeus.stub( tiler_regex,
      { method: :get}  )
            .and_return(
              Typhoeus::Response.new(code: 404, body: '')
            )

    # For relator->permission
    user_id = UUIDTools::UUID.timestamp_create.to_s
    user_name = 'whatever'
    user_apikey = '123'
    CartoDB::Visualization::Relator.any_instance.stubs(:user).returns(@user)
  end

  after(:all) do
    CartoDB::Visualization::Member.any_instance.stubs(:has_named_map?).returns(false)
    @user.destroy
  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'
<commit_msg>Stub Typhoeus::Response total_time for named maps<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'

  def tiler_port
    Cartodb.config[:tiler]['internal']['port']
  end

  def public_tiler_port
    Cartodb.config[:tiler]['public']['port']
  end

  def tiler_regex
    if (tiler_port == '8888')
      %r{http:\/\/127\.0\.0\.1:8888\/api\/v1\/map\/named\/[a-zA-Z0-9_]+\?api_key=.*}
    else
      %r{http:\/\/127\.0\.0\.1:8181\/api\/v1\/map\/named\/[a-zA-Z0-9_]+\?api_key=.*}
    end
  end

  def named_maps_url(user_api_key)
    "http://127.0.0.1:#{tiler_port}/api/v1/map/named?api_key=#{user_api_key}"
  end

  def named_map_url(template_id, user_api_key)
    "http://127.0.0.1:#{tiler_port}/api/v1/map/named/#{template_id}?api_key=#{user_api_key}"
  end

  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, :private_tables_enabled => true )

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

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

    Typhoeus.stub( tiler_regex,
      { method: :get}  )
            .and_return(
              Typhoeus::Response.new(code: 404, body: '')
            )
    
    Typhoeus::Response.any_instance.stubs(:total_time).returns(1)

    # For relator->permission
    user_id = UUIDTools::UUID.timestamp_create.to_s
    user_name = 'whatever'
    user_apikey = '123'
    CartoDB::Visualization::Relator.any_instance.stubs(:user).returns(@user)
  end

  after(:all) do
    CartoDB::Visualization::Member.any_instance.stubs(:has_named_map?).returns(false)
    @user.destroy
  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'

{'fraction_non_alphanumeric': 0.14977277217941118, 'fraction_numerical': 0.040110650069156296, 'mean_word_length': 3.8072174738841404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4914311', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1861, 'n_words': 296}