Skip to content

Commit

Permalink
Merge pull request #86 from maxmind/greg/optional-ip
Browse files Browse the repository at this point in the history
Make the IP address optional
  • Loading branch information
horgh authored Oct 14, 2020
2 parents 52ec725 + f7ba14d commit 2ce0e3e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
19 changes: 9 additions & 10 deletions MaxMind.MinFraud.UnitTest/Request/DeviceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,54 @@ namespace MaxMind.MinFraud.UnitTest.Request
{
public class DeviceTest
{
private IPAddress IP { get; } = IPAddress.Parse("1.1.1.1");

[Fact]
public void TestIPAddress()
{
var device = new Device(ipAddress: IP);
Assert.Equal(IP, device.IPAddress);
var ip = IPAddress.Parse("1.1.1.1");
var device = new Device(ipAddress: ip);
Assert.Equal(ip, device.IPAddress);
}

[Fact]
public void TestUserAgent()
{
var ua = "Mozila 5";
var device = new Device(ipAddress: IP, userAgent: ua);
var device = new Device(userAgent: ua);
Assert.Equal(ua, device.UserAgent);
}

[Fact]
public void TestAcceptLanguage()
{
var al = "en-US";
var device = new Device(IP, acceptLanguage: al);
var device = new Device(acceptLanguage: al);
Assert.Equal(al, device.AcceptLanguage);
}

[Fact]
public void TestSessionAge()
{
var device = new Device(IP, sessionAge: 3600);
var device = new Device(sessionAge: 3600);
Assert.Equal(3600, device.SessionAge);
}

[Fact]
public void TestSessionAgeIsNegative()
{
Assert.Throws<ArgumentException>(() => new Device(IP, sessionAge: -1));
Assert.Throws<ArgumentException>(() => new Device(sessionAge: -1));
}

[Fact]
public void TestSessionId()
{
var device = new Device(IP, sessionId: "foo");
var device = new Device(sessionId: "foo");
Assert.Equal("foo", device.SessionId);
}

[Fact]
public void TestSessionIdIsTooLong()
{
Assert.Throws<ArgumentException>(() => new Device(IP, sessionId: new string('x', 256)));
Assert.Throws<ArgumentException>(() => new Device(sessionId: new string('x', 256)));
}
}
}
25 changes: 12 additions & 13 deletions MaxMind.MinFraud.UnitTest/Request/MinFraudRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,74 @@ namespace MaxMind.MinFraud.UnitTest.Request
{
public class MinFraudRequestTest
{
private Device Device { get; } = new Device(IPAddress.Parse("1.1.1.1"));

[Fact]
public void TestAccount()
{
var request = new Transaction(device: Device, account: new Account(userId: "1"));
var request = new Transaction(account: new Account(userId: "1"));
Assert.Equal("1", request.Account!.UserId);
}

[Fact]
public void TestBilling()
{
var request = new Transaction(device: Device, billing: new Billing(address: "add"));
var request = new Transaction(billing: new Billing(address: "add"));
Assert.Equal("add", request.Billing!.Address);
}

[Fact]
public void TestCreditCard()
{
var request = new Transaction(device: Device, creditCard: new CreditCard(bankName: "name"));
var request = new Transaction(creditCard: new CreditCard(bankName: "name"));
Assert.Equal("name", request.CreditCard!.BankName);
}

[Fact]
public void TestDevice()
{
var request = new Transaction(device: Device);
Assert.Equal(IPAddress.Parse("1.1.1.1"), request.Device.IPAddress);
var ip = IPAddress.Parse("1.1.1.1");
var request = new Transaction(device: new Device(ip));
Assert.Equal(ip, request.Device?.IPAddress);
}

[Fact]
public void TestEmail()
{
var request = new Transaction(device: Device, email: new Email(domain: "test.com"));
var request = new Transaction(email: new Email(domain: "test.com"));
Assert.Equal("test.com", request.Email!.Domain);
}

[Fact]
public void TestEvent()
{
var request = new Transaction(device: Device, userEvent: new Event(shopId: "1"));
var request = new Transaction(userEvent: new Event(shopId: "1"));
Assert.Equal("1", request.Event!.ShopId);
}

[Fact]
public void TestOrder()
{
var request = new Transaction(device: Device, order: new Order(affiliateId: "af1"));
var request = new Transaction(order: new Order(affiliateId: "af1"));
Assert.Equal("af1", request.Order!.AffiliateId);
}

[Fact]
public void TestPayment()
{
var request = new Transaction(device: Device, payment: new Payment(declineCode: "d"));
var request = new Transaction(payment: new Payment(declineCode: "d"));
Assert.Equal("d", request.Payment!.DeclineCode);
}

[Fact]
public void TestShipping()
{
var request = new Transaction(device: Device, shipping: new Shipping(lastName: "l"));
var request = new Transaction(shipping: new Shipping(lastName: "l"));
Assert.Equal("l", request.Shipping!.LastName);
}

[Fact]
public void TestShoppingCart()
{
var request = new Transaction(device: Device,
var request = new Transaction(
shoppingCart: new List<ShoppingCartItem> { new ShoppingCartItem(itemId: "1") });
Assert.Equal("1", request.ShoppingCart![0].ItemId);
}
Expand Down
1 change: 0 additions & 1 deletion MaxMind.MinFraud.UnitTest/WebServiceClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public async Task TestInsufficientFunds()
[InlineData("ACCOUNT_ID_REQUIRED")]
[InlineData("AUTHORIZATION_INVALID")]
[InlineData("LICENSE_KEY_REQUIRED")]
[InlineData("USER_ID_REQUIRED")]
public async Task TestInvalidAuth(string code)
{
var exception = await Record.ExceptionAsync(async () => await CreateInsightsError(
Expand Down
8 changes: 4 additions & 4 deletions MaxMind.MinFraud/Request/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public sealed class Device
/// the device used in the transaction.</param>
/// <param name="sessionAge">The number of seconds between the
/// creation of the user's session and the time of the transaction.
/// Note that sessionAge is not the duration of the current visit, but
/// Note that sessionAge is not the duration of the current visit, but
/// the time since the start of the first visit.</param>
/// <param name="sessionId">A string up to 255 characters in length.
/// This is an ID that uniquely identifies a visitor's session on the
/// site.</param>
public Device(
IPAddress ipAddress,
IPAddress? ipAddress = null,
string? userAgent = null,
string? acceptLanguage = null,
double? sessionAge = null,
Expand Down Expand Up @@ -56,7 +56,7 @@ public Device(
/// in the transaction.
/// </summary>
[JsonProperty("ip_address")]
public IPAddress IPAddress { get; }
public IPAddress? IPAddress { get; }

/// <summary>
/// The HTTP “User-Agent” header of the browser used in the
Expand Down Expand Up @@ -97,4 +97,4 @@ public override string ToString()
return $"IPAddress: {IPAddress}, UserAgent: {UserAgent}, AcceptLanguage: {AcceptLanguage}, SessionAge: {SessionAge}, SessionId: {SessionId}";
}
}
}
}
8 changes: 4 additions & 4 deletions MaxMind.MinFraud/Request/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class Transaction
/// for a general overview of the request sent to the web
/// service.
/// </summary>
/// <param name="device">Information about the device used in the transaction. This param is required.</param>
/// <param name="device">Information about the device used in the transaction.</param>
/// <param name="account">Information about the account used in the transaction.</param>
/// <param name="billing">Billing information used in the transaction.</param>
/// <param name="creditCard">Information about the credit card used in the transaction.</param>
Expand All @@ -28,7 +28,7 @@ public sealed class Transaction
/// <param name="shipping">Shipping information used in the transaction.</param>
/// <param name="shoppingCart">List of shopping items in the transaction.</param>
public Transaction(
Device device,
Device? device = null,
Account? account = null,
Billing? billing = null,
CreditCard? creditCard = null,
Expand Down Expand Up @@ -82,7 +82,7 @@ public Transaction(
/// Information about the device used in the transaction.
/// </summary>
[JsonProperty("device")]
public Device Device { get; }
public Device? Device { get; }

/// <summary>
/// Information about the email used in the transaction.
Expand Down Expand Up @@ -130,4 +130,4 @@ public override string ToString()
$"Account: {Account}, Billing: {Billing}, CreditCard: {CreditCard}, Device: {Device}, Email: {Email}, Event: {Event}, Order: {Order}, Payment: {Payment}, Shipping: {Shipping}, ShoppingCart: {ShoppingCart}";
}
}
}
}
1 change: 0 additions & 1 deletion MaxMind.MinFraud/WebServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ private static void HandleErrorWithJsonBody(WebServiceError error, HttpResponseM
case "ACCOUNT_ID_REQUIRED":
case "AUTHORIZATION_INVALID":
case "LICENSE_KEY_REQUIRED":
case "USER_ID_REQUIRED":
throw new AuthenticationException(error.Error);
case "INSUFFICIENT_FUNDS":
throw new InsufficientFundsException(error.Error);
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ are sending to minFraud:

```csharp
var transaction = new Transaction(
device: new Device(System.Net.IPAddress.Parse("152.216.7.110"),
userAgent:
"Mozilla/5.0 (X11; Linux x86_64)",
device: new Device(
ipAddress: System.Net.IPAddress.Parse("152.216.7.110"),
userAgent: "Mozilla/5.0 (X11; Linux x86_64)",
acceptLanguage: "en-US,en;q=0.8"
),
account:
Expand All @@ -70,7 +70,6 @@ var transaction = new Transaction(
);
```


After creating the request object, send a non-blocking minFraud Score request
by calling the `ScoreAsync` method using the `await` keyword from a method
marked as async:
Expand Down Expand Up @@ -163,9 +162,9 @@ public class MinFraudExample
static public async Task MinFraudAsync()
{
var transaction = new Transaction(
device: new Device(System.Net.IPAddress.Parse("152.216.7.110"),
userAgent:
"Mozilla/5.0 (X11; Linux x86_64)",
device: new Device(
ipAddress: System.Net.IPAddress.Parse("152.216.7.110"),
userAgent: "Mozilla/5.0 (X11; Linux x86_64)",
acceptLanguage: "en-US,en;q=0.8",
sessionAge: 3600,
sessionId: "a333a4e127f880d8820e56a66f40717c"
Expand Down
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Release Notes

* This library now requires .NET Framework 4.5 or greater or .NET Standard
2.0 or greater.
* The device IP address is no longer a required input.
* Added `Tsys` to the `PaymentProcessor` enum.

2.8.0 (2020-09-25)
Expand Down

0 comments on commit 2ce0e3e

Please sign in to comment.