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.

action_test.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "path"
  7. "testing"
  8. "code.gitea.io/gitea/models/db"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAction_GetRepoPath(t *testing.T) {
  16. assert.NoError(t, unittest.PrepareTestDatabase())
  17. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{}).(*repo_model.Repository)
  18. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  19. action := &Action{RepoID: repo.ID}
  20. assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
  21. }
  22. func TestAction_GetRepoLink(t *testing.T) {
  23. assert.NoError(t, unittest.PrepareTestDatabase())
  24. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{}).(*repo_model.Repository)
  25. owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
  26. action := &Action{RepoID: repo.ID}
  27. setting.AppSubURL = "/suburl"
  28. expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
  29. assert.Equal(t, expected, action.GetRepoLink())
  30. }
  31. func TestGetFeeds(t *testing.T) {
  32. // test with an individual user
  33. assert.NoError(t, unittest.PrepareTestDatabase())
  34. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  35. actions, err := GetFeeds(db.DefaultContext, GetFeedsOptions{
  36. RequestedUser: user,
  37. Actor: user,
  38. IncludePrivate: true,
  39. OnlyPerformedBy: false,
  40. IncludeDeleted: true,
  41. })
  42. assert.NoError(t, err)
  43. if assert.Len(t, actions, 1) {
  44. assert.EqualValues(t, 1, actions[0].ID)
  45. assert.EqualValues(t, user.ID, actions[0].UserID)
  46. }
  47. actions, err = GetFeeds(db.DefaultContext, GetFeedsOptions{
  48. RequestedUser: user,
  49. Actor: user,
  50. IncludePrivate: false,
  51. OnlyPerformedBy: false,
  52. })
  53. assert.NoError(t, err)
  54. assert.Len(t, actions, 0)
  55. }
  56. func TestGetFeedsForRepos(t *testing.T) {
  57. assert.NoError(t, unittest.PrepareTestDatabase())
  58. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  59. privRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
  60. pubRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 8}).(*repo_model.Repository)
  61. // private repo & no login
  62. actions, err := GetFeeds(db.DefaultContext, GetFeedsOptions{
  63. RequestedRepo: privRepo,
  64. IncludePrivate: true,
  65. })
  66. assert.NoError(t, err)
  67. assert.Len(t, actions, 0)
  68. // public repo & no login
  69. actions, err = GetFeeds(db.DefaultContext, GetFeedsOptions{
  70. RequestedRepo: pubRepo,
  71. IncludePrivate: true,
  72. })
  73. assert.NoError(t, err)
  74. assert.Len(t, actions, 1)
  75. // private repo and login
  76. actions, err = GetFeeds(db.DefaultContext, GetFeedsOptions{
  77. RequestedRepo: privRepo,
  78. IncludePrivate: true,
  79. Actor: user,
  80. })
  81. assert.NoError(t, err)
  82. assert.Len(t, actions, 1)
  83. // public repo & login
  84. actions, err = GetFeeds(db.DefaultContext, GetFeedsOptions{
  85. RequestedRepo: pubRepo,
  86. IncludePrivate: true,
  87. Actor: user,
  88. })
  89. assert.NoError(t, err)
  90. assert.Len(t, actions, 1)
  91. }
  92. func TestGetFeeds2(t *testing.T) {
  93. // test with an organization user
  94. assert.NoError(t, unittest.PrepareTestDatabase())
  95. org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
  96. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  97. actions, err := GetFeeds(db.DefaultContext, GetFeedsOptions{
  98. RequestedUser: org,
  99. Actor: user,
  100. IncludePrivate: true,
  101. OnlyPerformedBy: false,
  102. IncludeDeleted: true,
  103. })
  104. assert.NoError(t, err)
  105. assert.Len(t, actions, 1)
  106. if assert.Len(t, actions, 1) {
  107. assert.EqualValues(t, 2, actions[0].ID)
  108. assert.EqualValues(t, org.ID, actions[0].UserID)
  109. }
  110. actions, err = GetFeeds(db.DefaultContext, GetFeedsOptions{
  111. RequestedUser: org,
  112. Actor: user,
  113. IncludePrivate: false,
  114. OnlyPerformedBy: false,
  115. IncludeDeleted: true,
  116. })
  117. assert.NoError(t, err)
  118. assert.Len(t, actions, 0)
  119. }
  120. func TestActivityReadable(t *testing.T) {
  121. tt := []struct {
  122. desc string
  123. user *user_model.User
  124. doer *user_model.User
  125. result bool
  126. }{{
  127. desc: "user should see own activity",
  128. user: &user_model.User{ID: 1},
  129. doer: &user_model.User{ID: 1},
  130. result: true,
  131. }, {
  132. desc: "anon should see activity if public",
  133. user: &user_model.User{ID: 1},
  134. result: true,
  135. }, {
  136. desc: "anon should NOT see activity",
  137. user: &user_model.User{ID: 1, KeepActivityPrivate: true},
  138. result: false,
  139. }, {
  140. desc: "user should see own activity if private too",
  141. user: &user_model.User{ID: 1, KeepActivityPrivate: true},
  142. doer: &user_model.User{ID: 1},
  143. result: true,
  144. }, {
  145. desc: "other user should NOT see activity",
  146. user: &user_model.User{ID: 1, KeepActivityPrivate: true},
  147. doer: &user_model.User{ID: 2},
  148. result: false,
  149. }, {
  150. desc: "admin should see activity",
  151. user: &user_model.User{ID: 1, KeepActivityPrivate: true},
  152. doer: &user_model.User{ID: 2, IsAdmin: true},
  153. result: true,
  154. }}
  155. for _, test := range tt {
  156. assert.Equal(t, test.result, activityReadable(test.user, test.doer), test.desc)
  157. }
  158. }
  159. func TestNotifyWatchers(t *testing.T) {
  160. assert.NoError(t, unittest.PrepareTestDatabase())
  161. action := &Action{
  162. ActUserID: 8,
  163. RepoID: 1,
  164. OpType: ActionStarRepo,
  165. }
  166. assert.NoError(t, NotifyWatchers(action))
  167. // One watchers are inactive, thus action is only created for user 8, 1, 4, 11
  168. unittest.AssertExistsAndLoadBean(t, &Action{
  169. ActUserID: action.ActUserID,
  170. UserID: 8,
  171. RepoID: action.RepoID,
  172. OpType: action.OpType,
  173. })
  174. unittest.AssertExistsAndLoadBean(t, &Action{
  175. ActUserID: action.ActUserID,
  176. UserID: 1,
  177. RepoID: action.RepoID,
  178. OpType: action.OpType,
  179. })
  180. unittest.AssertExistsAndLoadBean(t, &Action{
  181. ActUserID: action.ActUserID,
  182. UserID: 4,
  183. RepoID: action.RepoID,
  184. OpType: action.OpType,
  185. })
  186. unittest.AssertExistsAndLoadBean(t, &Action{
  187. ActUserID: action.ActUserID,
  188. UserID: 11,
  189. RepoID: action.RepoID,
  190. OpType: action.OpType,
  191. })
  192. }
  193. func TestGetFeedsCorrupted(t *testing.T) {
  194. assert.NoError(t, unittest.PrepareTestDatabase())
  195. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
  196. unittest.AssertExistsAndLoadBean(t, &Action{
  197. ID: 8,
  198. RepoID: 1700,
  199. })
  200. actions, err := GetFeeds(db.DefaultContext, GetFeedsOptions{
  201. RequestedUser: user,
  202. Actor: user,
  203. IncludePrivate: true,
  204. })
  205. assert.NoError(t, err)
  206. assert.Len(t, actions, 0)
  207. }