Debian Codegraph @

client/client_test.go

raw
1 package client
2
3 import (
4 "net/http"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/mitch000001/go-hbci/domain"
10 https "github.com/mitch000001/go-hbci/transport/https"
11 )
12
13 func TestClientBalances(t *testing.T) {
14 transport := &https.MockHTTPTransport{}
15 defer setMockHTTPTransport(transport)()
16
17 c := newTestClient()
18
19 account := domain.AccountInformation{
20 AccountConnection: domain.AccountConnection{AccountID: "100000000", CountryCode: 280, BankID: "10000000"},
21 UserID: "100000000",
22 Currency: "EUR",
23 Name1: "Muster",
24 Name2: "Max",
25 AllowedBusinessTransactions: []domain.BusinessTransaction{
26 {ID: "HKSAL", NeededSignatures: 1},
27 },
28 }
29
30 c.pinTanDialog.Accounts = []domain.AccountInformation{
31 account,
32 }
33
34 syncResponse := encryptedTestMessage(
35 "abcde",
36 "HIRMG:2:2:1+0020::Auftrag entgegengenommen'",
37 "HISYN:193:4:5+LRZYhZNbV2IBAAAd0?+VNqlkXrAQA'",
38 "HIBPA:2:2:+12+280:10000000+Bank Name+3+1+201:210:220+0'",
39 "HISALS:3:5:4+3+1'",
40 )
41 initResponse := encryptedTestMessage(
42 "abcde",
43 "HIRMG:2:2:1+0020::Auftrag entgegengenommen'",
44 "HIKIM:3:2+ec-Karte+Ihre neue ec-Karte liegt zur Abholung bereit.'",
45 )
46 balanceResponse := encryptedTestMessage(
47 "abcde",
48 "HIRMG:2:2:1+0020::Auftrag entgegengenommen'",
49 "HISAL:3:5:1+100000000::280:10000000+Sichteinlagen+EUR+C:1000,15:EUR:20150812+C:20,:EUR:20150812+500,:EUR+1499,85:EUR'",
50 )
51 dialogEndResponseMessage := encryptedTestMessage("abcde", "HIRMG:2:2:1+0020::Der Auftrag wurde ausgeführt'")
52
53 transport.SetResponsePayloads([][]byte{
54 syncResponse,
55 dialogEndResponseMessage,
56 initResponse,
57 balanceResponse,
58 dialogEndResponseMessage,
59 })
60
61 balances, err := c.AccountBalances(account.AccountConnection, true)
62 if err != nil {
63 t.Logf("Expected no error, got %T:%v\n", err, err)
64 t.Fail()
65 }
66
67 date, _ := time.Parse("20060102", "20150812")
68
69 expectedBalance := domain.AccountBalance{
70 Account: domain.AccountConnection{AccountID: "100000000", CountryCode: 280, BankID: "10000000"},
71 ProductName: "Sichteinlagen",
72 Currency: "EUR",
73 BookedBalance: domain.Balance{
74 Amount: domain.Amount{Amount: 1000.15, Currency: "EUR"},
75 TransmissionDate: date,
76 },
77 EarmarkedBalance: &domain.Balance{
78 Amount: domain.Amount{Amount: 20, Currency: "EUR"},
79 TransmissionDate: date,
80 },
81 CreditLimit: &domain.Amount{Amount: 500, Currency: "EUR"},
82 AvailableAmount: &domain.Amount{Amount: 1499.85, Currency: "EUR"},
83 }
84
85 if len(balances) != 1 {
86 t.Logf("Expected balances length to equal 1, was %d\n", len(balances))
87 t.Fail()
88 } else {
89 if !reflect.DeepEqual(balances[0], expectedBalance) {
90 t.Logf("Expected balance to equal\n%#v\n\tgot\n%#v\n", expectedBalance, balances[0])
91 t.Fail()
92 }
93 }
94 }
95
96 func newTestClient() *Client {
97 config := Config{
98 URL: "https://localhost",
99 AccountID: "12345",
100 BankID: "10000000",
101 PIN: "abcde",
102 HBCIVersion: domain.HBCIVersion220,
103 }
104 c, err := New(config)
105 if err != nil {
106 panic(err)
107 }
108 c.pinTanDialog.SetClientSystemID("xyz")
109 return c
110 }
111
112 func setMockHTTPTransport(transport http.RoundTripper) func() {
113 originHTTPTransport := http.DefaultTransport
114 http.DefaultTransport = transport
115 return func() {
116 http.DefaultTransport = originHTTPTransport
117 }
118 }