2017-02-17 03:02:11 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
package repo
|
2017-02-17 03:02:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-11-10 04:22:19 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2017-02-17 03:02:11 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsWatching(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 03:02:11 -05:00
|
|
|
|
|
|
|
assert.True(t, IsWatching(1, 1))
|
|
|
|
assert.True(t, IsWatching(4, 1))
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.True(t, IsWatching(11, 1))
|
2017-02-17 03:02:11 -05:00
|
|
|
|
|
|
|
assert.False(t, IsWatching(1, 5))
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.False(t, IsWatching(8, 1))
|
2021-11-16 03:53:21 -05:00
|
|
|
assert.False(t, IsWatching(unittest.NonexistentID, unittest.NonexistentID))
|
2017-02-17 03:02:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWatchers(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 03:02:11 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
watches, err := GetWatchers(db.DefaultContext, repo.ID)
|
2017-02-17 03:02:11 -05:00
|
|
|
assert.NoError(t, err)
|
2017-10-24 13:36:19 -04:00
|
|
|
// One watchers are inactive, thus minus 1
|
|
|
|
assert.Len(t, watches, repo.NumWatches-1)
|
2017-02-17 03:02:11 -05:00
|
|
|
for _, watch := range watches {
|
|
|
|
assert.EqualValues(t, repo.ID, watch.RepoID)
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
watches, err = GetWatchers(db.DefaultContext, unittest.NonexistentID)
|
2017-02-17 03:02:11 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watches, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepository_GetWatchers(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-17 03:02:11 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2017-02-17 03:02:11 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
for _, watcher := range watchers {
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID})
|
2017-02-17 03:02:11 -05:00
|
|
|
}
|
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
repo = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2017-02-17 03:02:11 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, 0)
|
|
|
|
}
|
|
|
|
|
2019-11-10 04:22:19 -05:00
|
|
|
func TestWatchIfAuto(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err := GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, repo.NumWatches)
|
|
|
|
|
|
|
|
setting.Service.AutoWatchOnChanges = false
|
|
|
|
|
|
|
|
prevCount := repo.NumWatches
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 8, 1, true))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should not add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 10, 1, true))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
setting.Service.AutoWatchOnChanges = true
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 8, 1, true))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should not add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, false))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Should add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, true))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount+1)
|
|
|
|
|
|
|
|
// Should remove watch, inhibit from adding auto
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchRepo(db.DefaultContext, 12, 1, false))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
|
|
|
|
// Must not add watch
|
2022-05-20 10:08:52 -04:00
|
|
|
assert.NoError(t, WatchIfAuto(db.DefaultContext, 12, 1, true))
|
2021-12-09 20:27:50 -05:00
|
|
|
watchers, err = GetRepoWatchers(repo.ID, db.ListOptions{Page: 1})
|
2019-11-10 04:22:19 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, watchers, prevCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRepoMode(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
assert.NoError(t, WatchRepoMode(12, 1, WatchModeAuto))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
|
2021-12-12 10:48:20 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeAuto}, 1)
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
assert.NoError(t, WatchRepoMode(12, 1, WatchModeNormal))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
|
2021-12-12 10:48:20 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeNormal}, 1)
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
assert.NoError(t, WatchRepoMode(12, 1, WatchModeDont))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1)
|
2021-12-12 10:48:20 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: WatchModeDont}, 1)
|
2019-11-10 04:22:19 -05:00
|
|
|
|
2021-12-12 10:48:20 -05:00
|
|
|
assert.NoError(t, WatchRepoMode(12, 1, WatchModeNone))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0)
|
2017-02-17 03:02:11 -05:00
|
|
|
}
|