2017-04-25 10:24:51 +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"
"testing"
2017-07-01 22:48:29 +03:00
"code.gitea.io/gitea/models"
2017-12-16 00:11:02 +03:00
"code.gitea.io/gitea/modules/test"
2017-07-01 22:48:29 +03:00
2017-04-25 10:24:51 +03:00
"github.com/stretchr/testify/assert"
2019-08-23 19:40:30 +03:00
"github.com/unknwon/i18n"
2017-04-25 10:24:51 +03:00
)
func TestViewUser ( t * testing . T ) {
2017-04-28 16:23:28 +03:00
prepareTestEnv ( t )
2017-04-25 10:24:51 +03:00
2017-06-10 03:41:36 +03:00
req := NewRequest ( t , "GET" , "/user2" )
2017-07-07 22:36:47 +03:00
MakeRequest ( t , req , http . StatusOK )
2017-04-25 10:24:51 +03:00
}
2017-07-01 22:48:29 +03:00
func TestRenameUsername ( t * testing . T ) {
prepareTestEnv ( t )
session := loginUser ( t , "user2" )
2017-07-07 22:36:47 +03:00
req := NewRequestWithValues ( t , "POST" , "/user/settings" , map [ string ] string {
2018-05-05 03:28:30 +03:00
"_csrf" : GetCSRF ( t , session , "/user/settings" ) ,
"name" : "newUsername" ,
"email" : "user2@example.com" ,
"language" : "en-us" ,
2017-07-01 22:48:29 +03:00
} )
2017-07-07 22:36:47 +03:00
session . MakeRequest ( t , req , http . StatusFound )
2017-07-01 22:48:29 +03:00
models . AssertExistsAndLoadBean ( t , & models . User { Name : "newUsername" } )
models . AssertNotExistsBean ( t , & models . User { Name : "user2" } )
}
func TestRenameInvalidUsername ( t * testing . T ) {
prepareTestEnv ( t )
invalidUsernames := [ ] string {
"%2f*" ,
"%2f." ,
"%2f.." ,
"%00" ,
"thisHas ASpace" ,
2019-01-24 17:12:17 +03:00
"p<A>tho>lo<gical" ,
2017-07-01 22:48:29 +03:00
}
session := loginUser ( t , "user2" )
for _ , invalidUsername := range invalidUsernames {
t . Logf ( "Testing username %s" , invalidUsername )
2017-07-07 22:36:47 +03:00
req := NewRequestWithValues ( t , "POST" , "/user/settings" , map [ string ] string {
"_csrf" : GetCSRF ( t , session , "/user/settings" ) ,
2017-07-01 22:48:29 +03:00
"name" : invalidUsername ,
"email" : "user2@example.com" ,
} )
2017-07-07 22:36:47 +03:00
resp := session . MakeRequest ( t , req , http . StatusOK )
htmlDoc := NewHTMLParser ( t , resp . Body )
2017-07-01 22:48:29 +03:00
assert . Contains ( t ,
htmlDoc . doc . Find ( ".ui.negative.message" ) . Text ( ) ,
i18n . Tr ( "en" , "form.alpha_dash_dot_error" ) ,
)
models . AssertNotExistsBean ( t , & models . User { Name : invalidUsername } )
}
}
func TestRenameReservedUsername ( t * testing . T ) {
prepareTestEnv ( t )
reservedUsernames := [ ] string {
2019-09-24 20:12:56 +03:00
"admin" ,
"api" ,
"attachments" ,
"avatars" ,
"explore" ,
2017-07-01 22:48:29 +03:00
"help" ,
2019-09-24 20:12:56 +03:00
"install" ,
"issues" ,
"login" ,
"metrics" ,
"notifications" ,
"org" ,
"pulls" ,
"repo" ,
2017-07-01 22:48:29 +03:00
"template" ,
2019-09-24 20:12:56 +03:00
"user" ,
2017-07-01 22:48:29 +03:00
}
session := loginUser ( t , "user2" )
for _ , reservedUsername := range reservedUsernames {
t . Logf ( "Testing username %s" , reservedUsername )
2017-07-07 22:36:47 +03:00
req := NewRequestWithValues ( t , "POST" , "/user/settings" , map [ string ] string {
2018-05-05 03:28:30 +03:00
"_csrf" : GetCSRF ( t , session , "/user/settings" ) ,
"name" : reservedUsername ,
"email" : "user2@example.com" ,
"language" : "en-us" ,
2017-07-01 22:48:29 +03:00
} )
2017-07-07 22:36:47 +03:00
resp := session . MakeRequest ( t , req , http . StatusFound )
2017-07-01 22:48:29 +03:00
2017-12-16 00:11:02 +03:00
req = NewRequest ( t , "GET" , test . RedirectURL ( resp ) )
2017-07-07 22:36:47 +03:00
resp = session . MakeRequest ( t , req , http . StatusOK )
htmlDoc := NewHTMLParser ( t , resp . Body )
2017-07-01 22:48:29 +03:00
assert . Contains ( t ,
htmlDoc . doc . Find ( ".ui.negative.message" ) . Text ( ) ,
2018-02-25 16:56:25 +03:00
i18n . Tr ( "en" , "user.form.name_reserved" , reservedUsername ) ,
2017-07-01 22:48:29 +03:00
)
models . AssertNotExistsBean ( t , & models . User { Name : reservedUsername } )
}
}
2019-04-14 19:43:56 +03:00
func TestExportUserGPGKeys ( t * testing . T ) {
prepareTestEnv ( t )
//Export empty key list
testExportUserGPGKeys ( t , "user1" , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
= twTO
-- -- - END PGP PUBLIC KEY BLOCK -- -- -
` )
//Import key
//User1 <user1@example.com>
session := loginUser ( t , "user1" )
token := getTokenForLoggedInUser ( t , session )
testCreateGPGKey ( t , session . MakeRequest , token , http . StatusCreated , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
mQENBFyy / VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh / 6 eFSRrjsusp3YQ / 0 8 NSfPPbcu8
0 M5G + VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
8 SR + lzp5n6ppUakcmRnxt3nGRBj1 + hEGkdgzyPo93iy + WioegY2lwCA9xMEo5dah
BmYxWx51zyiXYlReTaxlyb3 / nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
510 OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAG0GVVzZXIxIDx1c2VyMUBl
eGFtcGxlLmNvbT6JAU4EEwEIADgWIQTQEbrYxmXsp1z3j7z9 + v0I6RSEHwUCXLL9
VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9 + v0I6RSEH22YCACFqL5 +
6 M0m18AMC / pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
u7 + j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj / ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
nsdSgyCkwRZXG + u3kT / wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
96 ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9 + OsfJVC
l7N5xxIawCuTQdbfuQENBFyy / VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
soO / HPj9dPQLTOiwXgSgSCd8C + LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
55 aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU / HVz5y
lPzxUUocgdbSi3GE3zbzijQzVJdyL / kw / KP7pKT / PPKKJ2C5NQDLy0XGKEHddXGR
EWKkVlRalxq / TjfaMR0bi3MpezBsQmp99ATPO / d7trayZUxQHRtXzGFiOXfDHATr
qN730sODjqvU + mpc / SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ / ABEBAAGJATYE
GAEIACAWIQTQEbrYxmXsp1z3j7z9 + v0I6RSEHwUCXLL9VQIbDAAKCRD9 + v0I6RSE
H7WoB / 4 tXl + 97 rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
C0TLXKur6NVYQMn01iyL + FZzRpEWNuYF3f9QeeLJ / + l2DafESNhNTy17 + RPmacK6
21 dccpqchByVw / UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy / n2
0 Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61 + Vr2GUbah6
7 XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy / TKTKhGgZNINj1iaPFyOkQgKR5M
GrE0MHOxUbc9tbtyk0F1SuzREUBH
= DDXw
-- -- - END PGP PUBLIC KEY BLOCK -- -- -
` )
//Export new key
testExportUserGPGKeys ( t , "user1" , ` -- -- - BEGIN PGP PUBLIC KEY BLOCK -- -- -
xsBNBFyy / VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh / 6 eFSRrjsusp3YQ / 0 8 NSfPPbcu8
0 M5G + VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
8 SR + lzp5n6ppUakcmRnxt3nGRBj1 + hEGkdgzyPo93iy + WioegY2lwCA9xMEo5dah
BmYxWx51zyiXYlReTaxlyb3 / nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
510 OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAHNGVVzZXIxIDx1c2VyMUBl
eGFtcGxlLmNvbT7CwI4EEwEIADgWIQTQEbrYxmXsp1z3j7z9 + v0I6RSEHwUCXLL9
VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9 + v0I6RSEH22YCACFqL5 +
6 M0m18AMC / pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
u7 + j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj / ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
nsdSgyCkwRZXG + u3kT / wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
96 ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9 + OsfJVC
l7N5xxIawCuTQdbfzsBNBFyy / VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
soO / HPj9dPQLTOiwXgSgSCd8C + LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
55 aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU / HVz5y
lPzxUUocgdbSi3GE3zbzijQzVJdyL / kw / KP7pKT / PPKKJ2C5NQDLy0XGKEHddXGR
EWKkVlRalxq / TjfaMR0bi3MpezBsQmp99ATPO / d7trayZUxQHRtXzGFiOXfDHATr
qN730sODjqvU + mpc / SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ / ABEBAAHCwHYE
GAEIACAWIQTQEbrYxmXsp1z3j7z9 + v0I6RSEHwUCXLL9VQIbDAAKCRD9 + v0I6RSE
H7WoB / 4 tXl + 97 rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
C0TLXKur6NVYQMn01iyL + FZzRpEWNuYF3f9QeeLJ / + l2DafESNhNTy17 + RPmacK6
21 dccpqchByVw / UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy / n2
0 Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61 + Vr2GUbah6
7 XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy / TKTKhGgZNINj1iaPFyOkQgKR5M
GrE0MHOxUbc9tbtyk0F1SuzREUBH
= WFf5
-- -- - END PGP PUBLIC KEY BLOCK -- -- -
` )
}
func testExportUserGPGKeys ( t * testing . T , user , expected string ) {
session := loginUser ( t , user )
t . Logf ( "Testing username %s export gpg keys" , user )
req := NewRequest ( t , "GET" , "/" + user + ".gpg" )
resp := session . MakeRequest ( t , req , http . StatusOK )
//t.Log(resp.Body.String())
assert . Equal ( t , expected , resp . Body . String ( ) )
}