Browse Source

Add integration test for user deleting account (#2891)

Ethan Koenig 1 year ago
parent
commit
47f40ccd5e
1 changed files with 45 additions and 2 deletions
  1. 45 2
      integrations/delete_user_test.go

+ 45 - 2
integrations/delete_user_test.go

@@ -5,13 +5,26 @@
5 5
 package integrations
6 6
 
7 7
 import (
8
+	"fmt"
8 9
 	"net/http"
9 10
 	"testing"
10 11
 
11 12
 	"code.gitea.io/gitea/models"
12 13
 )
13 14
 
14
-func TestDeleteUser(t *testing.T) {
15
+func assertUserDeleted(t *testing.T, userID int64) {
16
+	models.AssertNotExistsBean(t, &models.User{ID: userID})
17
+	models.AssertNotExistsBean(t, &models.Follow{UserID: userID})
18
+	models.AssertNotExistsBean(t, &models.Follow{FollowID: userID})
19
+	models.AssertNotExistsBean(t, &models.Repository{OwnerID: userID})
20
+	models.AssertNotExistsBean(t, &models.Access{UserID: userID})
21
+	models.AssertNotExistsBean(t, &models.OrgUser{UID: userID})
22
+	models.AssertNotExistsBean(t, &models.IssueUser{UID: userID})
23
+	models.AssertNotExistsBean(t, &models.TeamUser{UID: userID})
24
+	models.AssertNotExistsBean(t, &models.Star{UID: userID})
25
+}
26
+
27
+func TestAdminDeleteUser(t *testing.T) {
15 28
 	prepareTestEnv(t)
16 29
 
17 30
 	session := loginUser(t, "user1")
@@ -22,6 +35,36 @@ func TestDeleteUser(t *testing.T) {
22 35
 	})
23 36
 	session.MakeRequest(t, req, http.StatusOK)
24 37
 
25
-	models.AssertNotExistsBean(t, &models.User{ID: 8})
38
+	assertUserDeleted(t, 8)
26 39
 	models.CheckConsistencyFor(t, &models.User{})
27 40
 }
41
+
42
+func TestUserDeleteAccount(t *testing.T) {
43
+	prepareTestEnv(t)
44
+
45
+	session := loginUser(t, "user8")
46
+	csrf := GetCSRF(t, session, "/user/settings/delete")
47
+	urlStr := fmt.Sprintf("/user/settings/delete?password=%s", userPassword)
48
+	req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
49
+		"_csrf": csrf,
50
+	})
51
+	session.MakeRequest(t, req, http.StatusFound)
52
+
53
+	assertUserDeleted(t, 8)
54
+	models.CheckConsistencyFor(t, &models.User{})
55
+}
56
+
57
+func TestUserDeleteAccountStillOwnRepos(t *testing.T) {
58
+	prepareTestEnv(t)
59
+
60
+	session := loginUser(t, "user2")
61
+	csrf := GetCSRF(t, session, "/user/settings/delete")
62
+	urlStr := fmt.Sprintf("/user/settings/delete?password=%s", userPassword)
63
+	req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
64
+		"_csrf": csrf,
65
+	})
66
+	session.MakeRequest(t, req, http.StatusFound)
67
+
68
+	// user should not have been deleted, because the user still owns repos
69
+	models.AssertExistsAndLoadBean(t, &models.User{ID: 2})
70
+}