<commit_before>require 'spec_helper_min'
require 'support/helpers'
require 'factories/carto_visualizations'
require 'base64'

describe Carto::Api::ApiKeysController do
  include_context 'users helper'
  include HelperMethods

  def response_grants_should_include_request_permissions(reponse_grants, table_permissions)
    table_permissions.each do |stp|
      response_tables = reponse_grants.find { |grant| grant['type'] == 'database'}['tables']
      response_permissions_for_table =
        response_tables.find { |rtp| rtp['schema'] == stp['schema'] && rtp['name'] == stp['name'] }['permissions']
      response_permissions_for_table.sort.should eq stp['permissions'].sort
    end
  end

  before(:all) do
    @auth_api_feature_flag = FactoryGirl.create(:feature_flag, name: 'auth_api', restricted: false)
  end

  after(:all) do
    @auth_api_feature_flag.destroy
  end

  def generate_api_key_url(req_params, name: nil)
    name ? api_key_url(req_params.merge(id: name)) : api_keys_url(req_params)
  end

  def user_req_params(user)
    { user_domain: user.username, api_key: user.api_key }
  end

  describe '#create' do
    before(:each) do
      @table1 = create_table(user_id: @carto_user1.id)
      @table2 = create_table(user_id: @carto_user1.id)
    end

    after(:each) do
      @table2.destroy
      @table1.destroy
    end

    it 'creates a new API key' do
      grants = [
        {
          type: "apis",
          apis: ["sql", "maps"]
        },
        {
          type: "database",
          tables: [
            {
              schema: @carto_user1.database_schema,
              name: @table1.name,
              permissions: [
                "insert",
                "select",
                "update",
                "delete"
              ]
            },
            {
              schema: @carto_user1.database_schema,
              name: @table2.name,
              permissions: [
                "select"
              ]
            }
          ]
        }
      ]
      name = 'wadus'
      payload = {
        name: name,
        grants: grants
      }
      post_json generate_api_key_url(user_req_params(@carto_user1)), payload do |response|
        response.status.should eq 201
        api_key_response = response.body
        api_key_response[:id].should_not be
        api_key_response[:name].should eq name
        api_key_response[:user][:username].should eq @carto_user1.username
        api_key_response[:type].should eq 'regular'
        api_key_response[:token].should_not be_empty

        request_table_permissions = grants.find { |grant| grant['type'] == 'database' }['tables']
        response_grants_should_include_request_permissions(api_key_response[:grants], request_table_permissions)

        api_key_response[:databaseConfig].should_not be

        Carto::ApiKey.where(name: api_key_response[:name]).each(&:destroy)
      end
    end

    it 'creates allows empty apis grants' do
      grants = [
        {
          "type" => "apis",
          "apis" => []
        },<commit_msg>Fix API key tests<commit_after>require 'spec_helper_min'
require 'support/helpers'
require 'factories/carto_visualizations'
require 'base64'

describe Carto::Api::ApiKeysController do
  include_context 'users helper'
  include HelperMethods

  def response_grants_should_include_request_permissions(reponse_grants, table_permissions)
    table_permissions.each do |stp|
      response_tables = reponse_grants.find { |grant| grant[:type] == 'database'}[:tables]
      response_permissions_for_table =
        response_tables.find { |rtp| rtp[:schema] == stp[:schema] && rtp[:name] == stp[:name] }[:permissions]
      response_permissions_for_table.sort.should eq stp[:permissions].sort
    end
  end

  before(:all) do
    @auth_api_feature_flag = FactoryGirl.create(:feature_flag, name: 'auth_api', restricted: false)
  end

  after(:all) do
    @auth_api_feature_flag.destroy
  end

  def generate_api_key_url(req_params, name: nil)
    name ? api_key_url(req_params.merge(id: name)) : api_keys_url(req_params)
  end

  def user_req_params(user)
    { user_domain: user.username, api_key: user.api_key }
  end

  describe '#create' do
    before(:each) do
      @table1 = create_table(user_id: @carto_user1.id)
      @table2 = create_table(user_id: @carto_user1.id)
    end

    after(:each) do
      @table2.destroy
      @table1.destroy
    end

    it 'creates a new API key' do
      grants = [
        {
          type: "apis",
          apis: ["sql", "maps"]
        },
        {
          type: "database",
          tables: [
            {
              schema: @carto_user1.database_schema,
              name: @table1.name,
              permissions: [
                "insert",
                "select",
                "update",
                "delete"
              ]
            },
            {
              schema: @carto_user1.database_schema,
              name: @table2.name,
              permissions: [
                "select"
              ]
            }
          ]
        }
      ]
      name = 'wadus'
      payload = {
        name: name,
        grants: grants
      }
      post_json generate_api_key_url(user_req_params(@carto_user1)), payload do |response|
        response.status.should eq 201
        api_key_response = response.body
        api_key_response[:id].should_not be
        api_key_response[:name].should eq name
        api_key_response[:user][:username].should eq @carto_user1.username
        api_key_response[:type].should eq 'regular'
        api_key_response[:token].should_not be_empty

        request_table_permissions = grants.find { |grant| grant[:type] == 'database' }[:tables]
        response_grants_should_include_request_permissions(api_key_response[:grants], request_table_permissions)

        api_key_response[:databaseConfig].should_not be

        Carto::ApiKey.where(name: api_key_response[:name]).each(&:destroy)
      end
    end

    it 'creates allows empty apis grants' do
      grants = [
        {
          "type" => "apis",
          "apis" => []
        },
{'fraction_non_alphanumeric': 0.10720157842814863, 'fraction_numerical': 0.005590266359750082, 'mean_word_length': 2.360773480662983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4435617', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1868, 'n_words': 347}