From b48d3260ff39d4b0306ddb1dc04aff52c58d22b2 Mon Sep 17 00:00:00 2001 From: Steven Burnett Date: Wed, 4 Oct 2017 10:10:05 -0600 Subject: [PATCH] create migration for populating terms Change-Id: I13eddbe900070b8f50ff5c8f6f016daa27ec87d1 Reviewed-on: https://gerrit.instructure.com/128447 Tested-by: Jenkins QA-Review: Steven Burnett Reviewed-by: Simon Williams Reviewed-by: Steven Burnett Product-Review: James Williams Reviewed-by: James Williams --- app/models/account.rb | 1 + app/models/terms_of_service.rb | 23 ++++++++++++++ ...0171004154613_populate_terms_of_service.rb | 31 +++++++++++++++++++ spec/models/terms_of_service_spec.rb | 15 ++++++++- spec/spec_helper.rb | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20171004154613_populate_terms_of_service.rb diff --git a/app/models/account.rb b/app/models/account.rb index a32e1f2202c..f6f301cf116 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1649,6 +1649,7 @@ class Account < ActiveRecord::Base work = -> do default_enrollment_term enable_canvas_authentication + TermsOfService.ensure_terms_for_account(self) if self.root_account? end return work.call if Rails.env.test? self.class.connection.after_transaction_commit(&work) diff --git a/app/models/terms_of_service.rb b/app/models/terms_of_service.rb index 7f66f11f6e3..46f77a7e8e7 100644 --- a/app/models/terms_of_service.rb +++ b/app/models/terms_of_service.rb @@ -21,4 +21,27 @@ class TermsOfService < ActiveRecord::Base belongs_to :account belongs_to :terms_of_service_content validates :terms_type, :passive, presence: true + + validate :validate_account_is_root + + cattr_accessor :skip_automatic_terms_creation + + def validate_account_is_root + if self.account_id_changed? && !self.account.root_account? + self.errors.add(:account, "must be root account") + end + end + + def self.ensure_terms_for_account(account) + unless !self.table_exists? || self.skip_automatic_terms_creation || account.terms_of_service + account.shard.activate do + account.create_terms_of_service!(term_options_for_account(account)) + end + end + end + + DEFAULT_OPTIONS = {:terms_type => "default_url"}.freeze + def self.term_options_for_account(account) + DEFAULT_OPTIONS + end end diff --git a/db/migrate/20171004154613_populate_terms_of_service.rb b/db/migrate/20171004154613_populate_terms_of_service.rb new file mode 100644 index 00000000000..935af6caada --- /dev/null +++ b/db/migrate/20171004154613_populate_terms_of_service.rb @@ -0,0 +1,31 @@ +# +# Copyright (C) 2017 - 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 . +# +# +class PopulateTermsOfService < ActiveRecord::Migration[4.2] + tag :postdeploy + disable_ddl_transaction! + + def up + Account.root_accounts.each do |ra| + TermsOfService.ensure_terms_for_account(ra) + end + end + + def down + end +end diff --git a/spec/models/terms_of_service_spec.rb b/spec/models/terms_of_service_spec.rb index 531bd768cc7..6d70e635f76 100644 --- a/spec/models/terms_of_service_spec.rb +++ b/spec/models/terms_of_service_spec.rb @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License along # with this program. If not, see . # -require_relative '../spec_helper' +require_relative '../sharding_spec_helper' describe TermsOfService do before :once do @ac = account_model @@ -43,4 +43,17 @@ describe TermsOfService do account: ac2) expect(tos.passive).to eq true end + + describe "#ensure_terms_for_account" do + before :each do + TermsOfService.skip_automatic_terms_creation = false + end + + it "should create a default terms_of_service on root account creation" do + ac2 = account_model + expect(ac2.terms_of_service.terms_type).to eq TermsOfService.term_options_for_account(ac2)[:terms_type] + sub = ac2.sub_accounts.create! + expect(sub.terms_of_service).to be nil + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fb70875f6b0..7db8e1b920a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -349,6 +349,7 @@ RSpec.configure do |config| RequestStore.clear! MultiCache.reset Course.enroll_user_call_count = 0 + TermsOfService.skip_automatic_terms_creation = true $spec_api_tokens = {} end