-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
executable file
·167 lines (131 loc) · 4.99 KB
/
api_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.ReleaseMode)
_, err := exec.Command("cp", []string{"data/testdb.db.backup", "data/testdb.db"}...).Output()
if err != nil {
log.Fatal(err)
}
}
func TestGetStatus(t *testing.T) {
router := gin.New()
router.PUT("/foo", getStatus)
req, _ := http.NewRequest("PUT", "/foo", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.Contains(resp.Body.String(), "\"success\":true"), true)
}
func TestMigrateDatabase(t *testing.T) {
router := gin.New()
router.PUT("/foo", migrateDatabase)
req, _ := http.NewRequest("PUT", "/foo?from=testdb&to=newdb", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.TrimSpace(resp.Body.String()), "{\"message\":\"Successfully migrated testdb to newdb\",\"success\":true}")
os.Remove("data/newdb.db")
}
func TestDeleteDatabase(t *testing.T) {
CopyFile("./data/testdb.db", "./data/deleteme.db")
router := gin.New()
router.DELETE("/foo", deleteDatabase)
req, _ := http.NewRequest("DELETE", "/foo?group=deleteme", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.TrimSpace(resp.Body.String()), "{\"message\":\"Successfully deleted deleteme\",\"success\":true}")
}
func TestCalculate(t *testing.T) {
router := gin.New()
router.GET("/foo", calculate)
req, _ := http.NewRequest("GET", "/foo?group=testdb", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.TrimSpace(resp.Body.String()), "{\"message\":\"Parameters optimized.\",\"success\":true}")
}
func TestUserLocations(t *testing.T) {
router := gin.New()
router.GET("/foo", userLocations)
req, _ := http.NewRequest("GET", "/foo?group=testdb", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.Contains(resp.Body.String(), "{\"message\":\"Correctly found\",\"success\":true,"), true)
}
func TestGetUserLocations(t *testing.T) {
router := gin.New()
router.GET("/foo", getUserLocations)
req, _ := http.NewRequest("GET", "/foo", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.TrimSpace(resp.Body.String()), "{\"message\":\"Error parsing request\",\"success\":false}")
}
func TestGetUserLocations2(t *testing.T) {
router := gin.New()
router.GET("/foo", getUserLocations)
req, _ := http.NewRequest("GET", "/foo?group=testdb&user=zack", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, strings.Contains(resp.Body.String(), "{\"message\":\"Correctly found locations.\""), true)
}
func TestPutMixinOverrideBad(t *testing.T) {
router := gin.New()
router.PUT("/foo", putMixinOverride)
req, _ := http.NewRequest("PUT", "/foo?group=testdb&mixin=100", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := `{"message":"mixin must be between 0 and 1","success":false}`
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}
func TestPutMixinOverrideGood(t *testing.T) {
router := gin.New()
router.PUT("/foo", putMixinOverride)
req, _ := http.NewRequest("PUT", "/foo?group=testdb&mixin=0", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := `{"message":"Overriding mixin for testdb, now set to 0","success":true}`
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}
func TestEditNetworkName(t *testing.T) {
router := gin.New()
router.GET("/foo", editNetworkName)
req, _ := http.NewRequest("GET", "/foo?group=testdb&oldname=0&newname=home", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := "{\"message\":\"Finished\",\"success\":true}"
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}
func TestEditName(t *testing.T) {
router := gin.New()
router.GET("/foo", editName)
req, _ := http.NewRequest("GET", "/foo?group=testdb&location=zakhome%20floor%202%20office&newname=office", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := "{\"message\":\"Changed name of 175 things\",\"success\":true}"
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}
func TestEditUserName(t *testing.T) {
router := gin.New()
router.GET("/foo", editUserName)
req, _ := http.NewRequest("GET", "/foo?group=testdb&user=zack&newname=zack2", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := "{\"message\":\"Changed name of 344 things\",\"success\":true}"
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}
func TestDeleteUser(t *testing.T) {
router := gin.New()
router.DELETE("/foo", deleteUser)
req, _ := http.NewRequest("DELETE", "/foo?group=testdb&[email protected]", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
response := "{\"message\":\"Deletes 0 things with user [email protected]\",\"success\":true}"
assert.Equal(t, strings.TrimSpace(resp.Body.String()), response)
}