Skip to content

Commit

Permalink
chore(SPV-802): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Jun 3, 2024
1 parent 096c454 commit d09bf49
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package paymail
import (
"fmt"
"net/http"
"strconv"
"testing"

"github.com/jarcoal/httpmock"
Expand All @@ -19,15 +20,16 @@ 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}"

// Prepare the payload
payload := &PikePaymentOutputsPayload{
SenderPaymail: "[email protected]",
Amount: 1000, // Example amount in satoshis
Amount: amount, // Example amount in satoshis
}

// Get the outputs template from PIKE
Expand All @@ -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()
Expand All @@ -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: "[email protected]",
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) {
Expand All @@ -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/[email protected]",
httpmock.NewStringResponder(
statusCode,
`{
"url": "https://example.com/outputs"
"reference": "1262077636c27af74c01bb4535a7a90e",
"outputs": [
{
"script": "76a9fd88ac",
"satoshis": `+strconv.Itoa(int(amount))+`
}
]
}`,
),
)
Expand All @@ -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{
Expand Down

0 comments on commit d09bf49

Please sign in to comment.