You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signin_test.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. package integrations
  5. import (
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/models/unittest"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/unknwon/i18n"
  13. )
  14. func testLoginFailed(t *testing.T, username, password, message string) {
  15. session := emptyTestSession(t)
  16. req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
  17. "_csrf": GetCSRF(t, session, "/user/login"),
  18. "user_name": username,
  19. "password": password,
  20. })
  21. resp := session.MakeRequest(t, req, http.StatusOK)
  22. htmlDoc := NewHTMLParser(t, resp.Body)
  23. resultMsg := htmlDoc.doc.Find(".ui.message>p").Text()
  24. assert.EqualValues(t, message, resultMsg)
  25. }
  26. func TestSignin(t *testing.T) {
  27. defer prepareTestEnv(t)()
  28. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  29. // add new user with user2's email
  30. user.Name = "testuser"
  31. user.LowerName = strings.ToLower(user.Name)
  32. user.ID = 0
  33. unittest.AssertSuccessfulInsert(t, user)
  34. samples := []struct {
  35. username string
  36. password string
  37. message string
  38. }{
  39. {username: "wrongUsername", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
  40. {username: "wrongUsername", password: "password", message: i18n.Tr("en", "form.username_password_incorrect")},
  41. {username: "user15", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
  42. {username: "user1@example.com", password: "wrongPassword", message: i18n.Tr("en", "form.username_password_incorrect")},
  43. // test for duplicate email
  44. {username: "user2@example.com", password: "password", message: i18n.Tr("en", "form.email_been_used")},
  45. }
  46. for _, s := range samples {
  47. testLoginFailed(t, s.username, s.password, s.message)
  48. }
  49. }