-
-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathsession_test.go
More file actions
136 lines (111 loc) · 3.8 KB
/
Copy pathsession_test.go
File metadata and controls
136 lines (111 loc) · 3.8 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
package api
import (
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/gotify/server/v2/auth"
"github.com/gotify/server/v2/auth/password"
"github.com/gotify/server/v2/mode"
"github.com/gotify/server/v2/model"
"github.com/gotify/server/v2/test"
"github.com/gotify/server/v2/test/testdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestSessionSuite(t *testing.T) {
suite.Run(t, new(SessionSuite))
}
type SessionSuite struct {
suite.Suite
db *testdb.Database
a *SessionAPI
ctx *gin.Context
recorder *httptest.ResponseRecorder
notified bool
}
func (s *SessionSuite) BeforeTest(suiteName, testName string) {
mode.Set(mode.TestDev)
s.recorder = httptest.NewRecorder()
s.db = testdb.NewDB(s.T())
s.ctx, _ = gin.CreateTestContext(s.recorder)
withURL(s.ctx, "http", "example.com")
s.notified = false
s.a = &SessionAPI{DB: s.db, NotifyDeleted: s.notify}
s.db.CreateUser(&model.User{
Name: "testuser",
Pass: password.CreatePassword("testpass", 5),
})
}
func (s *SessionSuite) notify(uint, string) {
s.notified = true
}
func (s *SessionSuite) AfterTest(suiteName, testName string) {
s.db.Close()
}
func (s *SessionSuite) Test_Login_Success() {
originalGenerateClientToken := generateClientToken
defer func() { generateClientToken = originalGenerateClientToken }()
generateClientToken = test.Tokens("Ctesttoken12345")
s.ctx.Request = httptest.NewRequest("POST", "/auth/local/login", strings.NewReader("name=test-browser"))
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
s.ctx.Request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("testuser:testpass")))
s.a.Login(s.ctx)
assert.Equal(s.T(), 200, s.recorder.Code)
// Verify HttpOnly cookie is set
cookies := s.recorder.Result().Cookies()
var sessionCookie *http.Cookie
for _, c := range cookies {
if c.Name == auth.CookieName {
sessionCookie = c
break
}
}
assert.NotNil(s.T(), sessionCookie)
assert.Equal(s.T(), "Ctesttoken12345", sessionCookie.Value)
assert.True(s.T(), sessionCookie.HttpOnly)
assert.Equal(s.T(), "/", sessionCookie.Path)
assert.Equal(s.T(), http.SameSiteStrictMode, sessionCookie.SameSite)
body := s.recorder.Body.String()
assert.Contains(s.T(), body, "testuser")
assert.NotContains(s.T(), body, "Ctesttoken12345")
clients, err := s.db.GetClientsByUser(1)
assert.NoError(s.T(), err)
assert.Len(s.T(), clients, 1)
assert.Equal(s.T(), "test-browser", clients[0].Name)
assert.Equal(s.T(), uint(auth.CookieMaxAge), clients[0].ExpiresAfterInactivitySeconds)
}
func (s *SessionSuite) Test_Login_WrongPassword() {
s.ctx.Request = httptest.NewRequest("POST", "/auth/local/login", strings.NewReader("name=test-browser"))
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
s.ctx.Request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("testuser:wrongpass")))
s.a.Login(s.ctx)
assert.Equal(s.T(), 401, s.recorder.Code)
// No cookie should be set
cookies := s.recorder.Result().Cookies()
for _, c := range cookies {
assert.NotEqual(s.T(), auth.CookieName, c.Name)
}
}
func (s *SessionSuite) Test_Logout_Success() {
client := s.db.User(5).NewClientWithToken(1, "Ctesttoken12345")
s.ctx.Request = httptest.NewRequest("POST", "/auth/logout", nil)
auth.RegisterClient(s.ctx, client)
s.a.Logout(s.ctx)
assert.Equal(s.T(), 200, s.recorder.Code)
assert.True(s.T(), s.notified)
cookies := s.recorder.Result().Cookies()
var sessionCookie *http.Cookie
for _, c := range cookies {
if c.Name == auth.CookieName {
sessionCookie = c
break
}
}
assert.NotNil(s.T(), sessionCookie)
assert.Equal(s.T(), "", sessionCookie.Value)
assert.True(s.T(), sessionCookie.MaxAge < 0)
s.db.AssertClientNotExist(1)
}