Backfill bindings for deleted keys

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

Refs: PLAT-3400

Change-Id: I0031911d9d6102eed96de672910dce0104db50be
Reviewed-on: https://gerrit.instructure.com/151064
Tested-by: Jenkins
Reviewed-by: Weston Dransfield <wdransfield@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
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-22 13:41:47 -06:00 committed by Stewie aka Nicholas Stewart
parent ae3e89536c
commit 28c9125c37
4 changed files with 163 additions and 0 deletions

View File

@ -45,6 +45,7 @@ class DeveloperKey < ActiveRecord::Base
validate :validate_redirect_uris
scope :nondeleted, -> { where("workflow_state<>'deleted'") }
scope :not_active, -> { where("workflow_state<>'active'") } # search for deleted & inactive keys
scope :visible, -> { where(visible: true) }
workflow do

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 BackfillDevKeyAccountBindingsForDeletedKeysMigration < ActiveRecord::Migration[5.1]
tag :postdeploy
def up
::DataFixup::BackfillDevKeyAccountBindingsForDeletedKeys.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,36 @@
#
# 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 BackfillDevKeyAccountBindingsForDeletedKeys
def self.run
DeveloperKey.not_active.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::OFF_STATE,
developer_key: developer_key
)
end
end
end

View File

@ -0,0 +1,97 @@
#
# 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::BackfillDevKeyAccountBindingsForDeletedKeys do
describe "#run" do
let(:active_key) { DeveloperKey.create! }
let(:inactive_key) { DeveloperKey.create!.tap(&:deactivate) }
let(:deleted_key) { DeveloperKey.create!.tap(&:destroy) }
it "backfills when no binding is present for deleted key" do
# Setup
key = deleted_key
key.developer_key_account_bindings.destroy_all
expect(key.developer_key_account_bindings.count).to eq(0)
# Backfill with a new binding with "off" state
described_class.run
# Reload binding info
key.reload
# Verify
expect(key.developer_key_account_bindings.count).to eq(1)
expect(
key.developer_key_account_bindings.first.workflow_state
).to eq(DeveloperKeyAccountBinding::OFF_STATE)
end
it "backfills when no binding is present for inactive key" do
# Setup
key = inactive_key
key.developer_key_account_bindings.destroy_all
expect(key.developer_key_account_bindings.count).to eq(0)
# Backfill with a new binding with "off" state
described_class.run
# Reload binding info
key.reload
# Verify
expect(key.developer_key_account_bindings.count).to eq(1)
expect(
key.developer_key_account_bindings.first.workflow_state
).to eq(DeveloperKeyAccountBinding::OFF_STATE)
end
it "does not backfill when no binding is present for an active key" do
# Setup
key = active_key
key.developer_key_account_bindings.destroy_all
expect(key.developer_key_account_bindings.count).to eq(0)
# No backfilling for active key
described_class.run
# Reload binding info
key.reload
# Verify
expect(key.developer_key_account_bindings.count).to eq(0)
end
it "does not backfill when a binding is present" do
# Setup
key = deleted_key
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
# Reload binding info
key.reload
# Verify
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