summaryrefslogtreecommitdiffstats
path: root/services/user/block_test.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /services/user/block_test.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'services/user/block_test.go')
-rw-r--r--services/user/block_test.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/services/user/block_test.go b/services/user/block_test.go
new file mode 100644
index 0000000..f9e95ed
--- /dev/null
+++ b/services/user/block_test.go
@@ -0,0 +1,92 @@
+// Copyright 2023 The Forgejo Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package user
+
+import (
+ "testing"
+
+ model "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/models/db"
+ repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// TestBlockUser will ensure that when you block a user, certain actions have
+// been taken, like unfollowing each other etc.
+func TestBlockUser(t *testing.T) {
+ require.NoError(t, unittest.PrepareTestDatabase())
+
+ doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5})
+ blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+
+ t.Run("Follow", func(t *testing.T) {
+ defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
+
+ // Follow each other.
+ require.NoError(t, user_model.FollowUser(db.DefaultContext, doer.ID, blockedUser.ID))
+ require.NoError(t, user_model.FollowUser(db.DefaultContext, blockedUser.ID, doer.ID))
+
+ require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
+
+ // Ensure they aren't following each other anymore.
+ assert.False(t, user_model.IsFollowing(db.DefaultContext, doer.ID, blockedUser.ID))
+ assert.False(t, user_model.IsFollowing(db.DefaultContext, blockedUser.ID, doer.ID))
+ })
+
+ t.Run("Watch", func(t *testing.T) {
+ defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
+
+ // Blocked user watch repository of doer.
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
+ require.NoError(t, repo_model.WatchRepo(db.DefaultContext, blockedUser.ID, repo.ID, true))
+
+ require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
+
+ // Ensure blocked user isn't following doer's repository.
+ assert.False(t, repo_model.IsWatching(db.DefaultContext, blockedUser.ID, repo.ID))
+ })
+
+ t.Run("Collaboration", func(t *testing.T) {
+ doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 16})
+ blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 18})
+ repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 22, OwnerID: doer.ID})
+ repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 21, OwnerID: doer.ID})
+ defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
+
+ isBlockedUserCollab := func(repo *repo_model.Repository) bool {
+ isCollaborator, err := repo_model.IsCollaborator(db.DefaultContext, repo.ID, blockedUser.ID)
+ require.NoError(t, err)
+ return isCollaborator
+ }
+
+ assert.True(t, isBlockedUserCollab(repo1))
+ assert.True(t, isBlockedUserCollab(repo2))
+
+ require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
+
+ assert.False(t, isBlockedUserCollab(repo1))
+ assert.False(t, isBlockedUserCollab(repo2))
+ })
+
+ t.Run("Pending transfers", func(t *testing.T) {
+ doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
+ blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
+ defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
+
+ unittest.AssertExistsIf(t, true, &repo_model.Repository{ID: 3, OwnerID: blockedUser.ID, Status: repo_model.RepositoryPendingTransfer})
+ unittest.AssertExistsIf(t, true, &model.RepoTransfer{ID: 1, RecipientID: doer.ID, DoerID: blockedUser.ID})
+
+ require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
+
+ unittest.AssertExistsIf(t, false, &model.RepoTransfer{ID: 1, RecipientID: doer.ID, DoerID: blockedUser.ID})
+
+ // Don't use AssertExistsIf, as it doesn't include the zero values in the condition such as `repo_model.RepositoryReady`.
+ repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3, OwnerID: blockedUser.ID})
+ assert.Equal(t, repo_model.RepositoryReady, repo.Status)
+ })
+}