code.gitea.io/gitea@v1.21.7/tests/integration/signin_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"code.gitea.io/gitea/models/unittest"
    12  	user_model "code.gitea.io/gitea/models/user"
    13  	"code.gitea.io/gitea/modules/translation"
    14  	"code.gitea.io/gitea/tests"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func testLoginFailed(t *testing.T, username, password, message string) {
    20  	session := emptyTestSession(t)
    21  	req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
    22  		"_csrf":     GetCSRF(t, session, "/user/login"),
    23  		"user_name": username,
    24  		"password":  password,
    25  	})
    26  	resp := session.MakeRequest(t, req, http.StatusOK)
    27  
    28  	htmlDoc := NewHTMLParser(t, resp.Body)
    29  	resultMsg := htmlDoc.doc.Find(".ui.message>p").Text()
    30  
    31  	assert.EqualValues(t, message, resultMsg)
    32  }
    33  
    34  func TestSignin(t *testing.T) {
    35  	defer tests.PrepareTestEnv(t)()
    36  
    37  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    38  
    39  	// add new user with user2's email
    40  	user.Name = "testuser"
    41  	user.LowerName = strings.ToLower(user.Name)
    42  	user.ID = 0
    43  	unittest.AssertSuccessfulInsert(t, user)
    44  
    45  	samples := []struct {
    46  		username string
    47  		password string
    48  		message  string
    49  	}{
    50  		{username: "wrongUsername", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
    51  		{username: "wrongUsername", password: "password", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
    52  		{username: "user15", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
    53  		{username: "user1@example.com", password: "wrongPassword", message: translation.NewLocale("en-US").Tr("form.username_password_incorrect")},
    54  	}
    55  
    56  	for _, s := range samples {
    57  		testLoginFailed(t, s.username, s.password, s.message)
    58  	}
    59  }