File size: 2,013 Bytes
530729e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package common

import (
	"fmt"
	"net/http"

	"github.com/GoAdminGroup/go-admin/modules/auth"
	"github.com/GoAdminGroup/go-admin/modules/config"
	"github.com/gavv/httpexpect"
)

func authTest(e *httpexpect.Expect) *http.Cookie {

	printlnWithColor("Auth", "blue")
	fmt.Println("============================")

	// login: show

	printlnWithColor("login: show", "green")
	e.GET(config.Url(config.GetLoginUrl())).Expect().Status(200)
	printlnWithColor("login: empty password", "green")
	e.POST(config.Url("/signin")).WithJSON(map[string]string{
		"username": "admin",
		"password": "",
	}).Expect().Status(400)

	// login

	printlnWithColor("login", "green")
	sesID := e.POST(config.Url("/signin")).WithForm(map[string]string{
		"username": "admin",
		"password": "admin",
	}).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw()

	// logout: without login

	printlnWithColor("logout: without login", "green")
	e.GET(config.Url("/logout")).Expect().
		Status(200)

	// logout

	printlnWithColor("logout", "green")
	e.GET(config.Url("/logout")).WithCookie(auth.DefaultCookieKey, sesID.Value).Expect().
		Status(200)

	// login again

	printlnWithColor("login again", "green")
	cookie1 := e.POST(config.Url("/signin")).WithForm(map[string]string{
		"username": "admin",
		"password": "admin",
	}).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw()

	printlnWithColor("login again:restrict users from logging in at the same time", "green")
	cookie2 := e.POST(config.Url("/signin")).WithForm(map[string]string{
		"username": "admin",
		"password": "admin",
	}).Expect().Status(200).Cookie(auth.DefaultCookieKey).Raw()

	// login success

	printlnWithColor("cookie failure", "green")
	e.GET(config.Url("/")).
		WithCookie(auth.DefaultCookieKey, cookie1.Value).Expect().
		Status(200).
		Body().Contains("login")

	printlnWithColor("login success", "green")
	e.GET(config.Url("/")).
		WithCookie(auth.DefaultCookieKey, cookie2.Value).Expect().
		Status(200).
		Body().Contains("Dashboard")

	return cookie2

}