github.com/kotovmak/go-admin@v1.1.1/tests/common/auth.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gavv/httpexpect" 8 "github.com/kotovmak/go-admin/modules/auth" 9 "github.com/kotovmak/go-admin/modules/config" 10 ) 11 12 func authTest(e *httpexpect.Expect) *http.Cookie { 13 14 printlnWithColor("Auth", "blue") 15 fmt.Println("============================") 16 17 // login: show 18 19 printlnWithColor("login: show", "green") 20 e.GET(config.Url(config.GetLoginUrl())).Expect().Status(200) 21 printlnWithColor("login: empty password", "green") 22 e.POST(config.Url("/signin")).WithJSON(map[string]string{ 23 "username": "admin", 24 "password": "", 25 }).Expect().Status(400) 26 27 // login 28 29 printlnWithColor("login", "green") 30 sesID := e.POST(config.Url("/signin")).WithForm(map[string]string{ 31 "username": "admin", 32 "password": "admin", 33 }).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw() 34 35 // logout: without login 36 37 printlnWithColor("logout: without login", "green") 38 e.GET(config.Url("/logout")).Expect(). 39 Status(200) 40 41 // logout 42 43 printlnWithColor("logout", "green") 44 e.GET(config.Url("/logout")).WithCookie(auth.DefaultCookieKey, sesID.Value).Expect(). 45 Status(200) 46 47 // login again 48 49 printlnWithColor("login again", "green") 50 cookie1 := e.POST(config.Url("/signin")).WithForm(map[string]string{ 51 "username": "admin", 52 "password": "admin", 53 }).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw() 54 55 printlnWithColor("login again:restrict users from logging in at the same time", "green") 56 cookie2 := e.POST(config.Url("/signin")).WithForm(map[string]string{ 57 "username": "admin", 58 "password": "admin", 59 }).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw() 60 61 // login success 62 63 printlnWithColor("cookie failure", "green") 64 e.GET(config.Url("/")). 65 WithCookie(auth.DefaultCookieKey, cookie1.Value).Expect(). 66 Status(200). 67 Body().Contains("login") 68 69 printlnWithColor("login success", "green") 70 e.GET(config.Url("/")). 71 WithCookie(auth.DefaultCookieKey, cookie2.Value).Expect(). 72 Status(200). 73 Body().Contains("Dashboard") 74 75 return cookie2 76 77 }