From 8aadf79f800187958ec92e4dda49cd3ac1872465 Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Wed, 23 Aug 2017 09:30:33 +0200 Subject: [PATCH] Add integration tests for signin (#2363) Signed-off-by: David Schneiderbauer --- integrations/integration_test.go | 15 +++++--- integrations/signin_test.go | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 integrations/signin_test.go diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 83c73ec662..2840b0fb69 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -160,6 +160,13 @@ const userPassword = "password" var loginSessionCache = make(map[string]*TestSession, 10) +func emptyTestSession(t testing.TB) *TestSession { + jar, err := cookiejar.New(nil) + assert.NoError(t, err) + + return &TestSession{jar: jar} +} + func loginUser(t testing.TB, userName string) *TestSession { if session, ok := loginSessionCache[userName]; ok { return session @@ -185,13 +192,13 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession ch.Add("Cookie", strings.Join(resp.Headers["Set-Cookie"], ";")) cr := http.Request{Header: ch} - jar, err := cookiejar.New(nil) - assert.NoError(t, err) + session := emptyTestSession(t) + baseURL, err := url.Parse(setting.AppURL) assert.NoError(t, err) - jar.SetCookies(baseURL, cr.Cookies()) + session.jar.SetCookies(baseURL, cr.Cookies()) - return &TestSession{jar: jar} + return session } type TestResponseWriter struct { diff --git a/integrations/signin_test.go b/integrations/signin_test.go new file mode 100644 index 0000000000..9eab5b5f03 --- /dev/null +++ b/integrations/signin_test.go @@ -0,0 +1,60 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "net/http" + "strings" + "testing" + + "code.gitea.io/gitea/models" + + "github.com/Unknwon/i18n" + "github.com/stretchr/testify/assert" +) + +func testLoginFailed(t *testing.T, username, password, message string) { + session := emptyTestSession(t) + req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{ + "_csrf": GetCSRF(t, session, "/user/login"), + "user_name": username, + "password": password, + }) + resp := session.MakeRequest(t, req, http.StatusOK) + + htmlDoc := NewHTMLParser(t, resp.Body) + resultMsg := htmlDoc.doc.Find(".ui.message>p").Text() + + assert.EqualValues(t, message, resultMsg) +} + +func TestSignin(t *testing.T) { + prepareTestEnv(t) + + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + + // add new user with user2's email + user.Name = "testuser" + user.LowerName = strings.ToLower(user.Name) + user.ID = 0 + models.AssertSuccessfulInsert(t, user) + + samples := []struct { + username string + password string + message string + }{ + {username: "wrongUsername", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, + {username: "wrongUsername", password: "password", message: i18n.Tr("en", "form.username_password_incorrect")}, + {username: "user15", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, + {username: "user1@example.com", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")}, + // test for duplicate email + {username: "user2@example.com", password: "password", message: i18n.Tr("en", "form.email_been_used")}, + } + + for _, s := range samples { + testLoginFailed(t, s.username, s.password, s.message) + } +}