Backfill dev key account bindings

Refs: PLAT-3021

Test Plan: For a dev key dk, delete its bindings, run the migration, and
verify dk has a binding with an "on" state.

Change-Id: Iae3628355817e69976ad255eebf7dfae673b9c14
Reviewed-on: https://gerrit.instructure.com/150221
Reviewed-by: Weston Dransfield <wdransfield@instructure.com>
Tested-by: Jenkins
QA-Review: August Thornton <august@instructure.com>
Product-Review: Stewie aka Nicholas Stewart <nstewart@instructure.com>
This commit is contained in:
Stewie (Nicholas Stewart) 2018-05-14 14:59:55 -06:00 committed by Stewie aka Nicholas Stewart
parent ff7a641981
commit b86b534193
5 changed files with 123 additions and 5 deletions

View File

@ -210,6 +210,10 @@ class DeveloperKey < ActiveRecord::Base
binding || DeveloperKeyAccountBinding.find_in_account_priority(accounts.reverse, self.id, false)
end
def owner_account
account || Account.site_admin
end
private
def binding_on_in_account?(target_account)
@ -232,10 +236,6 @@ class DeveloperKey < ActiveRecord::Base
owner_account.developer_key_account_bindings.create!(developer_key: self)
end
def owner_account
account || Account.site_admin
end
def set_require_scopes
return unless api_token_scoping_on?
self.require_scopes = self.scopes.present?

View File

@ -0,0 +1,29 @@
#
# Copyright (C) 2018 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
class BackfillDevKeyAccountBinding < ActiveRecord::Migration[5.1]
tag :postdeploy
def up
::DataFixup::BackfillDevKeyAccountBindings.send_later_if_production_enqueue_args(
:run,
priority: Delayed::LOW_PRIORITY,
max_attempts: 1,
n_strand: 'long_datafixups'
)
end
end

View File

@ -0,0 +1,35 @@
#
# Copyright (C) 2018 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
module DataFixup
module BackfillDevKeyAccountBindings
def self.run
DeveloperKey.nondeleted.find_each do |developer_key|
find_or_create_default_account_binding(developer_key)
end
end
def self.find_or_create_default_account_binding(developer_key)
return if developer_key.owner_account.developer_key_account_bindings.where(developer_key: developer_key).exists?
developer_key.owner_account.developer_key_account_bindings.create!(
workflow_state: DeveloperKeyAccountBinding::ON_STATE,
developer_key: developer_key
)
end
end
end

View File

@ -0,0 +1,55 @@
#
# Copyright (C) 2018 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'spec_helper'
describe DataFixup::BackfillDevKeyAccountBindings do
describe "#run" do
let(:key) { DeveloperKey.create! }
it "backfills when no binding is present" do
# Setup
key.developer_key_account_bindings.destroy_all
expect(key.developer_key_account_bindings.count).to eq(0)
# Backfill with a new binding with "on" state
described_class.run
# Verify
key.reload
expect(key.developer_key_account_bindings.count).to eq(1)
expect(
key.developer_key_account_bindings.first.workflow_state
).to eq(DeveloperKeyAccountBinding::ON_STATE)
end
it "does not backfill when a binding is present" do
# Setup
expect(key.developer_key_account_bindings.count).to eq(1)
original_binding_id = key.developer_key_account_bindings.first.id
# The backfill should not change the binding's state
described_class.run
# Verify
key.reload
expect(key.developer_key_account_bindings.count).to eq(1)
expect(key.developer_key_account_bindings.first.id).to eq(original_binding_id)
end
end
end

View File

@ -113,7 +113,6 @@ describe DeveloperKey do
end
end
context 'when site admin' do
it 'it creates a binding on save' do
key = DeveloperKey.create!(account: nil)