From d09bf49df1c61d1e033d3f2d77811946b782cfb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lewandowski?= Date: Mon, 3 Jun 2024 15:03:48 +0200 Subject: [PATCH] chore(SPV-802): fix tests --- pike_test.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pike_test.go b/pike_test.go index a7c022d..1842dc6 100644 --- a/pike_test.go +++ b/pike_test.go @@ -3,6 +3,7 @@ package paymail import ( "fmt" "net/http" + "strconv" "testing" "github.com/jarcoal/httpmock" @@ -19,7 +20,8 @@ func ExampleClient_GetOutputsTemplate() { // Setup a mock HTTP client client := newTestClient(nil) - mockPIKEOutputs(http.StatusOK) + var amount uint64 = 1000 + mockPIKEOutputs(http.StatusOK, amount) // Assume we have a PIKE Outputs URL pikeOutputsURL := "https://test.com/v1/bsvalias/pike/outputs/{alias}@{domain.tld}" @@ -27,7 +29,7 @@ func ExampleClient_GetOutputsTemplate() { // Prepare the payload payload := &PikePaymentOutputsPayload{ SenderPaymail: "joedoe@example.com", - Amount: 1000, // Example amount in satoshis + Amount: amount, // Example amount in satoshis } // Get the outputs template from PIKE @@ -36,8 +38,8 @@ func ExampleClient_GetOutputsTemplate() { fmt.Printf("error getting outputs template: %s", err.Error()) return } - fmt.Printf("found outputs template: %+v", outputs) - // Output: found outputs template: &{URL:https://example.com/outputs} + fmt.Printf("found outputs template, reference: %+v", outputs.Reference) + // Output: found outputs template, reference: 1262077636c27af74c01bb4535a7a90e } // TestClient_GetOutputsTemplate will test the method GetOutputsTemplate() @@ -48,17 +50,19 @@ func TestClient_GetOutputsTemplate(t *testing.T) { client := newTestClient(t) t.Run("successful PIKE outputs response", func(t *testing.T) { - mockPIKEOutputs(http.StatusOK) + var amount uint64 = 1000 + mockPIKEOutputs(http.StatusOK, amount) outputsURL := "https://" + testDomain + "/v1/bsvalias/pike/outputs/{alias}@{domain.tld}" payload := &PikePaymentOutputsPayload{ SenderPaymail: "joedoe@example.com", - Amount: 1000, + Amount: amount, } response, err := client.GetOutputsTemplate(outputsURL, "alias", "domain.tld", payload) require.NoError(t, err) require.NotNil(t, response) - require.Equal(t, "https://example.com/outputs", response.URL) + require.NotNil(t, response.Reference) + require.Equal(t, payload.Amount, response.Outputs[0].Satoshis) }) t.Run("PIKE outputs response error", func(t *testing.T) { @@ -79,12 +83,18 @@ func TestClient_GetOutputsTemplate(t *testing.T) { } // mockPIKEOutputs is used for mocking the PIKE outputs response -func mockPIKEOutputs(statusCode int) { +func mockPIKEOutputs(statusCode int, amount uint64) { httpmock.RegisterResponder(http.MethodPost, "https://"+testDomain+"/v1/bsvalias/pike/outputs/alias@domain.tld", httpmock.NewStringResponder( statusCode, `{ - "url": "https://example.com/outputs" + "reference": "1262077636c27af74c01bb4535a7a90e", + "outputs": [ + { + "script": "76a9fd88ac", + "satoshis": `+strconv.Itoa(int(amount))+` + } + ] }`, ), ) @@ -96,7 +106,7 @@ func BenchmarkClient_GetOutputsTemplate(b *testing.B) { defer httpmock.DeactivateAndReset() client := newTestClient(nil) - mockPIKEOutputs(http.StatusOK) + mockPIKEOutputs(http.StatusOK, 1000) outputsURL := "https://example.com/v1/bsvalias/pike/outputs/{alias}@{domain.tld}" payload := &PikePaymentOutputsPayload{