add ObserverPairingCode model

closes MBL-10642

test plan:
- in the rails console,
  - create an ObserverPairingCode for a student
    @student.generate_observer_pairing_code
  - read pairing codes for a student
    @student.observer_pairing_codes
- generated codes should expire in 1 day
- expired and deleted codes should not be returned
  in the above call to read codes

Change-Id: I1c0bf25bca4ef34016cff94ce721e3d339e251e1
Reviewed-on: https://gerrit.instructure.com/152105
Reviewed-by: Matthew Sessions <msessions@instructure.com>
Product-Review: Matthew Sessions <msessions@instructure.com>
QA-Review: Matthew Sessions <msessions@instructure.com>
Tested-by: Jenkins
This commit is contained in:
Cameron Sutter 2018-05-31 15:06:06 -06:00
parent f27490c001
commit e474a8120c
4 changed files with 113 additions and 0 deletions

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 ObserverPairingCode < ActiveRecord::Base
belongs_to :user, inverse_of: :observer_pairing_codes
validates :user_id, :expires_at, :code, presence: true
scope :active, -> { where("workflow_state<>'deleted' AND expires_at > ?", Time.zone.now) }
def destroy
self.workflow_state = 'deleted'
self.save!
end
end

View File

@ -61,6 +61,8 @@ class User < ActiveRecord::Base
has_many :observer_enrollments
has_many :observee_enrollments, :foreign_key => :associated_user_id, :class_name => 'ObserverEnrollment'
has_many :observer_pairing_codes, -> { where("workflow_state<>'deleted' AND expires_at > ?", Time.zone.now) }, dependent: :destroy, inverse_of: :user
has_many :as_student_observation_links, -> { where.not(:workflow_state => 'deleted') }, class_name: 'UserObservationLink',
foreign_key: :user_id, dependent: :destroy, inverse_of: :student
has_many :as_observer_observation_links, -> { where.not(:workflow_state => 'deleted') }, class_name: 'UserObservationLink',
@ -2843,4 +2845,8 @@ class User < ActiveRecord::Base
end
User.where(:id => id_token_map.keys).to_a.select { |u| u.token == id_token_map[u.id] }
end
def generate_observer_pairing_code
observer_pairing_codes.create(expires_at: 1.day.from_now, code: SecureRandom.hex(3))
end
end

View File

@ -0,0 +1,31 @@
#
# 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 CreateObserverPairingCodes < ActiveRecord::Migration[5.1]
tag :predeploy
def change
create_table :observer_pairing_codes do |t|
t.belongs_to :user, :null => false, :foreign_key => true
t.string :code, :null => false, :limit => 10
t.timestamp :expires_at, :null => false, :index => true
t.string :workflow_state, :default => 'active', :null => false, :index => true
t.timestamps
end
end
end

View File

@ -0,0 +1,47 @@
#
# 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_relative '../spec_helper'
describe ObserverPairingCode do
before :once do
@student = user_model
end
it 'can pass validations' do
code = ObserverPairingCode.create(user: @student, expires_at: 1.day.from_now, code: SecureRandom.hex(3))
expect(code.valid?).to eq true
end
it 'can be generated from a user' do
code = @student.generate_observer_pairing_code
expect(code).not_to be_nil
end
it 'can generate more than one code' do
code = @student.generate_observer_pairing_code
code2 = @student.generate_observer_pairing_code
expect(code2.id).not_to eq code.id
end
it 'ignores expired codes' do
ObserverPairingCode.create(user: @student, expires_at: 1.day.ago, code: SecureRandom.hex(3))
codes = @student.observer_pairing_codes
expect(codes.length).to eq 0
end
end