8d3fab8944
This solves two bugs. One bug is that due to the JOIN with the `forgejo_blocked_users` table, duplicated users were generated if a user had more than one user blocked, this lead to receiving more than one entry in the actions table. The other bug is that if a user blocked more than one user, it would still receive a action entry by a blocked user, because the SQL query would not exclude the other duplicated users that was generated by the JOIN. The new solution is somewhat non-optimal in my eyes, but it's better than rewriting the query to become a potential perfomance blocker (usage of WHERE IN, which cannot be rewritten to a JOIN). It simply removes the watchers after it was retrieved by the SQL query. (cherry picked from commit c63c00b39b8bd2ed3a69ed044933a9626bfca2c1) (cherry picked from commit ad3cdc5705e00961426b2cff499425e30d9332fa)
152 lines
5.9 KiB
Go
152 lines
5.9 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIsWatching(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 1, 1))
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 4, 1))
|
|
assert.True(t, repo_model.IsWatching(db.DefaultContext, 11, 1))
|
|
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, 1, 5))
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, 8, 1))
|
|
assert.False(t, repo_model.IsWatching(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID))
|
|
}
|
|
|
|
func TestGetWatchers(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
watches, err := repo_model.GetWatchers(db.DefaultContext, repo.ID)
|
|
assert.NoError(t, err)
|
|
// One watchers are inactive, thus minus 1
|
|
assert.Len(t, watches, repo.NumWatches-1)
|
|
for _, watch := range watches {
|
|
assert.EqualValues(t, repo.ID, watch.RepoID)
|
|
}
|
|
|
|
watches, err = repo_model.GetWatchers(db.DefaultContext, unittest.NonexistentID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watches, 0)
|
|
}
|
|
|
|
func TestRepository_GetWatchers(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
for _, watcher := range watchers {
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: watcher.ID, RepoID: repo.ID})
|
|
}
|
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 9})
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, 0)
|
|
}
|
|
|
|
func TestWatchIfAuto(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
watchers, err := repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
setting.Service.AutoWatchOnChanges = false
|
|
|
|
prevCount := repo.NumWatches
|
|
|
|
// Must not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
// Should not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 10, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
setting.Service.AutoWatchOnChanges = true
|
|
|
|
// Must not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 8, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
// Should not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, false))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
// Should add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount+1)
|
|
|
|
// Should remove watch, inhibit from adding auto
|
|
assert.NoError(t, repo_model.WatchRepo(db.DefaultContext, 12, 1, false))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
// Must not add watch
|
|
assert.NoError(t, repo_model.WatchIfAuto(db.DefaultContext, 12, 1, true))
|
|
watchers, err = repo_model.GetRepoWatchers(db.DefaultContext, repo.ID, db.ListOptions{Page: 1})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, watchers, prevCount)
|
|
}
|
|
|
|
func TestWatchRepoMode(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
|
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeAuto))
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeAuto}, 1)
|
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeNormal))
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeNormal}, 1)
|
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeDont))
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 1)
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1, Mode: repo_model.WatchModeDont}, 1)
|
|
|
|
assert.NoError(t, repo_model.WatchRepoMode(db.DefaultContext, 12, 1, repo_model.WatchModeNone))
|
|
unittest.AssertCount(t, &repo_model.Watch{UserID: 12, RepoID: 1}, 0)
|
|
}
|
|
|
|
func TestUnwatchRepos(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: 4, RepoID: 1})
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Watch{UserID: 4, RepoID: 2})
|
|
|
|
err := repo_model.UnwatchRepos(db.DefaultContext, 4, []int64{1, 2})
|
|
assert.NoError(t, err)
|
|
|
|
unittest.AssertNotExistsBean(t, &repo_model.Watch{UserID: 4, RepoID: 1})
|
|
unittest.AssertNotExistsBean(t, &repo_model.Watch{UserID: 4, RepoID: 2})
|
|
}
|