add query scopes to message handler

fixes: PLAT-649

test-plan
*specs should pass

Change-Id: I1902a1e5ad516ee544d1981b2ba56b35cce45691
Reviewed-on: https://gerrit.instructure.com/41262
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Nathan Mills <nathanm@instructure.com>
QA-Review: Nathan Mills <nathanm@instructure.com>
Reviewed-by: Brad Humphrey <brad@instructure.com>
This commit is contained in:
Nathan Mills 2014-09-17 12:32:32 -06:00
parent 0491ae3211
commit 60d48cc0fc
2 changed files with 81 additions and 0 deletions

View File

@ -28,5 +28,12 @@ module Lti
validates_presence_of :message_type, :resource_handler, :launch_path
scope :by_message_types, lambda { |*message_types| where('lti_message_handlers.message_type IN (?)', message_types) }
scope :for_context, lambda { |context|
tool_proxies = ToolProxy.find_active_proxies_for_context(context)
joins(:resource_handler).where('lti_resource_handlers.tool_proxy_id' => tool_proxies)
}
end
end

View File

@ -21,6 +21,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
module Lti
describe MessageHandler do
let (:account) { Account.create }
let (:product_family) { ProductFamily.create(vendor_code: '123', product_code: 'abc', vendor_name: 'acme', root_account: account) }
describe 'validations' do
before(:each) do
subject.message_type = 'message_type'
@ -45,9 +48,80 @@ module Lti
subject.save
subject.errors.first.should == [:resource_handler, "can't be blank"]
end
end
describe 'scope #message_type' do
it 'returns all message_handlers for a message_type' do
mh1 = create_message_handler
mh2 = create_message_handler
mh3 = create_message_handler(create_resource_handler, message_type: 'content_item')
message_handlers = described_class.by_message_types('basic-lti-launch-request')
message_handlers.count.should == 2
end
it 'returns all message_handlers for mutlipe message types' do
rh = create_resource_handler
mh1 = create_message_handler(rh)
mh2 = create_message_handler(rh, message_type: 'other_type')
mh3 = create_message_handler(rh, message_type: 'content_item')
message_handlers = described_class.by_message_types('basic-lti-launch-request', 'other_type')
message_handlers.count.should == 2
end
end
describe 'scope #for_tool_proxies' do
it 'returns all message_handlers for a tool proxy' do
tp = create_tool_proxy
tp.bindings.create(context: account)
message_handlers = (1..3).map do |code|
rh = create_resource_handler(tp, resource_type_code: code)
create_message_handler(rh)
end
Set.new(described_class.for_context(account)).should == Set.new(message_handlers)
end
it 'returns all message_handlers for multiple tool_proxy' do
tool_proxies = (1..3).map {|_| create_tool_proxy}
message_handlers = tool_proxies.map do |tp|
tp.bindings.create(context: account)
rh = create_resource_handler(tp)
create_message_handler(rh)
end
Set.new(described_class.for_context(account)).should == Set.new(message_handlers)
end
end
def create_tool_proxy(opts = {})
default_opts = {
context: account,
shared_secret: 'shared_secret',
guid: SecureRandom.uuid,
product_version: '1.0beta',
lti_version: 'LTI-2p0',
product_family: product_family,
workflow_state: 'active',
raw_data: 'some raw data'
}
ToolProxy.create(default_opts.merge(opts))
end
def create_resource_handler(tool_proxy = create_tool_proxy, opts = {})
default_opts = {resource_type_code: 'code', name: 'resource name', tool_proxy: tool_proxy}
ResourceHandler.create(default_opts.merge(opts))
end
def create_message_handler(resource_handler = create_resource_handler, opts = {})
default_ops = {message_type: 'basic-lti-launch-request', launch_path: 'https://samplelaunch/blti', resource_handler: resource_handler}
MessageHandler.create(default_ops.merge(opts))
end
end
end