parent
30c963a5f0
commit
61a1c93f0f
89 changed files with 15859 additions and 2 deletions
78
server/apis/auth_test.go
Normal file
78
server/apis/auth_test.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package apis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
import (
|
||||
"quickshare/server/libs/cfg"
|
||||
"quickshare/server/libs/encrypt"
|
||||
"quickshare/server/libs/httputil"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
conf := cfg.NewConfig()
|
||||
|
||||
type testCase struct {
|
||||
Desc string
|
||||
AdminId string
|
||||
AdminPwd string
|
||||
Result interface{}
|
||||
VerifyToken bool
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
testCase{
|
||||
Desc: "invalid input",
|
||||
AdminId: "",
|
||||
AdminPwd: "",
|
||||
Result: httputil.Err401,
|
||||
VerifyToken: false,
|
||||
},
|
||||
testCase{
|
||||
Desc: "account not match",
|
||||
AdminId: "unknown",
|
||||
AdminPwd: "unknown",
|
||||
Result: httputil.Err401,
|
||||
VerifyToken: false,
|
||||
},
|
||||
testCase{
|
||||
Desc: "succeed to login",
|
||||
AdminId: conf.AdminId,
|
||||
AdminPwd: conf.AdminPwd,
|
||||
Result: httputil.Ok200,
|
||||
VerifyToken: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
srv := NewSrvShare(conf)
|
||||
res := &stubWriter{Headers: map[string][]string{}}
|
||||
ret := srv.login(testCase.AdminId, testCase.AdminPwd, res)
|
||||
|
||||
if ret != testCase.Result {
|
||||
t.Fatalf("login: reponse=%v testCase=%v", ret, testCase.Result)
|
||||
}
|
||||
|
||||
// verify cookie (only token.adminid part))
|
||||
if testCase.VerifyToken {
|
||||
cookieVal := strings.Replace(
|
||||
res.Header().Get("Set-Cookie"),
|
||||
fmt.Sprintf("%s=", conf.KeyToken),
|
||||
"",
|
||||
1,
|
||||
)
|
||||
|
||||
gotTokenStr := strings.Split(cookieVal, ";")[0]
|
||||
token := encrypt.JwtEncrypterMaker(conf.SecretKey)
|
||||
token.FromStr(gotTokenStr)
|
||||
gotToken, found := token.Get(conf.KeyAdminId)
|
||||
if !found || conf.AdminId != gotToken {
|
||||
t.Fatalf("login: token admin id unmatch got=%v expect=%v", gotToken, conf.AdminId)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue