@@ -7,6 +7,8 @@ package models
77import (
88 "testing"
99
10+ "code.gitea.io/gitea/modules/setting"
11+
1012 "github.com/stretchr/testify/assert"
1113)
1214
@@ -15,8 +17,10 @@ func TestIsWatching(t *testing.T) {
1517
1618 assert .True (t , IsWatching (1 , 1 ))
1719 assert .True (t , IsWatching (4 , 1 ))
20+ assert .True (t , IsWatching (11 , 1 ))
1821
1922 assert .False (t , IsWatching (1 , 5 ))
23+ assert .False (t , IsWatching (8 , 1 ))
2024 assert .False (t , IsWatching (NonexistentID , NonexistentID ))
2125}
2226
@@ -78,7 +82,7 @@ func TestNotifyWatchers(t *testing.T) {
7882 }
7983 assert .NoError (t , NotifyWatchers (action ))
8084
81- // One watchers are inactive, thus action is only created for user 8, 1, 4
85+ // One watchers are inactive, thus action is only created for user 8, 1, 4, 11
8286 AssertExistsAndLoadBean (t , & Action {
8387 ActUserID : action .ActUserID ,
8488 UserID : 8 ,
@@ -97,4 +101,106 @@ func TestNotifyWatchers(t *testing.T) {
97101 RepoID : action .RepoID ,
98102 OpType : action .OpType ,
99103 })
104+ AssertExistsAndLoadBean (t , & Action {
105+ ActUserID : action .ActUserID ,
106+ UserID : 11 ,
107+ RepoID : action .RepoID ,
108+ OpType : action .OpType ,
109+ })
110+ }
111+
112+ func TestWatchIfAuto (t * testing.T ) {
113+ assert .NoError (t , PrepareTestDatabase ())
114+
115+ repo := AssertExistsAndLoadBean (t , & Repository {ID : 1 }).(* Repository )
116+ watchers , err := repo .GetWatchers (1 )
117+ assert .NoError (t , err )
118+ assert .Len (t , watchers , repo .NumWatches )
119+
120+ setting .Service .AutoWatchOnClone = false
121+ setting .Service .AutoWatchOnChanges = false
122+
123+ prevCount := repo .NumWatches
124+
125+ // Must not add watch
126+ assert .NoError (t , WatchIfAuto (8 , 1 , false ))
127+ watchers , err = repo .GetWatchers (1 )
128+ assert .NoError (t , err )
129+ assert .Len (t , watchers , prevCount )
130+
131+ // Should not add watch
132+ assert .NoError (t , WatchIfAuto (10 , 1 , false ))
133+ watchers , err = repo .GetWatchers (1 )
134+ assert .NoError (t , err )
135+ assert .Len (t , watchers , prevCount )
136+
137+ setting .Service .AutoWatchOnClone = true
138+
139+ // Should not add watch
140+ assert .NoError (t , WatchIfAuto (10 , 1 , true ))
141+ watchers , err = repo .GetWatchers (1 )
142+ assert .NoError (t , err )
143+ assert .Len (t , watchers , prevCount )
144+
145+ // Should add watch
146+ assert .NoError (t , WatchIfAuto (10 , 1 , false ))
147+ watchers , err = repo .GetWatchers (1 )
148+ assert .NoError (t , err )
149+ assert .Len (t , watchers , prevCount + 1 )
150+
151+ prevCount ++
152+
153+ setting .Service .AutoWatchOnClone = false
154+ setting .Service .AutoWatchOnChanges = true
155+
156+ // Must not add watch
157+ assert .NoError (t , WatchIfAuto (8 , 1 , false ))
158+ watchers , err = repo .GetWatchers (1 )
159+ assert .NoError (t , err )
160+ assert .Len (t , watchers , prevCount )
161+
162+ // Should not add watch
163+ assert .NoError (t , WatchIfAuto (12 , 1 , false ))
164+ watchers , err = repo .GetWatchers (1 )
165+ assert .NoError (t , err )
166+ assert .Len (t , watchers , prevCount )
167+
168+ // Should add watch
169+ assert .NoError (t , WatchIfAuto (12 , 1 , true ))
170+ watchers , err = repo .GetWatchers (1 )
171+ assert .NoError (t , err )
172+ assert .Len (t , watchers , prevCount + 1 )
173+
174+ // Should remove watch, inhibit from adding auto
175+ assert .NoError (t , WatchRepo (12 , 1 , false ))
176+ watchers , err = repo .GetWatchers (1 )
177+ assert .NoError (t , err )
178+ assert .Len (t , watchers , prevCount )
179+
180+ // Must not add watch
181+ assert .NoError (t , WatchIfAuto (12 , 1 , false ))
182+ watchers , err = repo .GetWatchers (1 )
183+ assert .NoError (t , err )
184+ assert .Len (t , watchers , prevCount )
185+ }
186+
187+ func TestWatchRepoMode (t * testing.T ) {
188+ assert .NoError (t , PrepareTestDatabase ())
189+
190+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 }, 0 )
191+
192+ assert .NoError (t , WatchRepoMode (12 , 1 , RepoWatchModeAuto ))
193+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 }, 1 )
194+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 , Mode : RepoWatchModeAuto }, 1 )
195+
196+ assert .NoError (t , WatchRepoMode (12 , 1 , RepoWatchModeNormal ))
197+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 }, 1 )
198+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 , Mode : RepoWatchModeNormal }, 1 )
199+
200+ assert .NoError (t , WatchRepoMode (12 , 1 , RepoWatchModeDont ))
201+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 }, 1 )
202+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 , Mode : RepoWatchModeDont }, 1 )
203+
204+ assert .NoError (t , WatchRepoMode (12 , 1 , RepoWatchModeNone ))
205+ AssertCount (t , & Watch {UserID : 12 , RepoID : 1 }, 0 )
100206}
0 commit comments