|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using EasyPost.Models.API; |
| 5 | +using EasyPost.Tests._Utilities; |
| 6 | +using EasyPost.Tests._Utilities.Attributes; |
| 7 | +using EasyPost.Utilities.Internal.Attributes; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace EasyPost.Tests.BetaFeaturesTests.ModelsTests |
| 11 | +{ |
| 12 | + public class ShipmentTests : UnitTest |
| 13 | + { |
| 14 | + public ShipmentTests() : base("shipment_with_parameters") |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + #region Tests |
| 19 | + |
| 20 | + #region Test CRUD Operations |
| 21 | + |
| 22 | + // If the shipment was purchased with a USPS rate, it must have had its insurance set to `0` when bought |
| 23 | + // so that USPS doesn't automatically insure it so we could manually insure it here. |
| 24 | + [Fact] |
| 25 | + [CrudOperations.Create] |
| 26 | + [Testing.Function] |
| 27 | + public async Task TestInsure() |
| 28 | + { |
| 29 | + UseVCR("insure"); |
| 30 | + |
| 31 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 32 | + // Set to 0 so USPS doesn't insure this automatically and we can insure the shipment manually |
| 33 | + shipmentData["insurance"] = 0; |
| 34 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 35 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 36 | + |
| 37 | + BetaFeatures.Parameters.Shipments.Insure insureParameters = new BetaFeatures.Parameters.Shipments.Insure |
| 38 | + { |
| 39 | + Amount = "100", |
| 40 | + }; |
| 41 | + |
| 42 | + shipment = await shipment.Insure(insureParameters); |
| 43 | + |
| 44 | + Assert.Equal("100.00", shipment.Insurance); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + [CrudOperations.Update] |
| 49 | + [Testing.Function] |
| 50 | + public async Task TestBuy() |
| 51 | + { |
| 52 | + UseVCR("buy"); |
| 53 | + |
| 54 | + Dictionary<string, object> shipmentData = Fixtures.FullShipment; |
| 55 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 56 | + |
| 57 | + // buy with rate ID |
| 58 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 59 | + Rate rate = shipment.LowestRate(); |
| 60 | + |
| 61 | + BetaFeatures.Parameters.Shipments.Buy buyParameters = new BetaFeatures.Parameters.Shipments.Buy(rate.Id); |
| 62 | + await shipment.Buy(buyParameters); |
| 63 | + |
| 64 | + Assert.NotNull(shipment.PostageLabel); |
| 65 | + |
| 66 | + // buy with rate |
| 67 | + shipment = await Client.Shipment.Create(shipmentParameters); |
| 68 | + rate = shipment.LowestRate(); |
| 69 | + |
| 70 | + buyParameters = new BetaFeatures.Parameters.Shipments.Buy(rate); |
| 71 | + await shipment.Buy(buyParameters); |
| 72 | + |
| 73 | + Assert.NotNull(shipment.PostageLabel); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + [CrudOperations.Update] |
| 78 | + [Testing.Parameters] |
| 79 | + public async Task TestBuyWithCarbonOffset() |
| 80 | + { |
| 81 | + UseVCR("buy_with_carbon_offset"); |
| 82 | + |
| 83 | + Dictionary<string, object> shipmentData = Fixtures.FullShipment; |
| 84 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 85 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 86 | + Rate rate = shipment.LowestRate(); |
| 87 | + |
| 88 | + BetaFeatures.Parameters.Shipments.Buy buyParameters = new BetaFeatures.Parameters.Shipments.Buy(rate) |
| 89 | + { |
| 90 | + CarbonOffset = true, |
| 91 | + }; |
| 92 | + |
| 93 | + await shipment.Buy(buyParameters); |
| 94 | + |
| 95 | + Assert.NotNull(shipment.Fees); |
| 96 | + bool carbonOffsetIncluded = shipment.Fees.Any(fee => fee.Type == "CarbonOffsetFee"); |
| 97 | + Assert.True(carbonOffsetIncluded); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + [CrudOperations.Update] |
| 102 | + [Testing.Parameters] |
| 103 | + public async Task TestBuyWithEndShipper() |
| 104 | + { |
| 105 | + UseVCR("buy_with_end_shipper"); |
| 106 | + |
| 107 | + Dictionary<string, object> endShipperData = Fixtures.CaAddress1; |
| 108 | + BetaFeatures.Parameters.EndShippers.Create endShipperParameters = Fixtures.Parameters.EndShippers.Create(endShipperData); |
| 109 | + EndShipper endShipper = await Client.EndShipper.Create(endShipperParameters); |
| 110 | + |
| 111 | + Dictionary<string, object> shipmentData = Fixtures.FullShipment; |
| 112 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 113 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 114 | + Rate rate = shipment.LowestRate(); |
| 115 | + |
| 116 | + BetaFeatures.Parameters.Shipments.Buy buyParameters = new BetaFeatures.Parameters.Shipments.Buy(rate) |
| 117 | + { |
| 118 | + EndShipperId = endShipper.Id, |
| 119 | + }; |
| 120 | + |
| 121 | + await shipment.Buy(buyParameters); |
| 122 | + |
| 123 | + Assert.NotNull(shipment.PostageLabel); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + [CrudOperations.Update] |
| 128 | + [Testing.Function] |
| 129 | + public async Task TestGenerateLabel() |
| 130 | + { |
| 131 | + UseVCR("generate_label"); |
| 132 | + |
| 133 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 134 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 135 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 136 | + |
| 137 | + BetaFeatures.Parameters.Shipments.GenerateLabel generateLabelParameters = new BetaFeatures.Parameters.Shipments.GenerateLabel |
| 138 | + { |
| 139 | + FileFormat = "ZPL", |
| 140 | + }; |
| 141 | + |
| 142 | + shipment = await shipment.GenerateLabel(generateLabelParameters); |
| 143 | + |
| 144 | + Assert.NotNull(shipment.PostageLabel.LabelZplUrl); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + [CrudOperations.Update] |
| 149 | + [Testing.Function] |
| 150 | + public async Task TestRegenerateRates() |
| 151 | + { |
| 152 | + UseVCR("regenerate_rates"); |
| 153 | + |
| 154 | + Dictionary<string, object> shipmentData = Fixtures.FullShipment; |
| 155 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 156 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 157 | + |
| 158 | + BetaFeatures.Parameters.Shipments.RegenerateRates regenerateRatesParameters = new BetaFeatures.Parameters.Shipments.RegenerateRates(); |
| 159 | + |
| 160 | + await shipment.RegenerateRates(regenerateRatesParameters); |
| 161 | + |
| 162 | + List<Rate> rates = shipment.Rates; |
| 163 | + |
| 164 | + Assert.NotNull(rates); |
| 165 | + foreach (Rate rate in rates) |
| 166 | + { |
| 167 | + Assert.IsType<Rate>(rate); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + [CrudOperations.Update] |
| 173 | + [Testing.Parameters] |
| 174 | + public async Task TestRegenerateRatesWithCarbonOffset() |
| 175 | + { |
| 176 | + UseVCR("regenerate_rates_with_carbon_offset"); |
| 177 | + |
| 178 | + Dictionary<string, object> shipmentData = Fixtures.OneCallBuyShipment; |
| 179 | + BetaFeatures.Parameters.Shipments.Create shipmentParameters = Fixtures.Parameters.Shipments.Create(shipmentData); |
| 180 | + Shipment shipment = await Client.Shipment.Create(shipmentParameters); |
| 181 | + List<Rate> baseRates = shipment.Rates; |
| 182 | + |
| 183 | + BetaFeatures.Parameters.Shipments.RegenerateRates regenerateRatesParameters = new BetaFeatures.Parameters.Shipments.RegenerateRates |
| 184 | + { |
| 185 | + CarbonOffset = true, |
| 186 | + }; |
| 187 | + await shipment.RegenerateRates(regenerateRatesParameters); |
| 188 | + |
| 189 | + List<Rate> newRatesWithCarbon = shipment.Rates; |
| 190 | + |
| 191 | + Rate baseRate = baseRates!.First(); |
| 192 | + Rate newRateWithCarbon = newRatesWithCarbon!.First(); |
| 193 | + |
| 194 | + Assert.Null(baseRate.CarbonOffset); |
| 195 | + Assert.NotNull(newRateWithCarbon.CarbonOffset); |
| 196 | + } |
| 197 | + |
| 198 | + #endregion |
| 199 | + |
| 200 | + #endregion |
| 201 | + } |
| 202 | +} |
0 commit comments