Skip to content

Commit

Permalink
Add show endpoint for reserved domains by user_unique_id
Browse files Browse the repository at this point in the history
- Add GET /api/v1/business_registry/reserve_domains/:user_unique_id endpoint
- Add output_reserved_domains method to FreeDomainReservationHolder model
- Add before_action to validate user_unique_id and find holder
- Add integration tests for new endpoint
- Add model tests for output_reserved_domains method
- Update routes to include new endpoint

The endpoint returns information about previously reserved domains including names,
passwords and expiration dates. Returns 404 if user_unique_id is invalid.
  • Loading branch information
OlegPhenomenon committed Dec 6, 2024
1 parent cb10395 commit 083b5dd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ module Api
module V1
module BusinessRegistry
class ReserveDomainsController < BaseController
before_action :validate_params
before_action :validate_params, only: :create
before_action :set_free_domain_reservation_holder, only: :show

def show
render_success(@free_domain_reservation_holder.output_reserved_domains)
end

def create
domain_names = params[:domain_names]
Expand All @@ -27,6 +32,11 @@ def create

private

def set_free_domain_reservation_holder
@free_domain_reservation_holder = FreeDomainReservationHolder.find_by(user_unique_id: params[:user_unique_id])
render_error("Reserved domains not found. Invalid user_unique_id", :not_found) unless @free_domain_reservation_holder
end

def validate_params
domain_names = params[:domain_names]

Expand Down
11 changes: 10 additions & 1 deletion app/models/free_domain_reservation_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ def reserved_domains
ReservedDomain.where(name: domain_names)
end

def output_reserved_domains
reserved_domains.map do |domain|
{
name: domain.name,
password: domain.password,
expire_at: domain.expire_at
}
end
end

private

def set_user_unique_id
self.user_unique_id = SecureRandom.uuid[0..9]
end

end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
get 'domain_names/:organization_name', to: 'domain_names#show', as: 'domain_names'
get 'long_reserve_domains_status', to: 'long_reserve_domains_status#show', as: 'long_reserve_domains_status'
post 'reserve_domains', to: 'reserve_domains#create', as: 'reserve_domains'
get 'reserve_domains/:user_unique_id', to: 'reserve_domains#show', as: 'reserve_domains_data'
post 'long_reserve_domains', to: 'long_reserve_domains#create', as: 'long_reserve_domains'
get 'registration_code', to: 'registration_code#show', as: 'registration_code'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,35 @@ def setup
assert_equal "No available domains", json_response['error']
end
end

test "should get reserved domains info by user_unique_id" do
# Create test data
domain_names = ["test1.test", "test2.test"]
result = ReservedDomain.reserve_domains_without_payment(domain_names)

# Make request
get api_v1_business_registry_reserve_domains_data_path(user_unique_id: result.user_unique_id),
headers: @auth_headers

assert_response :success
json_response = JSON.parse(response.body)

# Check response structure
assert_equal 2, json_response.length
json_response.each do |domain|
assert_includes domain_names, domain['name']
assert_not_nil domain['password']
assert_not_nil domain['expire_at']
assert Time.parse(domain['expire_at']) > Time.current
end
end

test "should return error for non-existent user_unique_id" do
get api_v1_business_registry_reserve_domains_data_path(user_unique_id: 'non-existent'),
headers: @auth_headers

assert_response :not_found
json_response = JSON.parse(response.body)
assert_equal "Reserved domains not found. Invalid user_unique_id", json_response['error']
end
end
24 changes: 24 additions & 0 deletions test/models/free_domain_reservation_holder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ def setup
)
assert_includes holder.reserved_domains, reserved_domain
end

test "output_reserved_domains should return correct structure" do
# Create test data
domain_names = ['test1.test', 'test2.test']
holder = FreeDomainReservationHolder.create!(domain_names: domain_names)

domain_names.each do |name|
ReservedDomain.create!(
name: name,
password: 'test-password',
expire_at: Time.current + 7.days
)
end

# Test the method
output = holder.output_reserved_domains

assert_equal domain_names.length, output.length
output.each do |domain|
assert_includes domain_names, domain[:name]
assert_not_nil domain[:password]
assert_not_nil domain[:expire_at]
end
end
end

0 comments on commit 083b5dd

Please sign in to comment.