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

added reset endpoint, removed puts statements #339

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative '../../spec/support/dynamoid_reset'

class TestController < ApplicationController
include DynamoidReset

skip_before_action :validate_token, only: [:reset]

def reset
return head :forbidden if Rails.env.production?
DynamoidReset.all
head :ok
end
end
3 changes: 0 additions & 3 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def event_ids
end

def attendee_id
attendee_id = "#{competition_id}-#{user_id}"
puts "returning attendee id: #{attendee_id}"
"#{competition_id}-#{user_id}"
end

Expand Down Expand Up @@ -164,7 +162,6 @@ def update_payment_lane(id, iso_amount, currency_iso, status)
private

def set_is_competing
puts "executing set is competing for: #{attendee_id}"
self.is_competing = true if competing_status == 'accepted'
end

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
unless Rails.env.production?
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
get '/test/reset', to: 'test#reset'
end

get '/healthcheck', to: 'healthcheck#index'
Expand Down
38 changes: 38 additions & 0 deletions spec/controllers/test_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'rails_helper'

describe TestController do
describe '#test_reset.test_env' do
it 'route is available when RAILS_ENV is test' do
get :reset
expect(response).to have_http_status(200)
end

it 'resets the database when called' do
FactoryBot.create_list(:registration, 3, registration_status: 'accepted')
expect(Registration.all.count).to eq(3)

get :reset
expect(Registration.all.count).to eq(0)
end
end

# Done in a separate describe because that's what our `around` wrapper requires
describe '#test_reset.prod_env' do
around(:each) do |example|
original_env = Rails.env
Rails.env = 'production'

example.run

Rails.env = original_env
end

it 'controller rejects request when RAILS_ENV is production' do
get :reset

expect(response).to have_http_status(403)
end
end
end
11 changes: 1 addition & 10 deletions spec/factories/request_factory.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# frozen_string_literal: true

require 'factory_bot_rails'

# Couldn't get the import from a support folder to work, so defining directly in the factory file
def fetch_jwt_token(user_id)
iat = Time.now.to_i
jti_raw = [JwtOptions.secret, iat].join(':').to_s
jti = Digest::MD5.hexdigest(jti_raw)
payload = { user_id: user_id, exp: Time.now.to_i + JwtOptions.expiry, sub: user_id, iat: iat, jti: jti }
token = JWT.encode payload, JwtOptions.secret, JwtOptions.algorithm
"Bearer #{token}"
end
require_relative '../support/jwt_token_generator'

FactoryBot.define do
factory :registration_request, class: Hash do
Expand Down
12 changes: 12 additions & 0 deletions spec/support/jwt_token_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'factory_bot_rails'

def fetch_jwt_token(user_id)
iat = Time.now.to_i
jti_raw = [JwtOptions.secret, iat].join(':').to_s
jti = Digest::MD5.hexdigest(jti_raw)
payload = { user_id: user_id, exp: Time.now.to_i + JwtOptions.expiry, sub: user_id, iat: iat, jti: jti }
token = JWT.encode payload, JwtOptions.secret, JwtOptions.algorithm
"Bearer #{token}"
end