<commit_before># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '.././../../factories/organizations_contexts'
require_relative '.././../../factories/visualization_creation_helpers'
require_relative '../../../../app/controllers/carto/api/database_groups_controller'

describe Carto::Api::DatabaseGroupsController do
  include_context 'organization with users helper'

  describe 'Groups management', :order => :defined do

    before(:all) do
      @no_auth_headers = {'CONTENT_TYPE'  => 'application/json', :format => "json" }
    end

    before(:each) do
      @carto_organization = Carto::Organization.find(@organization.id)
    end

    it "Throws 401 error without http auth" do
      post api_v1_databases_group_create_url(user_domain: @org_user_owner.username, database_name: @carto_organization.database_name), {}, @no_auth_headers
      response.status.should == 401
    end

    it '#creates a new group from name and role, and initializes display_name as name' do
      group_information = { name: 'org_group', database_role: 'g_org_database_group' }
      post api_v1_databases_group_create_url(database_name: @carto_organization.database_name), group_information.to_json, org_metadata_api_headers
      response.status.should == 200
      group = Carto::Group.where(organization_id: @carto_organization.id, database_role: group_information[:database_role], name: group_information[:name], display_name: group_information[:name]).first
      group.should_not be_nil
    end

    it '#creates return 409 if a group with that data has already been created' do
      group_information = { name: 'org_group', database_role: 'g_org_database_group' }
      post api_v1_databases_group_create_url(database_name: @carto_organization.database_name), group_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#rename a new group from name and role' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      group_information = { name: 'org_group_2', database_role: 'g_org_database_group_2' }
      put api_v1_databases_group_update_url(database_name: group.database_name, old_name: group.name), group_information.to_json, org_metadata_api_headers
      response.status.should == 200
      updated_group = Carto::Group.find(group.id)
      updated_group.should_not be_nil
      updated_group.name.should eq group_information[:name]
      updated_group.database_role.should eq group_information[:database_role]
      updated_group.display_name.should eq group.display_name
    end

    it '#rename triggers 409 if it looks like renaming already occurred: existing new name, nonexisting old name' do
      group_old_information = { name: 'org_group', database_role: 'g_org_database_group' }
      group_new_information = { name: 'org_group_2', database_role: 'g_org_database_group_2' }
      put api_v1_databases_group_update_url(database_name: @carto_organization.database_name, old_name: group_old_information[:name]), group_new_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#rename triggers 500 if renaming can\'t be done and there is no previous match' do
      group_old_information = { name: 'org_group', database_role: 'g_org_database_group' }
      group_new_information = { name: 'org_group_2', database_role: 'g_org_database_group_WRONG' }
      put api_v1_databases_group_update_url(database_name: @carto_organization.database_name, old_name: group_old_information[:name]), group_new_information.to_json, org_metadata_api_headers
      response.status.should == 500
    end

    it '#add_member from username' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      user_information = { username: @org_user_1.username }
      post api_v1_databases_group_add_member_url(database_name: group.database_name, name: group.name), user_information.to_json, org_metadata_api_headers
      response.status.should == 200
      group.reload
      group.users.collect(&:username).should include(@org_user_1.username)
    end

    it '#add_member returns 409 if username is already added' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      user_information = { username: @org_user_1.username }
      post api_v1_databases_group_add_member_url(database_name: group.database_name, name: group.name), user_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#update_permission granting read to a table' do
      bypass_named_maps
      @table_user_2 = create_table_with_options(@org_user_2)

      group = Carto::Group.where(organization_id: @carto_organization.id).first
      permission = { 'access' => 'r' }
      put api_v1_databases_group_update_permission_url(database_name: group.database_name, name: group.name, username: @org_user_2.username, table_name: @table_user_2['name']), permission.to_json, org_metadata_api_headers
      response.status.should == 200

      permission = ::Permission.where(entity_id: @table_user_2['table_visualization']['id']).first
      permission.should_not be_nil

      expected_acl = [
          {
              type: Permission::TYPE_GROUP,
              entity: {
                  id:         group.id,
                  name:       group.name
              },
              access: Permission::ACCESS_READONLY
          }
      ]
      permission.to_poro[:acl].should == expected_acl

      # URL generation for users of the granted group not table owners
      user = group.users.first
      user.id.should_not == @org_user_2.id
      vis_id = @table_user_2['table_visualization']['id']
      host = "#{user.username}.localhost.lan"
      url = api_v1_visualizations_show_url(user_domain: user.username, id: vis_id, api_key: user.api_key).gsub('www.example.com', host)
      get_json url, {}, http_json_headers do |response|
        response.status.should == 200

        response.body[:url].should == "http://#{host}:#{Cartodb.config[:http_port]}/user/#{user.username}/tables/#{@org_user_2.username}.#{@table_user_2['name']}"<commit_msg>Support for alternative urls at tests<commit_after># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '.././../../factories/organizations_contexts'
require_relative '.././../../factories/visualization_creation_helpers'
require_relative '../../../../app/controllers/carto/api/database_groups_controller'

describe Carto::Api::DatabaseGroupsController do
  include_context 'organization with users helper'

  describe 'Groups management', :order => :defined do

    before(:all) do
      @no_auth_headers = {'CONTENT_TYPE'  => 'application/json', :format => "json" }
    end

    before(:each) do
      @carto_organization = Carto::Organization.find(@organization.id)
    end

    it "Throws 401 error without http auth" do
      post api_v1_databases_group_create_url(user_domain: @org_user_owner.username, database_name: @carto_organization.database_name), {}, @no_auth_headers
      response.status.should == 401
    end

    it '#creates a new group from name and role, and initializes display_name as name' do
      group_information = { name: 'org_group', database_role: 'g_org_database_group' }
      post api_v1_databases_group_create_url(database_name: @carto_organization.database_name), group_information.to_json, org_metadata_api_headers
      response.status.should == 200
      group = Carto::Group.where(organization_id: @carto_organization.id, database_role: group_information[:database_role], name: group_information[:name], display_name: group_information[:name]).first
      group.should_not be_nil
    end

    it '#creates return 409 if a group with that data has already been created' do
      group_information = { name: 'org_group', database_role: 'g_org_database_group' }
      post api_v1_databases_group_create_url(database_name: @carto_organization.database_name), group_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#rename a new group from name and role' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      group_information = { name: 'org_group_2', database_role: 'g_org_database_group_2' }
      put api_v1_databases_group_update_url(database_name: group.database_name, old_name: group.name), group_information.to_json, org_metadata_api_headers
      response.status.should == 200
      updated_group = Carto::Group.find(group.id)
      updated_group.should_not be_nil
      updated_group.name.should eq group_information[:name]
      updated_group.database_role.should eq group_information[:database_role]
      updated_group.display_name.should eq group.display_name
    end

    it '#rename triggers 409 if it looks like renaming already occurred: existing new name, nonexisting old name' do
      group_old_information = { name: 'org_group', database_role: 'g_org_database_group' }
      group_new_information = { name: 'org_group_2', database_role: 'g_org_database_group_2' }
      put api_v1_databases_group_update_url(database_name: @carto_organization.database_name, old_name: group_old_information[:name]), group_new_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#rename triggers 500 if renaming can\'t be done and there is no previous match' do
      group_old_information = { name: 'org_group', database_role: 'g_org_database_group' }
      group_new_information = { name: 'org_group_2', database_role: 'g_org_database_group_WRONG' }
      put api_v1_databases_group_update_url(database_name: @carto_organization.database_name, old_name: group_old_information[:name]), group_new_information.to_json, org_metadata_api_headers
      response.status.should == 500
    end

    it '#add_member from username' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      user_information = { username: @org_user_1.username }
      post api_v1_databases_group_add_member_url(database_name: group.database_name, name: group.name), user_information.to_json, org_metadata_api_headers
      response.status.should == 200
      group.reload
      group.users.collect(&:username).should include(@org_user_1.username)
    end

    it '#add_member returns 409 if username is already added' do
      group = Carto::Group.where(organization_id: @carto_organization.id).first
      user_information = { username: @org_user_1.username }
      post api_v1_databases_group_add_member_url(database_name: group.database_name, name: group.name), user_information.to_json, org_metadata_api_headers
      response.status.should == 409
    end

    it '#update_permission granting read to a table' do
      bypass_named_maps
      @table_user_2 = create_table_with_options(@org_user_2)

      group = Carto::Group.where(organization_id: @carto_organization.id).first
      permission = { 'access' => 'r' }
      put api_v1_databases_group_update_permission_url(database_name: group.database_name, name: group.name, username: @org_user_2.username, table_name: @table_user_2['name']), permission.to_json, org_metadata_api_headers
      response.status.should == 200

      permission = ::Permission.where(entity_id: @table_user_2['table_visualization']['id']).first
      permission.should_not be_nil

      expected_acl = [
          {
              type: Permission::TYPE_GROUP,
              entity: {
                  id:         group.id,
                  name:       group.name
              },
              access: Permission::ACCESS_READONLY
          }
      ]
      permission.to_poro[:acl].should == expected_acl

      # URL generation for users of the granted group not table owners
      user = group.users.first
      user.id.should_not == @org_user_2.id
      vis_id = @table_user_2['table_visualization']['id']
      # subdomain test simulation
      host = "#{user.organization.name}.localhost.lan"
      url = api_v1_visualizations_show_url(user_domain: user.username, id: vis_id, api_key: user.api_key).gsub('www.example.com', host)
      get_json url, {}, http_json_headers do |response|
        response.status.should == 200

        [
          "http://#{host}:#{Cartodb.config[:http_port]}/user/#{user.username}/tables/#{@org_user_2.username}.#{@table_user_2['name']}",
          "http://#{host}:#{Cartodb.config[:http_port]}/u/#{user.username}/tables/#{@org_user_2.username}.#{@table_user_2['name']}",
        ].should include(response.body[:url])
{'fraction_non_alphanumeric': 0.10319802217082702, 'fraction_numerical': 0.011803174096817928, 'mean_word_length': 4.124642419288925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5560930', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3748, 'n_words': 749}