Skip to content

Commit

Permalink
LTI-42: Add rubocop and automated test with test with travis to LTI2P… (
Browse files Browse the repository at this point in the history
#7)

* LTI-42: Add rubocop and automated test with test with travis to LTI2Provider gem

travis changes

gemfile add rubocop

bundle install

rubocop autocorrect

rubocop no offenses

add manifest.js file

change manifest.js

changed travis build from rspec to rails test

no db

rubocop frozen_string_literal

LTI-42: Add rubocop and automated test with test with travis to LTI2Provider gem

* Update rails_lti2_provider.gemspec

* Updated rubocop changes

* update travis ruby version

* rubocop in line with broker's

Co-authored-by: Jesus Federico <[email protected]>
  • Loading branch information
Billzzhang and jfederico authored Oct 7, 2020
1 parent 840ad41 commit 520e264
Show file tree
Hide file tree
Showing 66 changed files with 560 additions and 369 deletions.
59 changes: 59 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
inherit_mode:
merge:
- Exclude
require:
- rubocop-rails
AllCops:
Exclude:
- bin/puma
- bin/pumactl
- bin/setup
- bin/rainbows
- bin/unicorn
- bin/unicorn_rails
- bin/update
- bin/yarn
- vendor/bundle/**/*
TargetRubyVersion: 2.6
Layout/HashAlignment:
EnforcedHashRocketStyle: [ key, table ]
EnforcedColonStyle: [ key, table ]
Layout/LineLength:
Max: 200
Metrics:
Enabled: false
Naming/MethodParameterName:
MinNameLength: 2
Rails:
Enabled: true
Rails/DynamicFindBy:
# The models used in this code aren't actually ActiveRecord
Enabled: false
Style/AsciiComments:
AllowedChars: [ © ]
Style/Documentation:
Enabled: false
Style/DoubleNegation:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Style/MethodCallWithArgsParentheses:
Enabled: true
IgnoredMethods:
- desc
- gem
- include
- require
- require_relative
- ruby
- raise
- source
- throw
Exclude:
- "**/*.xml.builder"
Style/SymbolArray:
MinSize: 6
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
sudo: required
language: ruby
cache: bundler
rvm:
- 2.7.1
jobs:
include:
- stage: test
name: rubocop
script: bundle install & bundle exec rubocop
if: env(TEST_IGNORE) IS NOT present
- stage: test
name: test
before_script:
- bundle install
script:
- export
- bundle exec rails test
if: env(TEST_IGNORE) IS NOT present
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

# Declare your gem's dependencies in rails_lti2_provider.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
Expand All @@ -11,4 +13,4 @@ gemspec
# your gem to rubygems.org.

# To use debugger
# gem 'debugger'
# gem 'debugger'
11 changes: 5 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
puts('You must `gem install bundler` and `bundle install` to run rake tasks')
end

require 'rdoc/task'
Expand All @@ -14,10 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/test_app/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'


APP_RAKEFILE = File.expand_path('spec/test_app/Rakefile', __dir__)
load('rails/tasks/engine.rake')

Bundler::GemHelper.install_tasks

5 changes: 5 additions & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// frozen_string_literal: true

//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
2 changes: 2 additions & 0 deletions app/controllers/rails_lti2_provider/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsLti2Provider
class ApplicationController < ActionController::Base
end
Expand Down
35 changes: 18 additions & 17 deletions app/controllers/rails_lti2_provider/tools_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsLti2Provider
class ToolsController < ApplicationController
include RailsLti2Provider::ControllerHelpers
Expand All @@ -8,52 +10,51 @@ def register
redirect_to_consumer(register_proxy(@registration))
end

#alternative action for custom registration workflow
# alternative action for custom registration workflow
def submit_proxy
registration = RailsLti2Provider::Registration.find(params[:registration_uuid])
begin
response = register_proxy(registration)
rescue IMS::LTI::ToolProxyRegistrationError
response = {
return_url: registration.registration_request.launch_presentation_return_url,
status: 'error',
message: "Failed to create a tool proxy",
return_url: registration.registration_request.launch_presentation_return_url,
status: 'error',
message: 'Failed to create a tool proxy',
}
end
redirect_to_consumer(response)
end

def show

end
def show; end

def apply_rereg
tool = Tool.where(uuid: params[:tool_proxy_guid]).first
registration = tool.registrations.where(correlation_id: params[:correlation_id]).first
render status: :not_found and return unless registration
render(status: :not_found) && return unless registration

tool_proxy = tool.tool_proxy
registered_proxy = registration.tool_proxy
if tc_secret = registered_proxy.tc_half_shared_secret
shared_secret = tc_secret + tool_proxy.security_contract.tp_half_shared_secret
else
shared_secret = tool_proxy.security_contract.shared_secret
end
shared_secret = if (tc_secret = registered_proxy.tc_half_shared_secret)
tc_secret + tool_proxy.security_contract.tp_half_shared_secret
else
tool_proxy.security_contract.shared_secret
end
tool.transaction do
tool.shared_secret= shared_secret
tool.shared_secret = shared_secret
tool.tool_settings = registered_proxy.as_json
tool.lti_version = registered_proxy.lti_version
tool.save!
registration.update!(workflow_state: 'registered')
end
render nothing: true
render(nothing: true)
end

def delete_rereg
tool = Tool.where(uuid: params[:tool_proxy_guid]).first
registration = tool.registrations.where(correlation_id: params[:correlation_id]).first
render status: :not_found and return unless registration
render(status: :not_found) && return unless registration

registration.delete!
end

end
end
2 changes: 2 additions & 0 deletions app/helpers/rails_lti2_provider/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsLti2Provider
module ApplicationHelper
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/rails_lti2_provider/tools_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RailsLti2Provider
module ToolsHelper
end
Expand Down
33 changes: 18 additions & 15 deletions app/models/rails_lti2_provider/lti_launch.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
# frozen_string_literal: true

require 'ims/lti'

module RailsLti2Provider
class LtiLaunch < ActiveRecord::Base
validates_presence_of :tool_id, :nonce
class LtiLaunch < ApplicationRecord
validates :tool_id, :nonce, presence: true
belongs_to :tool
serialize :message

def self.check_launch(lti_message)
tool = Tool.find_by_uuid(lti_message.oauth_consumer_key)
raise Unauthorized.new(:invalid_key) unless tool
raise Unauthorized.new(:invalid_signature) unless IMS::LTI::Services::MessageAuthenticator.new(lti_message.launch_url, lti_message.post_params.merge(lti_message.oauth_params), tool.shared_secret).valid_signature?
raise Unauthorized.new(:invalid_nonce) if tool.lti_launches.where(nonce: lti_message.oauth_nonce).count > 0
raise Unauthorized.new(:request_too_old) if DateTime.strptime(lti_message.oauth_timestamp,'%s') < 5.minutes.ago

Rails.logger.info "Removing the old launches from before #{1.day.ago}"
tool.lti_launches.where('created_at < ?', 1.day.ago).delete_all
raise Unauthorized, :invalid_key unless tool
unless IMS::LTI::Services::MessageAuthenticator
.new(lti_message.launch_url,
lti_message.post_params.merge(lti_message.oauth_params), tool.shared_secret).valid_signature?
raise Unauthorized, :invalid_signature
end
raise Unauthorized, :invalid_nonce if tool.lti_launches.where(nonce: lti_message.oauth_nonce).count.positive?
raise Unauthorized, :request_to_old if DateTime.strptime(lti_message.oauth_timestamp, '%s') < 5.minutes.ago

launch = tool.lti_launches.create!(nonce: lti_message.oauth_nonce, message: lti_message.post_params)
Rails.logger.info "Launch created launch=#{launch.inspect}"
launch
tool.lti_launches.where('created_at > ?', 1.day.ago).delete_all
tool.lti_launches.create(nonce: lti_message.oauth_nonce, message: lti_message.post_params)
end

def message
IMS::LTI::Models::Messages::Message.generate(read_attribute(:message))
IMS::LTI::Models::Messages::Message.generate(self[:message])
end

def jwt_body
read_attribute(:message)
self[:message]
end

class Unauthorized < StandardError;
class Unauthorized < StandardError
attr_reader :error

def initialize(error = :unknown)
@error = error
end
Expand Down
12 changes: 5 additions & 7 deletions app/models/rails_lti2_provider/registration.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
# frozen_string_literal: true

module RailsLti2Provider
class Registration < ActiveRecord::Base
class Registration < ApplicationRecord
validates :correlation_id, uniqueness: true, allow_nil: true
serialize :tool_proxy_json, JSON
serialize :registration_request_params, JSON
belongs_to :tool, optional: true

def register

end

def register; end

def registration_request
@registration_request ||= IMS::LTI::Models::Messages::Message.generate(registration_request_params)
end

def tool_proxy
IMS::LTI::Models::ToolProxy.from_json(self.tool_proxy_json)
IMS::LTI::Models::ToolProxy.from_json(tool_proxy_json)
end

def tool_consumer_profile
@tool_consumer_profile ||= IMS::LTI::Services::ToolProxyRegistrationService.new(registration_request).tool_consumer_profile
end

end
end
4 changes: 3 additions & 1 deletion app/models/rails_lti2_provider/tenant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module RailsLti2Provider
class Tenant < ApplicationRecord
has_many :tools
has_many :tools, dependent: :restrict_with_exception
end
end
22 changes: 11 additions & 11 deletions app/models/rails_lti2_provider/tool.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# frozen_string_literal: true

module RailsLti2Provider
class Tool < ActiveRecord::Base
validates_presence_of :shared_secret, :uuid, :tool_settings, :lti_version
class Tool < ApplicationRecord
validates :shared_secret, :uuid, :tool_settings, :lti_version, presence: true
serialize :tool_settings
belongs_to :tenant, foreign_key: :tenant_id
has_many :lti_launches
has_many :registrations
belongs_to :tenant, inverse_of: :tools
has_many :lti_launches, dependent: :restrict_with_exception
has_many :registrations, dependent: :restrict_with_exception

def tool_proxy
IMS::LTI::Models::ToolProxy.from_json(tool_settings)
end

def self.find_by_issuer(issuer, options = {})
if options.any?
puts options.inspect
Tool.where(uuid: issuer).each do |tool|
Rails.logger.warn(options.inspect)
Tool.where(uuid: issuer).find_each do |tool|
tool_settings = JSON.parse(tool.tool_settings)
match = true
options.each do |key, value|
if tool_settings[key] != options[key]
match = false
end
options.each do |key, _value|
match = false if tool_settings[key] != options[key]
end
return tool if match
end
Expand Down
8 changes: 5 additions & 3 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/rails_lti2_provider/engine', __FILE__)
ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/rails_lti2_provider/engine', __dir__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
Expand Down
6 changes: 4 additions & 2 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
Expand All @@ -7,8 +9,8 @@
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'
Expand Down
Loading

0 comments on commit 520e264

Please sign in to comment.