|
| 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