Browse Source

Add integration tests for signin (#2363)

Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
David Schneiderbauer 1 year ago
parent
commit
8aadf79f80
2 changed files with 71 additions and 4 deletions
  1. 11 4
      integrations/integration_test.go
  2. 60 0
      integrations/signin_test.go

+ 11 - 4
integrations/integration_test.go

@@ -160,6 +160,13 @@ const userPassword = "password"
160 160
 
161 161
 var loginSessionCache = make(map[string]*TestSession, 10)
162 162
 
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
+
163 170
 func loginUser(t testing.TB, userName string) *TestSession {
164 171
 	if session, ok := loginSessionCache[userName]; ok {
165 172
 		return session
@@ -185,13 +192,13 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
185 192
 	ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";"))
186 193
 	cr := http.Request{Header: ch}
187 194
 
188
-	jar, err := cookiejar.New(nil)
189
-	assert.NoError(t, err)
195
+	session := emptyTestSession(t)
196
+
190 197
 	baseURL, err := url.Parse(setting.AppURL)
191 198
 	assert.NoError(t, err)
192
-	jar.SetCookies(baseURL, cr.Cookies())
199
+	session.jar.SetCookies(baseURL, cr.Cookies())
193 200
 
194
-	return &TestSession{jar: jar}
201
+	return session
195 202
 }
196 203
 
197 204
 type TestResponseWriter struct {

+ 60 - 0
integrations/signin_test.go

@@ -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: "user1@example.com", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
53
+		// test for duplicate email
54
+		{username: "user2@example.com", 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
+}