forked from schollz/find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
executable file
·135 lines (108 loc) · 3.82 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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os/exec"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.ReleaseMode)
out, err := exec.Command("cp", []string{"data/testdb.db.backup", "data/testdb.db"}...).Output()
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
}
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, resp.Body.String(), "{\"message\":\"Parameters optimized.\",\"success\":true}\n")
}
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, resp.Body.String(), "{\"message\":\"Error parsing request\",\"success\":false}\n")
}
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, 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, 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}\n"
assert.Equal(t, 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}\n"
assert.Equal(t, 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}\n"
assert.Equal(t, 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}\n"
assert.Equal(t, resp.Body.String(), response)
}