Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

delete_user_test.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. )
  11. func assertUserDeleted(t *testing.T, userID int64) {
  12. models.AssertNotExistsBean(t, &models.User{ID: userID})
  13. models.AssertNotExistsBean(t, &models.Follow{UserID: userID})
  14. models.AssertNotExistsBean(t, &models.Follow{FollowID: userID})
  15. models.AssertNotExistsBean(t, &models.Repository{OwnerID: userID})
  16. models.AssertNotExistsBean(t, &models.Access{UserID: userID})
  17. models.AssertNotExistsBean(t, &models.OrgUser{UID: userID})
  18. models.AssertNotExistsBean(t, &models.IssueUser{UID: userID})
  19. models.AssertNotExistsBean(t, &models.TeamUser{UID: userID})
  20. models.AssertNotExistsBean(t, &models.Star{UID: userID})
  21. }
  22. func TestAdminDeleteUser(t *testing.T) {
  23. prepareTestEnv(t)
  24. session := loginUser(t, "user1")
  25. csrf := GetCSRF(t, session, "/admin/users/8")
  26. req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
  27. "_csrf": csrf,
  28. })
  29. session.MakeRequest(t, req, http.StatusOK)
  30. assertUserDeleted(t, 8)
  31. models.CheckConsistencyFor(t, &models.User{})
  32. }
  33. func TestUserDeleteAccount(t *testing.T) {
  34. prepareTestEnv(t)
  35. session := loginUser(t, "user8")
  36. csrf := GetCSRF(t, session, "/user/settings/delete")
  37. urlStr := fmt.Sprintf("/user/settings/delete?password=%s", userPassword)
  38. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  39. "_csrf": csrf,
  40. })
  41. session.MakeRequest(t, req, http.StatusFound)
  42. assertUserDeleted(t, 8)
  43. models.CheckConsistencyFor(t, &models.User{})
  44. }
  45. func TestUserDeleteAccountStillOwnRepos(t *testing.T) {
  46. prepareTestEnv(t)
  47. session := loginUser(t, "user2")
  48. csrf := GetCSRF(t, session, "/user/settings/delete")
  49. urlStr := fmt.Sprintf("/user/settings/delete?password=%s", userPassword)
  50. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  51. "_csrf": csrf,
  52. })
  53. session.MakeRequest(t, req, http.StatusFound)
  54. // user should not have been deleted, because the user still owns repos
  55. models.AssertExistsAndLoadBean(t, &models.User{ID: 2})
  56. }