You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  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. defer 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. defer prepareTestEnv(t)()
  35. session := loginUser(t, "user8")
  36. csrf := GetCSRF(t, session, "/user/settings/account")
  37. urlStr := fmt.Sprintf("/user/settings/account/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. defer prepareTestEnv(t)()
  47. session := loginUser(t, "user2")
  48. csrf := GetCSRF(t, session, "/user/settings/account")
  49. urlStr := fmt.Sprintf("/user/settings/account/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. }