| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package notifications_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/api/client/notifications" | 
					
						
							|  |  |  | 	apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/id" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/oauth" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/testrig" | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	"github.com/stretchr/testify/suite" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) getNotifications( | 
					
						
							|  |  |  | 	account *gtsmodel.Account, | 
					
						
							|  |  |  | 	token *gtsmodel.Token, | 
					
						
							|  |  |  | 	user *gtsmodel.User, | 
					
						
							|  |  |  | 	maxID string, | 
					
						
							|  |  |  | 	minID string, | 
					
						
							|  |  |  | 	limit int, | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 	types []string, | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	excludeTypes []string, | 
					
						
							|  |  |  | 	expectedHTTPStatus int, | 
					
						
							|  |  |  | 	expectedBody string, | 
					
						
							|  |  |  | ) ([]*apimodel.Notification, string, error) { | 
					
						
							|  |  |  | 	// instantiate recorder + test context | 
					
						
							|  |  |  | 	recorder := httptest.NewRecorder() | 
					
						
							|  |  |  | 	ctx, _ := testrig.CreateGinTestContext(recorder, nil) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedAccount, account) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(token)) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) | 
					
						
							|  |  |  | 	ctx.Set(oauth.SessionAuthorizedUser, user) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// create the request | 
					
						
							|  |  |  | 	ctx.Request = httptest.NewRequest(http.MethodGet, config.GetProtocol()+"://"+config.GetHost()+"/api/"+notifications.BasePath, nil) | 
					
						
							|  |  |  | 	ctx.Request.Header.Set("accept", "application/json") | 
					
						
							|  |  |  | 	query := url.Values{} | 
					
						
							|  |  |  | 	if maxID != "" { | 
					
						
							|  |  |  | 		query.Set(notifications.MaxIDKey, maxID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if minID != "" { | 
					
						
							|  |  |  | 		query.Set(notifications.MinIDKey, maxID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if limit != 0 { | 
					
						
							|  |  |  | 		query.Set(notifications.LimitKey, strconv.Itoa(limit)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 	if len(types) > 0 { | 
					
						
							|  |  |  | 		query[notifications.TypesKey] = types | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if len(excludeTypes) > 0 { | 
					
						
							|  |  |  | 		query[notifications.ExcludeTypesKey] = excludeTypes | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ctx.Request.URL.RawQuery = query.Encode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// trigger the handler | 
					
						
							|  |  |  | 	suite.notificationsModule.NotificationsGETHandler(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// read the response | 
					
						
							|  |  |  | 	result := recorder.Result() | 
					
						
							|  |  |  | 	defer result.Body.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	b, err := io.ReadAll(result.Body) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	errs := gtserror.NewMultiError(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check code | 
					
						
							|  |  |  | 	if resultCode := recorder.Code; expectedHTTPStatus != resultCode { | 
					
						
							|  |  |  | 		errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if we got an expected body, return early | 
					
						
							|  |  |  | 	if expectedBody != "" { | 
					
						
							|  |  |  | 		if string(b) != expectedBody { | 
					
						
							|  |  |  | 			errs.Appendf("expected %s got %s", expectedBody, string(b)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil, "", errs.Combine() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp := make([]*apimodel.Notification, 0) | 
					
						
							|  |  |  | 	if err := json.Unmarshal(b, &resp); err != nil { | 
					
						
							|  |  |  | 		return nil, "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return resp, result.Header.Get("Link"), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test that we can retrieve at least one notification and the expected Link header. | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) TestGetNotificationsSingle() { | 
					
						
							|  |  |  | 	testAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 	testToken := suite.testTokens["local_account_1"] | 
					
						
							|  |  |  | 	testUser := suite.testUsers["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maxID := "" | 
					
						
							|  |  |  | 	minID := "" | 
					
						
							|  |  |  | 	limit := 10 | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 	types := []string(nil) | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	excludeTypes := []string(nil) | 
					
						
							|  |  |  | 	expectedHTTPStatus := http.StatusOK | 
					
						
							|  |  |  | 	expectedBody := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notifications, linkHeader, err := suite.getNotifications( | 
					
						
							|  |  |  | 		testAccount, | 
					
						
							|  |  |  | 		testToken, | 
					
						
							|  |  |  | 		testUser, | 
					
						
							|  |  |  | 		maxID, | 
					
						
							|  |  |  | 		minID, | 
					
						
							|  |  |  | 		limit, | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 		types, | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 		excludeTypes, | 
					
						
							|  |  |  | 		expectedHTTPStatus, | 
					
						
							|  |  |  | 		expectedBody, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		suite.FailNow(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.Len(notifications, 1) | 
					
						
							|  |  |  | 	suite.Equal(`<http://localhost:8080/api/v1/notifications?limit=10&max_id=01F8Q0ANPTWW10DAKTX7BRPBJP>; rel="next", <http://localhost:8080/api/v1/notifications?limit=10&min_id=01F8Q0ANPTWW10DAKTX7BRPBJP>; rel="prev"`, linkHeader) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Add some extra notifications of different types than the fixture's single fav notification per account. | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) addMoreNotifications(testAccount *gtsmodel.Account) { | 
					
						
							|  |  |  | 	for _, b := range []*gtsmodel.Notification{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			ID:               id.NewULID(), | 
					
						
							|  |  |  | 			NotificationType: gtsmodel.NotificationFollowRequest, | 
					
						
							|  |  |  | 			TargetAccountID:  testAccount.ID, | 
					
						
							|  |  |  | 			OriginAccountID:  suite.testAccounts["local_account_2"].ID, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			ID:               id.NewULID(), | 
					
						
							|  |  |  | 			NotificationType: gtsmodel.NotificationFollow, | 
					
						
							|  |  |  | 			TargetAccountID:  testAccount.ID, | 
					
						
							|  |  |  | 			OriginAccountID:  suite.testAccounts["remote_account_2"].ID, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2025-05-22 12:26:11 +02:00
										 |  |  | 		if err := suite.db.Put(suite.T().Context(), b); err != nil { | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 			suite.FailNow(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test that we can exclude a notification type. | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) TestGetNotificationsExcludeOneType() { | 
					
						
							|  |  |  | 	testAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 	testToken := suite.testTokens["local_account_1"] | 
					
						
							|  |  |  | 	testUser := suite.testUsers["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.addMoreNotifications(testAccount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maxID := "" | 
					
						
							|  |  |  | 	minID := "" | 
					
						
							|  |  |  | 	limit := 10 | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 	types := []string(nil) | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	excludeTypes := []string{"follow_request"} | 
					
						
							|  |  |  | 	expectedHTTPStatus := http.StatusOK | 
					
						
							|  |  |  | 	expectedBody := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notifications, _, err := suite.getNotifications( | 
					
						
							|  |  |  | 		testAccount, | 
					
						
							|  |  |  | 		testToken, | 
					
						
							|  |  |  | 		testUser, | 
					
						
							|  |  |  | 		maxID, | 
					
						
							|  |  |  | 		minID, | 
					
						
							|  |  |  | 		limit, | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 		types, | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 		excludeTypes, | 
					
						
							|  |  |  | 		expectedHTTPStatus, | 
					
						
							|  |  |  | 		expectedBody, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		suite.FailNow(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This should not include the follow request notification. | 
					
						
							|  |  |  | 	suite.Len(notifications, 2) | 
					
						
							|  |  |  | 	for _, notification := range notifications { | 
					
						
							|  |  |  | 		suite.NotEqual("follow_request", notification.Type) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test that we can fetch only a single notification type. | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) TestGetNotificationsIncludeOneType() { | 
					
						
							|  |  |  | 	testAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 	testToken := suite.testTokens["local_account_1"] | 
					
						
							|  |  |  | 	testUser := suite.testUsers["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.addMoreNotifications(testAccount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maxID := "" | 
					
						
							|  |  |  | 	minID := "" | 
					
						
							|  |  |  | 	limit := 10 | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 	types := []string{"favourite"} | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 	excludeTypes := []string(nil) | 
					
						
							|  |  |  | 	expectedHTTPStatus := http.StatusOK | 
					
						
							|  |  |  | 	expectedBody := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notifications, _, err := suite.getNotifications( | 
					
						
							|  |  |  | 		testAccount, | 
					
						
							|  |  |  | 		testToken, | 
					
						
							|  |  |  | 		testUser, | 
					
						
							|  |  |  | 		maxID, | 
					
						
							|  |  |  | 		minID, | 
					
						
							|  |  |  | 		limit, | 
					
						
							| 
									
										
										
										
											2024-06-18 18:18:35 +02:00
										 |  |  | 		types, | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | 		excludeTypes, | 
					
						
							|  |  |  | 		expectedHTTPStatus, | 
					
						
							|  |  |  | 		expectedBody, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		suite.FailNow(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This should only include the fav notification. | 
					
						
							|  |  |  | 	suite.Len(notifications, 1) | 
					
						
							|  |  |  | 	for _, notification := range notifications { | 
					
						
							|  |  |  | 		suite.Equal("favourite", notification.Type) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 18:22:45 +01:00
										 |  |  | // Test including an unknown notification type, it should be ignored. | 
					
						
							|  |  |  | func (suite *NotificationsTestSuite) TestGetNotificationsIncludeUnknownType() { | 
					
						
							|  |  |  | 	testAccount := suite.testAccounts["local_account_1"] | 
					
						
							|  |  |  | 	testToken := suite.testTokens["local_account_1"] | 
					
						
							|  |  |  | 	testUser := suite.testUsers["local_account_1"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	suite.addMoreNotifications(testAccount) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maxID := "" | 
					
						
							|  |  |  | 	minID := "" | 
					
						
							|  |  |  | 	limit := 10 | 
					
						
							|  |  |  | 	types := []string{"favourite", "something.weird"} | 
					
						
							|  |  |  | 	excludeTypes := []string(nil) | 
					
						
							|  |  |  | 	expectedHTTPStatus := http.StatusOK | 
					
						
							|  |  |  | 	expectedBody := "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	notifications, _, err := suite.getNotifications( | 
					
						
							|  |  |  | 		testAccount, | 
					
						
							|  |  |  | 		testToken, | 
					
						
							|  |  |  | 		testUser, | 
					
						
							|  |  |  | 		maxID, | 
					
						
							|  |  |  | 		minID, | 
					
						
							|  |  |  | 		limit, | 
					
						
							|  |  |  | 		types, | 
					
						
							|  |  |  | 		excludeTypes, | 
					
						
							|  |  |  | 		expectedHTTPStatus, | 
					
						
							|  |  |  | 		expectedBody, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		suite.FailNow(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This should only include the fav notification. | 
					
						
							|  |  |  | 	suite.Len(notifications, 1) | 
					
						
							|  |  |  | 	for _, notification := range notifications { | 
					
						
							|  |  |  | 		suite.Equal("favourite", notification.Type) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 12:50:50 -07:00
										 |  |  | func TestBookmarkTestSuite(t *testing.T) { | 
					
						
							|  |  |  | 	suite.Run(t, new(NotificationsTestSuite)) | 
					
						
							|  |  |  | } |