Fix specs and move some methods from Sequel to AR

pull/15879/head
Alberto Miedes Garcés 4 years ago
parent f066759551
commit 150bbb5eb4

@ -34,4 +34,9 @@ module Carto::OrganizationSoftLimits
def soft_mapzen_routing_limit?
owner.try(:soft_mapzen_routing_limit)
end
def db_size_in_bytes
users.map(&:db_size_in_bytes).sum
end
end

@ -206,6 +206,10 @@ module Carto
(remaining > 0 ? remaining : 0)
end
def get_api_calls(options = {})
users.map { |u| u.get_api_calls(options).sum }.sum
end
private
def ensure_auth_saml_configuration

@ -48,6 +48,12 @@ class Organization < Sequel::Model
DEFAULT_OBS_GENERAL_QUOTA = 0
DEFAULT_MAPZEN_ROUTING_QUOTA = nil
delegate :get_api_calls, to: :carto_organization
def carto_organization
@carto_organization ||= Carto::Organization.find(id)
end
def default_password_expiration_in_d
Cartodb.get_config(:passwords, 'expiration_in_d')
end
@ -182,19 +188,6 @@ class Organization < Sequel::Model
users.select { |u| owner && u.id != owner.id }
end
##
# SLOW! Checks redis data (geocoding and isolines) for every user in every organization
# delta: get organizations who are also this percentage below their limit.
# example: 0.20 will get all organizations at 80% of their map view limit
#
def self.overquota(delta = 0)
Carto::Organization.overquota(delta)
end
def get_api_calls(options = {})
users.map{ |u| u.get_api_calls(options).sum }.sum
end
def get_geocoding_calls(options = {})
require_organization_owner_presence!
date_from, date_to = quota_dates(options)
@ -250,10 +243,6 @@ class Organization < Sequel::Model
(remaining > 0 ? remaining : 0)
end
def db_size_in_bytes
users.map(&:db_size_in_bytes).sum.to_i
end
def assigned_quota
users_dataset.sum(:quota_in_bytes).to_i
end

@ -442,16 +442,13 @@ describe Organization do
end
end
describe "#get_api_calls and #get_geocodings" do
before(:each) do
@organization = create_organization_with_users(name: 'overquota-org')
end
after(:each) do
@organization.destroy
end
it "should return the sum of the api_calls for all organization users" do
::User.any_instance.stubs(:get_api_calls).returns (0..30).to_a
@organization.get_api_calls.should == (0..30).to_a.sum * @organization.users.size
describe '#get_api_calls and #get_geocodings' do
let(:organization) { create_organization_with_users }
before { Carto::User.any_instance.stubs(:get_api_calls).returns (0..30).to_a }
it 'should return the sum of the api_calls for all organization users' do
expect(organization.get_api_calls).to eq((0..30).to_a.sum * organization.users.size)
end
end

@ -120,7 +120,7 @@ feature "Superadmin's organization API" do
end
end
it "gets overquota organizations" do
Organization.stubs(:overquota).returns [@organization1]
Carto::Organization.stubs(:overquota).returns [@organization1]
get_json superadmin_organizations_path, { overquota: true }, superadmin_headers do |response|
response.status.should == 200
response.body[0]["name"].should == @organization1.name
@ -128,7 +128,7 @@ feature "Superadmin's organization API" do
end
end
it "returns geocoding and mapviews quotas and uses for all organizations" do
Organization.stubs(:overquota).returns [@organization1]
Carto::Organization.stubs(:overquota).returns [@organization1]
::User.any_instance.stubs(:get_geocoding_calls).returns(100)
::User.any_instance.stubs(:get_api_calls).returns (0..30).to_a
get_json superadmin_organizations_path, { overquota: true }, superadmin_headers do |response|

Loading…
Cancel
Save