This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbanken_test.go
177 lines (157 loc) · 6.48 KB
/
sbanken_test.go
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
package sbanken
import (
"context"
"testing"
"github.com/engvik/sbanken-go/internal/transport"
)
const baseURL = "https://publicapi.sbanken.no/apibeta/api/"
var (
testListAccountsEndpoint = baseURL + "v2/Accounts"
testReadAccountEndpoint = baseURL + "v2/Accounts/test-account"
testListCardsEndpoint = baseURL + "v2/Cards"
testListEfakturasEndpoint = baseURL + "v2/Efaktura"
testListEfakturasQueryEndpoint = baseURL + "v2/Efaktura?index=1"
testPayEfakturaEndpoint = baseURL + "v2/Efaktura"
testListNewEfakturasEndpoint = baseURL + "v2/Efaktura/new"
testListNewEfakturasQueryEndpoint = baseURL + "v2/Efaktura/new?index=1"
testReadEfakturaEndpoint = baseURL + "v2/Efaktura/test-efaktura"
testListPaymentsEndpoint = baseURL + "v2/Payments/test-account"
testListPaymentsQueryEndpoint = baseURL + "v2/Payments/test-account?index=1"
testReadPaymentsEndpoint = baseURL + "v2/Payments/test-account/test-payment"
testListStandingOrdersEndpoint = baseURL + "v2/StandingOrders/test-account"
testListTransactionsEndpoint = baseURL + "v2/Transactions/test-account"
testListTransactionsQueryEndpoint = baseURL + "v2/Transactions/test-account?index=1"
testListArchivedTransactionsEndpoint = baseURL + "v2/Transactions/archive/test-account"
testListArchivedTransactionsQueryEndpoint = baseURL + "v2/Transactions/archive/test-account?index=1"
testTransferEndpoint = baseURL + "v2/Transfers"
testCustomersEndpoint = baseURL + "v2/Customers"
testListArchiveMessagesEndpoint = baseURL + "v2/Mailbox/archive"
testListArchiveMessagesQueryEndpoint = baseURL + "v2/Mailbox/archive?index=1"
testListInboxMessagesEndpoint = baseURL + "v2/Mailbox/inbox"
testListInboxMessagesQueryEndpoint = baseURL + "v2/Mailbox/inbox?index=1"
testReadAndDeleteArchiveMessageEndpoint = baseURL + "v2/Mailbox/archive/1337"
testReadAndDeleteInboxMessageEndpoint = baseURL + "v2/Mailbox/inbox/1337"
testMoveArchiveMessageEndpoint = baseURL + "v2/Mailbox/archive/1337/move"
testMoveInboxMessageEndpoint = baseURL + "v2/Mailbox/inbox/1337/move"
testUpdateArchiveMessageReadStatusEndpoint = baseURL + "v2/Mailbox/archive/1337/readstatus"
testUpdateInboxMessageReadStatusEndpoint = baseURL + "v2/Mailbox/inbox/1337/readstatus"
testGetArchiveMessageCountEndpoint = baseURL + "v2/Mailbox/archive/count"
testGetInboxMessageCountEndpoint = baseURL + "v2/Mailbox/inbox/count"
)
type testBehavior string
type testTransportClient struct{}
func (c testTransportClient) Authorize(ctx context.Context) error {
return nil
}
func (c testTransportClient) Request(ctx context.Context, r *transport.HTTPRequest) ([]byte, int, error) {
switch r.URL {
case testListAccountsEndpoint:
return testListAccountsEndpointResponse(getTestBehavior(ctx))
case testReadAccountEndpoint:
return testReadAccountEndpointResponse(getTestBehavior(ctx))
case testListCardsEndpoint:
return testListCardsEndpointResponse(getTestBehavior(ctx))
case testListEfakturasEndpoint:
fallthrough
case testListEfakturasQueryEndpoint:
fallthrough
case testPayEfakturaEndpoint:
fallthrough
case testListNewEfakturasEndpoint:
fallthrough
case testListNewEfakturasQueryEndpoint:
return testListPayEfakturasEndpointResponses(getTestBehavior(ctx))
case testReadEfakturaEndpoint:
return testReadEfakturaEndpointResponse(getTestBehavior(ctx))
case testListPaymentsEndpoint:
fallthrough
case testListPaymentsQueryEndpoint:
return testListPaymentEndpointResponses(getTestBehavior(ctx))
case testReadPaymentsEndpoint:
return testReadPaymentEndpointResponse(getTestBehavior(ctx))
case testListStandingOrdersEndpoint:
return testListStandingOrdersEndpointResponse(getTestBehavior(ctx))
case testListTransactionsEndpoint:
fallthrough
case testListTransactionsQueryEndpoint:
fallthrough
case testListArchivedTransactionsEndpoint:
fallthrough
case testListArchivedTransactionsQueryEndpoint:
return testListTransactionsEndpointResponse(getTestBehavior(ctx))
case testTransferEndpoint:
return testTransferEndpointResponse(getTestBehavior(ctx))
case testCustomersEndpoint:
return testCustomersEndpointResponse(getTestBehavior(ctx))
case testListArchiveMessagesEndpoint:
fallthrough
case testListArchiveMessagesQueryEndpoint:
fallthrough
case testListInboxMessagesEndpoint:
fallthrough
case testListInboxMessagesQueryEndpoint:
return testListMessagesEndpointResponse(getTestBehavior(ctx))
case testReadAndDeleteArchiveMessageEndpoint:
fallthrough
case testReadAndDeleteInboxMessageEndpoint:
return testReadAndDeleteMessageEndpointResponse(getTestBehavior(ctx))
case testMoveArchiveMessageEndpoint:
fallthrough
case testMoveInboxMessageEndpoint:
return testMoveMessageEndpointResponse(getTestBehavior(ctx))
case testUpdateArchiveMessageReadStatusEndpoint:
fallthrough
case testUpdateInboxMessageReadStatusEndpoint:
return testUpdateMessageReadStatusEndpointResponse(getTestBehavior(ctx))
case testGetArchiveMessageCountEndpoint:
fallthrough
case testGetInboxMessageCountEndpoint:
return testGetMessageCountEndpointResponse(getTestBehavior(ctx))
default:
return nil, 0, nil
}
}
func getTestBehavior(ctx context.Context) string {
if v := ctx.Value(testBehavior("test-behavior")); v != nil {
return v.(string)
}
return ""
}
var testHTTPResponseError = transport.HTTPResponse{
IsError: true,
ErrorCode: 100,
ErrorMessage: "an error occurred",
ErrorType: "Error",
}
func newTestClient(ctx context.Context, t *testing.T) (*Client, error) {
t.Helper()
cfg := &Config{
ClientID: "some-client-id",
ClientSecret: "some-client-secret",
skipAuth: true,
}
c, err := NewClient(ctx, cfg, nil)
if err != nil {
return nil, err
}
c.transport = testTransportClient{}
return c, err
}
func TestNewClient(t *testing.T) {
ctx := context.Background()
c, err := newTestClient(ctx, t)
if err != nil {
t.Fatalf("error setting up test: %v", err)
}
t.Run("should have bankBaseURL set", func(t *testing.T) {
exp := "https://publicapi.sbanken.no/apibeta/api"
if c.bankBaseURL != exp {
t.Errorf("unexpected baseURL: got %s, exp %s", c.bankBaseURL, exp)
}
})
t.Run("should have transport set", func(t *testing.T) {
if c.transport == nil {
t.Errorf("expected transport to be set")
}
})
}