Files
Atay-Makhzan/tests/integration/api_notification_test.go
T

235 lines
8.1 KiB
Go
Raw Normal View History

2020-01-09 12:56:32 +01:00
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2020-01-09 12:56:32 +01:00
2022-09-02 15:18:23 -04:00
package integration
2020-01-09 12:56:32 +01:00
import (
"fmt"
"net/http"
"testing"
activities_model "code.gitea.io/gitea/models/activities"
2023-01-17 16:46:03 -05:00
auth_model "code.gitea.io/gitea/models/auth"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
2020-01-09 12:56:32 +01:00
api "code.gitea.io/gitea/modules/structs"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2020-01-09 12:56:32 +01:00
"github.com/stretchr/testify/assert"
)
func TestAPINotification(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2020-01-09 12:56:32 +01:00
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
thread5 := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{ID: 5})
assert.NoError(t, thread5.LoadAttributes(t.Context()))
2020-01-09 12:56:32 +01:00
session := loginUser(t, user2.Name)
2023-06-04 14:57:16 -04:00
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteNotification, auth_model.AccessTokenScopeWriteRepository)
2020-01-09 12:56:32 +01:00
MakeRequest(t, NewRequest(t, "GET", "/api/v1/notifications"), http.StatusUnauthorized)
2020-01-09 12:56:32 +01:00
// -- GET /notifications --
// test filter
2022-01-20 18:46:10 +01:00
since := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801
2025-04-01 12:14:01 +02:00
req := NewRequest(t, "GET", "/api/v1/notifications?since="+since).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
var apiNL []api.NotificationThread
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 1)
assert.EqualValues(t, 5, apiNL[0].ID)
// test filter
2022-01-20 18:46:10 +01:00
before := "2000-01-01T01%3A06%3A59%2B00%3A00" // 946688819
2020-01-09 12:56:32 +01:00
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?all=%s&before=%s", "true", before)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 3)
assert.EqualValues(t, 4, apiNL[0].ID)
2021-06-07 07:27:09 +02:00
assert.True(t, apiNL[0].Unread)
assert.False(t, apiNL[0].Pinned)
2020-01-09 12:56:32 +01:00
assert.EqualValues(t, 3, apiNL[1].ID)
2021-06-07 07:27:09 +02:00
assert.False(t, apiNL[1].Unread)
assert.True(t, apiNL[1].Pinned)
2020-01-09 12:56:32 +01:00
assert.EqualValues(t, 2, apiNL[2].ID)
2021-06-07 07:27:09 +02:00
assert.False(t, apiNL[2].Unread)
assert.False(t, apiNL[2].Pinned)
2020-01-09 12:56:32 +01:00
// -- GET /repos/{owner}/{repo}/notifications --
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread", user2.Name, repo1.Name)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 1)
assert.EqualValues(t, 4, apiNL[0].ID)
// -- GET /repos/{owner}/{repo}/notifications -- multiple status-types
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread&status-types=pinned", user2.Name, repo1.Name)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 2)
assert.EqualValues(t, 4, apiNL[0].ID)
assert.True(t, apiNL[0].Unread)
assert.False(t, apiNL[0].Pinned)
assert.EqualValues(t, 3, apiNL[1].ID)
assert.False(t, apiNL[1].Unread)
assert.True(t, apiNL[1].Pinned)
MakeRequest(t, NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d", 1)), http.StatusUnauthorized)
2020-01-09 12:56:32 +01:00
// -- GET /notifications/threads/{id} --
// get forbidden
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d", 1)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
MakeRequest(t, req, http.StatusForbidden)
2020-01-09 12:56:32 +01:00
// get own
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d", thread5.ID)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
var apiN api.NotificationThread
DecodeJSON(t, resp, &apiN)
assert.EqualValues(t, 5, apiN.ID)
2021-06-07 07:27:09 +02:00
assert.False(t, apiN.Pinned)
assert.True(t, apiN.Unread)
2025-03-31 07:53:48 +02:00
assert.Equal(t, "issue4", apiN.Subject.Title)
2020-01-09 12:56:32 +01:00
assert.EqualValues(t, "Issue", apiN.Subject.Type)
assert.Equal(t, thread5.Issue.APIURL(t.Context()), apiN.Subject.URL)
2025-03-31 07:53:48 +02:00
assert.Equal(t, thread5.Repository.HTMLURL(), apiN.Repository.HTMLURL)
2020-01-09 12:56:32 +01:00
MakeRequest(t, NewRequest(t, "GET", "/api/v1/notifications/new"), http.StatusUnauthorized)
2024-04-22 13:48:42 +02:00
newStruct := struct {
New int64 `json:"new"`
}{}
// -- check notifications --
req = NewRequest(t, "GET", "/api/v1/notifications/new").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2024-04-22 13:48:42 +02:00
DecodeJSON(t, resp, &newStruct)
2024-12-15 11:41:29 +01:00
assert.Positive(t, newStruct.New)
2020-01-09 12:56:32 +01:00
// -- mark notifications as read --
req = NewRequest(t, "GET", "/api/v1/notifications?status-types=unread").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 2)
2022-01-20 18:46:10 +01:00
lastReadAt := "2000-01-01T00%3A50%3A01%2B00%3A00" // 946687801 <- only Notification 4 is in this filter ...
req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s", user2.Name, repo1.Name, lastReadAt)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
MakeRequest(t, req, http.StatusResetContent)
2020-01-09 12:56:32 +01:00
req = NewRequest(t, "GET", "/api/v1/notifications?status-types=unread").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2020-01-09 12:56:32 +01:00
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 1)
// -- PATCH /notifications/threads/{id} --
req = NewRequest(t, "PATCH", fmt.Sprintf("/api/v1/notifications/threads/%d", thread5.ID)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
MakeRequest(t, req, http.StatusResetContent)
2020-01-09 12:56:32 +01:00
assert.Equal(t, activities_model.NotificationStatusUnread, thread5.Status)
thread5 = unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{ID: 5})
assert.Equal(t, activities_model.NotificationStatusRead, thread5.Status)
// -- check notifications --
req = NewRequest(t, "GET", "/api/v1/notifications/new").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
2024-04-22 13:48:42 +02:00
DecodeJSON(t, resp, &newStruct)
2024-12-15 11:41:29 +01:00
assert.Zero(t, newStruct.New)
2020-01-09 12:56:32 +01:00
}
func TestAPINotificationPUT(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
thread5 := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{ID: 5})
assert.NoError(t, thread5.LoadAttributes(t.Context()))
session := loginUser(t, user2.Name)
2023-06-04 14:57:16 -04:00
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteNotification)
// Check notifications are as expected
req := NewRequest(t, "GET", "/api/v1/notifications?all=true").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusOK)
var apiNL []api.NotificationThread
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 4)
assert.EqualValues(t, 5, apiNL[0].ID)
assert.True(t, apiNL[0].Unread)
assert.False(t, apiNL[0].Pinned)
assert.EqualValues(t, 4, apiNL[1].ID)
assert.True(t, apiNL[1].Unread)
assert.False(t, apiNL[1].Pinned)
assert.EqualValues(t, 3, apiNL[2].ID)
assert.False(t, apiNL[2].Unread)
assert.True(t, apiNL[2].Pinned)
assert.EqualValues(t, 2, apiNL[3].ID)
assert.False(t, apiNL[3].Unread)
assert.False(t, apiNL[3].Pinned)
//
// Notification ID 2 is the only one with status-type read & pinned
// change it to unread.
//
req = NewRequest(t, "PUT", "/api/v1/notifications?status-types=read&status-type=pinned&to-status=unread").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusResetContent)
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 1)
assert.EqualValues(t, 2, apiNL[0].ID)
assert.True(t, apiNL[0].Unread)
assert.False(t, apiNL[0].Pinned)
//
2026-02-28 20:23:20 +01:00
// Now notification ID 2 is the first in the list and is unread.
//
req = NewRequest(t, "GET", "/api/v1/notifications?all=true").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiNL)
assert.Len(t, apiNL, 4)
assert.EqualValues(t, 2, apiNL[0].ID)
assert.True(t, apiNL[0].Unread)
assert.False(t, apiNL[0].Pinned)
}
func TestAPINotificationPublicOnly(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
thread5 := unittest.AssertExistsAndLoadBean(t, &activities_model.Notification{ID: 5})
token := getUserToken(t, user2.Name, auth_model.AccessTokenScopeReadNotification, auth_model.AccessTokenScopePublicOnly)
req := NewRequest(t, "GET", "/api/v1/notifications").
AddTokenAuth(token)
MakeRequest(t, req, http.StatusForbidden)
req = NewRequest(t, "GET", "/api/v1/notifications/new").
AddTokenAuth(token)
MakeRequest(t, req, http.StatusForbidden)
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications/threads/%d", thread5.ID)).
AddTokenAuth(token)
MakeRequest(t, req, http.StatusForbidden)
}