2020-02-04 08:03:15 +00:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
2020-03-30 19:57:56 +01:00
|
|
|
"time"
|
2020-02-04 08:03:15 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNotifications(t *testing.T) {
|
|
|
|
log.Println("== TestNotifications ==")
|
|
|
|
|
|
|
|
// init user2
|
|
|
|
c := newTestClient()
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
user1, _, err := c.GetMyUserInfo()
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user2 := createTestUser(t, "notify2", c)
|
|
|
|
|
2022-03-29 00:09:57 +01:00
|
|
|
// create 2 repos
|
2020-02-04 08:03:15 +00:00
|
|
|
repoA, err := createTestRepo(t, "TestNotifications_A", c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
c.sudo = user2.UserName
|
|
|
|
repoB, err := createTestRepo(t, "TestNotifications_B", c)
|
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.WatchRepo(user1.UserName, repoA.Name)
|
2020-02-04 08:03:15 +00:00
|
|
|
c.sudo = ""
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
c.sudo = user2.UserName
|
2022-05-15 16:47:38 +01:00
|
|
|
notifications, _, err := c.ReadNotifications(MarkNotificationOptions{})
|
2020-09-14 03:37:09 +01:00
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
assert.Len(t, notifications, 0)
|
2020-09-14 03:37:09 +01:00
|
|
|
count, _, err := c.CheckNotifications()
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.EqualValues(t, 0, count)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
c.sudo = ""
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err = c.CreateIssue(repoA.Owner.UserName, repoA.Name, CreateIssueOption{Title: "A Issue", Closed: false})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2024-01-06 05:14:12 +00:00
|
|
|
time.Sleep(time.Second * 5)
|
2020-02-04 08:03:15 +00:00
|
|
|
|
|
|
|
// CheckNotifications of user2
|
|
|
|
c.sudo = user2.UserName
|
2020-09-14 03:37:09 +01:00
|
|
|
count, _, err = c.CheckNotifications()
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 2, count)
|
|
|
|
|
|
|
|
// ListNotifications
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err := c.ListNotifications(ListNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nList, 2)
|
|
|
|
for _, n := range nList {
|
|
|
|
assert.EqualValues(t, true, n.Unread)
|
|
|
|
assert.EqualValues(t, "Issue", n.Subject.Type)
|
2021-06-30 22:06:50 +01:00
|
|
|
assert.EqualValues(t, NotifySubjectOpen, nList[0].Subject.State)
|
|
|
|
assert.EqualValues(t, NotifySubjectOpen, nList[1].Subject.State)
|
2020-02-04 08:03:15 +00:00
|
|
|
if n.Subject.Title == "A Issue" {
|
|
|
|
assert.EqualValues(t, repoA.Name, n.Repository.Name)
|
|
|
|
} else if n.Subject.Title == "B Issue" {
|
|
|
|
assert.EqualValues(t, repoB.Name, n.Repository.Name)
|
|
|
|
} else {
|
|
|
|
assert.Error(t, fmt.Errorf("ListNotifications returned a Issue witch should not"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoNotifications
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err = c.ListRepoNotifications(repoA.Owner.UserName, repoA.Name, ListNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nList, 1)
|
|
|
|
assert.EqualValues(t, "A Issue", nList[0].Subject.Title)
|
|
|
|
// ReadRepoNotifications
|
2022-05-15 16:47:38 +01:00
|
|
|
notifications, _, err = c.ReadRepoNotifications(repoA.Owner.UserName, repoA.Name, MarkNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
assert.Len(t, notifications, 1)
|
2020-02-04 08:03:15 +00:00
|
|
|
|
|
|
|
// GetThread
|
2020-09-14 03:37:09 +01:00
|
|
|
n, _, err := c.GetNotification(nList[0].ID)
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, false, n.Unread)
|
|
|
|
assert.EqualValues(t, "A Issue", n.Subject.Title)
|
|
|
|
|
|
|
|
// ReadNotifications
|
2022-05-15 16:47:38 +01:00
|
|
|
notifications, _, err = c.ReadNotifications(MarkNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
assert.Len(t, notifications, 1)
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err = c.ListNotifications(ListNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, nList, 0)
|
|
|
|
|
|
|
|
// ReadThread
|
2020-02-04 15:15:35 +00:00
|
|
|
iState := StateClosed
|
2020-02-04 08:03:15 +00:00
|
|
|
c.sudo = ""
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2024-01-06 05:14:12 +00:00
|
|
|
time.Sleep(time.Second * 5)
|
2020-03-30 19:57:56 +01:00
|
|
|
|
2020-02-04 08:03:15 +00:00
|
|
|
c.sudo = user2.UserName
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err = c.ListNotifications(ListNotificationOptions{})
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
count, _, err = c.CheckNotifications()
|
2020-02-04 08:03:15 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-30 19:57:56 +01:00
|
|
|
assert.EqualValues(t, 1, count)
|
2021-06-30 22:06:50 +01:00
|
|
|
if assert.Len(t, nList, 1) {
|
|
|
|
assert.EqualValues(t, NotifySubjectClosed, nList[0].Subject.State)
|
2022-05-15 16:47:38 +01:00
|
|
|
notification, _, err := c.ReadNotification(nList[0].ID)
|
2020-09-14 03:37:09 +01:00
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
assert.EqualValues(t, notification.ID, nList[0].ID)
|
2020-03-30 19:57:56 +01:00
|
|
|
}
|
2020-07-21 13:28:16 +01:00
|
|
|
|
|
|
|
c.sudo = ""
|
2022-05-15 16:47:38 +01:00
|
|
|
notifications, _, err = c.ReadNotifications(MarkNotificationOptions{})
|
2020-07-21 13:28:16 +01:00
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
assert.Len(t, notifications, 2)
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}})
|
2020-07-21 13:28:16 +01:00
|
|
|
assert.NoError(t, err)
|
2024-01-06 05:14:12 +00:00
|
|
|
if assert.Len(t, nList, 2) {
|
|
|
|
notification, _, err := c.ReadNotification(nList[0].ID, NotifyStatusPinned)
|
|
|
|
assert.EqualValues(t, notification.ID, nList[0].ID)
|
|
|
|
assert.NoError(t, err)
|
2022-05-15 16:47:38 +01:00
|
|
|
|
2024-01-06 05:14:12 +00:00
|
|
|
notification, _, err = c.ReadNotification(nList[1].ID, NotifyStatusUnread)
|
|
|
|
assert.EqualValues(t, notification.ID, nList[1].ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}})
|
2020-07-21 13:28:16 +01:00
|
|
|
assert.NoError(t, err)
|
2021-06-30 22:06:50 +01:00
|
|
|
if assert.Len(t, nList, 2) {
|
2022-03-29 00:09:57 +01:00
|
|
|
assert.EqualValues(t, NotifySubjectOpen, nList[0].Subject.State)
|
|
|
|
assert.EqualValues(t, NotifySubjectOpen, nList[1].Subject.State)
|
2021-06-30 22:06:50 +01:00
|
|
|
}
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|