| 1 |
package dialog |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/mitch000001/go-hbci/charset" |
| 11 |
"github.com/mitch000001/go-hbci/domain" |
| 12 |
"github.com/mitch000001/go-hbci/message" |
| 13 |
"github.com/mitch000001/go-hbci/segment" |
| 14 |
) |
| 15 |
|
| 16 |
func TestPinTanDialogSendMessage(t *testing.T) { |
| 17 |
transport := &mockHTTPSTransport{} |
| 18 |
|
| 19 |
d := newTestPinTanDialog(transport) |
| 20 |
account := domain.AccountInformation{ |
| 21 |
AccountConnection: domain.AccountConnection{AccountID: "100000000", CountryCode: 280, BankID: "10000000"}, |
| 22 |
UserID: "100000000", |
| 23 |
Currency: "EUR", |
| 24 |
Name1: "Muster", |
| 25 |
Name2: "Max", |
| 26 |
AllowedBusinessTransactions: []domain.BusinessTransaction{ |
| 27 |
domain.BusinessTransaction{ID: "HKSAL", NeededSignatures: 1}, |
| 28 |
}, |
| 29 |
} |
| 30 |
d.Accounts = []domain.AccountInformation{ |
| 31 |
account, |
| 32 |
} |
| 33 |
initResponse := encryptedTestMessage( |
| 34 |
"abcde", |
| 35 |
"HIRMG:2:2:1+0020::Auftrag entgegengenommen'", |
| 36 |
) |
| 37 |
balanceResponse := encryptedTestMessage( |
| 38 |
"abcde", |
| 39 |
"HIRMG:2:2:1+0020::Auftrag entgegengenommen'", |
| 40 |
"HISAL:3:5:1+100000000::280:10000000+Sichteinlagen+EUR+C:1000,15:EUR:20150812+C:20,:EUR:20150812+500,:EUR+1499,85:EUR'", |
| 41 |
) |
| 42 |
dialogEndResponseMessage := encryptedTestMessage("abcde", "HIRMG:2:2:1+0020::Der Auftrag wurde ausgeführt'") |
| 43 |
transport.SetResponseMessages([][]byte{ |
| 44 |
initResponse, |
| 45 |
balanceResponse, |
| 46 |
dialogEndResponseMessage, |
| 47 |
}) |
| 48 |
|
| 49 |
accountBalanceRequest := segment.NewAccountBalanceRequestV5(account.AccountConnection, false) |
| 50 |
|
| 51 |
res, err := d.SendMessage(message.NewHBCIMessage(d.hbciVersion, accountBalanceRequest)) |
| 52 |
|
| 53 |
if err != nil { |
| 54 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 55 |
t.Fail() |
| 56 |
} |
| 57 |
|
| 58 |
if res == nil { |
| 59 |
t.Logf("Expected result not to be nil\n") |
| 60 |
t.Fail() |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestPinTanDialogSyncClientSystemID(t *testing.T) { |
| 65 |
transport := &mockHTTPSTransport{} |
| 66 |
|
| 67 |
d := newTestPinTanDialog(transport) |
| 68 |
|
| 69 |
syncResponseMessage := encryptedTestMessage( |
| 70 |
"newDialogID", |
| 71 |
"HIRMG:2:2:1+0100::Dialog beendet'", |
| 72 |
"HIBPA:3:2:+12+280:10000000+Bank Name+3+1+201:210:220+0'", |
| 73 |
"HIPINS:4:1:+1+1+0+5:38:6:USERID:CUSTID:HKSAL:N:HKUEB:J'", |
| 74 |
"HISYN:5:3:8+newClientSystemID'", |
| 75 |
"HIUPA:6:2:7+12345+4+0'", |
| 76 |
"HIUPD:7:4:8+12345::280:1000000+54321+EUR+Muster+Max+++HKTAN:1+HKKAZ:1'", |
| 77 |
) |
| 78 |
|
| 79 |
dialogEndResponseMessage := encryptedTestMessage("newDialogID", "HIRMG:2:2:1+0020::Auftrag entgegengenommen'") |
| 80 |
|
| 81 |
transport.SetResponseMessages([][]byte{ |
| 82 |
syncResponseMessage, |
| 83 |
dialogEndResponseMessage, |
| 84 |
}) |
| 85 |
|
| 86 |
if len(d.Accounts) != 0 { |
| 87 |
t.Logf("Expected no accounts, got %d\n", len(d.Accounts)) |
| 88 |
t.Fail() |
| 89 |
} |
| 90 |
|
| 91 |
dialogID := d.dialogID |
| 92 |
if dialogID != initialDialogID { |
| 93 |
t.Logf("Expected dialogID to equal\n%q\n\tgot\n%q\n", initialDialogID, dialogID) |
| 94 |
t.Fail() |
| 95 |
} |
| 96 |
|
| 97 |
bankParamData := d.BankParameterData |
| 98 |
if bankParamData.Version != 0 { |
| 99 |
t.Logf("Expected BPD version to equal 0, was %d\n", bankParamData.Version) |
| 100 |
t.Fail() |
| 101 |
} |
| 102 |
|
| 103 |
pinTransactions := bankParamData.PinTanBusinessTransactions |
| 104 |
if pinTransactions != nil { |
| 105 |
t.Logf("Expected PinTanBusinessTransactions to be nil, was %+#v\n", pinTransactions) |
| 106 |
t.Fail() |
| 107 |
} |
| 108 |
|
| 109 |
userParamData := d.UserParameterData |
| 110 |
if userParamData.Version != 0 { |
| 111 |
t.Logf("Expected UPD version to equal 0, was %d\n", userParamData.Version) |
| 112 |
t.Fail() |
| 113 |
} |
| 114 |
|
| 115 |
res, err := d.SyncClientSystemID() |
| 116 |
|
| 117 |
if err != nil { |
| 118 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 119 |
t.Fail() |
| 120 |
} |
| 121 |
|
| 122 |
expected := "newClientSystemID" |
| 123 |
|
| 124 |
if res != expected { |
| 125 |
t.Logf("Expected response to equal\n%q\n\tgot\n%q\n", expected, res) |
| 126 |
t.Fail() |
| 127 |
} |
| 128 |
|
| 129 |
if d.ClientSystemID != expected { |
| 130 |
t.Logf("Expected ClientSystemID to equal %q, got %q\n", expected, d.ClientSystemID) |
| 131 |
t.Fail() |
| 132 |
} |
| 133 |
|
| 134 |
bankParamData = d.BankParameterData |
| 135 |
if bankParamData.Version != 12 { |
| 136 |
t.Logf("Expected BankParameterData version to equal 12, was %d\n", bankParamData.Version) |
| 137 |
t.Fail() |
| 138 |
} |
| 139 |
|
| 140 |
expectedPinTanTransactions := map[string]bool{ |
| 141 |
"HKSAL": false, |
| 142 |
"HKUEB": true, |
| 143 |
} |
| 144 |
|
| 145 |
pinTransactions = bankParamData.PinTanBusinessTransactions |
| 146 |
if !reflect.DeepEqual(expectedPinTanTransactions, pinTransactions) { |
| 147 |
t.Logf("Expected PinTanBusinessTransactions to equal\n%+#v\n\tgot\n%+#v\n", expectedPinTanTransactions, pinTransactions) |
| 148 |
t.Fail() |
| 149 |
} |
| 150 |
|
| 151 |
userParamData = d.UserParameterData |
| 152 |
if userParamData.Version != 4 { |
| 153 |
t.Logf("Expected UPD version to equal 4, was %d\n", userParamData.Version) |
| 154 |
t.Fail() |
| 155 |
} |
| 156 |
|
| 157 |
dialogID = d.dialogID |
| 158 |
if dialogID != "newDialogID" { |
| 159 |
t.Logf("Expected dialogID to equal\n%q\n\tgot\n%q\n", "newDialogID", dialogID) |
| 160 |
t.Fail() |
| 161 |
} |
| 162 |
|
| 163 |
accounts := d.Accounts |
| 164 |
|
| 165 |
if len(accounts) == 0 { |
| 166 |
t.Logf("Expected %d accounts, got 0\n", 1) |
| 167 |
t.Fail() |
| 168 |
} |
| 169 |
|
| 170 |
if len(accounts) > 0 { |
| 171 |
account := accounts[0] |
| 172 |
expected := domain.AccountInformation{ |
| 173 |
AccountConnection: domain.AccountConnection{AccountID: "12345", CountryCode: 280, BankID: "1000000"}, |
| 174 |
UserID: "54321", |
| 175 |
Currency: "EUR", |
| 176 |
Name1: "Muster", |
| 177 |
Name2: "Max", |
| 178 |
AllowedBusinessTransactions: []domain.BusinessTransaction{ |
| 179 |
domain.BusinessTransaction{ID: "HKTAN", NeededSignatures: 1}, |
| 180 |
domain.BusinessTransaction{ID: "HKKAZ", NeededSignatures: 1}, |
| 181 |
}, |
| 182 |
} |
| 183 |
if !reflect.DeepEqual(expected, account) { |
| 184 |
t.Logf("Expected account to eqaul\n%+#v\n\tgot\n%+#v\n", expected, account) |
| 185 |
t.Fail() |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// message errors |
| 190 |
syncResponseMessage = encryptedTestMessage("ABCDE", "HIRMG:2:2:1+9000::Nachricht enthält Fehler'") |
| 191 |
|
| 192 |
transport.SetResponseMessages([][]byte{ |
| 193 |
syncResponseMessage, |
| 194 |
[]byte(""), |
| 195 |
}) |
| 196 |
|
| 197 |
res, err = d.SyncClientSystemID() |
| 198 |
|
| 199 |
if err == nil { |
| 200 |
t.Logf("Expected error, got nil\n") |
| 201 |
t.Fail() |
| 202 |
} |
| 203 |
|
| 204 |
if err != nil { |
| 205 |
errMessage := err.Error() |
| 206 |
expectedMessage := "institute returned errors:\nMessageAcknowledgement for message 0 (), segment 1: Code: 9000, Position: none, Text: 'Nachricht enthält Fehler'" |
| 207 |
if expectedMessage != errMessage { |
| 208 |
t.Logf("Expected error to equal\n%q\n\tgot\n%q\n", expectedMessage, errMessage) |
| 209 |
t.Fail() |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func TestPinTanDialogInit(t *testing.T) { |
| 215 |
transport := &mockHTTPSTransport{} |
| 216 |
|
| 217 |
d := newTestPinTanDialog(transport) |
| 218 |
|
| 219 |
dialogID := d.dialogID |
| 220 |
if dialogID != initialDialogID { |
| 221 |
t.Logf("Expected dialogID to equal\n%q\n\tgot\n%q\n", initialDialogID, dialogID) |
| 222 |
t.Fail() |
| 223 |
} |
| 224 |
|
| 225 |
initResponse := encryptedTestMessage( |
| 226 |
"newDialogID", |
| 227 |
"HIRMG:2:2:1+0020::Auftrag entgegengenommen'", |
| 228 |
"HIKIM:10:2+ec-Karte+Ihre neue ec-Karte liegt zur Abholung bereit.'", |
| 229 |
) |
| 230 |
transport.SetResponseMessage(initResponse) |
| 231 |
|
| 232 |
err := d.init() |
| 233 |
|
| 234 |
if err != nil { |
| 235 |
t.Logf("Expected no error, got %T:%v\n", err, err) |
| 236 |
t.Fail() |
| 237 |
} |
| 238 |
|
| 239 |
dialogID = d.dialogID |
| 240 |
if dialogID != "newDialogID" { |
| 241 |
t.Logf("Expected dialogID to equal\n%q\n\tgot\n%q\n", "newDialogID", dialogID) |
| 242 |
t.Fail() |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func newTestPinTanDialog(transport *mockHTTPSTransport) *PinTanDialog { |
| 247 |
cfg := Config{ |
| 248 |
BankID: domain.BankID{CountryCode: 280, ID: "10000000"}, |
| 249 |
HBCIURL: "http://localhost", |
| 250 |
UserID: "12345", |
| 251 |
HBCIVersion: segment.HBCI220, |
| 252 |
} |
| 253 |
|
| 254 |
d := NewPinTanDialog(cfg) |
| 255 |
d.SetPin("abcde") |
| 256 |
d.SetClientSystemID("xyz") |
| 257 |
d.transport = transport |
| 258 |
return d |
| 259 |
} |
| 260 |
|
| 261 |
func encryptedTestMessage(dialogID string, encryptedData ...string) []byte { |
| 262 |
encryptionHeader := "HNVSK:998:2:+998+1+1::0+1:20150713:173634+2:2:13:@8@\x00\x00\x00\x00\x00\x00\x00\x00:5:1:+280:10000000:12345:V:0:0+0+'" |
| 263 |
encryptionData := fmt.Sprintf("HNVSD:999:1:+@%d@%s'", len(charset.ToISO8859_1(strings.Join(encryptedData, ""))), charset.ToISO8859_1(strings.Join(encryptedData, ""))) |
| 264 |
messageEnd := fmt.Sprintf("HNHBS:%d:1:+1'", len(encryptedData)+1) |
| 265 |
messageHeader := fmt.Sprintf("HNHBK:1:3+%012d+220+%s+1+'", 31+len(dialogID)+len(encryptionHeader)+len(encryptionData)+len(messageEnd), dialogID) |
| 266 |
encryptedMessage := [][]byte{ |
| 267 |
charset.ToISO8859_1(messageHeader), |
| 268 |
charset.ToISO8859_1(encryptionHeader), |
| 269 |
[]byte(encryptionData), |
| 270 |
charset.ToISO8859_1(messageEnd), |
| 271 |
} |
| 272 |
return bytes.Join(encryptedMessage, []byte("")) |
| 273 |
} |