diff --git a/.gitignore b/.gitignore index 98b75505..f251d917 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,7 @@ process.yml localhost.crt localhost.key -/public/assets \ No newline at end of file +/public/assets + +# ignore rspec coverage +coverage/* \ No newline at end of file diff --git a/Gemfile b/Gemfile index 5bf2267c..319aa725 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri gem 'dotenv-rails' + gem 'rails-controller-testing' gem 'rspec' gem 'rspec-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index b86e6f1b..5a7738d1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -228,6 +228,10 @@ GEM bundler (>= 1.3.0) railties (= 6.0.3.7) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.5) + actionpack (>= 5.0.1.rc1) + actionview (>= 5.0.1.rc1) + activesupport (>= 5.0.1.rc1) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -383,6 +387,7 @@ DEPENDENCIES pg (>= 0.4.4) puma (>= 4.3.5) rails (>= 6.0.3.3) + rails-controller-testing rails_lti2_provider! react-rails redis (~> 4.2) diff --git a/lib/tasks/db_tenants.rake b/lib/tasks/db_tenants.rake index a95fb162..465b0d8b 100644 --- a/lib/tasks/db_tenants.rake +++ b/lib/tasks/db_tenants.rake @@ -42,7 +42,7 @@ namespace :db do exit(1) end puts("Deleting keys for tenant '#{tenant.uid}'") - RailsLti2Provider::Tool.delete_all(tenant: tenant) + RailsLti2Provider::Tool.delete_by(tenant: tenant) puts("Deleting tenant '#{tenant.uid}'") tenant.delete rescue StandardError => e @@ -60,7 +60,7 @@ namespace :db do next if tenant.uid.empty? puts("Deleting keys for tenant '#{tenant.uid}'") - RailsLti2Provider::Tool.delete_all(tenant: tenant) + RailsLti2Provider::Tool.delete_by(tenant: tenant) puts("Deleting tenant '#{tenant.uid}'") tenant.delete end diff --git a/spec/controllers/registration_controller_spec.rb b/spec/controllers/registration_controller_spec.rb new file mode 100644 index 00000000..f66d5734 --- /dev/null +++ b/spec/controllers/registration_controller_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +require 'rails_helper' + +RSpec.describe(RegistrationController, type: :controller) do + before do + ENV['DEVELOPER_MODE_ENABLED'] = 'true' + tenant = RailsLti2Provider::Tenant.create!(uid: '') + reg = { + issuer: 'issuer', + client_id: 'client_id', + key_set_url: 'key_set_url', + auth_token_url: 'auth_token_url', + auth_login_url: 'auth_login_url', + } + @app = RailsLti2Provider::Tool.create!( + uuid: 'issuer', + shared_secret: 'client_id', + tool_settings: reg.to_json, + lti_version: '1.3.0', + tenant: tenant + ) + end + + describe 'GET registration/new' do + it 'gives a successful response' do + get :new + expect(response).to(be_successful) + end + end + + describe 'POST registration/submit' do + it 'adds another tool' do + post :submit, params: { + iss: 'test.com', + client_id: 'test_secret', + key_set_url: 'key.test.com', + auth_token_url: 'auth-token.test.com', + auth_login_url: 'auth-login.test.com', + } + expect(response).to(redirect_to(:registration_list)) + end + end + + describe 'GET registration/list' do + it 'gives a successful response' do + get :list + expect(response).to(be_successful) + end + + it 'gives a proper list of all items' do + get :list + current_tools = RailsLti2Provider::Tool.where(lti_version: '1.3.0').pluck(:tool_settings) + current_tools.map! do |reg| + JSON.parse(reg) + end + expect(assigns(:registrations)).to(eq(current_tools)) + end + end + + describe 'GET registration/edit' do + it 'gives a successful response' do + get :edit, params: { client_id: @app.shared_secret, reg_id: @app.uuid } + expect(response).to(be_successful) + end + it 'edits an existing tool' do + post :submit, params: { + iss: 'test.com', + client_id: 'test_secret', + key_set_url: 'edit-key.test.com', + auth_token_url: 'edit-auth-token.test.com', + auth_login_url: 'edit-auth-login.test.com', + } + expect(response).to(redirect_to(:registration_list)) + end + end +end diff --git a/spec/rake_tasks/rake_apps_spec.rb b/spec/rake_tasks/rake_apps_spec.rb new file mode 100644 index 00000000..2a483275 --- /dev/null +++ b/spec/rake_tasks/rake_apps_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +require 'rails_helper' +require 'rake' + +RSpec.describe('rake tasks involving apps') do + before do + Rake.application.rake_require('tasks/db_apps') + Rake::Task.define_task(:environment) + + @app = Doorkeeper::Application.create!(name: 'test-app', uid: 'key', secret: 'secret', \ + redirect_uri: 'https://test.example.com', scopes: 'api') + end + describe 'calling db:app' do + it 'displays the existing app' do + appinfo ||= @app.attributes.select { |key, _value| %w[name uid secret redirect_uri].include?(key) } + expect do + Rake.application.invoke_task('db:apps:showall') + end.to(output(appinfo.to_json + "\n").to_stdout) + end + it 'and adding an app' do + Rake.application.invoke_task('db:apps:add[test-add-app,https://test.example.com,key-add,secret-add]') + end + it 'and updating an app' do + Rake.application.invoke_task('db:apps:update[test-app,https://test.example.com,key-updated,secret-updated]') + end + it 'and deleting an app' do + Rake.application.invoke_task('db:apps:delete[test-app]') + end + end +end diff --git a/spec/rake_tasks/rake_keys_spec.rb b/spec/rake_tasks/rake_keys_spec.rb new file mode 100644 index 00000000..db9f2c9e --- /dev/null +++ b/spec/rake_tasks/rake_keys_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +require 'rails_helper' +require 'rake' + +RSpec.describe('rake tasks involving keys') do + before do + Rake.application.rake_require('tasks/db_keys') + Rake::Task.define_task(:environment) + + @tenant = RailsLti2Provider::Tenant.create!(uid: 'tenant') + @other_tenant = RailsLti2Provider::Tenant.create!(uid: 'other-tenant') + @single_tenant_key = RailsLti2Provider::Tool.create!(uuid: 'single-key', shared_secret: 'secret', lti_version: 'LTI-1p0', + tool_settings: 'none', tenant: RailsLti2Provider::Tenant.create!(uid: '')) + @multi_tenant_key = RailsLti2Provider::Tool.create!(uuid: 'multi-key', shared_secret: 'secret', lti_version: 'LTI-1p0', + tool_settings: 'none', tenant: @tenant) + end + describe 'calling db:keys' do + it 'displays the existing keys' do + expected_keys_text = "'single-key'='secret'\n'multi-key'='secret' for tenant 'tenant'\n" + expect do + Rake.application.invoke_task('db:keys:showall') + end.to(output(expected_keys_text).to_stdout) + end + it 'and adding an key in single tenant' do + Rake.application.invoke_task('db:keys:add[single-test-key, secret]') + end + it 'and adding an key in multi tenant' do + Rake.application.invoke_task('db:keys:add[single-test-key, secret, tenant]') + end + it 'and updating an key in single tenant' do + Rake.application.invoke_task('db:keys:update[single-key,secret-updated]') + end + it 'and updating an key in multi tenant' do + Rake.application.invoke_task('db:keys:update[multi-key,secret-updated, other-tenant]') + end + it 'and deleting an key' do + Rake.application.invoke_task('db:keys:delete[single-key]') + end + end +end diff --git a/spec/rake_tasks/rake_tenant_spec.rb b/spec/rake_tasks/rake_tenant_spec.rb new file mode 100644 index 00000000..817c72c1 --- /dev/null +++ b/spec/rake_tasks/rake_tenant_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +require 'rails_helper' +require 'rake' + +RSpec.describe('rake tasks involving tenants') do + before do + Rake.application.rake_require('tasks/db_tenants') + Rake::Task.define_task(:environment) + @tenant = RailsLti2Provider::Tenant.create!(uid: 'tenant') + end + + describe 'calling db:tenants' do + it 'and displays the tenant' do + tenant_text = /Tenant with uid 'tenant'/ + expect do + Rake.application.invoke_task('db:tenants:showall') + end.to(output(tenant_text).to_stdout) + end + it 'and adds a tenant' do + Rake.application.invoke_task('db:tenants:add[test-tenant]') + end + it 'and deletes a tenant' do + Rake.application.invoke_task('db:tenants:delete[tenant]') + end + it 'and deletes all tenants' do + Rake.application.invoke_task('db:tenants:deleteall') + end + end +end