defmodule Dnsimple.CertificatesTest do
  use TestCase, async: false
  use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney

  @module Dnsimple.Certificates
  @client %Dnsimple.Client{access_token: "i-am-a-token", base_url: "https://api.dnsimple.test"}
  @account_id 1010

  describe ".list_certificates" do
    setup do
      url = "#{@client.base_url}/v2/#{@account_id}/domains/dnsimple.us/certificates"
      {:ok, fixture: "listCertificates/success.http", method: "get", url: url}
    end

    test "returns the certificates in a Dnsimple.Response", %{fixture: fixture, method: method, url: url} do
      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        {:ok, response} = @module.list_certificates(@client, @account_id, "dnsimple.us")
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert is_list(data)
        assert length(data) == 2
        assert Enum.all?(data, fn(single) -> single.__struct__ == Dnsimple.Certificate end)
      end
    end

    test "sends custom headers", %{fixture: fixture, method: method, url: url} do
      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        @module.list_certificates(@client, "1010", "dnsimple.us", headers: %{"X-Header" => "X-Value"})
      end
    end

    test "supports sorting", %{fixture: fixture, method: method} do
      url = "#{@client.base_url}/v2/1010/domains/dnsimple.us/certificates?sort=id%3Adesc"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        @module.list_certificates(@client, "1010", "dnsimple.us", sort: "id:desc")
      end
    end

    test "supports filtering", %{fixture: fixture, method: method} do
      url = "#{@client.base_url}/v2/1010/domains/dnsimple.us/certificates?common_name_like=www"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        @module.list_certificates(@client, "1010", "dnsimple.us", filter: [common_name_like: "www"])
      end
    end
  end


  describe ".get_certificate" do
    test "returns the certificate in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/bingo.pizza/certificates/101967"
      fixture = "getCertificate/success.http"
      method  = "get"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        {:ok, response} = @module.get_certificate(@client, @account_id, "bingo.pizza", _certificate_id = 101967)
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.Certificate
        assert data.id == 101967
        assert data.domain_id == 289333
        assert data.contact_id == 2511
        assert data.common_name == "www.bingo.pizza"
        assert data.alternate_names == []
        assert data.auto_renew == false
        assert data.expires_at == "2020-09-16T18:10:13Z"
      end
    end
  end


  describe ".download_certificate" do
    test "returns the certificate in a Dnsimple.Response" do
      url = "#{@client.base_url}/v2/1010/domains/example.com/certificates/22289/download"
      use_cassette :stub, ExvcrUtils.response_fixture("downloadCertificate/success.http", method: "get", url: url) do
        {:ok, response} = @module.download_certificate(@client, "1010", "example.com", _certificate_id = "22289")
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.Certificate
        assert data.server == "-----BEGIN CERTIFICATE-----\nMIIE7TCCA9WgAwIBAgITAPpTe4O3vjuQ9L4gLsogi/ukujANBgkqhkiG9w0BAQsF\nADAiMSAwHgYDVQQDDBdGYWtlIExFIEludGVybWVkaWF0ZSBYMTAeFw0xNjA2MTEx\nNzQ4MDBaFw0xNjA5MDkxNzQ4MDBaMBkxFzAVBgNVBAMTDnd3dy53ZXBwb3MubmV0\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtzCcMfWoQRt5AMEY0HUb\n2GaraL1GsWOo6YXdPfe+YDvtnmDw23NcoTX7VSeCgU9M3RKs19AsCJcRNTLJ2dmD\nrAuyCTud9YTAaXQcTOLUhtO8T8+9AFVIva2OmAlKCR5saBW3JaRxW7V2aHEd/d1s\ns1CvNOO7jNppc9NwGSnDHcn3rqNv/U3MaU0gpJJRqsKkvcLU6IHJGgxyQ6AbpwJD\nIqBnzkjHu2IuhGEbRuMjyWLA2qtsjyVlfPotDxUdVouUQpz7dGHUFrLR7ma8QAYu\nOfl1ZMyrc901HGMa7zwbnFWurs3fed7vAosTRZIjnn72/3Wo7L9RiMB+vwr3N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54DANBgkq\nhkiG9w0BAQsFAAOCAgEABYSu4Il+fI0MYU42OTmEj+1HqQ5DvyAeyCA6sGuZdwjF\nUGeVOv3NnLyfofuUOjEbY5irFCDtnv+0ckukUZN9lz4Q2YjWGUpW4TTu3ieTsaC9\nAFvCSgNHJyWSVtWvB5XDxsqawl1KzHzzwr132bF2rtGtazSqVqK9E07sGHMCf+zp\nDQVDVVGtqZPHwX3KqUtefE621b8RI6VCl4oD30Olf8pjuzG4JKBFRFclzLRjo/h7\nIkkfjZ8wDa7faOjVXx6n+eUQ29cIMCzr8/rNWHS9pYGGQKJiY2xmVC9h12H99Xyf\nzWE9vb5zKP3MVG6neX1hSdo7PEAb9fqRhHkqVsqUvJlIRmvXvVKTwNCP3eCjRCCI\nPTAvjV+4ni786iXwwFYNz8l3PmPLCyQXWGohnJ8iBm+5nk7O2ynaPVW0U2W+pt2w\nSVuvdDM5zGv2f9ltNWUiYZHJ1mmO97jSY/6YfdOUH66iRtQtDkHBRdkNBsMbD+Em\n2TgBldtHNSJBfB3pm9FblgOcJ0FSWcUDWJ7vO0+NTXlgrRofRT6pVywzxVo6dND0\nWzYlTWeUVsO40xJqhgUQRER9YLOLxJ0O6C8i0xFxAMKOtSdodMB3RIwt7RFQ0uyt\nn5Z5MqkYhlMI3J1tPRTp1nEt9fyGspBOO05gi148Qasp+3N+svqKomoQglNoAxU=\n-----END CERTIFICATE-----"]
      end
    end
  end


  describe ".get_certificate_private_key" do
    test "returns the private key in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/example.com/certificates/22289/private_key"
      fixture = "getCertificatePrivateKey/success.http"
      method  = "get"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        {:ok, response} = @module.get_certificate_private_key(@client, @account_id, "example.com", _certificate_id = "22289")
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.Certificate
        assert data.private_key == "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIB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WH2cdYFOOMCgYBhrJnvffINC/d64Pp+BpP8\nyKN+lav5K6t3AWd4H2rVeJS5W7ijiLTIq8QdPNayUyE1o+S8695WrhGTF/aO3+ZD\nKQufikZHiQ7B43d7xL7BVBF0WK3lateGnEVyh7dIjMOdj92Wj4B6mv2pjQ2VvX/p\nAEWVLCtg24/+zL64VgxmXQKBgGosyXj1Zu2ldJcQ28AJxup3YVLilkNje4AXC2No\n6RCSvlAvm5gpcNGE2vvr9lX6YBKdl7FGt8WXBe/sysNEFfgmm45ZKOBCUn+dHk78\nqaeeQHKHdxMBy7utZWdgSqt+ZS299NgaacA3Z9kVIiSLDS4V2VeW7riujXXP/9TJ\nnxaRAoGBAMWXOfNVzfTyrKff6gvDWH+hqNICLyzvkEn2utNY9Q6WwqGuY9fvP/4Z\nXzc48AOBzUr8OeA4sHKJ79sJirOiWHNfD1swtvyVzsFZb6moiNwD3Ce/FzYCa3lQ\nU8blTH/uqpR2pSC6whzJ/lnSdqHUqhyp00000000000000000000\n-----END RSA PRIVATE KEY-----\n"
      end
    end
  end


  describe ".purchase_letsencrypt_certificate" do
    test "returns the Certificate in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/bingo.pizza/certificates/letsencrypt"
      method  = "post"
      fixture = "purchaseLetsencryptCertificate/success.http"
      attributes = %{}
      {:ok, body} = Poison.encode(attributes)

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url, request_body: body) do
        {:ok, response} = @module.purchase_letsencrypt_certificate(@client, @account_id, "bingo.pizza")
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.CertificatePurchase
        assert data.id == 101967
        assert data.certificate_id == 101967
        assert data.auto_renew == false
      end
    end
  end


  describe ".issue_letsencrypt_certificate" do
    test "returns the Certificate in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/bingo.pizza/certificates/letsencrypt/101967/issue"
      method  = "post"
      fixture = "issueLetsencryptCertificate/success.http"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        {:ok, response} = @module.issue_letsencrypt_certificate(@client, @account_id, "bingo.pizza", _certificate_id = 101967)
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.Certificate
        assert data.id == 101967
      end
    end
  end


  describe ".purchase_letsencrypt_certificate_renewal" do
    test "returns the CertificateRenewal in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/bingo.pizza/certificates/letsencrypt/101967/renewals"
      method  = "post"
      fixture = "purchaseRenewalLetsencryptCertificate/success.http"
      attributes = %{
        auto_renew: true
      }
      {:ok, body} = Poison.encode(attributes)

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url, request_body: body) do
        {:ok, response} = @module.purchase_letsencrypt_certificate_renewal(@client, @account_id, "bingo.pizza", _certificate_id = 101967, attributes)
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.CertificateRenewal
        assert data.id == 65082
        assert data.old_certificate_id == 101967
        assert data.new_certificate_id == 101972
      end
    end
  end


  describe ".issue_letsencrypt_certificate_renewal" do
    test "returns the Certificate in a Dnsimple.Response" do
      url     = "#{@client.base_url}/v2/#{@account_id}/domains/bingo.pizza/certificates/letsencrypt/101967/renewals/101972/issue"
      method  = "post"
      fixture = "issueRenewalLetsencryptCertificate/success.http"

      use_cassette :stub, ExvcrUtils.response_fixture(fixture, method: method, url: url) do
        {:ok, response} = @module.issue_letsencrypt_certificate_renewal(@client, @account_id, "bingo.pizza", _certificate_id = 101967, _certificate_renewal_id = 101972)
        assert response.__struct__ == Dnsimple.Response

        data = response.data
        assert data.__struct__ == Dnsimple.Certificate
        assert data.id == 101972
      end
    end
  end

end
