Complete auction integration

This commit is contained in:
Artur Beljajev 2019-02-15 13:32:07 +02:00
parent f1148bd4f4
commit 446b9fbf77
6 changed files with 62 additions and 6 deletions

View file

@ -8,12 +8,12 @@ module Api
end end
def show def show
auction = Auction.find_by(uuid: params[:uuid]) auction = Auction.find_by!(uuid: params[:uuid])
render json: serializable_hash(auction) render json: serializable_hash(auction)
end end
def update def update
auction = Auction.find_by(uuid: params[:uuid]) auction = Auction.find_by!(uuid: params[:uuid])
case params[:status] case params[:status]
when Auction.statuses[:awaiting_payment] when Auction.statuses[:awaiting_payment]
@ -24,6 +24,8 @@ module Api
auction.mark_as_payment_received auction.mark_as_payment_received
when Auction.statuses[:payment_not_received] when Auction.statuses[:payment_not_received]
auction.mark_as_payment_not_received auction.mark_as_payment_not_received
when Auction.statuses[:domain_not_registered]
auction.mark_as_domain_not_registered
else else
raise "Invalid status #{params[:status]}" raise "Invalid status #{params[:status]}"
end end

View file

@ -6,6 +6,7 @@ class Auction < ActiveRecord::Base
payment_received: 'payment_received', payment_received: 'payment_received',
payment_not_received: 'payment_not_received', payment_not_received: 'payment_not_received',
domain_registered: 'domain_registered', domain_registered: 'domain_registered',
domain_not_registered: 'domain_not_registered',
} }
PENDING_STATUSES = [statuses[:started], PENDING_STATUSES = [statuses[:started],
@ -43,6 +44,15 @@ class Auction < ActiveRecord::Base
end end
end end
def mark_as_domain_not_registered
self.status = self.class.statuses[:domain_not_registered]
transaction do
save!
restart
end
end
def domain_registrable?(registration_code = nil) def domain_registrable?(registration_code = nil)
payment_received? && registration_code_matches?(registration_code) payment_received? && registration_code_matches?(registration_code)
end end

View file

@ -46,7 +46,7 @@ Updates auction.
### Parameters ### Parameters
| Field name | Required | Type | Allowed values | Description | | Field name | Required | Type | Allowed values | Description |
| ---------- | -------- | ---- | -------------- | ----------- | | ---------- | -------- | ---- | -------------- | ----------- |
| status | no | String | "awaiting_payment", "no_bids", "payment_received", "payment_not_received" | status | no | String | "awaiting_payment", "no_bids", "payment_received", "payment_not_received", "domain_not_registered"
## Request ## Request
``` ```

View file

@ -23,4 +23,10 @@ class ApiV1AuctionDetailsTest < ActionDispatch::IntegrationTest
'status' => Auction.statuses[:no_bids] }), ActiveSupport::JSON 'status' => Auction.statuses[:no_bids] }), ActiveSupport::JSON
.decode(response.body) .decode(response.body)
end end
end
def test_auction_not_found
assert_raises ActiveRecord::RecordNotFound do
get api_v1_auction_path('non-existing-uuid'), nil, 'Content-Type' => Mime::JSON.to_s
end
end
end

View file

@ -89,6 +89,15 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
assert DNS::DomainName.new('auction.test').at_auction? assert DNS::DomainName.new('auction.test').at_auction?
end end
def test_restarts_an_auction_when_domain_is_not_registered
@auction.update!(domain: 'auction.test', status: Auction.statuses[:payment_received])
patch api_v1_auction_path(@auction.uuid), { status: Auction.statuses[:domain_not_registered] }
.to_json, 'Content-Type' => Mime::JSON.to_s
assert DNS::DomainName.new('auction.test').at_auction?
end
def test_inaccessible_when_ip_address_is_not_allowed def test_inaccessible_when_ip_address_is_not_allowed
ENV['auction_api_allowed_ips'] = '' ENV['auction_api_allowed_ips'] = ''
@ -97,4 +106,11 @@ class ApiV1AuctionUpdateTest < ActionDispatch::IntegrationTest
assert_response :unauthorized assert_response :unauthorized
end end
end
def test_auction_not_found
assert_raises ActiveRecord::RecordNotFound do
patch api_v1_auction_path('non-existing-uuid'), { status: Auction.statuses[:no_bids] }.to_json,
'Content-Type' => Mime::JSON.to_s
end
end
end

View file

@ -15,7 +15,8 @@ class AuctionTest < ActiveSupport::TestCase
'awaiting_payment' => 'awaiting_payment', 'awaiting_payment' => 'awaiting_payment',
'payment_received' => 'payment_received', 'payment_received' => 'payment_received',
'payment_not_received' => 'payment_not_received', 'payment_not_received' => 'payment_not_received',
'domain_registered' => 'domain_registered' }), Auction.statuses 'domain_registered' => 'domain_registered',
'domain_not_registered' => 'domain_not_registered' }), Auction.statuses
end end
def test_selling_domain_starts_new_auction def test_selling_domain_starts_new_auction
@ -95,6 +96,27 @@ class AuctionTest < ActiveSupport::TestCase
assert new_auction.started? assert new_auction.started?
end end
def test_marking_as_domain_not_registered
@auction.update!(status: Auction.statuses[:payment_received])
@auction.mark_as_domain_not_registered
@auction.reload
assert @auction.domain_not_registered?
end
def test_restarts_an_auction_when_domain_is_not_registered
@auction.update!(domain: 'auction.test', status: Auction.statuses[:domain_not_registered])
assert_difference 'Auction.count' do
@auction.mark_as_domain_not_registered
end
new_auction = Auction.last
assert_equal 'auction.test', new_auction.domain
assert new_auction.started?
end
def test_domain_registrable def test_domain_registrable
assert @auction.no_bids? assert @auction.no_bids?
assert_not @auction.domain_registrable? assert_not @auction.domain_registrable?