From 81a36d23f60a1ae1f5bc1a698de0d8c720e16d4f Mon Sep 17 00:00:00 2001 From: Duncan <52967253+dunkOnIT@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:03:17 +0200 Subject: [PATCH] Add controller test for creating a registration (#623) * create test passing * rubocop changes * set queue adapter to inline * rubocop changes --------- Co-authored-by: FinnIckler --- config/environments/test.rb | 3 ++ config/initializers/webmock.rb | 2 +- .../registration_controller_spec.rb | 30 +++++++++++++++++++ spec/factories/request_factory.rb | 14 +++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/config/environments/test.rb b/config/environments/test.rb index 3926fdbd..317bb28f 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -10,6 +10,9 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. + # Have handler work items off the queue itself + config.active_job.queue_adapter = :inline + # Save logs to folder config.logger = Logger.new(Rails.root.join('log/test.log')) diff --git a/config/initializers/webmock.rb b/config/initializers/webmock.rb index 1c10c871..7c9c70d8 100644 --- a/config/initializers/webmock.rb +++ b/config/initializers/webmock.rb @@ -2,5 +2,5 @@ if Rails.env.local? require 'webmock' - WebMock.disable_net_connect!(allow: 'http://localstack:4566') + WebMock.disable_net_connect!(allow: [/localhost/, /localstack/]) end diff --git a/spec/controllers/registration_controller_spec.rb b/spec/controllers/registration_controller_spec.rb index 0473cbdb..b9f3cc85 100644 --- a/spec/controllers/registration_controller_spec.rb +++ b/spec/controllers/registration_controller_spec.rb @@ -3,6 +3,36 @@ require 'rails_helper' describe RegistrationController do + describe '#create', :tag do + before do + @registration_request = FactoryBot.build(:registration_request) + stub_request(:get, UserApi.permissions_path(@registration_request['user_id'])).to_return( + status: 200, + body: FactoryBot.build(:permissions).to_json, + headers: { 'Content-Type' => 'application/json' }, + ) + stub_request(:post, EmailApi.registration_email_path).to_return(status: 200, body: { emails_sent: 1 }.to_json) + end + + it 'successfully creates a registration' do + @competition = FactoryBot.build(:competition) + stub_request(:get, CompetitionApi.comp_api_url(@competition['id'])).to_return( + status: 200, + body: @competition.except('qualifications').to_json, + headers: { 'Content-Type' => 'application/json' }, + ) + + request.headers['Authorization'] = @registration_request['jwt_token'] + post :create, params: @registration_request, as: :json + sleep 1 # Give the queue time to work off the registration - perhaps this should be a queue length query instead? + + expect(response.code).to eq('202') + + created_registration = Registration.find("#{@competition['id']}-#{@registration_request['user_id']}") + expect(created_registration.event_ids).to eq(@registration_request['competing']['event_ids']) + end + end + describe '#update' do # NOTE: This code only needs to run once before the assertions, but before(:create) doesnt work because `request` defined then before do diff --git a/spec/factories/request_factory.rb b/spec/factories/request_factory.rb index 6bdc7aca..0bd8295c 100644 --- a/spec/factories/request_factory.rb +++ b/spec/factories/request_factory.rb @@ -107,3 +107,17 @@ initialize_with { attributes.stringify_keys } end end + +FactoryBot.define do + factory :permissions, class: Hash do + can_attend_competitions { { 'scope' => '*' } } + can_organize_competitions { { 'scope' => [] } } + can_administer_competitions { { 'scope' => [] } } + + transient do + user_id { nil } + end + + initialize_with { attributes.stringify_keys } + end +end