This repository was archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_auth_test.go
More file actions
145 lines (132 loc) · 3.51 KB
/
Copy pathbasic_auth_test.go
File metadata and controls
145 lines (132 loc) · 3.51 KB
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
package basicauth
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vicanso/elton"
"github.com/vicanso/hes"
)
func TestNoVildatePanic(t *testing.T) {
assert := assert.New(t)
defer func() {
r := recover()
assert.NotNil(r)
assert.Equal(r.(error), errRequireValidateFunction)
}()
New(Config{})
}
func TestBasicAuth(t *testing.T) {
m := New(Config{
Validate: func(account, pwd string, c *elton.Context) (bool, error) {
if account == "tree.xie" && pwd == "password" {
return true, nil
}
if account == "n" {
return false, hes.New("account is invalid")
}
return false, nil
},
})
req := httptest.NewRequest("GET", "https://aslant.site/", nil)
t.Run("skip", func(t *testing.T) {
assert := assert.New(t)
done := false
mSkip := New(Config{
Validate: func(account, pwd string, c *elton.Context) (bool, error) {
return false, nil
},
Skipper: func(c *elton.Context) bool {
return true
},
})
e := elton.New()
e.Use(mSkip)
e.GET("/", func(c *elton.Context) error {
done = true
return nil
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.True(done)
})
t.Run("no auth header", func(t *testing.T) {
assert := assert.New(t)
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
return nil
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusUnauthorized)
assert.Equal(resp.Header().Get(elton.HeaderWWWAuthenticate), `basic realm="basic auth tips"`)
})
t.Run("auth validate fail", func(t *testing.T) {
assert := assert.New(t)
e := elton.New()
e.Use(m)
e.GET("/", func(c *elton.Context) error {
return nil
})
req.Header.Set(elton.HeaderAuthorization, "basic YTpi")
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusUnauthorized)
assert.Equal(resp.Body.String(), "category=elton-basic-auth, message=unAuthorized")
req.Header.Set(elton.HeaderAuthorization, "basic bjph")
resp = httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusBadRequest)
assert.Equal(resp.Body.String(), "message=account is invalid")
})
t.Run("validate error", func(t *testing.T) {
assert := assert.New(t)
mValidateFail := New(Config{
Validate: func(account, pwd string, c *elton.Context) (bool, error) {
return false, errors.New("abcd")
},
})
e := elton.New()
e.Use(mValidateFail)
e.GET("/", func(c *elton.Context) error {
return nil
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusBadRequest)
assert.Equal(resp.Body.String(), "category=elton-basic-auth, message=abcd")
})
t.Run("auth success", func(t *testing.T) {
assert := assert.New(t)
e := elton.New()
e.Use(m)
done := false
e.GET("/", func(c *elton.Context) error {
done = true
return nil
})
req.Header.Set(elton.HeaderAuthorization, "basic dHJlZS54aWU6cGFzc3dvcmQ=")
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.True(done)
})
}
// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()
// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.9 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}