44package integration
55
66import (
7+ "encoding/base64"
78 "fmt"
89 "net/http"
10+ "net/url"
911 "testing"
12+ "time"
1013
1114 activities_model "code.gitea.io/gitea/models/activities"
1215 auth_model "code.gitea.io/gitea/models/auth"
@@ -15,6 +18,7 @@ import (
1518 "code.gitea.io/gitea/models/unittest"
1619 user_model "code.gitea.io/gitea/models/user"
1720 api "code.gitea.io/gitea/modules/structs"
21+ repo_service "code.gitea.io/gitea/services/repository"
1822 "code.gitea.io/gitea/tests"
1923
2024 "github.com/stretchr/testify/assert"
@@ -213,3 +217,137 @@ func TestAPINotificationPUT(t *testing.T) {
213217 assert .True (t , apiNL [0 ].Unread )
214218 assert .False (t , apiNL [0 ].Pinned )
215219}
220+
221+ func TestAPICommitNotification (t * testing.T ) {
222+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
223+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
224+ repo1 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
225+
226+ session := loginUser (t , user2 .Name )
227+ token1 := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeWriteRepository )
228+
229+ content := "This is a test commit"
230+ contentEncoded := base64 .StdEncoding .EncodeToString ([]byte (content ))
231+ // push a commit with @user2 in the commit message, it's expected to create a notification
232+ createFileOptions := api.CreateFileOptions {
233+ FileOptions : api.FileOptions {
234+ BranchName : "master" ,
235+ NewBranchName : "master" ,
236+ Message : "This is a test commit to mention @user2" ,
237+ Author : api.Identity {
238+ Name : "Anne Doe" ,
239+ 240+ },
241+ Committer : api.Identity {
242+ Name : "John Doe" ,
243+ 244+ },
245+ Dates : api.CommitDateOptions {
246+ Author : time .Unix (946684810 , 0 ),
247+ Committer : time .Unix (978307190 , 0 ),
248+ },
249+ },
250+ ContentBase64 : contentEncoded ,
251+ }
252+
253+ req := NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/contents/new_commit_notification.txt" , user2 .Name , repo1 .Name ), & createFileOptions ).
254+ AddTokenAuth (token1 )
255+ resp := MakeRequest (t , req , http .StatusCreated )
256+
257+ // Check notifications are as expected
258+ token2 := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeWriteNotification )
259+ req = NewRequest (t , "GET" , "/api/v1/notifications?all=true" ).
260+ AddTokenAuth (token2 )
261+ resp = MakeRequest (t , req , http .StatusOK )
262+ var apiNL []api.NotificationThread
263+ DecodeJSON (t , resp , & apiNL )
264+
265+ assert .Equal (t , api .NotifySubjectCommit , apiNL [0 ].Subject .Type )
266+ assert .Equal (t , "This is a test commit to mention @user2" , apiNL [0 ].Subject .Title )
267+ assert .True (t , apiNL [0 ].Unread )
268+ assert .False (t , apiNL [0 ].Pinned )
269+ })
270+ }
271+
272+ func TestAPIReleaseNotification (t * testing.T ) {
273+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
274+ user1 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 1 })
275+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
276+ repo1 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
277+
278+ session1 := loginUser (t , user1 .Name )
279+ token1 := getTokenForLoggedInUser (t , session1 , auth_model .AccessTokenScopeWriteRepository )
280+
281+ // user1 create a release, it's expected to create a notification
282+ createNewReleaseUsingAPI (t , token1 , user2 , repo1 , "v0.0.2" , "" , "v0.0.2 is released" , "test notification release" )
283+
284+ // user2 login to check notifications
285+ session2 := loginUser (t , user2 .Name )
286+
287+ // Check notifications are as expected
288+ token2 := getTokenForLoggedInUser (t , session2 , auth_model .AccessTokenScopeWriteNotification )
289+ req := NewRequest (t , "GET" , "/api/v1/notifications?all=true" ).
290+ AddTokenAuth (token2 )
291+ resp := MakeRequest (t , req , http .StatusOK )
292+ var apiNL []api.NotificationThread
293+ DecodeJSON (t , resp , & apiNL )
294+
295+ assert .Equal (t , api .NotifySubjectRelease , apiNL [0 ].Subject .Type )
296+ assert .Equal (t , "v0.0.2 is released" , apiNL [0 ].Subject .Title )
297+ assert .True (t , apiNL [0 ].Unread )
298+ assert .False (t , apiNL [0 ].Pinned )
299+ })
300+ }
301+
302+ func TestAPIRepoTransferNotification (t * testing.T ) {
303+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
304+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
305+
306+ session1 := loginUser (t , user2 .Name )
307+ token1 := getTokenForLoggedInUser (t , session1 , auth_model .AccessTokenScopeWriteRepository , auth_model .AccessTokenScopeWriteUser )
308+
309+ // create repo to move
310+ repoName := "moveME"
311+ apiRepo := new (api.Repository )
312+ req := NewRequestWithJSON (t , "POST" , "/api/v1/user/repos" , & api.CreateRepoOption {
313+ Name : repoName ,
314+ Description : "repo move around" ,
315+ Private : false ,
316+ Readme : "Default" ,
317+ AutoInit : true ,
318+ }).AddTokenAuth (token1 )
319+ resp := MakeRequest (t , req , http .StatusCreated )
320+ DecodeJSON (t , resp , apiRepo )
321+
322+ defer func () {
323+ _ = repo_service .DeleteRepositoryDirectly (db .DefaultContext , apiRepo .ID )
324+ }()
325+
326+ // repo user1/moveME created, now transfer it to org6
327+ repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : apiRepo .ID })
328+ session2 := loginUser (t , user2 .Name )
329+ token2 := getTokenForLoggedInUser (t , session2 , auth_model .AccessTokenScopeWriteRepository )
330+ req = NewRequestWithJSON (t , "POST" , fmt .Sprintf ("/api/v1/repos/%s/%s/transfer" , repo .OwnerName , repo .Name ), & api.TransferRepoOption {
331+ NewOwner : "org6" ,
332+ TeamIDs : nil ,
333+ }).AddTokenAuth (token2 )
334+ MakeRequest (t , req , http .StatusCreated )
335+
336+ // user5 login to check notifications, because user5 is a member of org6's owners team
337+ user5 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 5 })
338+ session5 := loginUser (t , user5 .Name )
339+
340+ // Check notifications are as expected
341+ token5 := getTokenForLoggedInUser (t , session5 , auth_model .AccessTokenScopeWriteNotification )
342+ req = NewRequest (t , "GET" , "/api/v1/notifications?all=true" ).
343+ AddTokenAuth (token5 )
344+ resp = MakeRequest (t , req , http .StatusOK )
345+ var apiNL []api.NotificationThread
346+ DecodeJSON (t , resp , & apiNL )
347+
348+ assert .Equal (t , api .NotifySubjectRepository , apiNL [0 ].Subject .Type )
349+ assert .Equal (t , "user2/moveME" , apiNL [0 ].Subject .Title )
350+ assert .True (t , apiNL [0 ].Unread )
351+ assert .False (t , apiNL [0 ].Pinned )
352+ })
353+ }
0 commit comments