From c7b06d7be57fbdbd224e3c8f6571d0784794d67d Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Mon, 7 Jan 2019 15:37:21 +0100 Subject: [PATCH] WHOIS privacy renewal (renewWhoisPrivacy) --- .../client/registrar_whois_privacy.rb | 20 +++++++ lib/dnsimple/struct.rb | 1 + lib/dnsimple/struct/whois_privacy_order.rb | 28 ++++++++++ .../client/registrar_whois_privacy_spec.rb | 55 +++++++++++++++++++ .../renewWhoisPrivacy/duplicated-order.http | 17 ++++++ .../renewWhoisPrivacy/success.http | 19 +++++++ .../whois-privacy-was-not-found.http | 17 ++++++ 7 files changed, 157 insertions(+) create mode 100644 lib/dnsimple/struct/whois_privacy_order.rb create mode 100644 spec/fixtures.http/renewWhoisPrivacy/duplicated-order.http create mode 100644 spec/fixtures.http/renewWhoisPrivacy/success.http create mode 100644 spec/fixtures.http/renewWhoisPrivacy/whois-privacy-was-not-found.http diff --git a/lib/dnsimple/client/registrar_whois_privacy.rb b/lib/dnsimple/client/registrar_whois_privacy.rb index 6aefa558..acaac3ea 100644 --- a/lib/dnsimple/client/registrar_whois_privacy.rb +++ b/lib/dnsimple/client/registrar_whois_privacy.rb @@ -62,6 +62,26 @@ def disable_whois_privacy(account_id, domain_name, options = {}) Dnsimple::Response.new(response, Struct::WhoisPrivacy.new(response["data"])) end + # Renews whois privacy for the domain. + # + # @see https://developer.dnsimple.com/v2/registrar/whois-privacy/#renew + # + # @example Renew whois privacy for "example.com": + # client.registrar.renew_whois_privacy(1010, "example.com") + # + # @param [Integer] account_id the account ID + # @param [#to_s] domain_name The domain name to check. + # @param [Hash] options + # @return [Struct::WhoisPrivacy] + # + # @raise [RequestError] When the request fails. + def renew_whois_privacy(account_id, domain_name, options = {}) + endpoint = whois_privacy_endpoint(account_id, domain_name) + "/renewals" + response = client.post(endpoint, nil, options) + + Dnsimple::Response.new(response, Struct::WhoisPrivacyOrder.new(response["data"])) + end + private diff --git a/lib/dnsimple/struct.rb b/lib/dnsimple/struct.rb index eb94f075..55367764 100644 --- a/lib/dnsimple/struct.rb +++ b/lib/dnsimple/struct.rb @@ -40,6 +40,7 @@ def initialize(attributes = {}) require_relative 'struct/user' require_relative 'struct/vanity_name_server' require_relative 'struct/whois_privacy' +require_relative 'struct/whois_privacy_order' require_relative 'struct/zone' require_relative 'struct/zone_file' require_relative 'struct/zone_distribution' diff --git a/lib/dnsimple/struct/whois_privacy_order.rb b/lib/dnsimple/struct/whois_privacy_order.rb new file mode 100644 index 00000000..db98bad2 --- /dev/null +++ b/lib/dnsimple/struct/whois_privacy_order.rb @@ -0,0 +1,28 @@ +module Dnsimple + module Struct + + class WhoisPrivacyOrder < Base + # @return [Integer] The associated domain ID. + attr_accessor :domain_id + + # @return [Integer] The associated WHOIS Privacy ID. + attr_accessor :whois_privacy_id + + # @return [String] The WHOIS Privacy order state. + attr_accessor :state + + # @return [Boolean] Whether the WHOIS Privacy is enabled for the domain. + attr_accessor :enabled + + # @return [String] The date the WHOIS Privacy will expire on. + attr_accessor :expires_on + + # @return [String] When the WHOIS Privacy was created in DNSimple. + attr_accessor :created_at + + # @return [String] When the WHOIS Privacy was last updated in DNSimple. + attr_accessor :updated_at + end + + end +end diff --git a/spec/dnsimple/client/registrar_whois_privacy_spec.rb b/spec/dnsimple/client/registrar_whois_privacy_spec.rb index b73847cb..55d3b3c6 100644 --- a/spec/dnsimple/client/registrar_whois_privacy_spec.rb +++ b/spec/dnsimple/client/registrar_whois_privacy_spec.rb @@ -116,4 +116,59 @@ end end + + describe "#renew_whois_privacy" do + let(:account_id) { 1010 } + + before do + stub_request(:post, %r{/v2/#{account_id}/registrar/domains/.+/whois_privacy/renewals$}) + .to_return(read_http_fixture("renewWhoisPrivacy/success.http")) + end + + it "builds the correct request" do + subject.renew_whois_privacy(account_id, domain_name = "example.com") + + expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/registrar/domains/#{domain_name}/whois_privacy/renewals") + .with(headers: { "Accept" => "application/json" }) + end + + it "returns the whois privacy order" do + response = subject.renew_whois_privacy(account_id, "example.com") + expect(response).to be_a(Dnsimple::Response) + + result = response.data + expect(result).to be_a(Dnsimple::Struct::WhoisPrivacyOrder) + expect(result.domain_id).to be_kind_of(Integer) + expect(result.whois_privacy_id).to be_kind_of(Integer) + expect(result.enabled).to be(true) + expect(result.expires_on).to be_kind_of(String) + end + + context "when whois privacy was't previously purchased" do + before do + stub_request(:post, %r{/v2/#{account_id}/registrar/domains/.+/whois_privacy/renewals$}) + .to_return(read_http_fixture("renewWhoisPrivacy/whois-privacy-was-not-found.http")) + end + + it "raises error" do + expect do + subject.renew_whois_privacy(account_id, "example.com") + end.to raise_error(Dnsimple::RequestError, "WHOIS privacy not found for example.com") + end + end + + context "when there is already a whois privacy renewal order in progress" do + before do + stub_request(:post, %r{/v2/#{account_id}/registrar/domains/.+/whois_privacy/renewals$}) + .to_return(read_http_fixture("renewWhoisPrivacy/duplicated-order.http")) + end + + it "raises error" do + expect do + subject.renew_whois_privacy(account_id, "example.com") + end.to raise_error(Dnsimple::RequestError, "The whois privacy for example.com has just been renewed, a new renewal cannot be started at this time") + end + end + end + end diff --git a/spec/fixtures.http/renewWhoisPrivacy/duplicated-order.http b/spec/fixtures.http/renewWhoisPrivacy/duplicated-order.http new file mode 100644 index 00000000..82ebfaf7 --- /dev/null +++ b/spec/fixtures.http/renewWhoisPrivacy/duplicated-order.http @@ -0,0 +1,17 @@ +HTTP/1.1 400 +server: nginx +date: Fri, 21 Dec 2018 16:21:31 GMT +content-type: application/json; charset=utf-8 +x-ratelimit-limit: 2400 +x-ratelimit-remaining: 2398 +x-ratelimit-reset: 1545411828 +cache-control: no-cache +x-request-id: 762d810d-a42c-4b1b-9382-9f56e5a339ff +x-runtime: 0.141307 +x-frame-options: DENY +x-content-type-options: nosniff +x-xss-protection: 1; mode=block +x-download-options: noopen +x-permitted-cross-domain-policies: none + +{"message":"The whois privacy for example.com has just been renewed, a new renewal cannot be started at this time","errors":{}} \ No newline at end of file diff --git a/spec/fixtures.http/renewWhoisPrivacy/success.http b/spec/fixtures.http/renewWhoisPrivacy/success.http new file mode 100644 index 00000000..b0e370f9 --- /dev/null +++ b/spec/fixtures.http/renewWhoisPrivacy/success.http @@ -0,0 +1,19 @@ +HTTP/1.1 200 +server: nginx +date: Fri, 21 Dec 2018 14:10:06 GMT +content-type: application/json; charset=utf-8 +x-ratelimit-limit: 2400 +x-ratelimit-remaining: 2397 +x-ratelimit-reset: 1545404617 +etag: W/"23030fb7c68d7b7da22a374b5f1b1743" +cache-control: max-age=0, private, must-revalidate +x-request-id: 0c31a341-8078-4851-b232-d0f3819910c8 +x-runtime: 2.015707 +x-frame-options: DENY +x-content-type-options: nosniff +x-xss-protection: 1; mode=block +x-download-options: noopen +x-permitted-cross-domain-policies: none +strict-transport-security: max-age=31536000 + +{"data":{"id":9,"domain_id":35879,"whois_privacy_id":478,"state":"new","expires_on":"2018-12-27","enabled":true,"created_at":"2018-12-21T14:10:04Z","updated_at":"2018-12-21T14:10:04Z"}} \ No newline at end of file diff --git a/spec/fixtures.http/renewWhoisPrivacy/whois-privacy-was-not-found.http b/spec/fixtures.http/renewWhoisPrivacy/whois-privacy-was-not-found.http new file mode 100644 index 00000000..33e91e52 --- /dev/null +++ b/spec/fixtures.http/renewWhoisPrivacy/whois-privacy-was-not-found.http @@ -0,0 +1,17 @@ +HTTP/1.1 400 +server: nginx +date: Mon, 07 Jan 2019 13:58:34 GMT +content-type: application/json; charset=utf-8 +x-ratelimit-limit: 2400 +x-ratelimit-remaining: 2399 +x-ratelimit-reset: 1546873114 +cache-control: no-cache +x-request-id: 5a43c5f0-4900-4563-ae9d-bd4a996312fa +x-runtime: 0.066139 +x-frame-options: DENY +x-content-type-options: nosniff +x-xss-protection: 1; mode=block +x-download-options: noopen +x-permitted-cross-domain-policies: none + +{"message":"WHOIS privacy not found for example.com","errors":{}} \ No newline at end of file