-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfakeConnection_test.go
100 lines (84 loc) · 2.66 KB
/
fakeConnection_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
package fiGo_test
import (
"encoding/json"
"testing"
"github.com/Jeffail/gabs"
"github.com/TobiEiss/fiGo"
)
// TestFakeConnectionImplementsEverything test only if the code compiles.
// If the code doesnt compile, the fakeConnection miss a function to implement
func TestFakeConnectionImplementsEverything(t *testing.T) {
var connection fiGo.IConnection
fakeConnection := fiGo.NewFakeConnection()
connection = fakeConnection
if connection == nil {
t.Fail()
}
}
// TestStoreUser creates a new user in fakeConnection
func TestStoreUser(t *testing.T) {
// create a new fakeConnection
fakeConnection := fiGo.NewFakeConnection()
// "store" a user
responseAsBytes, err := fakeConnection.CreateUser("TestUser", "test@test.de", "mySuperSecretPassword")
failOnError(t, err)
// try to get the recovery-password
var responseAsMap map[string]string
err = json.Unmarshal(responseAsBytes, &responseAsMap)
failOnError(t, err)
}
// TestCredentialLogin tests a login.
// 1. Create a new user
// 2. Login new TestUser
// 3. Check the informations
func TestCredentialLogin(t *testing.T) {
username := "test@test.de"
password := "mySuperSecretPassword"
// create a new fakeConnection
fakeConnection := fiGo.NewFakeConnection()
// "store" a user
fakeConnection.CreateUser("TestUser", username, password)
// login
userByte, err := fakeConnection.CredentialLogin(username, password)
failOnError(t, err)
// try to get informations
var userAsMap map[string]interface{}
err = json.Unmarshal(userByte, &userAsMap)
failOnError(t, err)
// check informations
if userAsMap["access_token"] == "" || userAsMap["expires_in"] == 0 ||
userAsMap["refresh_token"] == "" || userAsMap["scope"] == nil || userAsMap["token_type"] == nil {
t.Fail()
}
}
// TestAddAccount
// 1. Create a new user
// 2. Login new TestUser
// 3. Add account
// -> Check task_token
func TestAddAccount(t *testing.T) {
username := "test@test.de"
password := "mySuperSecretPassword"
// create a new fakeConnection
fakeConnection := fiGo.NewFakeConnection()
// "store" a user
fakeConnection.CreateUser("TestUser", username, password)
// login
userByte, _ := fakeConnection.CredentialLogin(username, password)
jsonParsed, _ := gabs.ParseJSON(userByte)
accessToken, _ := jsonParsed.Path("access_token").Data().(string)
// add account
responseByte, err := fakeConnection.SetupNewBankAccount(accessToken, "90090042", "de", []string{"demo", "demo"})
failOnError(t, err)
jsonParsed, err = gabs.ParseJSON(responseByte)
taskToken, ok := jsonParsed.Path("task_token").Data().(string)
if taskToken == "" || !ok || err != nil {
t.Fail()
}
}
func failOnError(t *testing.T, err error) {
if err != nil {
t.Log(err)
t.Fail()
}
}