-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
154 lines (123 loc) · 3.53 KB
/
main_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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeropage/mukgoorm/cmd"
"github.com/zeropage/mukgoorm/setting"
)
func initialize() {
dir := setting.GetDirectory()
dir.Path = "testdata"
pwd := setting.GetPassword()
pwd.AdminPwd = "admin"
pwd.ROnlyPwd = "readonly"
}
var session string
func init() {
initialize()
r := NewEngine()
data := url.Values{}
pwd := setting.GetPassword()
data.Set("password", pwd.AdminPwd)
req, _ := http.NewRequest("POST", "/login", bytes.NewBufferString(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
session = w.Header().Get("Set-Cookie")
}
// This code came from gin-gonic/gin/routes_test.go
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
initialize()
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func PerformRequestWithSession(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
req.Header.Add("Cookie", session)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestAuthoritySuccess(t *testing.T) {
r := NewEngine()
w := PerformRequestWithSession(r, "GET", "/")
assert.Equal(t, http.StatusOK, w.Code)
}
func TestAuthorityFail(t *testing.T) {
r := NewEngine()
w := PerformRequest(r, "GET", "/")
assert.Equal(t, http.StatusSeeOther, w.Code)
}
func TestAllRoutesExist(t *testing.T) {
routetests := []struct {
method string
location string
expectStatusCode uint32
}{
{"GET", "/", http.StatusNotFound},
// TODO generate from setting
{"GET", fmt.Sprintf("/down?dir=%s", setting.GetDirectory().Path), http.StatusNotFound},
{"GET", "/login", http.StatusNotFound},
{"POST", "/login", http.StatusNotFound},
{"GET", "/set-password", http.StatusNotFound},
{"POST", "/set-password", http.StatusNotFound},
{"DELETE", "/delete", http.StatusNotFound},
}
r := NewEngine()
for _, rt := range routetests {
w := PerformRequest(r, rt.method, rt.location)
assert.NotEqual(t, rt.expectStatusCode, w.Code)
}
}
func TestListSuccess(t *testing.T) {
r := NewEngine()
loc := fmt.Sprintf("/?dir=%s", setting.GetDirectory().Path)
w := PerformRequestWithSession(r, "GET", loc)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestListFail(t *testing.T) {
r := NewEngine()
w := PerformRequestWithSession(r, "GET", "/?dir=/")
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestCheckStartOptionsFail(t *testing.T) {
defer func() {
r := recover()
assert.NotNil(t, r)
}()
for _, args := range [][]string{
{"-A 1234", "-R 1234"},
{"-A 1234", "-D tmp/dat"},
{"-R 1234", "-D tmp/dat"}} {
cmd.RootCmd.SetArgs(args)
main()
}
}
func TestDelete(t *testing.T) {
dir := path.Join(setting.GetDirectory().Path, "deletefile")
if _, err := os.Create(dir); err != nil {
t.Fatalf("Fail to create file: %s, %v", dir, err)
}
r := NewEngine()
loc := fmt.Sprintf("/delete?dir=%s", dir)
w := PerformRequestWithSession(r, "DELETE", loc)
assert.Equal(t, http.StatusOK, w.Code)
w = PerformRequestWithSession(r, "DELETE", loc)
assert.Equal(t, http.StatusNotAcceptable, w.Code)
}
func TestLogout(t *testing.T) {
r := NewEngine()
w := PerformRequestWithSession(r, "DELETE", "/logout")
assert.Equal(t, http.StatusOK, w.Code)
w = PerformRequestWithSession(r, "GET", "/")
assert.Equal(t, http.StatusSeeOther, w.Code)
}