From 1362052ad51c10c6082f32e0a300745cb8cb9264 Mon Sep 17 00:00:00 2001 From: Marc Phillips Date: Thu, 13 Jun 2019 17:13:52 -0600 Subject: [PATCH] Add additional context to live events fixes PLAT-4441 Test Plan: - see that they are added correctly given the context for each value being added Change-Id: I60127a66981b7a1b8aa1bc157446a3e83c61f379 Reviewed-on: https://gerrit.instructure.com/197643 Tested-by: Jenkins Reviewed-by: Clint Furse Reviewed-by: Weston Dransfield QA-Review: Marc Phillips Product-Review: Marc Phillips --- app/controllers/application_controller.rb | 3 ++ app/models/context.rb | 13 ++++++ doc/api/live_events.md | 3 ++ .../application_controller_spec.rb | 46 ++++++++++++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 59a1465cbf2..7e49a1686c4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2410,10 +2410,12 @@ class ApplicationController < ActionController::Base ctx[:user_id] = @current_user.global_id if @current_user ctx[:time_zone] = @current_user.time_zone if @current_user + ctx[:developer_key_id] = @access_token.developer_key.global_id if @access_token ctx[:real_user_id] = @real_current_user.global_id if @real_current_user ctx[:context_type] = @context.class.to_s if @context ctx[:context_id] = @context.global_id if @context ctx[:context_sis_source_id] = @context.sis_source_id if @context.respond_to?(:sis_source_id) + ctx[:context_account_id] = Context.get_account_or_parent_account(@context)&.global_id if @context if @context_membership ctx[:context_role] = @@ -2432,6 +2434,7 @@ class ApplicationController < ActionController::Base end ctx[:hostname] = request.host + ctx[:http_method] = request.method ctx[:user_agent] = request.headers['User-Agent'] ctx[:client_ip] = request.remote_ip ctx[:url] = request.url diff --git a/app/models/context.rb b/app/models/context.rb index 3f5ac2058bd..efcc50a4648 100644 --- a/app/models/context.rb +++ b/app/models/context.rb @@ -302,6 +302,19 @@ module Context end end + def self.get_account_or_parent_account(context) + case context + when Account + context.root_account? ? context : context.parent_account + when Course + get_account(context.account) + when CourseSection + get_account(context.course) + when Group + get_account(context.context) + end + end + def is_a_context? true end diff --git a/doc/api/live_events.md b/doc/api/live_events.md index 023a1d86f5e..13a2d1b8757 100644 --- a/doc/api/live_events.md +++ b/doc/api/live_events.md @@ -36,6 +36,7 @@ event originated as part of a web request: | Name | Type | Description | | ---- | ---- | ----------- | | `user_id` | String | The Canvas id of the currently logged in user. | +| `developer_key_id` | String | The id of the Developer Key used to create the access token for user. | | `real_user_id` | String | If the current user is being masqueraded, this is the Canvas id of the masquerading user. | | `time_zone` | String | Time zone of the currently logged in user. | | `user_login` | String | The login of the current user. | @@ -46,8 +47,10 @@ event originated as part of a web request: | `context_type` | String | The type of context where the event happened. | | `context_id` | String | The Canvas id of the current context. Always use the `context_type` when using this id to lookup the object. | | `context_sis_source_id` | String | The Canvas SIS source id of the current context. | +| `context_account_id` | String | The account id of the current context. This is the actual accound the context is attached to. | | `role` | String | The role of the current user in the current context. | | `hostname` | String | The hostname of the current request | +| `http_method` | String | The method of the request that triggered the event. | | `producer` | String | The name of the producer of an event. Will always be 'canvas' when an event is originating in canvas. | | `request_id` | String | The identifier for this request. | | `session_id` | String | The session identifier for this request. Can be used to correlate events in the same session for a user. | diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 80eb0c96316..3452e2329e8 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -25,6 +25,7 @@ RSpec.describe ApplicationController do host_with_port: "www.example.com", host: "www.example.com", url: "http://www.example.com", + method: "GET", headers: {}, format: double(:html? => true), user_agent: nil, @@ -1096,7 +1097,8 @@ describe ApplicationController do user_agent: 'Rails Testing', client_ip: '0.0.0.0', producer: 'canvas', - url: 'http://test.host' + url: 'http://test.host', + http_method: 'GET' } end @@ -1188,6 +1190,27 @@ describe ApplicationController do end end + context 'when an access_token exists' do + let(:real_access_token_attributes) do + { + developer_key: double(global_id: '1111') + } + end + + let(:expected_context_attributes) do + { + developer_key_id: '1111' + }.merge(non_conditional_values) + end + + it 'sets the correct attributes on the LiveEvent context' do + real_access_token = double(real_access_token_attributes) + controller.instance_variable_set(:@access_token, real_access_token) + 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 { @@ -1224,7 +1247,8 @@ describe ApplicationController do let(:expected_context_attributes) do { context_type: 'Class', - context_id: 'context_global_id' + context_id: 'context_global_id', + context_account_id: nil }.merge(non_conditional_values) end @@ -1234,6 +1258,24 @@ describe ApplicationController do controller.send(:setup_live_events_context) expect(LiveEvents.get_context).to eq(expected_context_attributes) end + + context 'when a course' do + let(:course) { course_model } + let(:expected_context_attributes) do + { + context_type: 'Course', + context_id: course.global_id.to_s, + context_account_id: course.account.global_id.to_s, + context_sis_source_id: nil + }.merge(non_conditional_values) + end + + it 'sets the correct attributes on the LiveEvent context' do + controller.instance_variable_set(:@context, course) + controller.send(:setup_live_events_context) + expect(LiveEvents.get_context).to eq(expected_context_attributes) + end + end end context 'when a context_membership exists' do