mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 04:37:30 +02:00
Add update endpoint for ContactRequests
This commit is contained in:
parent
1bbacbf56c
commit
b708cebbfd
5 changed files with 61 additions and 9 deletions
|
@ -9,14 +9,24 @@ module Api
|
||||||
|
|
||||||
ContactRequest.save_record(contact_request_params)
|
ContactRequest.save_record(contact_request_params)
|
||||||
head(:created)
|
head(:created)
|
||||||
rescue ActionController::ParameterMissing
|
rescue StandardError
|
||||||
head(:bad_request)
|
head(:bad_request)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update; end
|
def update
|
||||||
|
return head(:bad_request) if params[:id].blank?
|
||||||
|
|
||||||
|
record = ContactRequest.find_by(id: params[:id])
|
||||||
|
return head(:not_found) unless record
|
||||||
|
|
||||||
|
record.update_status(contact_request_params[:status])
|
||||||
|
head(:ok)
|
||||||
|
rescue StandardError
|
||||||
|
head(:bad_request)
|
||||||
|
end
|
||||||
|
|
||||||
def contact_request_params
|
def contact_request_params
|
||||||
params.require(:contact_request).permit(:email, :whois_record_id, :name, :status, :id)
|
params.require(:contact_request).permit(:email, :whois_record_id, :name, :status)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,11 @@ class ContactRequest < ApplicationRecord
|
||||||
contact_request.save!
|
contact_request.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_status(status)
|
||||||
|
self.status = status
|
||||||
|
save!
|
||||||
|
end
|
||||||
|
|
||||||
def self.create_random_secret
|
def self.create_random_secret
|
||||||
SecureRandom.hex(64)
|
SecureRandom.hex(64)
|
||||||
end
|
end
|
||||||
|
|
|
@ -91,7 +91,7 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :auctions, only: %i[index show update], param: :uuid
|
resources :auctions, only: %i[index show update], param: :uuid
|
||||||
resources :contact_requests, only: %i[create update], param: :uuid
|
resources :contact_requests, only: %i[create update], param: :id
|
||||||
resources :bounces, only: %i[create]
|
resources :bounces, only: %i[create]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
8
test/fixtures/contact_requests.yml
vendored
Normal file
8
test/fixtures/contact_requests.yml
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
new:
|
||||||
|
whois_record_id: 1
|
||||||
|
email: aaa@bbb.com
|
||||||
|
name: Testname
|
||||||
|
status: new
|
||||||
|
secret: somesecret
|
||||||
|
valid_to: 2010-07-05
|
||||||
|
|
|
@ -4,20 +4,22 @@ class ApiV1ContactRequestTest < ActionDispatch::IntegrationTest
|
||||||
def setup
|
def setup
|
||||||
@api_key = "Basic #{ENV['api_shared_key']}"
|
@api_key = "Basic #{ENV['api_shared_key']}"
|
||||||
@headers = { "Authorization": "#{@api_key}" }
|
@headers = { "Authorization": "#{@api_key}" }
|
||||||
@json_body = { "contact_request": valid_contact_request_body }.as_json
|
@json_create = { "contact_request": valid_contact_request_create }.as_json
|
||||||
|
@json_update = { "contact_request": valid_contact_request_update }.as_json
|
||||||
|
@contact_request = contact_requests(:new)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_authorizes_api_request
|
def test_authorizes_api_request
|
||||||
post api_v1_contact_requests_path, params: @json_body, headers: @headers
|
post api_v1_contact_requests_path, params: @json_create, headers: @headers
|
||||||
assert_response :created
|
assert_response :created
|
||||||
|
|
||||||
invalid_headers = { "Authorization": "Basic invalid_api_key" }
|
invalid_headers = { "Authorization": "Basic invalid_api_key" }
|
||||||
post api_v1_contact_requests_path, params: @json_body, headers: invalid_headers
|
post api_v1_contact_requests_path, params: @json_create, headers: invalid_headers
|
||||||
assert_response :unauthorized
|
assert_response :unauthorized
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_saves_new_contact_request
|
def test_saves_new_contact_request
|
||||||
request_body = @json_body.dup
|
request_body = @json_create.dup
|
||||||
random_mail = "#{rand(10000..99999)}@registry.test"
|
random_mail = "#{rand(10000..99999)}@registry.test"
|
||||||
request_body['contact_request']['email'] = random_mail
|
request_body['contact_request']['email'] = random_mail
|
||||||
|
|
||||||
|
@ -29,11 +31,38 @@ class ApiV1ContactRequestTest < ActionDispatch::IntegrationTest
|
||||||
assert ContactRequest::STATUS_NEW, contact_request.status
|
assert ContactRequest::STATUS_NEW, contact_request.status
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_contact_request_body
|
def test_updates_existing_contact_request
|
||||||
|
request_body = @json_update.dup
|
||||||
|
|
||||||
|
put api_v1_contact_request_path(@contact_request.id), params: request_body, headers: @headers
|
||||||
|
assert_response :ok
|
||||||
|
|
||||||
|
@contact_request.reload
|
||||||
|
assert ContactRequest::STATUS_CONFIRMED, @contact_request.status
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_not_updates_if_status_error
|
||||||
|
request_body = @json_update.dup
|
||||||
|
request_body['contact_request']['status'] = 'some_error_status'
|
||||||
|
|
||||||
|
put api_v1_contact_request_path(@contact_request.id), params: request_body, headers: @headers
|
||||||
|
assert_response 400
|
||||||
|
|
||||||
|
@contact_request.reload
|
||||||
|
assert ContactRequest::STATUS_NEW, @contact_request.status
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_contact_request_create
|
||||||
{
|
{
|
||||||
"email": "aaa@bbb.com",
|
"email": "aaa@bbb.com",
|
||||||
"whois_record_id": "1",
|
"whois_record_id": "1",
|
||||||
"name": "test"
|
"name": "test"
|
||||||
}.as_json
|
}.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def valid_contact_request_update
|
||||||
|
{
|
||||||
|
"status": "#{ContactRequest::STATUS_CONFIRMED}",
|
||||||
|
}.as_json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue