File: billing_test.go

package info (click to toggle)
golang-github-dnsimple-dnsimple-go 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: makefile: 3
file content (190 lines) | stat: -rw-r--r-- 5,325 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package dnsimple

import (
	"context"
	"encoding/json"
	"io"
	"net/http"
	"net/url"
	"testing"

	"github.com/shopspring/decimal"
	"github.com/stretchr/testify/assert"
)

func toDecimal(t *testing.T, s string) decimal.Decimal {
	d, err := decimal.NewFromString(s)
	if err != nil {
		assert.Nilf(t, err, "toDecimal() error = %v", err)
	}

	return d
}

func TestBillingService_ListCharges_Success(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/listCharges/success.http")

		testMethod(t, r, "GET")
		testHeaders(t, r)
		testQuery(t, r, url.Values{})

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	response, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{})

	assert.NoError(t, err)
	assert.Equal(t, response.Pagination, &Pagination{CurrentPage: 1, PerPage: 30, TotalPages: 1, TotalEntries: 3})
	assert.Equal(t, response.Data, []Charge{
		{
			InvoicedAt:    "2023-08-17T05:53:36Z",
			TotalAmount:   toDecimal(t, "14.50"),
			BalanceAmount: toDecimal(t, "0.00"),
			Reference:     "1-2",
			State:         "collected",
			Items: []ChargeItem{
				{
					Description:      "Register bubble-registered.com",
					Amount:           toDecimal(t, "14.50"),
					ProductId:        1,
					ProductType:      "domain-registration",
					ProductReference: "bubble-registered.com",
				},
			},
		},
		{
			InvoicedAt:    "2023-08-17T05:57:53Z",
			TotalAmount:   toDecimal(t, "14.50"),
			BalanceAmount: toDecimal(t, "0.00"),
			Reference:     "2-2",
			State:         "refunded",
			Items: []ChargeItem{
				{
					Description:      "Register example.com",
					Amount:           toDecimal(t, "14.50"),
					ProductId:        2,
					ProductType:      "domain-registration",
					ProductReference: "example.com",
				},
			},
		},
		{
			InvoicedAt:    "2023-10-24T07:49:05Z",
			TotalAmount:   toDecimal(t, "1099999.99"),
			BalanceAmount: toDecimal(t, "0.00"),
			Reference:     "4-2",
			State:         "collected",
			Items: []ChargeItem{
				{
					Description:      "Test Line Item 1",
					Amount:           toDecimal(t, "99999.99"),
					ProductId:        0,
					ProductType:      "manual",
					ProductReference: "",
				},
				{
					Description:      "Test Line Item 2",
					Amount:           toDecimal(t, "1000000.00"),
					ProductId:        0,
					ProductType:      "manual",
					ProductReference: "",
				},
			},
		},
	})
}

func TestBillingService_ListCharges_Fail400BadFilter(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/listCharges/fail-400-bad-filter.http")

		testMethod(t, r, "GET")
		testHeaders(t, r)
		testQuery(t, r, url.Values{})

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	_, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{})

	assert.Equal(t, err.(*ErrorResponse).Message, "Invalid date format must be ISO8601 (YYYY-MM-DD)")
}

func TestBillingService_ListCharges_Fail403(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/listCharges/fail-403.http")

		testMethod(t, r, "GET")
		testHeaders(t, r)
		testQuery(t, r, url.Values{})

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	_, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{})

	assert.Equal(t, err.(*ErrorResponse).Message, "Permission Denied. Required Scope: billing:*:read")
}

func TestUnmarshalCharge(t *testing.T) {
	tests := []struct {
		name    string
		jsonStr string
		want    Charge
		wantErr bool
	}{
		{
			name:    "valid json",
			jsonStr: `{"total_amount": "123.45", "balance_amount": "67.89"}`,
			want: Charge{
				TotalAmount:   decimal.NewFromFloat(123.45),
				BalanceAmount: decimal.NewFromFloat(67.89),
			},
			wantErr: false,
		},
		{
			name:    "zero values",
			jsonStr: `{"total_amount": "0.00", "balance_amount": "0.00"}`,
			want: Charge{
				TotalAmount:   decimal.NewFromFloat(0.00),
				BalanceAmount: decimal.NewFromFloat(0.00),
			},
			wantErr: false,
		},
		{
			name:    "invalid amount value",
			jsonStr: `{"total_amount": "123.45", "balance_amount": "abc"}`,
			want: Charge{
				TotalAmount:   decimal.NewFromFloat(123.45),
				BalanceAmount: decimal.Decimal{},
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var got Charge
			err := json.Unmarshal([]byte(tt.jsonStr), &got)
			if (err != nil) != tt.wantErr {
				t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			assert.Truef(t, got.TotalAmount.Equals(tt.want.TotalAmount), "TotalAmount: got %v, want %v\nTesting: %s\n%s", got.TotalAmount, tt.want.TotalAmount, tt.name, tt.jsonStr)
			assert.Truef(t, got.BalanceAmount.Equals(tt.want.BalanceAmount), "BalanceAmount: got %v, want %v\nTesting: %s\n%s", got.BalanceAmount, tt.want.BalanceAmount, tt.name, tt.jsonStr)
		})
	}
}