You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_heatmap_test.go 2.7KB

Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities_test
  4. import (
  5. "fmt"
  6. "testing"
  7. "time"
  8. activities_model "code.gitea.io/gitea/models/activities"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/json"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestGetUserHeatmapDataByUser(t *testing.T) {
  17. testCases := []struct {
  18. desc string
  19. userID int64
  20. doerID int64
  21. CountResult int
  22. JSONResult string
  23. }{
  24. {
  25. "self looks at action in private repo",
  26. 2, 2, 1, `[{"timestamp":1603227600,"contributions":1}]`,
  27. },
  28. {
  29. "admin looks at action in private repo",
  30. 2, 1, 1, `[{"timestamp":1603227600,"contributions":1}]`,
  31. },
  32. {
  33. "other user looks at action in private repo",
  34. 2, 3, 0, `[]`,
  35. },
  36. {
  37. "nobody looks at action in private repo",
  38. 2, 0, 0, `[]`,
  39. },
  40. {
  41. "collaborator looks at action in private repo",
  42. 16, 15, 1, `[{"timestamp":1603267200,"contributions":1}]`,
  43. },
  44. {
  45. "no action action not performed by target user",
  46. 3, 3, 0, `[]`,
  47. },
  48. {
  49. "multiple actions performed with two grouped together",
  50. 10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`,
  51. },
  52. }
  53. // Prepare
  54. assert.NoError(t, unittest.PrepareTestDatabase())
  55. // Mock time
  56. timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
  57. defer timeutil.Unset()
  58. for _, tc := range testCases {
  59. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tc.userID})
  60. doer := &user_model.User{ID: tc.doerID}
  61. _, err := unittest.LoadBeanIfExists(doer)
  62. assert.NoError(t, err)
  63. if tc.doerID == 0 {
  64. doer = nil
  65. }
  66. // get the action for comparison
  67. actions, count, err := activities_model.GetFeeds(db.DefaultContext, activities_model.GetFeedsOptions{
  68. RequestedUser: user,
  69. Actor: doer,
  70. IncludePrivate: true,
  71. OnlyPerformedBy: true,
  72. IncludeDeleted: true,
  73. })
  74. assert.NoError(t, err)
  75. // Get the heatmap and compare
  76. heatmap, err := activities_model.GetUserHeatmapDataByUser(user, doer)
  77. var contributions int
  78. for _, hm := range heatmap {
  79. contributions += int(hm.Contributions)
  80. }
  81. assert.NoError(t, err)
  82. assert.Len(t, actions, contributions, "invalid action count: did the test data became too old?")
  83. assert.Equal(t, count, int64(contributions))
  84. assert.Equal(t, tc.CountResult, contributions, fmt.Sprintf("testcase '%s'", tc.desc))
  85. // Test JSON rendering
  86. jsonData, err := json.Marshal(heatmap)
  87. assert.NoError(t, err)
  88. assert.Equal(t, tc.JSONResult, string(jsonData))
  89. }
  90. }