code.gitea.io/gitea@v1.21.7/services/user/user_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package user
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/models"
    13  	"code.gitea.io/gitea/models/auth"
    14  	"code.gitea.io/gitea/models/db"
    15  	"code.gitea.io/gitea/models/organization"
    16  	repo_model "code.gitea.io/gitea/models/repo"
    17  	"code.gitea.io/gitea/models/unittest"
    18  	user_model "code.gitea.io/gitea/models/user"
    19  	"code.gitea.io/gitea/modules/setting"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestMain(m *testing.M) {
    25  	unittest.MainTest(m, &unittest.TestOptions{
    26  		GiteaRootPath: filepath.Join("..", ".."),
    27  	})
    28  }
    29  
    30  func TestDeleteUser(t *testing.T) {
    31  	test := func(userID int64) {
    32  		assert.NoError(t, unittest.PrepareTestDatabase())
    33  		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
    34  
    35  		ownedRepos := make([]*repo_model.Repository, 0, 10)
    36  		assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&ownedRepos, &repo_model.Repository{OwnerID: userID}))
    37  		if len(ownedRepos) > 0 {
    38  			err := DeleteUser(db.DefaultContext, user, false)
    39  			assert.Error(t, err)
    40  			assert.True(t, models.IsErrUserOwnRepos(err))
    41  			return
    42  		}
    43  
    44  		orgUsers := make([]*organization.OrgUser, 0, 10)
    45  		assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&orgUsers, &organization.OrgUser{UID: userID}))
    46  		for _, orgUser := range orgUsers {
    47  			if err := models.RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil {
    48  				assert.True(t, organization.IsErrLastOrgOwner(err))
    49  				return
    50  			}
    51  		}
    52  		assert.NoError(t, DeleteUser(db.DefaultContext, user, false))
    53  		unittest.AssertNotExistsBean(t, &user_model.User{ID: userID})
    54  		unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{})
    55  	}
    56  	test(2)
    57  	test(4)
    58  	test(8)
    59  	test(11)
    60  
    61  	org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
    62  	assert.Error(t, DeleteUser(db.DefaultContext, org, false))
    63  }
    64  
    65  func TestPurgeUser(t *testing.T) {
    66  	test := func(userID int64) {
    67  		assert.NoError(t, unittest.PrepareTestDatabase())
    68  		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID})
    69  
    70  		err := DeleteUser(db.DefaultContext, user, true)
    71  		assert.NoError(t, err)
    72  
    73  		unittest.AssertNotExistsBean(t, &user_model.User{ID: userID})
    74  		unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{})
    75  	}
    76  	test(2)
    77  	test(4)
    78  	test(8)
    79  	test(11)
    80  
    81  	org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
    82  	assert.Error(t, DeleteUser(db.DefaultContext, org, false))
    83  }
    84  
    85  func TestCreateUser(t *testing.T) {
    86  	user := &user_model.User{
    87  		Name:               "GiteaBot",
    88  		Email:              "GiteaBot@gitea.io",
    89  		Passwd:             ";p['////..-++']",
    90  		IsAdmin:            false,
    91  		Theme:              setting.UI.DefaultTheme,
    92  		MustChangePassword: false,
    93  	}
    94  
    95  	assert.NoError(t, user_model.CreateUser(db.DefaultContext, user))
    96  
    97  	assert.NoError(t, DeleteUser(db.DefaultContext, user, false))
    98  }
    99  
   100  func TestRenameUser(t *testing.T) {
   101  	assert.NoError(t, unittest.PrepareTestDatabase())
   102  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 21})
   103  
   104  	t.Run("Non-Local", func(t *testing.T) {
   105  		u := &user_model.User{
   106  			Type:      user_model.UserTypeIndividual,
   107  			LoginType: auth.OAuth2,
   108  		}
   109  		assert.ErrorIs(t, RenameUser(db.DefaultContext, u, "user_rename"), user_model.ErrUserIsNotLocal{})
   110  	})
   111  
   112  	t.Run("Same username", func(t *testing.T) {
   113  		assert.ErrorIs(t, RenameUser(db.DefaultContext, user, user.Name), user_model.ErrUsernameNotChanged{UID: user.ID, Name: user.Name})
   114  	})
   115  
   116  	t.Run("Non usable username", func(t *testing.T) {
   117  		usernames := []string{"--diff", "aa.png", ".well-known", "search", "aaa.atom"}
   118  		for _, username := range usernames {
   119  			t.Run(username, func(t *testing.T) {
   120  				assert.Error(t, user_model.IsUsableUsername(username))
   121  				assert.Error(t, RenameUser(db.DefaultContext, user, username))
   122  			})
   123  		}
   124  	})
   125  
   126  	t.Run("Only capitalization", func(t *testing.T) {
   127  		caps := strings.ToUpper(user.Name)
   128  		unittest.AssertNotExistsBean(t, &user_model.User{ID: user.ID, Name: caps})
   129  		unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: user.Name})
   130  
   131  		assert.NoError(t, RenameUser(db.DefaultContext, user, caps))
   132  
   133  		unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: caps})
   134  		unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: caps})
   135  	})
   136  
   137  	t.Run("Already exists", func(t *testing.T) {
   138  		existUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
   139  
   140  		assert.ErrorIs(t, RenameUser(db.DefaultContext, user, existUser.Name), user_model.ErrUserAlreadyExist{Name: existUser.Name})
   141  		assert.ErrorIs(t, RenameUser(db.DefaultContext, user, existUser.LowerName), user_model.ErrUserAlreadyExist{Name: existUser.LowerName})
   142  		newUsername := fmt.Sprintf("uSEr%d", existUser.ID)
   143  		assert.ErrorIs(t, RenameUser(db.DefaultContext, user, newUsername), user_model.ErrUserAlreadyExist{Name: newUsername})
   144  	})
   145  
   146  	t.Run("Normal", func(t *testing.T) {
   147  		oldUsername := user.Name
   148  		newUsername := "User_Rename"
   149  
   150  		assert.NoError(t, RenameUser(db.DefaultContext, user, newUsername))
   151  		unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: newUsername, LowerName: strings.ToLower(newUsername)})
   152  
   153  		redirectUID, err := user_model.LookupUserRedirect(db.DefaultContext, oldUsername)
   154  		assert.NoError(t, err)
   155  		assert.EqualValues(t, user.ID, redirectUID)
   156  
   157  		unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: user.Name})
   158  	})
   159  }
   160  
   161  func TestCreateUser_Issue5882(t *testing.T) {
   162  	// Init settings
   163  	_ = setting.Admin
   164  
   165  	passwd := ".//.;1;;//.,-=_"
   166  
   167  	tt := []struct {
   168  		user               *user_model.User
   169  		disableOrgCreation bool
   170  	}{
   171  		{&user_model.User{Name: "GiteaBot", Email: "GiteaBot@gitea.io", Passwd: passwd, MustChangePassword: false}, false},
   172  		{&user_model.User{Name: "GiteaBot2", Email: "GiteaBot2@gitea.io", Passwd: passwd, MustChangePassword: false}, true},
   173  	}
   174  
   175  	setting.Service.DefaultAllowCreateOrganization = true
   176  
   177  	for _, v := range tt {
   178  		setting.Admin.DisableRegularOrgCreation = v.disableOrgCreation
   179  
   180  		assert.NoError(t, user_model.CreateUser(db.DefaultContext, v.user))
   181  
   182  		u, err := user_model.GetUserByEmail(db.DefaultContext, v.user.Email)
   183  		assert.NoError(t, err)
   184  
   185  		assert.Equal(t, !u.AllowCreateOrganization, v.disableOrgCreation)
   186  
   187  		assert.NoError(t, DeleteUser(db.DefaultContext, v.user, false))
   188  	}
   189  }