<commit_before>require 'spec_helper_min'
require 'support/helpers'

describe Carto::Api::Public::FederatedTablesController do
  include_context 'users helper'
  include HelperMethods

  describe '#list' do
    before(:each) do
      host! "#{@user1.username}.localhost.lan"
    end

    it 'returns 200 with the federated server list' do
      @params = { api_key: @user1.api_key, page: 1, per_page: 10 }

      get_json api_v4_federated_servers_list_servers_url(@params) do |response|
        expect(response.status).to eq(200)

        expect(response.body[:total]).to eq(2)

        expect(response.body[:result][0][:name]).to eq('amazon')
        expect(response.body[:result][0][:dbname]).to eq('testdb')
        expect(response.body[:result][0][:host]).to eq('myhostname.us-east-2.rds.amazonaws.com')

        expect(response.body[:result][1][:name]).to eq('azure')
        expect(response.body[:result][1][:dbname]).to eq('db')
        expect(response.body[:result][1][:host]).to eq('us-east-2.azure.com')
      end
    end

    it 'returns 401 when non authenticated user' do
      @params = { page: 1, per_page: 10 }

      get_json api_v4_federated_servers_list_servers_url(@params) do |response|
        expect(response.status).to eq(401)
      end
    end
  end
end<commit_msg>Check a user with regular api key cannot list federated servers<commit_after>require 'spec_helper_min'
require 'support/helpers'

describe Carto::Api::Public::FederatedTablesController do
  include_context 'users helper'
  include HelperMethods

  describe '#list' do
    before(:each) do
      host! "#{@user1.username}.localhost.lan"
    end

    it 'returns 200 with the federated server list' do
      @params = { api_key: @user1.api_key, page: 1, per_page: 10 }

      get_json api_v4_federated_servers_list_servers_url(@params) do |response|
        expect(response.status).to eq(200)

        expect(response.body[:total]).to eq(2)

        expect(response.body[:result][0][:name]).to eq('amazon')
        expect(response.body[:result][0][:dbname]).to eq('testdb')
        expect(response.body[:result][0][:host]).to eq('myhostname.us-east-2.rds.amazonaws.com')

        expect(response.body[:result][1][:name]).to eq('azure')
        expect(response.body[:result][1][:dbname]).to eq('db')
        expect(response.body[:result][1][:host]).to eq('us-east-2.azure.com')
      end
    end

    it 'returns 401 when non authenticated user' do
      @params = { page: 1, per_page: 10 }

      get_json api_v4_federated_servers_list_servers_url(@params) do |response|
        expect(response.status).to eq(401)
      end
    end

    it 'returns 403 when using a regular API key' do
      @params = { api_key: @user1.api_key, page: 1, per_page: 10 }
      api_key = FactoryGirl.create(:api_key_apis, user_id: @user1.id)

      get_json api_v4_federated_servers_list_servers_url(@params.merge(api_key: api_key.token)) do |response|
        expect(response.status).to eq(403)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.14042126379137412, 'fraction_numerical': 0.02474088933467068, 'mean_word_length': 3.660436137071651, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '829066', 'n_tokens_mistral': 1037, 'n_tokens_neox': 982, 'n_words': 202}