<commit_before># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '../../../../app/controllers/carto/api/organizations_controller'
require 'helpers/unique_names_helper'

describe Carto::Api::OrganizationsController do
  include_context 'organization with users helper'
  include UniqueNamesHelper
  include Rack::Test::Methods
  include Warden::Test::Helpers

  describe 'users unauthenticated behaviour' do

    it 'returns 401 for not logged users' do
      get api_v1_organization_users_url(id: @organization.id), @headers
      last_response.status.should == 401
    end
  end

  describe 'users' do

    before(:all) do
      @org_user_3 = create_test_user(unique_name('user'), @organization)
      @group_1 = FactoryGirl.create(:random_group, display_name: 'g_1', organization: @carto_organization)
      @group_1.add_user(@org_user_1.username)
    end

    after(:all) do
      @group_1.destroy
      stub_named_maps_calls
      delete_user_data(@org_user_3)
      @org_user_3.destroy
    end

    before(:each) do
      login(@org_user_1)
    end

    # INFO: listing users though API is now needed for permission granting, for example
    it 'returns 200 for users requesting an organization that they are not owners of' do
      get api_v1_organization_users_url(id: @organization_2.id, api_key: @org_user_1.api_key), @headers
      last_response.status.should == 200
    end

    it 'returns organization users sorted by username' do
      get api_v1_organization_users_url(id: @organization.id, api_key: @org_user_1.api_key), @headers
      last_response.status.should == 200
      json_body = JSON.parse(last_response.body)
      ids = json_body['users'].map { |u| u['id'] }
      ids[0].should == @org_user_1.id
      ids[1].should == @org_user_2.id
      ids[2].should == @org_user_3.id
    end

    it 'returns organization users paged with totals' do
      page = 0
      per_page = 2
      displayed_ids = []
      total_count = @organization.users.count

      while page * per_page < total_count do
        page += 1
        get api_v1_organization_users_url(id: @organization.id, api_key: @org_user_1.api_key, page: page, per_page: per_page), @headers

        last_response.status.should == 200
        json_body = JSON.parse(last_response.body)
        ids = json_body['users'].map { |u| u['id'] }

        # Display different ids:
        (ids & displayed_ids).empty?.should == true

        expected_count = [per_page, total_count - displayed_ids.count].min<commit_msg>Fix test that depended on specific naming<commit_after># encoding: utf-8

require_relative '../../../spec_helper'
require_relative '../../../../app/controllers/carto/api/organizations_controller'
require 'helpers/unique_names_helper'

describe Carto::Api::OrganizationsController do
  include_context 'organization with users helper'
  include UniqueNamesHelper
  include Rack::Test::Methods
  include Warden::Test::Helpers

  describe 'users unauthenticated behaviour' do

    it 'returns 401 for not logged users' do
      get api_v1_organization_users_url(id: @organization.id), @headers
      last_response.status.should == 401
    end
  end

  describe 'users' do

    before(:all) do
      @org_user_3 = create_test_user(unique_name('user'), @organization)
      @group_1 = FactoryGirl.create(:random_group, display_name: 'g_1', organization: @carto_organization)
      @group_1.add_user(@org_user_1.username)
    end

    after(:all) do
      @group_1.destroy
      stub_named_maps_calls
      delete_user_data(@org_user_3)
      @org_user_3.destroy
    end

    before(:each) do
      login(@org_user_1)
    end

    # INFO: listing users though API is now needed for permission granting, for example
    it 'returns 200 for users requesting an organization that they are not owners of' do
      get api_v1_organization_users_url(id: @organization_2.id, api_key: @org_user_1.api_key), @headers
      last_response.status.should == 200
    end

    it 'returns organization users sorted by username' do
      get api_v1_organization_users_url(id: @organization.id, api_key: @org_user_1.api_key), @headers
      last_response.status.should == 200
      json_body = JSON.parse(last_response.body)
      ids = json_body['users'].map { |u| u['id'] }
      ids[0].should == @org_user_owner.id
      ids[1].should == @org_user_1.id
      ids[2].should == @org_user_2.id
      ids[3].should == @org_user_3.id
    end

    it 'returns organization users paged with totals' do
      page = 0
      per_page = 2
      displayed_ids = []
      total_count = @organization.users.count

      while page * per_page < total_count do
        page += 1
        get api_v1_organization_users_url(id: @organization.id, api_key: @org_user_1.api_key, page: page, per_page: per_page), @headers

        last_response.status.should == 200
        json_body = JSON.parse(last_response.body)
        ids = json_body['users'].map { |u| u['id'] }

        # Display different ids:
        (ids & displayed_ids).empty?.should == true

        expected_count = [per_page, total_count - displayed_ids.count].min
{'fraction_non_alphanumeric': 0.10473766640563821, 'fraction_numerical': 0.017815191855912293, 'mean_word_length': 3.3666666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3432184', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1582, 'n_words': 381}