diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 6bdc0e7e..3871f5bc 100644 --- a/Gemfile +++ b/Gemfile @@ -51,7 +51,7 @@ group :development, :test do gem 'rswag' # webmock for mocking responses from other microservices - gem 'webmock' + gem 'webmock', require: false end group :development do diff --git a/config/initializers/webmock.rb b/config/initializers/webmock.rb new file mode 100644 index 00000000..ee630640 --- /dev/null +++ b/config/initializers/webmock.rb @@ -0,0 +1,4 @@ +# if Rails.env.test? || Rails.env.development? +# require 'webmock' +# WebMock.disable_net_connect!(allow_localhost: true) +# end diff --git a/spec/fixtures/competition_details.json b/spec/fixtures/competition_details.json new file mode 100644 index 00000000..8d8ff267 --- /dev/null +++ b/spec/fixtures/competition_details.json @@ -0,0 +1,5 @@ +{ + "competitions": [ + {"id":"CubingZANationalChampionship2023","name":"CubingZA National Championship 2023","registration_open":"2023-05-05T04:00:00.000Z","registration_close":"2023-06-14T00:00:00.000Z","announced_at":"2023-05-01T15:59:53.000Z","start_date":"2023-06-16","end_date":"2023-06-18","competitor_limit":120,"cancelled_at":null,"url":"https://www.worldcubeassociation.org/competitions/CubingZANationalChampionship2023","website":"https://www.worldcubeassociation.org/competitions/CubingZANationalChampionship2023","short_name":"CubingZA Nationals 2023","city":"Johannesburg","venue_address":"South Africa, 28 Droste Cres, Droste Park, Johannesburg, 2094","venue_details":"","latitude_degrees":-26.21117,"longitude_degrees":28.06449,"country_iso2":"ZA","event_ids":["333","222","444","555","666","777","333bf","333oh","clock","minx","pyram","skewb","sq1","444bf","555bf","333mbf"],"delegates":[{"id":1306,"created_at":"2015-08-05T19:20:45.000Z","updated_at":"2023-05-30T06:04:43.000Z","name":"Maverick Pearson","delegate_status":"candidate_delegate","wca_id":"2014PEAR02","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2014PEAR02","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"email":"mpearson@worldcubeassociation.org","region":"South Africa","senior_delegate_id":125297,"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2014PEAR02/1616059289.jpg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2014PEAR02/1616059289_thumb.jpg","is_default":false}},{"id":64330,"created_at":"2017-07-06T17:59:59.000Z","updated_at":"2023-05-30T07:20:41.000Z","name":"Marike Faught","delegate_status":"trainee_delegate","wca_id":"2015FAUG01","gender":"f","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2015FAUG01","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"email":"marikefaught@gmail.com","region":"South Africa","senior_delegate_id":125297,"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2015FAUG01/1670326982.jpeg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2015FAUG01/1670326982_thumb.jpeg","is_default":false}},{"id":66046,"created_at":"2017-07-17T19:26:43.000Z","updated_at":"2023-05-27T09:12:35.000Z","name":"Timothy Lawrance","delegate_status":"candidate_delegate","wca_id":"2017LAWR04","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2017LAWR04","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"email":"tlawrance@worldcubeassociation.org","region":"South Africa","senior_delegate_id":125297,"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2017LAWR04/1500401257.png","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2017LAWR04/1500401257_thumb.png","is_default":false}},{"id":156416,"created_at":"2019-05-08T06:41:17.000Z","updated_at":"2023-05-31T06:51:11.000Z","name":"Joshua Christian Marais","delegate_status":"trainee_delegate","wca_id":"2019MARA05","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2019MARA05","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"email":"joshuac.marais@gmail.com","region":"South Africa","senior_delegate_id":125297,"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019MARA05/1654594914.jpg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019MARA05/1654594914_thumb.jpg","is_default":false}},{"id":158816,"created_at":"2019-05-26T08:58:39.000Z","updated_at":"2023-05-22T09:15:27.000Z","name":"Duncan Hobbs","delegate_status":"candidate_delegate","wca_id":"2019HOBB02","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2019HOBB02","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"email":"dhobbs@worldcubeassociation.org","region":"South Africa","senior_delegate_id":125297,"class":"user","teams":[{"id":608,"friendly_id":"wst","leader":false,"name":"Duncan Hobbs","senior_member":true,"wca_id":"2019HOBB02","avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019HOBB02/1652339003.jpeg","thumb":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019HOBB02/1652339003_thumb.jpeg"}}}],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019HOBB02/1652339003.jpeg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2019HOBB02/1652339003_thumb.jpeg","is_default":false}}],"organizers":[{"id":78952,"created_at":"2017-10-26T16:37:48.000Z","updated_at":"2023-05-31T06:27:56.000Z","name":"Dylan Swarts","delegate_status":null,"wca_id":"2017SWAR03","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2017SWAR03","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2017SWAR03/1586299943.jpg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2017SWAR03/1586299943_thumb.jpg","is_default":false}},{"id":226445,"created_at":"2021-03-01T14:53:01.000Z","updated_at":"2023-05-25T05:25:10.000Z","name":"Anthony Kalaya Rush","delegate_status":null,"wca_id":"2022RUSH01","gender":"m","country_iso2":"ZA","url":"https://www.worldcubeassociation.org/persons/2022RUSH01","country":{"id":"South Africa","name":"South Africa","continentId":"_Africa","iso2":"ZA"},"class":"user","teams":[],"avatar":{"url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2022RUSH01/1655038462.jpeg","pending_url":"https://www.worldcubeassociation.org/assets/missing_avatar_thumb-d77f478a307a91a9d4a083ad197012a391d5410f6dd26cb0b0e3118a5de71438.png","thumb_url":"https://avatars.worldcubeassociation.org/uploads/user/avatar/2022RUSH01/1655038462_thumb.jpeg","is_default":false}}],"class":"competition"} + ] +} diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..c718176c --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,63 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +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')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + abort e.to_s.strip +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/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 + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # 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://rspec.info/features/6-0/rspec-rails + 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/requests/registration_spec.rb b/spec/requests/registration_spec.rb index 6743ce66..9b80ce79 100644 --- a/spec/requests/registration_spec.rb +++ b/spec/requests/registration_spec.rb @@ -1,48 +1,72 @@ require 'swagger_helper' +require_relative '../support/helpers/registration_spec_helper' -RSpec.describe 'api/registration', type: :request do - path '/api/registration' do - get 'List registrations' do - parameter competition_id: :string +# TODO: Once I'm happy with this test file, get it working using the docker-compose.test.yml file + +RSpec.describe 'v1 Registrations API', type: :request do + + # TODO: Move this out to a helper file + def get_competition_details competition_id + # Open the json file called 'competition_details.json' + File.open("#{Rails.root}/spec/fixtures/competition_details.json", 'r') do |f| + competition_details = JSON.parse(f.read) + + # Retrieve the competition details + competition_details['competitions'].each do |competition| + return competition.to_json if competition['id'] == competition_id + end + end + end + + path '/registrations' do # This could also be /api/v1/registrations/{competition_id}? + get 'List registrations for a given competition_id' do + parameter name: :competition_id, in: :query, type: :string, required: true produces 'application/json' + context 'success responses' do + competition_id = 'CubingZANationalChampionship2023' + + before do + competition_details = get_competition_details(competition_id) + # puts "#{competition_details['id']}" - context "success responses" do + # Stub the request to the Competition Service + stub_request(:get, "https://www.worldcubeassociation.org/api/v0/competitions/#{competition_id}") + .to_return(status: 200, body: competition_details) + end response '200', 'request and response conform to schema' do - schema type: :object, - properties { - # TODO when database structure is finalised - } - - let!(:competition_id) { 'RegistrationTestComp'} - run_test! - end + # TODO: Define response schema + let!(:competition_id) { competition_id } - response '200', 'User is allowed to access registration data (various scenarios)' do + run_test! + # TODO: Validate that the response returns the expected data - may not be needed after spec is added end - response '400', 'Competition ID parameter mis-spelled' do - end + # response '200', 'User is allowed to access registration data (various scenarios)' do + # end - response '400', 'Competition ID not provided' do + # response '200', 'Valid competition_id but no registrations for it' do + # end end - response '401', 'Tampered JWT token rejected' do - end + # response '400', 'Competition ID parameter mis-spelled' do + # end - response '404', 'Comeptition ID doesnt exist' do - end + # response '400', 'Competition ID not provided' do + # end - response '403', 'User is not allowed to access registration data (various scenarios)' do - end - - response '502', 'Competition service unavailable' do - end + # response '401', 'Tampered JWT token rejected' do + # end + # response '404', 'Comeptition ID doesnt exist' do + # end + # response '403', 'User is not allowed to access registration data (various scenarios)' do + # end + # response '502', 'Competition service unavailable' do + # end end end - # test change end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..a67e00fb --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,100 @@ +require 'webmock' +require 'webmock/rspec' + +WebMock.disable_net_connect!(allow_localhost: true) +WebMock.allow_net_connect! + +# 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. +# +# See https://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 + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows 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. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # 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: + # https://rspec.info/features/3-12/rspec-core/configuration/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 diff --git a/spec/support/helpers/registration_spec_helper.rb b/spec/support/helpers/registration_spec_helper.rb new file mode 100644 index 00000000..14f29452 --- /dev/null +++ b/spec/support/helpers/registration_spec_helper.rb @@ -0,0 +1,15 @@ +module Helpers + module Registration + def get_competition_details competition_id + # Open the json file called 'competition_details.json' + File.open("#{Rails.root}/spec/fixtures/competition_details.json", 'r') do |f| + competition_details = JSON.parse(f.read) + + # Retrieve the competition details + competition_details['competitions'].each do |competition| + puts competition if competition['id'] == competition_id + end + end + end + end +end diff --git a/spec/test_helper.rb b/spec/test_helper.rb deleted file mode 100644 index 353f1af7..00000000 --- a/spec/test_helper.rb +++ /dev/null @@ -1,13 +0,0 @@ -ENV["RAILS_ENV"] ||= "test" -require_relative "../config/environment" -require "rails/test_help" - -class ActiveSupport::TestCase - # Run tests in parallel with specified worker - parallelize(workers: :number_of_processors) - - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end diff --git a/swagger/v1/swagger.yaml b/swagger/v1/swagger.yaml new file mode 100644 index 00000000..2f84576c --- /dev/null +++ b/swagger/v1/swagger.yaml @@ -0,0 +1,23 @@ +--- +openapi: 3.0.1 +info: + title: API V1 + version: v1 +paths: + "/api/v1/registrations": + get: + summary: List registrations for a given competition_id + parameters: + - name: competition_id + in: query + required: true + schema: + type: string + responses: + '400': + description: request and response conform to schema +servers: +- url: https://{defaultHost} + variables: + defaultHost: + default: www.example.com