-
Notifications
You must be signed in to change notification settings - Fork 12
/
request_test.go
125 lines (88 loc) · 2.62 KB
/
request_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
// +build !mock
package synapse
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
var credentials map[string]interface{}
var requestData map[string]interface{}
var userID string = "5e6917b85b5a1e0081e0e309" // Change this to test a user on your platform
var testID string
var authKey string
/********** METHODS **********/
func init() {
data, err := readFile("client_credentials")
if err != nil {
panic(err)
}
reqData, requestErr := readFile("request_methods")
if requestErr != nil {
panic(err)
}
credentials = data["clientData"].(map[string]interface{})
requestData = reqData
}
func createRequestClient() *Request {
return &Request{
clientID: credentials["clientID"].(string),
clientSecret: credentials["clientSecret"].(string),
fingerprint: credentials["fingerprint"].(string),
ipAddress: credentials["ipAddress"].(string),
}
}
/********** TESTS **********/
func Test_Get(t *testing.T) {
assert := assert.New(t)
testReq := createRequestClient()
res, err := testReq.Get(buildURL(path["users"], userID), nil)
assert.NoError(err)
assert.NotNil(res)
}
func Test_Post(t *testing.T) {
assert := assert.New(t)
testReq := createRequestClient()
testData := requestData["POST"].(map[string]interface{})["data"]
jsonData, jsonErr := json.Marshal(testData)
if jsonErr != nil {
t.Error(jsonErr)
}
userRes, userErr := testReq.Get(buildURL(path["users"], userID), nil)
if userErr != nil {
t.Error(userErr)
}
rt := readStream(userRes)["refresh_token"].(string)
authRes, authErr := testReq.Post(buildURL(path["auth"], userID), `{ "refresh_token": "`+rt+`" }`, nil)
if authErr != nil {
t.Error(authErr)
}
authKey = readStream(authRes)["oauth_key"].(string)
testReq.authKey = authKey
res, err := testReq.Post(buildURL(path["users"], userID, "nodes"), string(jsonData), nil)
if err != nil {
t.Error(err)
}
testID = readStream(res)["nodes"].([]interface{})[0].(map[string]interface{})["_id"].(string)
assert.NotNil(string(res))
}
func Test_Patch(t *testing.T) {
assert := assert.New(t)
testReq := createRequestClient()
testReq.authKey = authKey
testData := requestData["PATCH"].(map[string]interface{})["data"]
jsonData, err := json.Marshal(testData)
if err != nil {
t.Error(err)
}
res, err := testReq.Patch(buildURL(path["users"], userID, "nodes", testID), string(jsonData), nil)
assert.NoError(err)
assert.NotNil(res)
}
func Test_Delete(t *testing.T) {
assert := assert.New(t)
testReq := createRequestClient()
testReq.authKey = authKey
res, err := testReq.Delete(buildURL(path["users"], userID, "nodes", testID))
assert.NoError(err)
assert.NotNil(res)
}