-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfakeConnection.go
145 lines (125 loc) · 4.74 KB
/
fakeConnection.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
package fiGo
import (
"encoding/json"
"errors"
"log"
"math/rand"
)
var (
// ErrCantFindUser means "Cant find user in fake-figo"
ErrCantFindUser = errors.New("cant find user")
)
// FakeConnection is a fakes the figoAPI
type FakeConnection struct {
Users []map[string]interface{}
}
// NewFakeConnection creates a new "fak-Connection" only in memory
func NewFakeConnection() *FakeConnection {
return &FakeConnection{Users: make([]map[string]interface{}, 0)}
}
// CreateUser "store" a user in this fake-Connection
func (fakeConnection *FakeConnection) CreateUser(name string, email string, password string) ([]byte, error) {
recoveryPassword := randStringRunes(10)
// "store" in fakeConnection
fakeConnection.Users = append(fakeConnection.Users, map[string]interface{}{
"name": name,
"email": email,
"password": password,
"recovery_password": recoveryPassword,
"access_token": randStringRunes(10),
"expires_in": 3600,
"refresh_token": randStringRunes(10),
"scope": "accounts=ro balance=ro transactions=ro offline",
"token_type": "Bearer"})
responseMap := map[string]string{"recovery_password": recoveryPassword}
return json.Marshal(responseMap)
}
// SetHost is not needed
func (fakeConnection *FakeConnection) SetHost(host string) {
// do nothing
}
// CredentialLogin returns a token
// -> Notice: first add a user via CreateUser!
func (fakeConnection *FakeConnection) CredentialLogin(username string, password string) ([]byte, error) {
// search user
for _, user := range fakeConnection.Users {
log.Println(user)
if user["email"] == username && user["password"] == password {
response := map[string]interface{}{
"access_token": user["access_token"],
"expires_in": user["expires_in"],
"refresh_token": user["refresh_token"],
"scope": user["scope"],
"token_type": user["token_type"],
}
return json.Marshal(response)
}
}
return nil, ErrCantFindUser
}
// SetupNewBankAccount sets up a new bank account for an existing account.
// -> Notice: you need an accessToken from CredentialLogin
func (fakeConnection *FakeConnection) SetupNewBankAccount(accessToken string, bankCode string, country string, credentials []string) ([]byte, error) {
for _, user := range fakeConnection.Users {
if user["access_token"] == accessToken {
user["banks"] = map[string]interface{}{
"bankCode": bankCode,
"country": country,
"credentials": credentials,
}
response := map[string]string{"task_token": randStringRunes(10)}
return json.Marshal(response)
}
}
return nil, ErrCantFindUser
}
// DeleteUser deletes a user from fakeConnection
// Notice: you need an accessToken from CredentialLogin
func (fakeConnection *FakeConnection) DeleteUser(accessToken string) ([]byte, error) {
for index, user := range fakeConnection.Users {
if user["access_token"] == accessToken {
fakeConnection.Users = append(fakeConnection.Users[:index], fakeConnection.Users[index+1:]...)
}
}
return nil, ErrCantFindUser
}
// RequestForTask should generate new transactions TODO
func (fakeConnection *FakeConnection) RequestForTask(accessToken string, taskToken string) ([]byte, error) {
return nil, nil
}
// RequestForTaskWithPinChallenge should generate new transactions TODO
func (fakeConnection *FakeConnection) RequestForTaskWithPinChallenge(accessToken string, taskToken string, pin string, savePin bool) ([]byte, error) {
return nil, nil
}
// RetrieveTransactionsOfAllAccounts TODO
func (fakeConnection *FakeConnection) RetrieveTransactionsOfAllAccounts(accessToken string, options ...TransactionOption) ([]byte, error) {
return nil, nil
}
// RetrieveAllBankAccounts TODO
func (fakeConnection *FakeConnection) RetrieveAllBankAccounts(accessToken string) ([]byte, error) {
return nil, nil
}
// RemoveBankAccount removes a bank account TODO
func (fakeConnection *FakeConnection) RemoveBankAccount(accessToken string, bankAccountID string) ([]byte, error) {
return nil, nil
}
// RetrieveSpecificTransaction TODO
func (fakeConnection *FakeConnection) RetrieveSpecificTransaction(accessToken string, transactionID string) ([]byte, error) {
return nil, nil
}
// ReadIndividualCatalogEntry TODO
func (fakeConnection *FakeConnection) ReadIndividualCatalogEntry(accessToken string, catalogCategory string, countryCode string, serviceID string) ([]byte, error) {
return nil, nil
}
// ReadStandingOrder TODO
func (fakeConnection *FakeConnection) ReadStandingOrder(accessToken string, options ...TransactionOption) ([]byte, error) {
return nil, nil
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}