Skip to content

Commit

Permalink
feat: adds the RegistrantChange API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
DXTimer committed Dec 8, 2023
1 parent 03811e6 commit 740d26c
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 16 deletions.
17 changes: 12 additions & 5 deletions src/dnsimple-test/FixtureLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -25,7 +26,7 @@ public FixtureLoader(string version, string fixture)
private string JsonPartFrom(string fixture)
{
Fixture = fixture;
LoadFixture();
LoadFixture();
var lastLine = GetLines(true).Last();
return IsValidJson(lastLine) ? lastLine : "";
}
Expand Down Expand Up @@ -54,7 +55,7 @@ private IEnumerable<string> GetLines(bool removeEmptyLines = false)
return LoadFixture().Split(new[] { "\r\n", "\r", "\n" },
removeEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
}


private string LoadFixture()
{
Expand All @@ -70,16 +71,22 @@ public List<Parameter> ExtractHeaders()

foreach (var line in GetLines())
{
if(String.IsNullOrEmpty(line))
if (String.IsNullOrEmpty(line))
break;
if (line.Contains(':'))
{
var header = line.Split(':');
headers.Add(new Parameter(header[0], header[1], ParameterType.HttpHeader));
headers.Add(new Parameter(header[0], header[1], ParameterType.HttpHeader));
}
}

return headers;
}

public HttpStatusCode ExtractStatusCode()
{
return (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode),
GetLines().First().Split(' ')[1]);
}
}
}
}
19 changes: 10 additions & 9 deletions src/dnsimple-test/MockDnsimpleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public MockDnsimpleClient(string fixture)
Webhooks = new WebhooksService(this);
Zones = new ZonesService(this);
}

public void StatusCode(HttpStatusCode statusCode)
{
((MockHttpService)Http).StatusCode = statusCode;
}

public void ChangeBaseUrlTo(string baseUrl)
{
// Not needed at the moment, but having to implement...
Expand All @@ -80,25 +80,25 @@ public void SetUserAgent(string customUserAgent)

public string RequestSentTo()
{
return ((MockHttpService) Http).RequestUrlSent.UrlDecode();
return ((MockHttpService)Http).RequestUrlSent.UrlDecode();
}

public Method HttpMethodUsed()
{
return ((MockHttpService) Http).MethodSent;
return ((MockHttpService)Http).MethodSent;
}

public string PayloadSent()
{
return ((MockHttpService) Http).PayloadSent;
return ((MockHttpService)Http).PayloadSent;
}
}

public class MockHttpService : HttpService
{
private readonly FixtureLoader _fixtureLoader;
private readonly string _baseUrl;

public HttpStatusCode StatusCode { get; set; }
public string RequestUrlSent { get; private set; }
public Method MethodSent { get; private set; }
Expand All @@ -122,7 +122,7 @@ public override IRestResponse Execute(IRestRequest request)
MethodSent = request.Method;
try
{
PayloadSent = (string) request.Parameters.Find(x =>
PayloadSent = (string)request.Parameters.Find(x =>
x.ContentType.Equals("application/json")).Value;
}
catch (Exception)
Expand Down Expand Up @@ -156,13 +156,14 @@ public class MockResponse : RestResponse
{
public MockResponse(FixtureLoader loader)
{
StatusCode = loader.ExtractStatusCode();
Content = loader.ExtractJsonPayload();
Headers = loader.ExtractHeaders();
}

public void SetHeaders(List<Parameter> headers)
{
Headers = headers;
}
}
}
}
198 changes: 198 additions & 0 deletions src/dnsimple-test/Services/RegistrarTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using dnsimple;
using dnsimple.Services.ListOptions;
using dnsimple.Services;
using NUnit.Framework;

Expand Down Expand Up @@ -53,6 +55,24 @@ public class RegistrarServicesTest
private const string AuthorizeTransferOutFixture =
"authorizeDomainTransferOut/success.http";

private const string CheckRegistrantChangeFixture =
"checkRegistrantChange/success.http";

private const string GetRegistrantChangeFixture =
"getRegistrantChange/success.http";

private const string ListRegistrantChangeFixture =
"listRegistrantChanges/success.http";

private const string CreateRegistrantChangeFixture =
"createRegistrantChange/success.http";

private const string DeleteRegistrantChangeFixture =
"deleteRegistrantChange/success.http";

private const string DeleteRegistrantChangeAsyncFixture =
"deleteRegistrantChange/success_async.http";

private DateTime CreatedAt { get; } = DateTime.ParseExact(
"2016-12-09T19:35:31Z", "yyyy-MM-ddTHH:mm:ssZ",
CultureInfo.CurrentCulture);
Expand Down Expand Up @@ -128,6 +148,184 @@ public void GetDomainPricesFailure(long accountId, string domainName,
});
}

[Test]
[TestCase(1010, "example.com",
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes/check")]
public void CheckRegistrantChange(long accountId, string domainId, string expectedUrl)
{
var client = new MockDnsimpleClient(CheckRegistrantChangeFixture);
var checkInput = new CheckRegistrantChangeInput
{
DomainId = domainId,
ContactId = 101
};
var check = client.Registrar.CheckRegistrantChange(accountId, checkInput)
.Data;

Assert.Multiple(() =>
{
Assert.AreEqual(101, check.ContactId);
Assert.AreEqual(101, check.DomainId);
Assert.IsInstanceOf<List<TldExtendedAttribute>>(check.ExtendedAttributes);
Assert.AreEqual(true, check.RegistryOwnerChange);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}

[Test]
[TestCase(1010, 101,
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes/101")]
public void GetRegistrantChange(long accountId, long registrantChangeId, string expectedUrl)
{
var client = new MockDnsimpleClient(GetRegistrantChangeFixture);
var check = client.Registrar.GetRegistrantChange(accountId, registrantChangeId)
.Data;

Assert.Multiple(() =>
{
Assert.AreEqual(101, check.Id);
Assert.AreEqual(101, check.AccountId);
Assert.AreEqual(101, check.ContactId);
Assert.AreEqual(101, check.DomainId);
Assert.AreEqual("new", check.State);
Assert.IsInstanceOf<Dictionary<string, string>>(check.ExtendedAttributes);
Assert.AreEqual(true, check.RegistryOwnerChange);
Assert.AreEqual(null, check.IrtLockLiftedBy);
Assert.AreEqual(CreatedAt, check.CreatedAt);
Assert.AreEqual(UpdatedAt, check.UpdatedAt);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}


[Test]
[TestCase(1010, "example.com",
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes")]
public void CreateRegistrantChange(long accountId, object domainId, string expectedUrl)
{
var client = new MockDnsimpleClient(CreateRegistrantChangeFixture);
var createInput = new CreateRegistrantChangeInput
{
DomainId = domainId,
ContactId = 101,
ExtendedAttributes = new Dictionary<string, string>
{
{ "x-foo", "bar" }
}
};
var check = client.Registrar.CreateRegistrantChange(accountId, createInput)
.Data;

Assert.Multiple(() =>
{
Assert.AreEqual(101, check.Id);
Assert.AreEqual(101, check.AccountId);
Assert.AreEqual(101, check.ContactId);
Assert.AreEqual(101, check.DomainId);
Assert.AreEqual("new", check.State);
Assert.IsInstanceOf<Dictionary<string, string>>(check.ExtendedAttributes);
Assert.AreEqual(true, check.RegistryOwnerChange);
Assert.AreEqual(null, check.IrtLockLiftedBy);
Assert.AreEqual(CreatedAt, check.CreatedAt);
Assert.AreEqual(UpdatedAt, check.UpdatedAt);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}

[Test]
[TestCase(1010,
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes")]
public void ListRegistrantChange(long accountId, string expectedUrl)
{
var client = new MockDnsimpleClient(ListRegistrantChangeFixture);
var registrantChanges = client.Registrar.ListRegistrantChanges(accountId)
.Data;

var registrantChange = registrantChanges.First();

Assert.Multiple(() =>
{
Assert.AreEqual(101, registrantChange.Id);
Assert.AreEqual(101, registrantChange.AccountId);
Assert.AreEqual(101, registrantChange.ContactId);
Assert.AreEqual(101, registrantChange.DomainId);
Assert.AreEqual("new", registrantChange.State);
Assert.IsInstanceOf<Dictionary<string, string>>(registrantChange.ExtendedAttributes);
Assert.AreEqual(true, registrantChange.RegistryOwnerChange);
Assert.AreEqual(null, registrantChange.IrtLockLiftedBy);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}

[Test]
[TestCase(1010,
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes?sort=id:asc&per_page=42&page=7")]
public void ListRegistrantChangesWithSortingAndFiltering(long accountId, string expectedUrl)
{
var client = new MockDnsimpleClient(ListRegistrantChangeFixture);
var options = new RegistrantChangesListOptions
{
Pagination = new Pagination
{
PerPage = 42,
Page = 7
}

}.SortById(Order.asc);

client.Registrar.ListRegistrantChanges(accountId, options);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
}


[Test]
[TestCase(1010, 101,
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes/101")]
public void DeleteRegistrantChange(long accountId, long registrantChangeId, string expectedUrl)
{
var client = new MockDnsimpleClient(DeleteRegistrantChangeFixture);
var response = client.Registrar.DeleteRegistrantChange(accountId, registrantChangeId);
var data = response.Data;

Assert.Multiple(() =>
{
Assert.AreEqual(true, response.IsEmpty);
// data is an empty RegistrantChange object
Assert.AreEqual(0, data.Id);
Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}


[Test]
[TestCase(1010, 101,
"https://api.sandbox.dnsimple.com/v2/1010/registrar/registrant_changes/101")]
public void DeleteRegistrantChangeAsync(long accountId, long registrantChangeId, string expectedUrl)
{
var client = new MockDnsimpleClient(DeleteRegistrantChangeAsyncFixture);
var response = client.Registrar.DeleteRegistrantChange(accountId, registrantChangeId);
var registrantChange = response.Data;

Assert.Multiple(() =>
{
Assert.AreEqual(false, response.IsEmpty);
Assert.AreEqual(101, registrantChange.Id);
Assert.AreEqual(101, registrantChange.AccountId);
Assert.AreEqual(101, registrantChange.ContactId);
Assert.AreEqual(101, registrantChange.DomainId);
Assert.AreEqual("cancelling", registrantChange.State);
Assert.IsInstanceOf<Dictionary<string, string>>(registrantChange.ExtendedAttributes);
Assert.AreEqual(true, registrantChange.RegistryOwnerChange);
Assert.AreEqual(null, registrantChange.IrtLockLiftedBy);

Assert.AreEqual(expectedUrl, client.RequestSentTo());
});
}

[Test]
[TestCase(1010, "ruby.codes",
Expand Down
Loading

0 comments on commit 740d26c

Please sign in to comment.