mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-26 17:43:04 -04:00
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
Fixes #10881 Call the proper function for each repository the user watches, so adjusting the watch count can be done properly. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10882 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: christopher-besch <mail@chris-besch.com> Co-committed-by: christopher-besch <mail@chris-besch.com>
155 lines
6.4 KiB
Go
155 lines
6.4 KiB
Go
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package user
|
|
|
|
import (
|
|
"testing"
|
|
|
|
model "forgejo.org/models"
|
|
actions_model "forgejo.org/models/actions"
|
|
"forgejo.org/models/db"
|
|
issues_model "forgejo.org/models/issues"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/models/unittest"
|
|
user_model "forgejo.org/models/user"
|
|
actions_module "forgejo.org/modules/actions"
|
|
|
|
"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))
|
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
|
oldNumWatchers := repo.NumWatches
|
|
|
|
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))
|
|
|
|
// Ensure the watcher count was reduced by one.
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
|
require.Equal(t, oldNumWatchers-1, repo.NumWatches)
|
|
})
|
|
|
|
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)
|
|
})
|
|
|
|
t.Run("Issues", func(t *testing.T) {
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: doer.ID})
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4, RepoID: repo.ID}, "is_closed = true")
|
|
|
|
_, err := issues_model.ChangeIssueStatus(db.DefaultContext, issue, blockedUser, false)
|
|
require.NoError(t, err)
|
|
|
|
_, err = issues_model.ChangeIssueStatus(db.DefaultContext, issue, doer, true)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
|
|
|
_, err = issues_model.ChangeIssueStatus(db.DefaultContext, issue, blockedUser, false)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("Pull requests actions are cancelled", func(t *testing.T) {
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2, OwnerID: doer.ID})
|
|
blockedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
defer user_model.UnblockUser(db.DefaultContext, doer.ID, blockedUser.ID)
|
|
|
|
pullRequestPosterID := blockedUser.ID
|
|
singleWorkflows, err := actions_module.JobParser([]byte(`
|
|
jobs:
|
|
job:
|
|
runs-on: docker
|
|
steps:
|
|
- run: echo OK
|
|
`))
|
|
require.NoError(t, err)
|
|
require.Len(t, singleWorkflows, 1)
|
|
runWaiting := &actions_model.ActionRun{
|
|
TriggerUserID: 2,
|
|
RepoID: repo.ID,
|
|
Status: actions_model.StatusWaiting,
|
|
PullRequestPosterID: pullRequestPosterID,
|
|
}
|
|
require.NoError(t, actions_model.InsertRun(t.Context(), runWaiting, singleWorkflows))
|
|
|
|
run := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: runWaiting.ID})
|
|
require.Equal(t, actions_model.StatusWaiting.String(), run.Status.String())
|
|
|
|
require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
|
|
|
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{ID: runWaiting.ID})
|
|
require.Equal(t, actions_model.StatusCancelled.String(), run.Status.String())
|
|
})
|
|
}
|