populate root_account on new DeveloperKey

refs PLAT-5518
flag=none

test plan:
* specs
* if you want, create a new DeveloperKey, set an account on it, and
note its root_account - it shouldn't be nil, and should match
the account's root account

Change-Id: I31810980aa6d6c234eb4ba0cfa3dcc21012ed250
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/234174
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Mysti Lilla <mysti@instructure.com>
QA-Review: Mysti Lilla <mysti@instructure.com>
Product-Review: Mysti Lilla <mysti@instructure.com>
This commit is contained in:
Xander Moffatt 2020-04-14 18:04:05 -06:00
parent e1bf14466f
commit 94463a0ffd
3 changed files with 41 additions and 0 deletions

View File

@ -491,6 +491,10 @@ class Account < ActiveRecord::Base
super super
end end
def resolved_root_account_id
root_account_id || id
end
def sub_accounts_as_options(indent = 0, preloaded_accounts = nil) def sub_accounts_as_options(indent = 0, preloaded_accounts = nil)
unless preloaded_accounts unless preloaded_accounts
preloaded_accounts = {} preloaded_accounts = {}

View File

@ -24,6 +24,7 @@ class DeveloperKey < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :account belongs_to :account
belongs_to :root_account, class_name: 'Account'
has_many :page_views has_many :page_views
has_many :access_tokens, -> { where(:workflow_state => "active") } has_many :access_tokens, -> { where(:workflow_state => "active") }
@ -42,6 +43,7 @@ class DeveloperKey < ActiveRecord::Base
before_save :nullify_empty_icon_url before_save :nullify_empty_icon_url
before_save :protect_default_key before_save :protect_default_key
before_save :set_require_scopes before_save :set_require_scopes
before_save :set_root_account
after_save :clear_cache after_save :clear_cache
after_update :invalidate_access_tokens_if_scopes_removed! after_update :invalidate_access_tokens_if_scopes_removed!
after_update :destroy_external_tools!, if: :destroy_external_tools? after_update :destroy_external_tools!, if: :destroy_external_tools?
@ -209,6 +211,10 @@ class DeveloperKey < ActiveRecord::Base
MultiCache.delete("developer_keys/#{vendor_code}") if vendor_code.present? MultiCache.delete("developer_keys/#{vendor_code}") if vendor_code.present?
end end
def set_root_account
self.root_account_id ||= account&.resolved_root_account_id
end
def authorized_for_account?(target_account) def authorized_for_account?(target_account)
return false unless binding_on_in_account?(target_account) return false unless binding_on_in_account?(target_account)
return true if account_id.blank? return true if account_id.blank?

View File

@ -647,6 +647,37 @@ describe DeveloperKey do
end end
end end
end end
describe 'after_save' do
describe 'set_root_account' do
context 'when account is not root account' do
let(:account) {
a = account_model
a.root_account = Account.create!
a.save!
a
}
it 'sets root account equal to account\'s root account' do
expect(developer_key_not_saved.root_account).to be_nil
developer_key_not_saved.account = account
developer_key_not_saved.save!
expect(developer_key_not_saved.root_account).to eq account.root_account
end
end
context 'when account is root account' do
let(:account) { account_model }
it 'set root account equal to account' do
expect(developer_key_not_saved.root_account).to be_nil
developer_key_not_saved.account = account
developer_key_not_saved.save!
expect(developer_key_not_saved.root_account).to eq account
end
end
end
end
end end
describe 'associations' do describe 'associations' do