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.

60 lines
1.8 KiB

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