-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
76 lines (69 loc) · 2.64 KB
/
handler_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
package neugo
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestIsLogged(t *testing.T) {
a := assert.New(t)
type testCase struct {
Content string
Expect error
}
testCases := []*testCase{
{Content: "<title>智慧东大--统一身份认证</title>", Expect: ErrorAuthFailed},
{Content: "<title>智慧东大</title>", Expect: ErrorAccountNeedsReset},
{Content: "<title>系统提示</title>", Expect: ErrorAccountBanned},
{Content: "<title><||__&^$></title>", Expect: nil},
{Content: "<>", Expect: nil},
}
for _, c := range testCases {
_, err := isLogged(c.Content)
a.Equal(c.Expect, err)
}
}
func TestGetArgs(t *testing.T) {
a := assert.New(t)
handler := http.NewServeMux()
handler.HandleFunc("/success", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`<form id="loginForm" action="/tpass/login?service=https%3A%2F%2Fportal.neu.edu.cn%2Ftp_up%2F" method="post">
<input type="hidden" id="rsa" name="rsa"/>
<input type="hidden" id="ul" name="ul"/>
<input type="hidden" id="pl" name="pl"/>
<input type="hidden" id="lt" name="lt" value="LT-324784-5WKhfINLQf4HWzozfafzSnEguyQ6Ox-tpass" />
<input type="hidden" name="execution" value="e3s1" />
<input type="hidden" name="_eventId" value="submit" />`))
})
handler.HandleFunc("/fail", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`<form id="loginForm" action="/tpass/login?service=https%3A%2F%2Fportal.neu.edu.cn%2Ftp_up%2F" method="post">
<input type="hidden" id="rsa" name="rsa"/>
<input type="hidden" id="ul" name="ul"/>
<input type="hidden" id="pl" name="pl"/>
<input type="hidden" id="lt" name="lt"/>
<input type="hidden" name="execution" value="e3s1" />
<input type="hidden" name="_eventId" value="submit" />`))
})
srv := httptest.NewServer(handler)
client := NewSession()
lt, err := getLT(client, srv.URL+"/success")
a.Nil(err)
a.Equal("LT-324784-5WKhfINLQf4HWzozfafzSnEguyQ6Ox-tpass", lt)
_, err = getLT(client, srv.URL+"/fail")
a.NotNil(err)
// meaningless
_, err = getLT(client, "")
a.NotNil(err)
}
func TestBuildAuthRequest(t *testing.T) {
a := assert.New(t)
r := buildAuthRequest("test", "test", "test",
"https://pass.neu.edu.cn/tpass/login?service=http%3A%2F%2F219.216.96.4%2Feams%2FhomeExt.action")
res, _ := io.ReadAll(r.Body)
_ = r.Body.Close()
a.Equal("rsa=testtesttest&ul=4&pl=4<=test&execution=e1s1&_eventId=submit",
string(res))
a.Equal("https://pass.neu.edu.cn/tpass/login?service=http%3A%2F%2F219.216.96.4%2Feams%2FhomeExt.action",
r.URL.String())
}