2017-08-23 03:30:33 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-08-23 03:30:33 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-08-23 03:30:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-10-13 20:56:41 -04:00
|
|
|
"net/url"
|
2017-08-23 03:30:33 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-10-13 20:56:41 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-26 10:19:22 -04:00
|
|
|
"code.gitea.io/gitea/modules/translation"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-08-23 03:30:33 -04:00
|
|
|
|
|
|
|
"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) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-08-23 03:30:33 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2017-08-23 03:30:33 -04:00
|
|
|
|
|
|
|
// add new user with user2's email
|
|
|
|
user.Name = "testuser"
|
|
|
|
user.LowerName = strings.ToLower(user.Name)
|
|
|
|
user.ID = 0
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertSuccessfulInsert(t, user)
|
2017-08-23 03:30:33 -04:00
|
|
|
|
|
|
|
samples := []struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
message string
|
|
|
|
}{
|
2022-06-26 10:19:22 -04:00
|
|
|
{username: "wrongUsername", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
|
|
|
|
{username: "wrongUsername", password: "password", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
|
|
|
|
{username: "user15", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
|
|
|
|
{username: "user1@example.com", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
|
2017-08-23 03:30:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range samples {
|
|
|
|
testLoginFailed(t, s.username, s.password, s.message)
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 20:56:41 -04:00
|
|
|
|
|
|
|
func TestSigninWithRememberMe(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
baseURL, _ := url.Parse(setting.AppURL)
|
|
|
|
|
|
|
|
session := emptyTestSession(t)
|
|
|
|
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, session, "/user/login"),
|
|
|
|
"user_name": user.Name,
|
|
|
|
"password": userPassword,
|
|
|
|
"remember": "on",
|
|
|
|
})
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
c := session.GetCookie(setting.CookieRememberName)
|
|
|
|
assert.NotNil(t, c)
|
|
|
|
|
|
|
|
session = emptyTestSession(t)
|
|
|
|
|
|
|
|
// Without session the settings page should not be reachable
|
|
|
|
req = NewRequest(t, "GET", "/user/settings")
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
req = NewRequest(t, "GET", "/user/login")
|
|
|
|
// Set the remember me cookie for the login GET request
|
|
|
|
session.jar.SetCookies(baseURL, []*http.Cookie{c})
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
// With session the settings page should be reachable
|
|
|
|
req = NewRequest(t, "GET", "/user/settings")
|
|
|
|
session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
}
|