2018-10-22 22:57:42 -04:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-10-22 22:57:42 -04:00
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
package activities_test
|
2018-10-22 22:57:42 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-10-18 16:12:26 -04:00
|
|
|
"time"
|
2018-10-24 09:17:21 -04:00
|
|
|
|
2022-08-24 22:31:57 -04:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2022-03-13 12:40:47 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-10-18 16:12:26 -04:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-07-24 12:03:58 -04:00
|
|
|
|
2018-10-24 09:17:21 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-22 22:57:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetUserHeatmapDataByUser(t *testing.T) {
|
2018-10-24 09:17:21 -04:00
|
|
|
testCases := []struct {
|
2022-03-10 09:54:51 -05:00
|
|
|
desc string
|
2018-10-24 09:17:21 -04:00
|
|
|
userID int64
|
2020-12-21 21:53:37 -05:00
|
|
|
doerID int64
|
2018-10-24 09:17:21 -04:00
|
|
|
CountResult int
|
|
|
|
JSONResult string
|
|
|
|
}{
|
2022-03-10 09:54:51 -05:00
|
|
|
{
|
|
|
|
"self looks at action in private repo",
|
|
|
|
2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"admin looks at action in private repo",
|
|
|
|
2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"other user looks at action in private repo",
|
|
|
|
2, 3, 0, `[]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nobody looks at action in private repo",
|
|
|
|
2, 0, 0, `[]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"collaborator looks at action in private repo",
|
|
|
|
16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no action action not performed by target user",
|
|
|
|
3, 3, 0, `[]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple actions performed with two grouped together",
|
|
|
|
10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`,
|
|
|
|
},
|
2018-10-24 09:17:21 -04:00
|
|
|
}
|
2018-10-22 22:57:42 -04:00
|
|
|
// Prepare
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2018-10-22 22:57:42 -04:00
|
|
|
|
2021-10-18 16:12:26 -04:00
|
|
|
// Mock time
|
2023-12-28 05:09:57 -05:00
|
|
|
timeutil.MockSet(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
|
|
|
|
defer timeutil.MockUnset()
|
2021-10-18 16:12:26 -04:00
|
|
|
|
2022-03-10 09:54:51 -05:00
|
|
|
for _, tc := range testCases {
|
2022-08-15 22:22:25 -04:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
|
2018-10-24 09:17:21 -04:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
doer := &user_model.User{ID: tc.doerID}
|
2021-11-16 03:53:21 -05:00
|
|
|
_, err := unittest.LoadBeanIfExists(doer)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2020-12-21 21:53:37 -05:00
|
|
|
if tc.doerID == 0 {
|
|
|
|
doer = nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:17:21 -04:00
|
|
|
// get the action for comparison
|
2023-02-24 16:15:10 -05:00
|
|
|
actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
|
2020-01-13 12:33:46 -05:00
|
|
|
RequestedUser: user,
|
2020-12-21 21:53:37 -05:00
|
|
|
Actor: doer,
|
2020-01-13 12:33:46 -05:00
|
|
|
IncludePrivate: true,
|
2020-12-21 21:53:37 -05:00
|
|
|
OnlyPerformedBy: true,
|
2020-01-13 12:33:46 -05:00
|
|
|
IncludeDeleted: true,
|
2018-10-24 09:17:21 -04:00
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2018-10-24 09:17:21 -04:00
|
|
|
|
|
|
|
// Get the heatmap and compare
|
2023-09-25 09:17:37 -04:00
|
|
|
heatmap, err := activities_model.GetUserHeatmapDataByUser(db.DefaultContext, user, doer)
|
2021-06-25 12:59:25 -04:00
|
|
|
var contributions int
|
|
|
|
for _, hm := range heatmap {
|
|
|
|
contributions += int(hm.Contributions)
|
|
|
|
}
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-25 12:59:25 -04:00
|
|
|
assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
|
2023-02-24 16:15:10 -05:00
|
|
|
assert.Equal(t, count, int64(contributions))
|
2024-08-14 05:43:42 -04:00
|
|
|
assert.Equal(t, tc.CountResult, contributions, tc.desc)
|
2018-10-24 09:17:21 -04:00
|
|
|
|
2021-03-14 14:52:12 -04:00
|
|
|
// Test JSON rendering
|
2018-10-24 09:17:21 -04:00
|
|
|
jsonData, err := json.Marshal(heatmap)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-11-11 04:31:03 -05:00
|
|
|
assert.JSONEq(t, tc.JSONResult, string(jsonData))
|
2018-10-24 09:17:21 -04:00
|
|
|
}
|
2018-10-22 22:57:42 -04:00
|
|
|
}
|