2017-08-23 10:30:33 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
"net/http"
"strings"
"testing"
"code.gitea.io/gitea/models"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2017-08-23 10:30:33 +03:00
"github.com/stretchr/testify/assert"
2019-08-23 19:40:30 +03:00
"github.com/unknwon/i18n"
2017-08-23 10:30:33 +03:00
)
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 ) {
2019-11-26 02:21:37 +03:00
defer prepareTestEnv ( t ) ( )
2017-08-23 10:30:33 +03:00
2021-09-19 14:49:59 +03:00
user := db . AssertExistsAndLoadBean ( t , & models . User { ID : 2 } ) . ( * models . User )
2017-08-23 10:30:33 +03:00
// add new user with user2's email
user . Name = "testuser"
user . LowerName = strings . ToLower ( user . Name )
user . ID = 0
2021-09-19 14:49:59 +03:00
db . AssertSuccessfulInsert ( t , user )
2017-08-23 10:30:33 +03:00
samples := [ ] struct {
username string
password string
message string
} {
{ username : "wrongUsername" , password : "wrongPassword" , message : i18n . Tr ( "en" , "form.username_password_incorrect" ) } ,
{ username : "wrongUsername" , password : "password" , message : i18n . Tr ( "en" , "form.username_password_incorrect" ) } ,
{ username : "user15" , password : "wrongPassword" , message : i18n . Tr ( "en" , "form.username_password_incorrect" ) } ,
{ username : "user1@example.com" , password : "wrongPassword" , message : i18n . Tr ( "en" , "form.username_password_incorrect" ) } ,
// test for duplicate email
{ username : "user2@example.com" , password : "password" , message : i18n . Tr ( "en" , "form.email_been_used" ) } ,
}
for _ , s := range samples {
testLoginFailed ( t , s . username , s . password , s . message )
}
}