diff --git a/Gemfile b/Gemfile index c6e9fd628865eaa9020a23ace522f5cc8d46b480..632fd727716b1196dda747eddaa922775551af5c 100644 --- a/Gemfile +++ b/Gemfile @@ -47,7 +47,6 @@ gem 'stackprof' group :development do gem 'annotate' - gem 'immigrant' # Generate ER Diagram from database (use: rake erd) @@ -95,6 +94,9 @@ end group :test do gem 'shoulda' gem 'minitest-reporters' + gem 'rspec-rails' + gem 'faker' + gem 'factory_girl_rails' end gem 'streamio-ffmpeg', '~> 1.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index caba06c98e1d5448a33b9931d3bdd601e03048ae..8ec46499af5aff41d4fbd0224e3cfe1cdcc84ab0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,7 +60,7 @@ GEM rake (>= 10.4, < 12.0) ansi (1.5.0) arel (6.0.3) - ast (2.2.0) + ast (2.3.0) axiom-types (0.1.1) descendants_tracker (~> 0.0.4) ice_nine (~> 0.11.0) @@ -74,12 +74,12 @@ GEM rack (>= 0.9.0) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) - brakeman (3.3.0) + brakeman (3.3.1) builder (3.2.2) bullet (5.1.0) activesupport (>= 3.0.0) uniform_notifier (~> 1.10.0) - byebug (9.0.4) + byebug (9.0.5) choice (0.2.0) chronic (0.10.2) climate_control (0.0.3) @@ -134,6 +134,7 @@ GEM devise_token_auth (0.1.37) devise (> 3.5.2, < 4.1) rails (< 6) + diff-lcs (1.2.5) dm-aggregates (1.2.0) dm-core (~> 1.2.0) dm-constraints (1.2.0) @@ -188,6 +189,13 @@ GEM erubis (2.7.0) eventmachine (1.0.9.1) execjs (2.7.0) + factory_girl (4.7.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.7.0) + factory_girl (~> 4.7.0) + railties (>= 3.0.0) + faker (1.6.3) + i18n (~> 0.5) faraday (0.9.2) multipart-post (>= 1.2, < 3) fastercsv (1.5.5) @@ -245,7 +253,7 @@ GEM mina (0.3.8) open4 (~> 1.3.4) rake - mini_portile2 (2.0.0) + mini_portile2 (2.1.0) minitest (5.9.0) minitest-reporters (1.1.9) ansi @@ -256,8 +264,9 @@ GEM multi_xml (0.5.5) multipart-post (2.0.0) net-http-persistent (2.9.4) - nokogiri (1.6.7.2) - mini_portile2 (~> 2.0.0.rc2) + nokogiri (1.6.8) + mini_portile2 (~> 2.1.0) + pkg-config (~> 1.1.7) oauth (0.5.1) oauth2 (1.1.0) faraday (>= 0.8, < 0.10) @@ -286,7 +295,7 @@ GEM omniauth-oauth (~> 1.1) open4 (1.3.4) orm_adapter (0.5.0) - paper_trail (5.1.0) + paper_trail (5.1.1) activerecord (>= 3.0, < 6.0) activesupport (>= 3.0, < 6.0) request_store (~> 1.1) @@ -302,6 +311,7 @@ GEM ast (~> 2.2) pg (0.18.4) phantomjs (2.1.1.0) + pkg-config (1.1.7) powerpack (0.1.1) ptools (1.3.3) public_activity (1.5.0) @@ -373,6 +383,23 @@ GEM responders (2.2.0) railties (>= 4.2.0, < 5.1) rmagick (2.15.4) + rspec-core (3.4.4) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.4.0) + rspec-rails (3.4.2) + actionpack (>= 3.0, < 4.3) + activesupport (>= 3.0, < 4.3) + railties (>= 3.0, < 4.3) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) rubocop (0.40.0) parser (>= 2.3.1.0, < 3.0) powerpack (~> 0.1) @@ -438,14 +465,14 @@ GEM sys-uname (1.0.2) ffi (>= 1.0.0) temple (0.7.7) - terminal-table (1.5.2) + terminal-table (1.6.0) thin (1.5.1) daemons (>= 1.0.9) eventmachine (>= 0.12.6) rack (>= 1.0.0) thor (0.19.1) thread_safe (0.3.5) - tilt (2.0.4) + tilt (2.0.5) tzinfo (1.2.2) thread_safe (~> 0.1) unicode-display_width (1.0.5) @@ -485,6 +512,8 @@ DEPENDENCIES devise_token_auth dspace_rest_client execjs + factory_girl_rails + faker feature flamegraph gitlab @@ -514,6 +543,7 @@ DEPENDENCIES rdoc redis-throttle! rmagick + rspec-rails rubocop rubycritic rubyzip diff --git a/spec/auth_helper.rb b/spec/auth_helper.rb new file mode 100644 index 0000000000000000000000000000000000000000..7a763c45af393eb438620e80d081c1ce2710609a --- /dev/null +++ b/spec/auth_helper.rb @@ -0,0 +1,4 @@ +# authenticate the test application +def auth_app(app, request) + request.headers['PortalMEC-AppID'] = app.application_id +end \ No newline at end of file diff --git a/spec/controllers/v1/collections_controller_spec.rb b/spec/controllers/v1/collections_controller_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..fc1eeaedc20f4ca214620bf4bd7a176c7a624a2f --- /dev/null +++ b/spec/controllers/v1/collections_controller_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe V1::CollectionsController, type: :controller do + fixtures :applications + + it { should use_before_action(:authenticate_user!) } + it { should use_before_action(:set_collection) } + it { should route(:get, '/v1/collections').to(action: :index) } + + describe 'GET #index' do + + context 'when application is authenticated' do + before do + auth_app applications(:test_app), @request + get :index + end + + it { expect(response.content_type).to eq('application/json') } + it { expect(response).to have_http_status(:ok) } + end + + context 'when application is not authenticated' do + before { get :index } + + it { expect(response).to have_http_status(:unauthorized) } + end + end + +end \ No newline at end of file diff --git a/spec/factories/applications.rb b/spec/factories/applications.rb new file mode 100644 index 0000000000000000000000000000000000000000..badcbdbec12f586ed5ddc0dadbe7e7b733ee6f8d --- /dev/null +++ b/spec/factories/applications.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + + factory :application do |f| + user + f.name { 'test app' } + f.domain { 'test.host' } + end + +end \ No newline at end of file diff --git a/spec/factories/collections.rb b/spec/factories/collections.rb new file mode 100644 index 0000000000000000000000000000000000000000..8b7761ccbaf537062a6a53b8b116a49e99ec451f --- /dev/null +++ b/spec/factories/collections.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + + factory :collection do |f| + owner + f.name { Faker::University.name } + f.privacy { 'public' } + end + +end \ No newline at end of file diff --git a/spec/factories/users.rb b/spec/factories/users.rb new file mode 100644 index 0000000000000000000000000000000000000000..09510c84e714612e680422705622ac5a55b8a16b --- /dev/null +++ b/spec/factories/users.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + + factory :user, aliases: [:owner] do |f| + f.name { Faker::Name.name } + f.email { Faker::Internet.email } + f.confirmed_at { '2015-10-10 15:30:22' } + end + +end \ No newline at end of file diff --git a/spec/models/application_spec.rb b/spec/models/application_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..004603d65dc7a0f1497017872bd5b5b9f9b98ec8 --- /dev/null +++ b/spec/models/application_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe Application, type: :model do + + describe 'validate model' do + it { should belong_to :user } + it { should validate_presence_of(:domain) } + it { should validate_presence_of(:application_id) } + end + +end \ No newline at end of file diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000000000000000000000000000000000000..258bbf11d75ef86fed2789992c2f51b5ca6f0d39 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,58 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'spec_helper' +require 'auth_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migration and applies them before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/test/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000000000000000000000000000000000000..61e27385c3ed6390048ef9df5ae4434c1afc92a7 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,92 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end