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.

414 lines
14 KiB

  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 integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. const privateActivityTestAdmin = "user1"
  14. const privateActivityTestUser = "user2"
  15. // user3 is an organization so it is not usable here
  16. const privateActivityTestOtherUser = "user4"
  17. // activity helpers
  18. func testPrivateActivityDoSomethingForActionEntries(t *testing.T) {
  19. repoBefore := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  20. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User)
  21. session := loginUser(t, privateActivityTestUser)
  22. token := getTokenForLoggedInUser(t, session)
  23. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repoBefore.Name, token)
  24. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
  25. Body: "test",
  26. Title: "test",
  27. })
  28. session.MakeRequest(t, req, http.StatusCreated)
  29. }
  30. // private activity helpers
  31. func testPrivateActivityHelperEnablePrivateActivity(t *testing.T) {
  32. session := loginUser(t, privateActivityTestUser)
  33. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  34. "_csrf": GetCSRF(t, session, "/user/settings"),
  35. "name": privateActivityTestUser,
  36. "email": privateActivityTestUser + "@example.com",
  37. "language": "en-us",
  38. "keep_activity_private": "1",
  39. })
  40. session.MakeRequest(t, req, http.StatusFound)
  41. }
  42. func testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc *HTMLDoc) bool {
  43. return htmlDoc.doc.Find(".feeds").Find(".news").Length() > 0
  44. }
  45. func testPrivateActivityHelperHasVisibleActivitiesFromSession(t *testing.T, session *TestSession) bool {
  46. req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
  47. resp := session.MakeRequest(t, req, http.StatusOK)
  48. htmlDoc := NewHTMLParser(t, resp.Body)
  49. return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
  50. }
  51. func testPrivateActivityHelperHasVisibleActivitiesFromPublic(t *testing.T) bool {
  52. req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
  53. resp := MakeRequest(t, req, http.StatusOK)
  54. htmlDoc := NewHTMLParser(t, resp.Body)
  55. return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
  56. }
  57. // heatmap UI helpers
  58. func testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc *HTMLDoc) bool {
  59. return htmlDoc.doc.Find("#user-heatmap").Length() > 0
  60. }
  61. func testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t *testing.T, session *TestSession) bool {
  62. req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
  63. resp := session.MakeRequest(t, req, http.StatusOK)
  64. htmlDoc := NewHTMLParser(t, resp.Body)
  65. return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
  66. }
  67. func testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t *testing.T, session *TestSession) bool {
  68. req := NewRequest(t, "GET", "/")
  69. resp := session.MakeRequest(t, req, http.StatusOK)
  70. htmlDoc := NewHTMLParser(t, resp.Body)
  71. return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
  72. }
  73. func testPrivateActivityHelperHasVisibleHeatmapFromPublic(t *testing.T) bool {
  74. req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
  75. resp := MakeRequest(t, req, http.StatusOK)
  76. htmlDoc := NewHTMLParser(t, resp.Body)
  77. return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
  78. }
  79. // heatmap API helpers
  80. func testPrivateActivityHelperHasHeatmapContentFromPublic(t *testing.T) bool {
  81. req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap", privateActivityTestUser)
  82. resp := MakeRequest(t, req, http.StatusOK)
  83. var items []*models.UserHeatmapData
  84. DecodeJSON(t, resp, &items)
  85. return len(items) != 0
  86. }
  87. func testPrivateActivityHelperHasHeatmapContentFromSession(t *testing.T, session *TestSession) bool {
  88. token := getTokenForLoggedInUser(t, session)
  89. req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap?token=%s", privateActivityTestUser, token)
  90. resp := session.MakeRequest(t, req, http.StatusOK)
  91. var items []*models.UserHeatmapData
  92. DecodeJSON(t, resp, &items)
  93. return len(items) != 0
  94. }
  95. // check activity visibility if the visibility is enabled
  96. func TestPrivateActivityNoVisibleForPublic(t *testing.T) {
  97. defer prepareTestEnv(t)()
  98. testPrivateActivityDoSomethingForActionEntries(t)
  99. visible := testPrivateActivityHelperHasVisibleActivitiesFromPublic(t)
  100. assert.True(t, visible, "user should have visible activities")
  101. }
  102. func TestPrivateActivityNoVisibleForUserItself(t *testing.T) {
  103. defer prepareTestEnv(t)()
  104. testPrivateActivityDoSomethingForActionEntries(t)
  105. session := loginUser(t, privateActivityTestUser)
  106. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  107. assert.True(t, visible, "user should have visible activities")
  108. }
  109. func TestPrivateActivityNoVisibleForOtherUser(t *testing.T) {
  110. defer prepareTestEnv(t)()
  111. testPrivateActivityDoSomethingForActionEntries(t)
  112. session := loginUser(t, privateActivityTestOtherUser)
  113. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  114. assert.True(t, visible, "user should have visible activities")
  115. }
  116. func TestPrivateActivityNoVisibleForAdmin(t *testing.T) {
  117. defer prepareTestEnv(t)()
  118. testPrivateActivityDoSomethingForActionEntries(t)
  119. session := loginUser(t, privateActivityTestAdmin)
  120. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  121. assert.True(t, visible, "user should have visible activities")
  122. }
  123. // check activity visibility if the visibility is disabled
  124. func TestPrivateActivityYesInvisibleForPublic(t *testing.T) {
  125. defer prepareTestEnv(t)()
  126. testPrivateActivityDoSomethingForActionEntries(t)
  127. testPrivateActivityHelperEnablePrivateActivity(t)
  128. visible := testPrivateActivityHelperHasVisibleActivitiesFromPublic(t)
  129. assert.False(t, visible, "user should have no visible activities")
  130. }
  131. func TestPrivateActivityYesVisibleForUserItself(t *testing.T) {
  132. defer prepareTestEnv(t)()
  133. testPrivateActivityDoSomethingForActionEntries(t)
  134. testPrivateActivityHelperEnablePrivateActivity(t)
  135. session := loginUser(t, privateActivityTestUser)
  136. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  137. assert.True(t, visible, "user should have visible activities")
  138. }
  139. func TestPrivateActivityYesInvisibleForOtherUser(t *testing.T) {
  140. defer prepareTestEnv(t)()
  141. testPrivateActivityDoSomethingForActionEntries(t)
  142. testPrivateActivityHelperEnablePrivateActivity(t)
  143. session := loginUser(t, privateActivityTestOtherUser)
  144. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  145. assert.False(t, visible, "user should have no visible activities")
  146. }
  147. func TestPrivateActivityYesVisibleForAdmin(t *testing.T) {
  148. defer prepareTestEnv(t)()
  149. testPrivateActivityDoSomethingForActionEntries(t)
  150. testPrivateActivityHelperEnablePrivateActivity(t)
  151. session := loginUser(t, privateActivityTestAdmin)
  152. visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
  153. assert.True(t, visible, "user should have visible activities")
  154. }
  155. // check heatmap visibility if the visibility is enabled
  156. func TestPrivateActivityNoHeatmapVisibleForPublic(t *testing.T) {
  157. defer prepareTestEnv(t)()
  158. testPrivateActivityDoSomethingForActionEntries(t)
  159. visible := testPrivateActivityHelperHasVisibleHeatmapFromPublic(t)
  160. assert.True(t, visible, "user should have visible heatmap")
  161. }
  162. func TestPrivateActivityNoHeatmapVisibleForUserItselfAtProfile(t *testing.T) {
  163. defer prepareTestEnv(t)()
  164. testPrivateActivityDoSomethingForActionEntries(t)
  165. session := loginUser(t, privateActivityTestUser)
  166. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  167. assert.True(t, visible, "user should have visible heatmap")
  168. }
  169. func TestPrivateActivityNoHeatmapVisibleForUserItselfAtDashboard(t *testing.T) {
  170. defer prepareTestEnv(t)()
  171. testPrivateActivityDoSomethingForActionEntries(t)
  172. session := loginUser(t, privateActivityTestUser)
  173. visible := testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t, session)
  174. assert.True(t, visible, "user should have visible heatmap")
  175. }
  176. func TestPrivateActivityNoHeatmapVisibleForOtherUser(t *testing.T) {
  177. defer prepareTestEnv(t)()
  178. testPrivateActivityDoSomethingForActionEntries(t)
  179. session := loginUser(t, privateActivityTestOtherUser)
  180. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  181. assert.True(t, visible, "user should have visible heatmap")
  182. }
  183. func TestPrivateActivityNoHeatmapVisibleForAdmin(t *testing.T) {
  184. defer prepareTestEnv(t)()
  185. testPrivateActivityDoSomethingForActionEntries(t)
  186. session := loginUser(t, privateActivityTestAdmin)
  187. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  188. assert.True(t, visible, "user should have visible heatmap")
  189. }
  190. // check heatmap visibility if the visibility is disabled
  191. // this behavior, in special the one for the admin, is
  192. // due to the fact that the heatmap is the same for all viewers;
  193. // otherwise, there is no reason for it
  194. func TestPrivateActivityYesHeatmapInvisibleForPublic(t *testing.T) {
  195. defer prepareTestEnv(t)()
  196. testPrivateActivityDoSomethingForActionEntries(t)
  197. testPrivateActivityHelperEnablePrivateActivity(t)
  198. visible := testPrivateActivityHelperHasVisibleHeatmapFromPublic(t)
  199. assert.False(t, visible, "user should have no visible heatmap")
  200. }
  201. func TestPrivateActivityYesHeatmapInvisibleForUserItselfAtProfile(t *testing.T) {
  202. defer prepareTestEnv(t)()
  203. testPrivateActivityDoSomethingForActionEntries(t)
  204. testPrivateActivityHelperEnablePrivateActivity(t)
  205. session := loginUser(t, privateActivityTestUser)
  206. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  207. assert.False(t, visible, "user should have no visible heatmap")
  208. }
  209. func TestPrivateActivityYesHeatmapInvisibleForUserItselfAtDashboard(t *testing.T) {
  210. defer prepareTestEnv(t)()
  211. testPrivateActivityDoSomethingForActionEntries(t)
  212. testPrivateActivityHelperEnablePrivateActivity(t)
  213. session := loginUser(t, privateActivityTestUser)
  214. visible := testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t, session)
  215. assert.False(t, visible, "user should have no visible heatmap")
  216. }
  217. func TestPrivateActivityYesHeatmapInvisibleForOtherUser(t *testing.T) {
  218. defer prepareTestEnv(t)()
  219. testPrivateActivityDoSomethingForActionEntries(t)
  220. testPrivateActivityHelperEnablePrivateActivity(t)
  221. session := loginUser(t, privateActivityTestOtherUser)
  222. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  223. assert.False(t, visible, "user should have no visible heatmap")
  224. }
  225. func TestPrivateActivityYesHeatmapInvsisibleForAdmin(t *testing.T) {
  226. defer prepareTestEnv(t)()
  227. testPrivateActivityDoSomethingForActionEntries(t)
  228. testPrivateActivityHelperEnablePrivateActivity(t)
  229. session := loginUser(t, privateActivityTestAdmin)
  230. visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
  231. assert.False(t, visible, "user should have no visible heatmap")
  232. }
  233. // check heatmap api provides content if the visibility is enabled
  234. func TestPrivateActivityNoHeatmapHasContentForPublic(t *testing.T) {
  235. defer prepareTestEnv(t)()
  236. testPrivateActivityDoSomethingForActionEntries(t)
  237. hasContent := testPrivateActivityHelperHasHeatmapContentFromPublic(t)
  238. assert.True(t, hasContent, "user should have heatmap content")
  239. }
  240. func TestPrivateActivityNoHeatmapHasContentForUserItself(t *testing.T) {
  241. defer prepareTestEnv(t)()
  242. testPrivateActivityDoSomethingForActionEntries(t)
  243. session := loginUser(t, privateActivityTestUser)
  244. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  245. assert.True(t, hasContent, "user should have heatmap content")
  246. }
  247. func TestPrivateActivityNoHeatmapHasContentForOtherUser(t *testing.T) {
  248. defer prepareTestEnv(t)()
  249. testPrivateActivityDoSomethingForActionEntries(t)
  250. session := loginUser(t, privateActivityTestOtherUser)
  251. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  252. assert.True(t, hasContent, "user should have heatmap content")
  253. }
  254. func TestPrivateActivityNoHeatmapHasContentForAdmin(t *testing.T) {
  255. defer prepareTestEnv(t)()
  256. testPrivateActivityDoSomethingForActionEntries(t)
  257. session := loginUser(t, privateActivityTestAdmin)
  258. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  259. assert.True(t, hasContent, "user should have heatmap content")
  260. }
  261. // check heatmap api provides no content if the visibility is disabled
  262. // this should be equal to the hidden heatmap at the UI
  263. func TestPrivateActivityYesHeatmapHasNoContentForPublic(t *testing.T) {
  264. defer prepareTestEnv(t)()
  265. testPrivateActivityDoSomethingForActionEntries(t)
  266. testPrivateActivityHelperEnablePrivateActivity(t)
  267. hasContent := testPrivateActivityHelperHasHeatmapContentFromPublic(t)
  268. assert.False(t, hasContent, "user should have no heatmap content")
  269. }
  270. func TestPrivateActivityYesHeatmapHasNoContentForUserItself(t *testing.T) {
  271. defer prepareTestEnv(t)()
  272. testPrivateActivityDoSomethingForActionEntries(t)
  273. testPrivateActivityHelperEnablePrivateActivity(t)
  274. session := loginUser(t, privateActivityTestUser)
  275. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  276. assert.False(t, hasContent, "user should have no heatmap content")
  277. }
  278. func TestPrivateActivityYesHeatmapHasNoContentForOtherUser(t *testing.T) {
  279. defer prepareTestEnv(t)()
  280. testPrivateActivityDoSomethingForActionEntries(t)
  281. testPrivateActivityHelperEnablePrivateActivity(t)
  282. session := loginUser(t, privateActivityTestOtherUser)
  283. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  284. assert.False(t, hasContent, "user should have no heatmap content")
  285. }
  286. func TestPrivateActivityYesHeatmapHasNoContentForAdmin(t *testing.T) {
  287. defer prepareTestEnv(t)()
  288. testPrivateActivityDoSomethingForActionEntries(t)
  289. testPrivateActivityHelperEnablePrivateActivity(t)
  290. session := loginUser(t, privateActivityTestAdmin)
  291. hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
  292. assert.False(t, hasContent, "user should have no heatmap content")
  293. }