Remove constant usage outside ApiKey class, replace with scopes

This avoids exposing class constants all over the place.
pull/13540/head
Javier Torres 7 years ago
parent ad6d6ac323
commit aa8d1084c6

@ -75,6 +75,9 @@ module Carto
after_destroy :drop_db_role, if: :regular?
after_destroy :remove_from_redis
scope :master, ->() { where(type: TYPE_MASTER) }
scope :default_public, ->() { where(type: TYPE_DEFAULT_PUBLIC) }
private_class_method :new, :create, :create!
def self.create_master_key!(user: Carto::User.find(scope_attributes['user_id']))

@ -1833,7 +1833,7 @@ class User < Sequel::Model
end
def sync_master_key
master_key = Carto::ApiKey.where(user_id: id, type: Carto::ApiKey::TYPE_MASTER).first
master_key = Carto::ApiKey.where(user_id: id).master.first
return unless master_key
# Workaround: User save is not yet commited, so AR doesn't see the new api_key
@ -1842,7 +1842,7 @@ class User < Sequel::Model
end
def sync_default_public_key
default_key = Carto::ApiKey.where(user_id: id, type: Carto::ApiKey::TYPE_DEFAULT_PUBLIC).first
default_key = Carto::ApiKey.where(user_id: id).default_public.first
return unless default_key
# Workaround: User save is not yet commited, so AR doesn't see the new database_schema

@ -309,7 +309,7 @@ Warden::Strategies.add(:auth_api) do
return fail! unless user_name == CartoDB.extract_subdomain(request)
user_id = $users_metadata.HGET("rails:users:#{user_name}", 'id')
return fail! unless Carto::ApiKey.where(user_id: user_id, type: Carto::ApiKey::TYPE_MASTER, token: token).exists?
return fail! unless Carto::ApiKey.where(user_id: user_id, token: token).master.exists?
success!(::User[user_id])
rescue

@ -253,7 +253,7 @@ describe Carto::ApiKey do
end
it 'shows public tables' do
api_key = @carto_user1.api_keys.find_by_type(Carto::ApiKey::TYPE_DEFAULT_PUBLIC)
api_key = @carto_user1.api_keys.default_public.first
api_key_permissions(api_key, @public_table.database_schema, @public_table.name).permissions.should eq ['select']
end
@ -261,7 +261,7 @@ describe Carto::ApiKey do
describe 'master api key' do
it 'user has a master key with the user db_role' do
api_key = @carto_user1.api_keys.find_by_type(Carto::ApiKey::TYPE_MASTER)
api_key = @carto_user1.api_keys.master.first
api_key.should be
api_key.db_role.should eq @carto_user1.database_username
api_key.db_password.should eq @carto_user1.database_password
@ -280,7 +280,7 @@ describe Carto::ApiKey do
end
it 'token must match user api key' do
api_key = @carto_user1.api_keys.find_by_type(Carto::ApiKey::TYPE_MASTER)
api_key = @carto_user1.api_keys.master.first
api_key.token = 'wadus'
api_key.save.should be_false
api_key.errors.full_messages.should include "Token must match user model for master keys"
@ -289,7 +289,7 @@ describe Carto::ApiKey do
describe 'default public api key' do
it 'user has a default public key with the public_db_user role' do
api_key = @carto_user1.api_keys.find_by_type(Carto::ApiKey::TYPE_DEFAULT_PUBLIC)
api_key = @carto_user1.api_keys.default_public.first
api_key.should be
api_key.db_role.should eq @carto_user1.database_public_username
api_key.db_password.should eq CartoDB::PUBLIC_DB_USER_PASSWORD
@ -308,7 +308,7 @@ describe Carto::ApiKey do
end
it 'cannot change token' do
api_key = @carto_user1.api_keys.find_by_type(Carto::ApiKey::TYPE_DEFAULT_PUBLIC)
api_key = @carto_user1.api_keys.default_public.first
api_key.token = 'wadus'
api_key.save.should be_false
api_key.errors.full_messages.should include "Token must be default_public for default public keys"

@ -2581,7 +2581,7 @@ describe User do
api_keys = Carto::ApiKey.where(user_id: @auth_api_user.id)
api_keys.should_not be_empty
master_api_key = Carto::ApiKey.where(user_id: @auth_api_user.id, type: Carto::ApiKey::TYPE_MASTER).first
master_api_key = Carto::ApiKey.where(user_id: @auth_api_user.id).master.first
master_api_key.should be
master_api_key.token.should eq @auth_api_user.api_key
end
@ -2595,7 +2595,7 @@ describe User do
end
it 'syncs api key changes with master api key' do
master_key = Carto::ApiKey.where(user_id: @auth_api_user.id, type: Carto::ApiKey::TYPE_MASTER).first
master_key = Carto::ApiKey.where(user_id: @auth_api_user.id).master.first
expect(@auth_api_user.api_key).to eq master_key.token
expect { @auth_api_user.regenerate_api_key }.to(change { @auth_api_user.api_key })

@ -262,8 +262,8 @@ describe Carto::Api::ApiKeysController do
end
it 'returns 403 if API key is master or default public' do
master_api_key = @carto_user.api_keys.find_by_type(Carto::ApiKey::TYPE_MASTER)
default_api_key = @carto_user.api_keys.find_by_type(Carto::ApiKey::TYPE_DEFAULT_PUBLIC)
master_api_key = @carto_user.api_keys.master.first
default_api_key = @carto_user.api_keys.default_public.first
delete_json generate_api_key_url(user_req_params(@user), name: master_api_key.name) do |response|
response.status.should eq 403
@ -438,7 +438,7 @@ describe Carto::Api::ApiKeysController do
describe 'header auth' do
before(:all) do
@master_api_key = @carto_user.api_keys.find_by_type(Carto::ApiKey::TYPE_MASTER)
@master_api_key = @carto_user.api_keys.master.first
end
def json_headers_with_auth

@ -12,7 +12,7 @@ describe 'Warden :auth_api Strategy' do
before :all do
@auth_api_feature_flag = FactoryGirl.create(:feature_flag, name: 'auth_api', restricted: false)
@user_api_keys = FactoryGirl.create(:valid_user)
@master_api_key = Carto::ApiKey.where(user_id: @user_api_keys.id, type: Carto::ApiKey::TYPE_MASTER).first
@master_api_key = Carto::ApiKey.where(user_id: @user_api_keys.id).master.first
end
after :all do

Loading…
Cancel
Save