-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_test.go
131 lines (116 loc) · 4.5 KB
/
mongo_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
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
package mongo
import (
"encoding/json"
"testing"
"github.com/kapetacom/sdk-go-config/providers"
"github.com/stretchr/testify/assert"
)
func TestGetProtocol(t *testing.T) {
t.Run("default protocol", func(t *testing.T) {
resInfo := &providers.ResourceInfo{}
expected := "mongodb"
actual := getProtocol(resInfo)
assert.Equal(t, expected, actual)
})
t.Run("protocol specified in options", func(t *testing.T) {
resInfo := &providers.ResourceInfo{Options: map[string]interface{}{"protocol": "mongodb+srv"}}
expected := "mongodb+srv"
actual := getProtocol(resInfo)
assert.Equal(t, expected, actual)
})
}
func TestDBName(t *testing.T) {
t.Run("default db name", func(t *testing.T) {
resInfo := &providers.ResourceInfo{}
resourceName := "test"
expected := "test"
actual := getDBName(resInfo, resourceName)
assert.Equal(t, expected, actual)
})
t.Run("db name specified in options", func(t *testing.T) {
resInfo := &providers.ResourceInfo{Options: map[string]interface{}{"dbName": "test_db"}}
resourceName := "test"
expected := "test_db"
actual := getDBName(resInfo, resourceName)
assert.Equal(t, expected, actual)
})
}
func TestCreateConnectionStringProtocolMongodb(t *testing.T) {
t.Run("create connection string with mongodb protocol", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Credentials: map[string]string{"username": "user", "password": "password"},
}
dbName := "test"
urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true"
actual, err := createConnectionString(resInfo, dbName)
assert.NoError(t, err)
assert.Equal(t, urlWithPort, actual)
})
t.Run("verify that the mongodb+srv connection string doesn't have a port and a direct connection", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Options: map[string]interface{}{"protocol": "mongodb+srv"},
Credentials: map[string]string{"username": "user", "password": "password"},
}
dbName := "test"
urlWithNoPort := "mongodb+srv://user:password@localhost/test?authSource=admin"
actual, err := createConnectionString(resInfo, dbName)
assert.NoError(t, err)
assert.Equal(t, urlWithNoPort, actual)
})
t.Run("verify that mongodb connection string contains directConnection", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Credentials: map[string]string{"username": "user", "password": "password"},
}
dbName := "test"
urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true"
actual, err := createConnectionString(resInfo, dbName)
assert.NoError(t, err)
assert.Equal(t, urlWithPort, actual)
})
t.Run("verify that mongodb connection string contains ssl true", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Credentials: map[string]string{"username": "user", "password": "password"},
Options: map[string]interface{}{"ssl": "true"},
}
dbName := "test"
urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true&ssl=true"
actual, err := createConnectionString(resInfo, dbName)
assert.NoError(t, err)
assert.Equal(t, urlWithPort, actual)
})
t.Run("verify that mongodb connection string contains ssl false", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Credentials: map[string]string{"username": "user", "password": "password"},
Options: map[string]interface{}{"ssl": "false"},
}
dbName := "test"
urlWithPort := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true&ssl=false"
actual, err := createConnectionString(resInfo, dbName)
assert.NoError(t, err)
assert.Equal(t, urlWithPort, actual)
})
}
func TestCreateConnectionString(t *testing.T) {
t.Run("create connection string", func(t *testing.T) {
resInfo := &providers.ResourceInfo{
Host: "localhost",
Port: json.Number("27017"),
Credentials: map[string]string{"username": "user", "password": "password"},
}
resourceName := "test"
expected := "mongodb://user:password@localhost:27017/test?authSource=admin&directConnection=true"
actual, err := createConnectionString(resInfo, resourceName)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})
}