From 630721d212bc10c2c566f6ba6418e81b19080812 Mon Sep 17 00:00:00 2001 From: August Thornton Date: Wed, 18 Mar 2020 11:34:27 -0600 Subject: [PATCH] spec: add Catalog consumer to Pact contract test suite closes USERS-436 test plan: - requires a pact contract in local or public broker - execute tests against Pact files - run `bin/contracts-verify-live-events` Change-Id: I847787780e1bc4e5024e0cd2c46001dac6e31dfa Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/230402 Tested-by: Service Cloud Jenkins QA-Review: August Thornton Product-Review: August Thornton Reviewed-by: Brian Watson Reviewed-by: Robin Kuss --- .../catalog/course_completed_spec.rb | 51 +++++++++++++++++++ .../live_events/live_events_pact_helper.rb | 43 +++++++--------- .../service_consumers/pact_config.rb | 7 ++- 3 files changed, 73 insertions(+), 28 deletions(-) create mode 100644 spec/contracts/service_consumers/live_events/catalog/course_completed_spec.rb diff --git a/spec/contracts/service_consumers/live_events/catalog/course_completed_spec.rb b/spec/contracts/service_consumers/live_events/catalog/course_completed_spec.rb new file mode 100644 index 00000000000..12a4ad56afb --- /dev/null +++ b/spec/contracts/service_consumers/live_events/catalog/course_completed_spec.rb @@ -0,0 +1,51 @@ +# +# Copyright (C) 2020 - 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 . +# + +require_relative '../live_events_pact_helper' + +RSpec.describe 'Canvas LMS Live Events', :pact_live_events do + describe 'course_completed' do + let(:live_event) do + LiveEvents::PactHelper::Event.new( + event_name: 'course_completed', event_subscriber: PactConfig::LiveEventConsumers::CATALOG + ) + end + + it 'keeps the contract' do + live_event.emit_with do + course_model + assignment_model(course: @course) + student_in_course(active_all: true, course: @course) + @user.update(email: 'user@example.com') + context_module = @course.context_modules.create! + tag = context_module.add_item({ id: @assignment.id, type: 'assignment' }) + context_module.completion_requirements = { tag.id => { type: 'must_submit' } } + context_module.update(requirement_count: 1) + context_module_progression = + context_module.context_module_progressions.create!( + user_id: @user.id, + workflow_state: 'completed', + requirements_met: [{ id: tag.id, type: 'must_submit' }] + ) + Canvas::LiveEvents.course_completed(context_module_progression) + end + + expect(live_event).to have_kept_the_contract + end + end +end diff --git a/spec/contracts/service_consumers/live_events/live_events_pact_helper.rb b/spec/contracts/service_consumers/live_events/live_events_pact_helper.rb index 13c0c0ab9c2..fb0bbd58d3c 100644 --- a/spec/contracts/service_consumers/live_events/live_events_pact_helper.rb +++ b/spec/contracts/service_consumers/live_events/live_events_pact_helper.rb @@ -26,6 +26,14 @@ module LiveEvents module PactHelper include PactConfig + def self.message_contract_for(consumer, event) + Pact::Messages.get_message_contract( + PactConfig::Providers::CANVAS_LMS_LIVE_EVENTS, + consumer, + event + ) + end + class Event attr_reader :event_message, :event_name, :event_settings, :event_subscriber, :stream_client @@ -66,11 +74,18 @@ module LiveEvents end def contract_message + # Canvas Live Event Subscribers + catalog = PactConfig::LiveEventConsumers::CATALOG + outcomes = PactConfig::LiveEventConsumers::OUTCOMES + quiz_lti = PactConfig::LiveEventConsumers::QUIZ_LTI + case event_subscriber - when PactConfig::LiveEventConsumers::QUIZ_LTI - LiveEvents::PactHelper.quiz_lti_contract_for(event_name) - when PactConfig::LiveEventConsumers::OUTCOMES - LiveEvents::PactHelper.outcomes_contract_for(event_name) + when catalog + LiveEvents::PactHelper.message_contract_for(catalog, event_name) + when outcomes + LiveEvents::PactHelper.message_contract_for(outcomes, event_name) + when quiz_lti + LiveEvents::PactHelper.message_contract_for(quiz_lti, event_name) else raise ArgumentError, "Invalid event_subscriber: #{event_subscriber}" end @@ -108,25 +123,5 @@ module LiveEvents } end end - - class << self - def quiz_lti_contract_for(event) - message_contract_for(PactConfig::LiveEventConsumers::QUIZ_LTI, event) - end - - def outcomes_contract_for(event) - message_contract_for(PactConfig::LiveEventConsumers::OUTCOMES, event) - end - - private - - def message_contract_for(consumer, event) - Pact::Messages.get_message_contract( - PactConfig::Providers::CANVAS_LMS_LIVE_EVENTS, - consumer, - event - ) - end - end end end diff --git a/spec/contracts/service_consumers/pact_config.rb b/spec/contracts/service_consumers/pact_config.rb index 0fb23597bcd..4411dd96f3e 100644 --- a/spec/contracts/service_consumers/pact_config.rb +++ b/spec/contracts/service_consumers/pact_config.rb @@ -37,7 +37,7 @@ module PactConfig FIU = 'lmsAPI'.freeze else # internal consumers - # These first 3 are legacy android consumers. ANDROID, below, represents our latest pact + # These first 3 are legacy android consumers. ANDROID, below, represents our latest pact # testing effort. ANDROID_STUDENT = 'Android Student'.freeze ANDROID_TEACHER = 'Android Teacher'.freeze @@ -53,6 +53,7 @@ module PactConfig # Add new Live Events consumers to this module module LiveEventConsumers + CATALOG = 'Catalog'.freeze OUTCOMES = 'Outcomes'.freeze QUIZ_LTI = 'Quiz LTI'.freeze end @@ -69,9 +70,7 @@ module PactConfig def broker_uri URI::HTTP.build( - scheme: protocol, - userinfo: "#{broker_username}:#{broker_password}", - host: broker_host + scheme: protocol, userinfo: "#{broker_username}:#{broker_password}", host: broker_host ).to_s end