Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Add controller test for creating a registration (#623)
Browse files Browse the repository at this point in the history
* create test passing

* rubocop changes

* set queue adapter to inline

* rubocop changes

---------

Co-authored-by: FinnIckler <finnickler@gmail.com>
  • Loading branch information
dunkOnIT and FinnIckler authored Aug 1, 2024
1 parent 33fdbc7 commit 81a36d2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/controllers/registration_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/factories/request_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 81a36d2

Please sign in to comment.