Add root_account_uuid to canvas LiveEvents

Fixes: PLAT-2369

Test-Plan:
  - spinup local live-event testing services
  - test out live-event spawning processes
    -- check live event docs for triggers of events
  - assert that the kinesis stream contains events with attributes
    containing `root_account_uuid`

Change-Id: I3ad3d64621c7726d30de9a8a8824e8b0e5d5aab2
Reviewed-on: https://gerrit.instructure.com/105621
Tested-by: Jenkins
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Tucker McKnight <tmcknight@instructure.com>
Product-Review: Jayce Higgins <jhiggins@instructure.com>
This commit is contained in:
Jayce Higgins 2017-03-17 17:19:33 -05:00
parent 0939898e56
commit 4420c72cfb
5 changed files with 222 additions and 2 deletions

View File

@ -2136,8 +2136,13 @@ class ApplicationController < ActionController::Base
def setup_live_events_context
ctx = {}
ctx[:root_account_id] = @domain_root_account.global_id if @domain_root_account
ctx[:root_account_lti_guid] = @domain_root_account.lti_guid if @domain_root_account
if @domain_root_account
ctx[:root_account_uuid] = @domain_root_account.uuid
ctx[:root_account_id] = @domain_root_account.global_id
ctx[:root_account_lti_guid] = @domain_root_account.lti_guid
end
ctx[:user_id] = @current_user.global_id if @current_user
ctx[:real_user_id] = @real_current_user.global_id if @real_current_user
ctx[:user_login] = @current_pseudonym.unique_id if @current_pseudonym

View File

@ -39,6 +39,7 @@ event originated as part of a web request:
| `real_user_id` | String | If the current user is being masqueraded, this is the Canvas id of the masquerading user. |
| `user_login` | String | The login of the current user. |
| `user_agent` | String | The User-Agent sent by the browser making the request. |
| `root_account_uuid` | String | The Canvas uuid of the root account associated with the current user. |
| `root_account_id` | String | The Canvas id of the root account associated with the current user. |
| `root_account_lti_guid` | String | The Canvas lti_guid of the root account associated with the current user. |
| `context_type` | String | The type of context where the event happened. |

View File

@ -11,6 +11,7 @@ module Canvas::LiveEvents
context_type: canvas_context.class.to_s,
context_id: canvas_context.global_id,
root_account_id: canvas_context.root_account.try(:global_id),
root_account_uuid: canvas_context.root_account.try(:uuid),
root_account_lti_guid: canvas_context.root_account.try(:lti_guid),
})
end

View File

@ -742,6 +742,194 @@ describe ApplicationController do
expect(discard).to be_empty
end
end
describe '#setup_live_events_context' do
let(:non_conditional_values) do
{
hostname: 'test.host',
user_agent: 'Rails Testing'
}
end
before(:each) do
Thread.current[:context] = nil
end
it 'stringifies the non-strings in the context attributes' do
current_user_attributes = { global_id: 12345 }
current_user = stub(current_user_attributes)
controller.instance_variable_set(:@current_user, current_user)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq({user_id: '12345'}.merge(non_conditional_values))
end
context 'when a domain_root_account exists' do
let(:root_account_attributes) do
{
uuid: 'account_uuid1',
global_id: 'account_global1',
lti_guid: 'lti1'
}
end
let(:expected_context_attributes) do
{
root_account_uuid: 'account_uuid1',
root_account_id: 'account_global1',
root_account_lti_guid: 'lti1'
}.merge(non_conditional_values)
end
it 'adds root account values to the LiveEvent context' do
root_account = stub(root_account_attributes)
controller.instance_variable_set(:@domain_root_account, root_account)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
context 'when a current_user exists' do
let(:current_user_attributes) do
{
global_id: 'user_global_id'
}
end
let(:expected_context_attributes) do
{
user_id: 'user_global_id'
}.merge(non_conditional_values)
end
it 'sets the correct attributes on the LiveEvent context' do
current_user = stub(current_user_attributes)
controller.instance_variable_set(:@current_user, current_user)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
context 'when a real current_user exists' do
let(:real_current_user_attributes) do
{
global_id: 'real_user_global_id'
}
end
let(:expected_context_attributes) do
{
real_user_id: 'real_user_global_id'
}.merge(non_conditional_values)
end
it 'sets the correct attributes on the LiveEvent context' do
real_current_user = stub(real_current_user_attributes)
controller.instance_variable_set(:@real_current_user, real_current_user)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
context 'when a real current_pseudonym exists' do
let(:current_pseudonym_attributes) do
{
unique_id: 'unique_id'
}
end
let(:expected_context_attributes) do
{
user_login: 'unique_id'
}.merge(non_conditional_values)
end
it 'sets the correct attributes on the LiveEvent context' do
current_pseudonym = stub(current_pseudonym_attributes)
controller.instance_variable_set(:@current_pseudonym, current_pseudonym)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
context 'when a canvas context exists' do
let(:canvas_context_attributes) do
{
class: Class,
global_id: 'context_global_id'
}
end
let(:expected_context_attributes) do
{
context_type: 'Class',
context_id: 'context_global_id'
}.merge(non_conditional_values)
end
it 'sets the correct attributes on the LiveEvent context' do
canvas_context = stub(canvas_context_attributes)
controller.instance_variable_set(:@context, canvas_context)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
context 'when a context_membership exists' do
context 'when the context has a role' do
it 'sets the correct attributes on the LiveEvent context' do
stubbed_role = stub({ name: 'name' })
context_membership = stub({role: stubbed_role})
controller.instance_variable_set(:@context_membership, context_membership)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq({ context_role: 'name' }.merge(non_conditional_values))
end
end
context 'when the context has a type' do
it 'sets the correct attributes on the LiveEvent context' do
context_membership = stub({ type: 'type' })
controller.instance_variable_set(:@context_membership, context_membership)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq({ context_role: 'type' }.merge(non_conditional_values))
end
end
context 'when the context has neither a role or type' do
it 'sets the correct attributes on the LiveEvent context' do
context_membership = stub({ class: Class })
controller.instance_variable_set(:@context_membership, context_membership)
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq({ context_role: 'Class' }.merge(non_conditional_values))
end
end
end
context 'when the current thread has a context key' do
let(:thread_attributes) do
{
request_id: 'request_id',
session_id: 'session_id'
}
end
let(:expected_context_attributes) do
{
request_id: 'request_id',
session_id: 'session_id'
}.merge(non_conditional_values)
end
it 'sets the correct attributes on the LiveEvent context' do
Thread.current[:context] = thread_attributes
controller.send(:setup_live_events_context)
expect(LiveEvents.get_context).to eq(expected_context_attributes)
end
end
end
end
describe WikiPagesController do

View File

@ -25,6 +25,29 @@ describe Canvas::LiveEvents do
expect(LiveEvents).to receive(:post_event).with(event_name, event_body, anything, event_context)
end
describe '.amended_context' do
it 'pulls the context from the canvas context' do
course = course_model
amended_context = Canvas::LiveEvents.amended_context(course)
context_id = course.global_id
context_type = course.class.to_s
root_account_id = course.root_account.global_id
root_account_uuid = course.root_account.uuid
root_account_lti_guid = course.root_account.lti_guid
expect(amended_context).to eq(
{
:context_id => context_id,
:context_type => context_type,
:root_account_id => root_account_id,
:root_account_uuid => root_account_uuid,
:root_account_lti_guid => root_account_lti_guid
}
)
end
end
describe ".enrollment_updated" do
it "should not include associated_user_id for non-observer enrollments" do
enrollment = course_with_student
@ -154,6 +177,7 @@ describe Canvas::LiveEvents do
describe ".grade_changed" do
let(:course_context) do
hash_including(
root_account_uuid: @course.root_account.uuid,
root_account_id: @course.root_account.global_id,
root_account_lti_guid: @course.root_account.lti_guid,
context_id: @course.global_id,
@ -237,6 +261,7 @@ describe Canvas::LiveEvents do
it "includes course context even when global course context unset" do
allow(LiveEvents).to receive(:get_context).and_return({
root_account_uuid: nil,
root_account_id: nil,
root_account_lti_guid: nil,
context_id: nil,