Beispiel #1
0
        public void TestFetch()
        {
            // Arrange
            string description = "capture description";

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.orderUrl + this.path + "/1002")).Return(this.httpWebRequest);

            string json = "{\r\n  \"capture_id\": \"" + this.captureId + "\",\r\n  \"description\": \"" + description + "\",\r\n  }";
            WebHeaderCollection headers = new WebHeaderCollection();

            headers[HttpResponseHeader.ContentType] = "application/json";

            HttpStatusCode status   = HttpStatusCode.OK;
            IResponse      response = new Response(status, headers, json);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, string.Empty)).Return(response);

            // Act
            CaptureData captureData = this.capture.Fetch();

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.captureId, captureData.CaptureId);
            Assert.AreEqual(description, captureData.Description);
            Assert.AreEqual(0, this.httpWebRequest.ContentLength);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Get);
        }
Beispiel #2
0
        public void TestFetch()
        {
            // Arrange
            string orderId     = "0003";
            string expectedUrl = "https://dummytesturi.test/checkout/v3/orders/0003";
            int    orderAmount = 1234;

            this.requestMock.Expect(x => x.CreateRequest(expectedUrl)).Return(this.httpWebRequest);

            string json = "{\r\n  \"order_id\": \"" + orderId + "\",\r\n  \"order_amount\": " + orderAmount + ",\r\n }";
            WebHeaderCollection headers = new WebHeaderCollection();

            headers[HttpResponseHeader.ContentType] = "application/json";

            IResponse response = new Response(HttpStatusCode.OK, headers, json);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, string.Empty)).Return(response);

            // Act
            this.checkoutOrder = new Klarna.Rest.Checkout.CheckoutOrder(this.connector, orderId);
            CheckoutOrderData checkoutOrderData = this.checkoutOrder.Fetch();

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(0, this.httpWebRequest.ContentLength);
            Assert.AreEqual(orderId, checkoutOrderData.OrderId);
            Assert.AreEqual(orderAmount, checkoutOrderData.OrderAmount);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Get);
        }
Beispiel #3
0
        public void TestUpdateCustomerDetails()
        {
            // Arrange
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.orderUrl + this.path + "/" + this.captureId + "/customer-details")).Return(this.httpWebRequest);

            UpdateCustomerDetails updateCustomerDetails = new UpdateCustomerDetails()
            {
                BillingAddress = TestsHelper.GetAddress1()
            };

            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.NoContent, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, updateCustomerDetails.ConvertToJson())).Return(response);

            // Act
            this.capture.UpdateCustomerDetails(updateCustomerDetails);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.httpWebRequest.ContentLength, updateCustomerDetails.ConvertToJson().Length);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Patch);
        }
Beispiel #4
0
        public void TestCreate()
        {
            // Arrange
            this.capture = new Klarna.Rest.OrderManagement.Capture(this.connector, this.orderUrl, string.Empty);
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.orderUrl + this.path)).Return(this.httpWebRequest);

            CaptureData captureData = new CaptureData()
            {
                Description    = "the desc...",
                CapturedAmount = 111
            };
            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.Created, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, captureData.ConvertToJson())).Return(response);

            // Act
            this.capture.Create(captureData);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.location, this.capture.Location.OriginalString);
            Assert.AreEqual(captureData.ConvertToJson().Length, this.httpWebRequest.ContentLength);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #5
0
        public void TestRefundCreated()
        {
            // Arrange
            Refund refund = new Refund()
            {
                RefundedAmount = 12345,
                Description    = "Refund description"
            };

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.path + '/' + this.orderId + "/refunds")).Return(this.httpWebRequest);

            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.Created, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, refund.ConvertToJson())).Return(response);

            // Act
            this.order.Refund(refund);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.httpWebRequest.ContentLength, refund.ConvertToJson().Length);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #6
0
        public void TestUpdateMerchantReferences()
        {
            // Arrange
            UpdateMerchantReferences updateMerchantReferences = new UpdateMerchantReferences()
            {
                MerchantReference1 = "A merchant reference",
                MerchantReference2 = "Another merchant reference"
            };

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.path + '/' + this.orderId + "/merchant-references")).Return(this.httpWebRequest);

            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.NoContent, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, updateMerchantReferences.ConvertToJson())).Return(response);

            // Act
            this.order.UpdateMerchantReferences(updateMerchantReferences);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.httpWebRequest.ContentLength, updateMerchantReferences.ConvertToJson().Length);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Patch);
        }
Beispiel #7
0
        public void TestFetch()
        {
            // Arrange
            int    orderAmount = 1234;
            string description = "A description";

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.path + '/' + this.orderId)).Return(this.httpWebRequest);

            string json        = "{\r\n  \"order_id\": \"" + this.orderId + "\",\r\n  \"order_amount\": " + orderAmount + ",\r\n  \"captures\": [\r\n    {\r\n      \"capture_id\": \"" + this.captureId + "\",\r\n       \"description\": \"" + description + "\",\r\n    }\r\n  ],\r\n}";
            string captureJson = JsonConvert.DeserializeObject <Klarna.Rest.Models.OrderData>(json).Captures[0].ConvertToJson();

            WebHeaderCollection headers = new WebHeaderCollection();

            headers[HttpResponseHeader.ContentType] = "application/json";

            IResponse fetchResponse = new Response(HttpStatusCode.OK, headers, json);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, string.Empty)).Return(fetchResponse);

            // Act
            Models.OrderData orderData = this.order.Fetch();

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.orderId, orderData.OrderId);
            Assert.AreEqual(orderAmount, orderData.OrderAmount);
            Assert.AreEqual(1, orderData.Captures.Count);
            Assert.AreEqual(this.captureId, orderData.Captures[0].CaptureId);
            Assert.AreEqual(description, orderData.Captures[0].Description);
            Assert.AreEqual(0, this.httpWebRequest.ContentLength);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Get);
        }
Beispiel #8
0
        public void Transport_Connector_CreateRequest_Basic()
        {
            // Arrange
            string     payload = "payload";
            HttpMethod method  = HttpMethod.Post;
            var        request = (HttpWebRequest)WebRequest.Create("https://somerandomuri.test");

            this.requestMock.Stub(x => x.CreateRequest(this.baseUrl.ToString())).Return(request);

            // Act
            this.connector.CreateRequest(this.baseUrl.ToString(), HttpMethod.Post, payload);

            // Assert
            TestsHelper.AssertRequest(this.merchantId, this.secret, request, method);
            Assert.AreEqual(payload.Length, request.ContentLength);
        }
Beispiel #9
0
        public void Transport_Connector_CreateRequest_UrlStartsWithBaseUrl()
        {
            // Arrange
            string     payload = "payload";
            string     newUrl  = this.baseUrl + "newUrl";
            HttpMethod method  = HttpMethod.Get;
            var        request = (HttpWebRequest)WebRequest.Create("https://somerandomuri.test");

            this.requestMock.Stub(x => x.CreateRequest(newUrl)).Return(request);

            // Act
            this.connector.CreateRequest(newUrl, method, payload);

            // Assert
            TestsHelper.AssertRequest(this.merchantId, this.secret, request, method);
            Assert.AreEqual(payload.Length, request.ContentLength);
        }
Beispiel #10
0
        public void Transport_Connector_CreateRequest_NewUrl()
        {
            // Arrange
            string     payload = "payload";
            string     newUrl  = "newUrl/sdf";
            HttpMethod method  = HttpMethod.Get;
            var        request = (HttpWebRequest)WebRequest.Create(this.baseUrl);

            this.requestMock.Stub(x => x.CreateRequest(this.baseUrl.ToString() + newUrl)).Return(request);

            // Act
            this.connector.CreateRequest(newUrl, method, payload);

            // Assert
            TestsHelper.AssertRequest(this.merchantId, this.secret, request, method);
            Assert.AreEqual(payload.Length, request.ContentLength);
        }
Beispiel #11
0
        public void TestTriggerSendOut()
        {
            // Arrange
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.orderUrl + this.path + "/" + this.captureId + "/trigger-send-out")).Return(this.httpWebRequest);

            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.NoContent, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, string.Empty)).Return(response);

            // Act
            this.capture.TriggerSendOut();

            // Assert
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #12
0
        public void TestExtendAuthorizationTime()
        {
            // Arrange
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.path + '/' + this.orderId + "/extend-authorization-time")).Return(this.httpWebRequest);
            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.NoContent, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, string.Empty)).Return(response);

            // Act
            this.order.ExtendAuthorizationTime();

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(0, this.httpWebRequest.ContentLength);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #13
0
        public void TestUpdate()
        {
            // Arrange
            CheckoutOrderData orderData1 = TestsHelper.GetCheckoutOrderData1();
            CheckoutOrderData orderData2 = TestsHelper.GetCheckoutOrderData2();

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl + this.path.TrimStart('/'))).Return(this.httpWebRequest);
            WebHeaderCollection headers = new WebHeaderCollection();

            headers[HttpResponseHeader.Location] = this.location;

            IResponse response = new Response(HttpStatusCode.Created, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, orderData1.ConvertToJson())).Return(response);

            this.httpWebRequest = (HttpWebRequest)WebRequest.Create(this.baseUrl);
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl + this.location)).Return(this.httpWebRequest);

            WebHeaderCollection headers2 = new WebHeaderCollection();

            headers2[HttpResponseHeader.ContentType] = "application/json";

            IResponse response2 = new Response(HttpStatusCode.OK, headers2, orderData2.ConvertToJson());

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, orderData2.ConvertToJson())).Return(response2);

            // Act
            this.checkoutOrder.Create(orderData1);
            CheckoutOrderData updatedCheckoutOrderData = this.checkoutOrder.Update(orderData2);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.location, this.checkoutOrder.Location.OriginalString);
            Assert.AreEqual(orderData2.ConvertToJson().Length, this.httpWebRequest.ContentLength);
            Assert.AreEqual(orderData2.PurchaseCountry, updatedCheckoutOrderData.PurchaseCountry);
            Assert.AreEqual(orderData2.PurchaseCurrency, updatedCheckoutOrderData.PurchaseCurrency);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #14
0
        public void TestCreate()
        {
            // Arrange
            CheckoutOrderData orderData = TestsHelper.GetCheckoutOrderData1();

            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl + this.path.TrimStart('/'))).Return(this.httpWebRequest);
            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;
            HttpStatusCode status   = HttpStatusCode.Created;
            IResponse      response = new Response(status, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, orderData.ConvertToJson())).Return(response);

            // Act
            this.checkoutOrder.Create(orderData);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.location, this.checkoutOrder.Location.OriginalString);
            Assert.AreEqual(orderData.ConvertToJson().Length, this.httpWebRequest.ContentLength);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }
Beispiel #15
0
        public void TestAddShippingInfo()
        {
            // Arrange
            this.requestMock.Expect(x => x.CreateRequest(this.baseUrl.ToString().TrimEnd('/') + this.orderUrl + this.path + "/" + this.captureId + "/shipping-info")).Return(this.httpWebRequest);

            var    shippingInfo         = TestsHelper.GetAddShippingInfo();
            string json                 = shippingInfo.ConvertToJson();
            WebHeaderCollection headers = new WebHeaderCollection();

            headers["Location"] = this.location;

            IResponse response = new Response(HttpStatusCode.NoContent, headers, string.Empty);

            this.requestMock.Expect(x => x.Send(this.httpWebRequest, json)).Return(response);

            // Act
            this.capture.AddShippingInfo(shippingInfo);

            // Assert
            this.requestMock.VerifyAllExpectations();
            Assert.AreEqual(this.httpWebRequest.ContentLength, json.Length);
            TestsHelper.AssertRequest(this.merchantId, this.secret, this.httpWebRequest, HttpMethod.Post);
        }