Skip to content

Commit 8aadf79

Browse files
daviianlafriks
authored andcommitted
Add integration tests for signin (#2363)
Signed-off-by: David Schneiderbauer <[email protected]>
1 parent fd6e910 commit 8aadf79

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

integrations/integration_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ const userPassword = "password"
160160

161161
var loginSessionCache = make(map[string]*TestSession, 10)
162162

163+
func emptyTestSession(t testing.TB) *TestSession {
164+
jar, err := cookiejar.New(nil)
165+
assert.NoError(t, err)
166+
167+
return &TestSession{jar: jar}
168+
}
169+
163170
func loginUser(t testing.TB, userName string) *TestSession {
164171
if session, ok := loginSessionCache[userName]; ok {
165172
return session
@@ -185,13 +192,13 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
185192
ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";"))
186193
cr := http.Request{Header: ch}
187194

188-
jar, err := cookiejar.New(nil)
189-
assert.NoError(t, err)
195+
session := emptyTestSession(t)
196+
190197
baseURL, err := url.Parse(setting.AppURL)
191198
assert.NoError(t, err)
192-
jar.SetCookies(baseURL, cr.Cookies())
199+
session.jar.SetCookies(baseURL, cr.Cookies())
193200

194-
return &TestSession{jar: jar}
201+
return session
195202
}
196203

197204
type TestResponseWriter struct {

integrations/signin_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"strings"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
14+
"github.com/Unknwon/i18n"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func testLoginFailed(t *testing.T, username, password, message string) {
19+
session := emptyTestSession(t)
20+
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
21+
"_csrf": GetCSRF(t, session, "/user/login"),
22+
"user_name": username,
23+
"password": password,
24+
})
25+
resp := session.MakeRequest(t, req, http.StatusOK)
26+
27+
htmlDoc := NewHTMLParser(t, resp.Body)
28+
resultMsg := htmlDoc.doc.Find(".ui.message>p").Text()
29+
30+
assert.EqualValues(t, message, resultMsg)
31+
}
32+
33+
func TestSignin(t *testing.T) {
34+
prepareTestEnv(t)
35+
36+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
37+
38+
// add new user with user2's email
39+
user.Name = "testuser"
40+
user.LowerName = strings.ToLower(user.Name)
41+
user.ID = 0
42+
models.AssertSuccessfulInsert(t, user)
43+
44+
samples := []struct {
45+
username string
46+
password string
47+
message string
48+
}{
49+
{username: "wrongUsername", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
50+
{username: "wrongUsername", password: "password", message: i18n.Tr("en", "form.username_password_incorrect")},
51+
{username: "user15", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
52+
{username: "[email protected]", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
53+
// test for duplicate email
54+
{username: "[email protected]", password: "password", message: i18n.Tr("en", "form.email_been_used")},
55+
}
56+
57+
for _, s := range samples {
58+
testLoginFailed(t, s.username, s.password, s.message)
59+
}
60+
}

0 commit comments

Comments
 (0)