| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strconv" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/mitch000001/go-hbci/client" |
| 12 |
"github.com/mitch000001/go-hbci/domain" |
| 13 |
"github.com/mitch000001/go-hbci/iban" |
| 14 |
) |
| 15 |
|
| 16 |
var testAccount domain.AccountConnection |
| 17 |
var sepaTestAccount domain.InternationalAccountConnection |
| 18 |
|
| 19 |
func TestClientAccountTransactions(t *testing.T) { |
| 20 |
skipWhenE2EDisabled(t) |
| 21 |
c := newClient() |
| 22 |
|
| 23 |
timeframe := domain.Timeframe{ |
| 24 |
StartDate: domain.NewShortDate(time.Now().AddDate(0, 0, -90)), |
| 25 |
} |
| 26 |
transactions, err := c.AccountTransactions(testAccount, timeframe, false, "") |
| 27 |
|
| 28 |
if err != nil { |
| 29 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 30 |
t.Fail() |
| 31 |
} |
| 32 |
|
| 33 |
if transactions == nil { |
| 34 |
t.Logf("Expected transactions not to be nil\n") |
| 35 |
t.Fail() |
| 36 |
} |
| 37 |
|
| 38 |
for _, tr := range transactions { |
| 39 |
fmt.Printf("Transaction: %s\n", tr) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestClientSepaAccountTransactions(t *testing.T) { |
| 44 |
skipWhenE2EDisabled(t) |
| 45 |
c := newClient() |
| 46 |
|
| 47 |
timeframe := domain.Timeframe{ |
| 48 |
StartDate: domain.NewShortDate(time.Now().AddDate(0, 0, -90)), |
| 49 |
} |
| 50 |
transactions, err := c.SepaAccountTransactions(sepaTestAccount, timeframe, false, "") |
| 51 |
|
| 52 |
if err != nil { |
| 53 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 54 |
t.Fail() |
| 55 |
} |
| 56 |
|
| 57 |
if transactions == nil { |
| 58 |
t.Logf("Expected transactions not to be nil\n") |
| 59 |
t.Fail() |
| 60 |
} |
| 61 |
|
| 62 |
for _, tr := range transactions { |
| 63 |
fmt.Printf("Transaction: %s\n", tr) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestClientAccountInformation(t *testing.T) { |
| 68 |
skipWhenE2EDisabled(t) |
| 69 |
c := newClient() |
| 70 |
|
| 71 |
err := c.AccountInformation(testAccount, true) |
| 72 |
|
| 73 |
if err != nil { |
| 74 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 75 |
// t.Fail() |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestClientAccountBalances(t *testing.T) { |
| 80 |
skipWhenE2EDisabled(t) |
| 81 |
c := newClient() |
| 82 |
|
| 83 |
balances, err := c.AccountBalances(testAccount, true) |
| 84 |
|
| 85 |
if err != nil { |
| 86 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 87 |
t.Fail() |
| 88 |
} |
| 89 |
|
| 90 |
if balances == nil { |
| 91 |
t.Logf("Expected balances not to be nil\n") |
| 92 |
t.Fail() |
| 93 |
} |
| 94 |
|
| 95 |
for _, balance := range balances { |
| 96 |
t.Logf("Balance: %s\n", balance) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestClientAccounts(t *testing.T) { |
| 101 |
skipWhenE2EDisabled(t) |
| 102 |
c := newClient() |
| 103 |
|
| 104 |
accounts, err := c.Accounts() |
| 105 |
|
| 106 |
if err != nil { |
| 107 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 108 |
t.Fail() |
| 109 |
} |
| 110 |
|
| 111 |
if accounts == nil { |
| 112 |
t.Logf("Expected accounts not to be nil\n") |
| 113 |
t.Fail() |
| 114 |
} |
| 115 |
|
| 116 |
for _, account := range accounts { |
| 117 |
t.Logf("Account: %s\tProduct name: %s\tCurrency: %s\n", account.AccountConnection.AccountID, account.ProductID, account.Currency) |
| 118 |
t.Logf("Allowed transactions:\n") |
| 119 |
for _, allowedTransaction := range account.AllowedBusinessTransactions { |
| 120 |
t.Logf("ID: %s\tNeeded signatures: %d\n", allowedTransaction.ID, allowedTransaction.NeededSignatures) |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestClientStatus(t *testing.T) { |
| 126 |
skipWhenE2EDisabled(t) |
| 127 |
c := newClient() |
| 128 |
|
| 129 |
statuus, err := c.Status(time.Now().Add(-48*time.Hour), time.Now(), 10, "") |
| 130 |
|
| 131 |
if err != nil { |
| 132 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 133 |
t.Fail() |
| 134 |
} |
| 135 |
|
| 136 |
for _, status := range statuus { |
| 137 |
t.Logf("Status: %s\n", status) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestAnonymousClientCommunicationAccess(t *testing.T) { |
| 142 |
skipWhenE2EDisabled(t) |
| 143 |
a := &client.AnonymousClient{ |
| 144 |
Client: newClient(), |
| 145 |
} |
| 146 |
|
| 147 |
from := domain.BankID{280, "78050000"} |
| 148 |
to := domain.BankID{280, "78050000"} |
| 149 |
|
| 150 |
res, err := a.CommunicationAccess(from, to, 10) |
| 151 |
|
| 152 |
if err != nil { |
| 153 |
t.Logf("Expected error to be nil, got %T:%v\n", err, err) |
| 154 |
// t.Fail() |
| 155 |
} |
| 156 |
|
| 157 |
if res != nil { |
| 158 |
t.Logf("Response: %s\n", res) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func newClient() *client.Client { |
| 163 |
configFile, err := os.Open("../.fints300.json") |
| 164 |
if err != nil && !os.IsNotExist(err) { |
| 165 |
panic(err) |
| 166 |
} |
| 167 |
var config client.Config |
| 168 |
if configFile != nil { |
| 169 |
jsonDecoder := json.NewDecoder(configFile) |
| 170 |
err = jsonDecoder.Decode(&config) |
| 171 |
if err != nil { |
| 172 |
panic(err) |
| 173 |
} |
| 174 |
} else { |
| 175 |
config = client.Config{ |
| 176 |
URL: os.Getenv("GOHBCI_URL"), |
| 177 |
AccountID: os.Getenv("GOHBCI_USERID"), |
| 178 |
BankID: os.Getenv("GOHBCI_BLZ"), |
| 179 |
PIN: os.Getenv("GOHBCI_PIN"), |
| 180 |
} |
| 181 |
} |
| 182 |
testAccount = domain.AccountConnection{AccountID: config.AccountID, CountryCode: 280, BankID: config.BankID} |
| 183 |
i, err := iban.NewGerman(config.BankID, config.AccountID) |
| 184 |
if err != nil { |
| 185 |
panic(err) |
| 186 |
} |
| 187 |
sepaTestAccount = domain.InternationalAccountConnection{ |
| 188 |
IBAN: string(i), |
| 189 |
AccountID: config.AccountID, |
| 190 |
BankID: domain.BankID{CountryCode: 280, ID: config.BankID}, |
| 191 |
} |
| 192 |
c, err := client.New(config) |
| 193 |
if err != nil { |
| 194 |
panic(err) |
| 195 |
} |
| 196 |
return c |
| 197 |
} |
| 198 |
|
| 199 |
func skipWhenE2EDisabled(t *testing.T) { |
| 200 |
if testE2E() { |
| 201 |
return |
| 202 |
} |
| 203 |
t.Skip("Enable client e2e tests by setting env var `GOHBCI_TEST_E2E_CLIENT` to `true`") |
| 204 |
} |
| 205 |
|
| 206 |
func testE2E() bool { |
| 207 |
val, ok := os.LookupEnv("GOHBCI_TEST_E2E_CLIENT") |
| 208 |
if !ok { |
| 209 |
return false |
| 210 |
} |
| 211 |
testE2E, err := strconv.ParseBool(val) |
| 212 |
if err != nil { |
| 213 |
return false |
| 214 |
} |
| 215 |
return testE2E |
| 216 |
} |