这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged

3.1 #75

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ShopifySharp.Tests/Playlists/Exceptions.playlist
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_a_single_line_error::should_parse_a_single_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_array::should_parse_an_array_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_string::should_parse_a_single_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_object::should_parse_an_array_error" /></Playlist>
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_a_single_line_error::should_parse_a_single_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_array::should_parse_an_array_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_string::should_parse_a_single_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_error_object::should_parse_an_array_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_receiving_an_oauth_code_used_error::should_parse_an_oauth_code_used_error" /><Add Test="ShopifySharp.Tests.ShopifyException_Tests.When_reaching_the_rate_limit::should_throw_a_rate_limit_exception" /></Playlist>
1 change: 1 addition & 0 deletions ShopifySharp.Tests/Playlists/OrderRisks.playlist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifyOrderRiskService_Tests.When_creating_a_risk::should_create_a_risk" /><Add Test="ShopifySharp.Tests.ShopifyOrderRiskService_Tests.When_listing_order_risks::should_list_order_risks" /><Add Test="ShopifySharp.Tests.ShopifyOrderRiskService_Tests.When_getting_a_risk::should_get_a_risk" /><Add Test="ShopifySharp.Tests.ShopifyOrderRiskService_Tests.When_updating_a_risk::should_update_a_risk" /><Add Test="ShopifySharp.Tests.ShopifyOrderRiskService_Tests.When_deleting_a_risk::should_delete_a_risk" /></Playlist>
1 change: 1 addition & 0 deletions ShopifySharp.Tests/Playlists/SmartCollections.playlist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_counting_smart_collections::should_count_smart_collections" /><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_creating_a_smart_collection::should_create_a_smart_collection" /><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_updating_a_smart_collection::should_update_a_smart_collection" /><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_deleting_a_smart_collection::should_delete_a_smart_collection" /><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_getting_a_smart_collection::should_get_a_smart_collection" /><Add Test="ShopifySharp.Tests.ShopifySmartCollectionService_Tests.When_listing_a_smart_collection::should_create_a_smart_collection" /></Playlist>
1 change: 1 addition & 0 deletions ShopifySharp.Tests/Playlists/Variants.playlist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_counting_product_variants::should_count_product_variants" /><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_creating_product_variants::should_create_a_product_variant" /><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_updating_product_variants::should_update_a_product_variant" /><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_listing_product_variants::should_list_product_variants" /><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_deleting_a_product_variant::should_delete_a_product_variant" /><Add Test="ShopifySharp.Tests.ShopifyProductVariantService_Tests.When_getting_product_variants::should_get_a_product_variant" /></Playlist>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -19,6 +19,22 @@ class ShopifyExceptionService : ShopifyService
/// <param name="shopAccessToken">An API access token for the shop.</param>
public ShopifyExceptionService(string myShopifyUrl, string shopAccessToken): base(myShopifyUrl, shopAccessToken) { }

/// <summary>
/// A method that will throw an exception which looks like {"error":"invalid_request","error_description":"The authorization code was not found or was already used"}
/// This error is thrown when trying to authorize an OAuth code that has already been used.
/// </summary>
public void ThrowOAuthCodeUsedException()
{
var response = new RestResponse()
{
RawBytes = Encoding.UTF8.GetBytes("{\"error\":\"invalid_request\",\"error_description\":\"The authorization code was not found or was already used\"}"),
StatusCode = HttpStatusCode.NotAcceptable,
StatusDescription = "Not Acceptable"
};

RequestEngine.CheckResponseExceptions(response);
}

/// <summary>
/// A method that will throw an exception which looks like { errors: "some error message"}
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Machine.Specifications;
using ShopifySharp.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyException_Tests
{
[Subject(typeof(ShopifyRateLimitException))]
class When_reaching_the_rate_limit
{
Establish context = () =>
{

};

Because of = () =>
{
try
{
throw new ShopifyRateLimitException();
}
catch (ShopifyException e)
{
CaughtRateExceptionWithBaseException = true;
}

var tasks = new List<Task<IEnumerable<ShopifyEvent>>>();

// Reach the rate limit of 40 requests per second
for (int i = 0; i < 100; i++)
{
tasks.Add(Service.ListAsync(new ShopifyEventListFilter()
{
Limit = 1,
}));
}

try
{
var result = Task.WhenAll(tasks.ToArray()).Await().AsTask.Result;
}
catch (AggregateException e)
{
Ex = e.InnerException;
}
catch (Exception e)
{
Ex = e;
}
};

It should_throw_a_rate_limit_exception = () =>
{
CaughtRateExceptionWithBaseException.ShouldBeTrue();
Ex.ShouldBeOfExactType<ShopifyRateLimitException>();

var e = (ShopifyException)Ex;

e.JsonError.ShouldNotBeNull();
e.Errors.Count.ShouldBeGreaterThanOrEqualTo(1);
e.Errors.First().Key.Equals("Error").ShouldBeTrue();
e.Errors.First().Value.First().ShouldEqual("Exceeded 2 calls per second for api client. Reduce request rates to resume uninterrupted service.");
};

Cleanup after = () =>
{

};

static ShopifyEventService Service = new ShopifyEventService(Utils.MyShopifyUrl, Utils.AccessToken);

static Exception Ex;

static bool CaughtRateExceptionWithBaseException = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyException_Tests
{
[Subject(typeof(ShopifyException))]
class When_receiving_an_oauth_code_used_error
{
Establish context = () =>
{

};

Because of = () =>
{
Ex = Catch.Exception(() => Service.ThrowOAuthCodeUsedException());
};

It should_parse_an_oauth_code_used_error = () =>
{
Ex.ShouldBeOfExactType<ShopifyException>();

var e = (ShopifyException)Ex;

e.Message.ShouldContain("authorization code was not found or was already used");
e.JsonError.ShouldNotBeNull();
e.Errors.Count.ShouldEqual(1);
e.Errors.First().Key.ShouldEqual("invalid_request");
e.Errors.First().Value.Count().ShouldEqual(1);
};

Cleanup after = () =>
{

};

static ShopifyExceptionService Service = new ShopifyExceptionService(Utils.MyShopifyUrl, Utils.AccessToken);

static Exception Ex;
}
}
45 changes: 45 additions & 0 deletions ShopifySharp.Tests/ShopifyOrderRiskService Tests/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyOrderRiskService_Tests
{
static class RiskUtils
{
public static string Message = "This looks risky!";

public static decimal Score = (decimal)0.85;

public static string Recommendation = "cancel";

public static string Source = "External";

public static bool CauseCancel = false;

public static bool Display = true;

public static ShopifyOrderRisk CreateRisk()
{
return new ShopifyOrderRisk()
{
Message = Message,
Score = Score,
Recommendation = Recommendation,
Source = Source,
CauseCancel = CauseCancel,
Display = Display,
};
}

public static async Task<long> GetOrderId()
{
var service = new ShopifyOrderService(Utils.MyShopifyUrl, Utils.AccessToken);
var orders = await service.ListAsync(new Filters.ShopifyOrderFilter()
{
Limit = 1,
Fields = "id"
});

return orders.First().Id.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Machine.Specifications;

namespace ShopifySharp.Tests.ShopifyOrderRiskService_Tests
{
[Subject(typeof(ShopifyOrderRiskService))]
class When_creating_a_risk
{
Establish context = () =>
{
OrderId = RiskUtils.GetOrderId().Await().AsTask.Result;
};

Because of = () =>
{
Risk = Service.CreateAsync(OrderId, RiskUtils.CreateRisk()).Await().AsTask.Result;
};

It should_create_a_risk = () =>
{
Risk.ShouldNotBeNull();
Risk.OrderId.ShouldEqual(OrderId);
Risk.Message.ShouldEqual(RiskUtils.Message);
Risk.Score.ShouldEqual(RiskUtils.Score);
Risk.Recommendation.ShouldEqual(RiskUtils.Recommendation);
Risk.Source.ShouldEqual(RiskUtils.Source);
Risk.CauseCancel.ShouldEqual(RiskUtils.CauseCancel);
Risk.Display.ShouldEqual(RiskUtils.Display);
};

Cleanup after = () =>
{
if (Risk != null)
{
Service.DeleteAsync(OrderId, Risk.Id.Value).Await();
}
};

static long OrderId;

static ShopifyOrderRisk Risk;

static ShopifyOrderRiskService Service = new ShopifyOrderRiskService(Utils.MyShopifyUrl, Utils.AccessToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Machine.Specifications;
using System;

namespace ShopifySharp.Tests.ShopifyOrderRiskService_Tests
{
[Subject(typeof(ShopifyOrderRiskService))]
class When_deleting_a_risk
{
Establish context = () =>
{
OrderId = RiskUtils.GetOrderId().Await().AsTask.Result;
RiskId = Service.CreateAsync(OrderId, RiskUtils.CreateRisk()).Await().AsTask.Result.Id.Value;
};

Because of = () =>
{
Ex = Catch.Exception(() => Service.DeleteAsync(OrderId, RiskId).Await());
};

It should_delete_a_risk = () =>
{
Ex.ShouldBeNull();
};

Cleanup after = () =>
{
};

static long OrderId;

static long RiskId;

static Exception Ex;

static ShopifyOrderRiskService Service = new ShopifyOrderRiskService(Utils.MyShopifyUrl, Utils.AccessToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Machine.Specifications;

namespace ShopifySharp.Tests.ShopifyOrderRiskService_Tests
{
[Subject(typeof(ShopifyOrderRiskService))]
class When_getting_a_risk
{
Establish context = () =>
{
OrderId = RiskUtils.GetOrderId().Await().AsTask.Result;
RiskId = Service.CreateAsync(OrderId, RiskUtils.CreateRisk()).Await().AsTask.Result.Id.Value;
};

Because of = () =>
{
Risk = Service.GetAsync(OrderId, RiskId).Await().AsTask.Result;
};

It should_get_a_risk = () =>
{
Risk.ShouldNotBeNull();
Risk.OrderId.ShouldEqual(OrderId);
Risk.Message.ShouldEqual(RiskUtils.Message);
Risk.Score.ShouldEqual(RiskUtils.Score);
Risk.Recommendation.ShouldEqual(RiskUtils.Recommendation);
Risk.Source.ShouldEqual(RiskUtils.Source);
Risk.CauseCancel.ShouldEqual(RiskUtils.CauseCancel);
Risk.Display.ShouldEqual(RiskUtils.Display);
};

Cleanup after = () =>
{
if (Risk != null)
{
Service.DeleteAsync(OrderId, Risk.Id.Value).Await();
}
};

static long RiskId;

static long OrderId;

static ShopifyOrderRisk Risk;

static ShopifyOrderRiskService Service = new ShopifyOrderRiskService(Utils.MyShopifyUrl, Utils.AccessToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Machine.Specifications;
using System.Collections.Generic;
using System.Linq;

namespace ShopifySharp.Tests.ShopifyOrderRiskService_Tests
{
[Subject(typeof(ShopifyOrderRiskService))]
class When_listing_order_risks
{
Establish context = () =>
{
OrderId = RiskUtils.GetOrderId().Await().AsTask.Result;
};

Because of = () =>
{
Risks = Service.ListAsync(OrderId).Await().AsTask.Result;
};

It should_list_order_risks = () =>
{
Risks.ShouldNotBeNull();

// Not all orders have a risk
if (Risks.Count() >= 1)
{
Risks.All(r => r.OrderId.HasValue).ShouldBeTrue();
Risks.All(r => string.IsNullOrEmpty(r.Message) == false).ShouldBeTrue();
Risks.All(r => r.Id.HasValue).ShouldBeTrue();
}
};

Cleanup after = () =>
{

};

static long OrderId { get; set; }

static ShopifyOrderRiskService Service = new ShopifyOrderRiskService(Utils.MyShopifyUrl, Utils.AccessToken);

static IEnumerable<ShopifyOrderRisk> Risks;
}
}
Loading