Skip to content
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
6 changes: 6 additions & 0 deletions app/controllers/shopkeeper_auth/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
class ShopkeeperAuth::RegistrationsController < DeviseTokenAuth::RegistrationsController
rate_limit to: 10, within: 3.minutes, only: :create,
with: -> {
render json: {code: 429, error_message: I18n.t("errors.messages.too_many_signups")},
status: :too_many_requests
}

before_action :set_confirm_success_url, only: %i[create]
before_action :configure_permitted_parameters

Expand Down
4 changes: 3 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.cache_store = :null_store
# Use memory store so ActionController::RateLimiting can persist counters
# between requests within a test; null_store would no-op the increments.
config.cache_store = :memory_store

# Render exception templates for rescuable exceptions and raise for other exceptions.
config.action_dispatch.show_exceptions = :rescuable
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ en:
limit_count_shop: "You can create up to %{limit_count} shops across all organizations."
limit_count_accounts_shopkeeper: "Organization members can be created up to %{limit_count}. Please contact the organization admin user or owner."
limit_count_item_tag: "You can create up to %{limit_count} item tags."
too_many_signups: "Too many sign-up attempts. Please try again later."
29 changes: 29 additions & 0 deletions test/integration/sign_up_throttle_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "test_helper"

class SignUpThrottleTest < ActionDispatch::IntegrationTest
def post_sign_up(email)
post shopkeeper_registration_url,
params: {
name: "Throttle Test",
email: email,
password: "password",
password_confirmation: "password",
time_zone: "Tokyo",
current_platform: "ios"
},
as: :json
end

test "the eleventh sign-up from the same IP within the window is rate-limited" do
10.times do |i|
post_sign_up("throttle#{i}@example.com")
assert_not_equal 429, response.status, "request #{i + 1} should not be throttled"
end

post_sign_up("throttle10@example.com")

assert_response :too_many_requests
assert_equal 429, response.parsed_body["code"]
assert_equal I18n.t("errors.messages.too_many_signups"), response.parsed_body["error_message"]
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module ActiveSupport
class TestCase
setup do
Dir[Rails.root.join("db", "fixtures", "test", "*.rb")].sort.each { |s| load s }
Rails.cache.clear
end

# Run tests in parallel with specified workers
Expand Down