code.gitea.io/gitea@v1.21.7/tests/integration/privateactivity_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	activities_model "code.gitea.io/gitea/models/activities"
    12  	auth_model "code.gitea.io/gitea/models/auth"
    13  	repo_model "code.gitea.io/gitea/models/repo"
    14  	"code.gitea.io/gitea/models/unittest"
    15  	user_model "code.gitea.io/gitea/models/user"
    16  	api "code.gitea.io/gitea/modules/structs"
    17  	"code.gitea.io/gitea/tests"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  const (
    23  	privateActivityTestAdmin = "user1"
    24  	privateActivityTestUser  = "user2"
    25  )
    26  
    27  // org3 is an organization so it is not usable here
    28  const privateActivityTestOtherUser = "user4"
    29  
    30  // activity helpers
    31  
    32  func testPrivateActivityDoSomethingForActionEntries(t *testing.T) {
    33  	repoBefore := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    34  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repoBefore.OwnerID})
    35  
    36  	session := loginUser(t, privateActivityTestUser)
    37  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    38  	urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repoBefore.Name, token)
    39  	req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
    40  		Body:  "test",
    41  		Title: "test",
    42  	})
    43  	session.MakeRequest(t, req, http.StatusCreated)
    44  }
    45  
    46  // private activity helpers
    47  
    48  func testPrivateActivityHelperEnablePrivateActivity(t *testing.T) {
    49  	session := loginUser(t, privateActivityTestUser)
    50  	req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
    51  		"_csrf":                 GetCSRF(t, session, "/user/settings"),
    52  		"name":                  privateActivityTestUser,
    53  		"email":                 privateActivityTestUser + "@example.com",
    54  		"language":              "en-US",
    55  		"keep_activity_private": "1",
    56  	})
    57  	session.MakeRequest(t, req, http.StatusSeeOther)
    58  }
    59  
    60  func testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc *HTMLDoc) bool {
    61  	return htmlDoc.doc.Find("#activity-feed").Find(".flex-item").Length() > 0
    62  }
    63  
    64  func testPrivateActivityHelperHasVisibleActivitiesFromSession(t *testing.T, session *TestSession) bool {
    65  	req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
    66  	resp := session.MakeRequest(t, req, http.StatusOK)
    67  
    68  	htmlDoc := NewHTMLParser(t, resp.Body)
    69  
    70  	return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
    71  }
    72  
    73  func testPrivateActivityHelperHasVisibleActivitiesFromPublic(t *testing.T) bool {
    74  	req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
    75  	resp := MakeRequest(t, req, http.StatusOK)
    76  
    77  	htmlDoc := NewHTMLParser(t, resp.Body)
    78  
    79  	return testPrivateActivityHelperHasVisibleActivitiesInHTMLDoc(htmlDoc)
    80  }
    81  
    82  // heatmap UI helpers
    83  
    84  func testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc *HTMLDoc) bool {
    85  	return htmlDoc.doc.Find("#user-heatmap").Length() > 0
    86  }
    87  
    88  func testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t *testing.T, session *TestSession) bool {
    89  	req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
    90  	resp := session.MakeRequest(t, req, http.StatusOK)
    91  
    92  	htmlDoc := NewHTMLParser(t, resp.Body)
    93  
    94  	return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
    95  }
    96  
    97  func testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t *testing.T, session *TestSession) bool {
    98  	req := NewRequest(t, "GET", "/")
    99  	resp := session.MakeRequest(t, req, http.StatusOK)
   100  
   101  	htmlDoc := NewHTMLParser(t, resp.Body)
   102  
   103  	return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
   104  }
   105  
   106  func testPrivateActivityHelperHasVisibleHeatmapFromPublic(t *testing.T) bool {
   107  	req := NewRequestf(t, "GET", "/%s?tab=activity", privateActivityTestUser)
   108  	resp := MakeRequest(t, req, http.StatusOK)
   109  
   110  	htmlDoc := NewHTMLParser(t, resp.Body)
   111  
   112  	return testPrivateActivityHelperHasVisibleHeatmapInHTMLDoc(htmlDoc)
   113  }
   114  
   115  // heatmap API helpers
   116  
   117  func testPrivateActivityHelperHasHeatmapContentFromPublic(t *testing.T) bool {
   118  	req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap", privateActivityTestUser)
   119  	resp := MakeRequest(t, req, http.StatusOK)
   120  
   121  	var items []*activities_model.UserHeatmapData
   122  	DecodeJSON(t, resp, &items)
   123  
   124  	return len(items) != 0
   125  }
   126  
   127  func testPrivateActivityHelperHasHeatmapContentFromSession(t *testing.T, session *TestSession) bool {
   128  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
   129  
   130  	req := NewRequestf(t, "GET", "/api/v1/users/%s/heatmap?token=%s", privateActivityTestUser, token)
   131  	resp := session.MakeRequest(t, req, http.StatusOK)
   132  
   133  	var items []*activities_model.UserHeatmapData
   134  	DecodeJSON(t, resp, &items)
   135  
   136  	return len(items) != 0
   137  }
   138  
   139  // check activity visibility if the visibility is enabled
   140  
   141  func TestPrivateActivityNoVisibleForPublic(t *testing.T) {
   142  	defer tests.PrepareTestEnv(t)()
   143  	testPrivateActivityDoSomethingForActionEntries(t)
   144  
   145  	visible := testPrivateActivityHelperHasVisibleActivitiesFromPublic(t)
   146  
   147  	assert.True(t, visible, "user should have visible activities")
   148  }
   149  
   150  func TestPrivateActivityNoVisibleForUserItself(t *testing.T) {
   151  	defer tests.PrepareTestEnv(t)()
   152  	testPrivateActivityDoSomethingForActionEntries(t)
   153  
   154  	session := loginUser(t, privateActivityTestUser)
   155  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   156  
   157  	assert.True(t, visible, "user should have visible activities")
   158  }
   159  
   160  func TestPrivateActivityNoVisibleForOtherUser(t *testing.T) {
   161  	defer tests.PrepareTestEnv(t)()
   162  	testPrivateActivityDoSomethingForActionEntries(t)
   163  
   164  	session := loginUser(t, privateActivityTestOtherUser)
   165  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   166  
   167  	assert.True(t, visible, "user should have visible activities")
   168  }
   169  
   170  func TestPrivateActivityNoVisibleForAdmin(t *testing.T) {
   171  	defer tests.PrepareTestEnv(t)()
   172  	testPrivateActivityDoSomethingForActionEntries(t)
   173  
   174  	session := loginUser(t, privateActivityTestAdmin)
   175  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   176  
   177  	assert.True(t, visible, "user should have visible activities")
   178  }
   179  
   180  // check activity visibility if the visibility is disabled
   181  
   182  func TestPrivateActivityYesInvisibleForPublic(t *testing.T) {
   183  	defer tests.PrepareTestEnv(t)()
   184  	testPrivateActivityDoSomethingForActionEntries(t)
   185  	testPrivateActivityHelperEnablePrivateActivity(t)
   186  
   187  	visible := testPrivateActivityHelperHasVisibleActivitiesFromPublic(t)
   188  
   189  	assert.False(t, visible, "user should have no visible activities")
   190  }
   191  
   192  func TestPrivateActivityYesVisibleForUserItself(t *testing.T) {
   193  	defer tests.PrepareTestEnv(t)()
   194  	testPrivateActivityDoSomethingForActionEntries(t)
   195  	testPrivateActivityHelperEnablePrivateActivity(t)
   196  
   197  	session := loginUser(t, privateActivityTestUser)
   198  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   199  
   200  	assert.True(t, visible, "user should have visible activities")
   201  }
   202  
   203  func TestPrivateActivityYesInvisibleForOtherUser(t *testing.T) {
   204  	defer tests.PrepareTestEnv(t)()
   205  	testPrivateActivityDoSomethingForActionEntries(t)
   206  	testPrivateActivityHelperEnablePrivateActivity(t)
   207  
   208  	session := loginUser(t, privateActivityTestOtherUser)
   209  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   210  
   211  	assert.False(t, visible, "user should have no visible activities")
   212  }
   213  
   214  func TestPrivateActivityYesVisibleForAdmin(t *testing.T) {
   215  	defer tests.PrepareTestEnv(t)()
   216  	testPrivateActivityDoSomethingForActionEntries(t)
   217  	testPrivateActivityHelperEnablePrivateActivity(t)
   218  
   219  	session := loginUser(t, privateActivityTestAdmin)
   220  	visible := testPrivateActivityHelperHasVisibleActivitiesFromSession(t, session)
   221  
   222  	assert.True(t, visible, "user should have visible activities")
   223  }
   224  
   225  // check heatmap visibility if the visibility is enabled
   226  
   227  func TestPrivateActivityNoHeatmapVisibleForPublic(t *testing.T) {
   228  	defer tests.PrepareTestEnv(t)()
   229  	testPrivateActivityDoSomethingForActionEntries(t)
   230  
   231  	visible := testPrivateActivityHelperHasVisibleHeatmapFromPublic(t)
   232  
   233  	assert.True(t, visible, "user should have visible heatmap")
   234  }
   235  
   236  func TestPrivateActivityNoHeatmapVisibleForUserItselfAtProfile(t *testing.T) {
   237  	defer tests.PrepareTestEnv(t)()
   238  	testPrivateActivityDoSomethingForActionEntries(t)
   239  
   240  	session := loginUser(t, privateActivityTestUser)
   241  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   242  
   243  	assert.True(t, visible, "user should have visible heatmap")
   244  }
   245  
   246  func TestPrivateActivityNoHeatmapVisibleForUserItselfAtDashboard(t *testing.T) {
   247  	defer tests.PrepareTestEnv(t)()
   248  	testPrivateActivityDoSomethingForActionEntries(t)
   249  
   250  	session := loginUser(t, privateActivityTestUser)
   251  	visible := testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t, session)
   252  
   253  	assert.True(t, visible, "user should have visible heatmap")
   254  }
   255  
   256  func TestPrivateActivityNoHeatmapVisibleForOtherUser(t *testing.T) {
   257  	defer tests.PrepareTestEnv(t)()
   258  	testPrivateActivityDoSomethingForActionEntries(t)
   259  
   260  	session := loginUser(t, privateActivityTestOtherUser)
   261  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   262  
   263  	assert.True(t, visible, "user should have visible heatmap")
   264  }
   265  
   266  func TestPrivateActivityNoHeatmapVisibleForAdmin(t *testing.T) {
   267  	defer tests.PrepareTestEnv(t)()
   268  	testPrivateActivityDoSomethingForActionEntries(t)
   269  
   270  	session := loginUser(t, privateActivityTestAdmin)
   271  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   272  
   273  	assert.True(t, visible, "user should have visible heatmap")
   274  }
   275  
   276  // check heatmap visibility if the visibility is disabled
   277  
   278  func TestPrivateActivityYesHeatmapInvisibleForPublic(t *testing.T) {
   279  	defer tests.PrepareTestEnv(t)()
   280  	testPrivateActivityDoSomethingForActionEntries(t)
   281  	testPrivateActivityHelperEnablePrivateActivity(t)
   282  
   283  	visible := testPrivateActivityHelperHasVisibleHeatmapFromPublic(t)
   284  
   285  	assert.False(t, visible, "user should have no visible heatmap")
   286  }
   287  
   288  func TestPrivateActivityYesHeatmapVisibleForUserItselfAtProfile(t *testing.T) {
   289  	defer tests.PrepareTestEnv(t)()
   290  	testPrivateActivityDoSomethingForActionEntries(t)
   291  	testPrivateActivityHelperEnablePrivateActivity(t)
   292  
   293  	session := loginUser(t, privateActivityTestUser)
   294  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   295  
   296  	assert.True(t, visible, "user should have visible heatmap")
   297  }
   298  
   299  func TestPrivateActivityYesHeatmapVisibleForUserItselfAtDashboard(t *testing.T) {
   300  	defer tests.PrepareTestEnv(t)()
   301  	testPrivateActivityDoSomethingForActionEntries(t)
   302  	testPrivateActivityHelperEnablePrivateActivity(t)
   303  
   304  	session := loginUser(t, privateActivityTestUser)
   305  	visible := testPrivateActivityHelperHasVisibleDashboardHeatmapFromSession(t, session)
   306  
   307  	assert.True(t, visible, "user should have visible heatmap")
   308  }
   309  
   310  func TestPrivateActivityYesHeatmapInvisibleForOtherUser(t *testing.T) {
   311  	defer tests.PrepareTestEnv(t)()
   312  	testPrivateActivityDoSomethingForActionEntries(t)
   313  	testPrivateActivityHelperEnablePrivateActivity(t)
   314  
   315  	session := loginUser(t, privateActivityTestOtherUser)
   316  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   317  
   318  	assert.False(t, visible, "user should have no visible heatmap")
   319  }
   320  
   321  func TestPrivateActivityYesHeatmapVisibleForAdmin(t *testing.T) {
   322  	defer tests.PrepareTestEnv(t)()
   323  	testPrivateActivityDoSomethingForActionEntries(t)
   324  	testPrivateActivityHelperEnablePrivateActivity(t)
   325  
   326  	session := loginUser(t, privateActivityTestAdmin)
   327  	visible := testPrivateActivityHelperHasVisibleProfileHeatmapFromSession(t, session)
   328  
   329  	assert.True(t, visible, "user should have visible heatmap")
   330  }
   331  
   332  // check heatmap api provides content if the visibility is enabled
   333  
   334  func TestPrivateActivityNoHeatmapHasContentForPublic(t *testing.T) {
   335  	defer tests.PrepareTestEnv(t)()
   336  	testPrivateActivityDoSomethingForActionEntries(t)
   337  
   338  	hasContent := testPrivateActivityHelperHasHeatmapContentFromPublic(t)
   339  
   340  	assert.True(t, hasContent, "user should have heatmap content")
   341  }
   342  
   343  func TestPrivateActivityNoHeatmapHasContentForUserItself(t *testing.T) {
   344  	defer tests.PrepareTestEnv(t)()
   345  	testPrivateActivityDoSomethingForActionEntries(t)
   346  
   347  	session := loginUser(t, privateActivityTestUser)
   348  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   349  
   350  	assert.True(t, hasContent, "user should have heatmap content")
   351  }
   352  
   353  func TestPrivateActivityNoHeatmapHasContentForOtherUser(t *testing.T) {
   354  	defer tests.PrepareTestEnv(t)()
   355  	testPrivateActivityDoSomethingForActionEntries(t)
   356  
   357  	session := loginUser(t, privateActivityTestOtherUser)
   358  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   359  
   360  	assert.True(t, hasContent, "user should have heatmap content")
   361  }
   362  
   363  func TestPrivateActivityNoHeatmapHasContentForAdmin(t *testing.T) {
   364  	defer tests.PrepareTestEnv(t)()
   365  	testPrivateActivityDoSomethingForActionEntries(t)
   366  
   367  	session := loginUser(t, privateActivityTestAdmin)
   368  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   369  
   370  	assert.True(t, hasContent, "user should have heatmap content")
   371  }
   372  
   373  // check heatmap api provides no content if the visibility is disabled
   374  // this should be equal to the hidden heatmap at the UI
   375  
   376  func TestPrivateActivityYesHeatmapHasNoContentForPublic(t *testing.T) {
   377  	defer tests.PrepareTestEnv(t)()
   378  	testPrivateActivityDoSomethingForActionEntries(t)
   379  	testPrivateActivityHelperEnablePrivateActivity(t)
   380  
   381  	hasContent := testPrivateActivityHelperHasHeatmapContentFromPublic(t)
   382  
   383  	assert.False(t, hasContent, "user should have no heatmap content")
   384  }
   385  
   386  func TestPrivateActivityYesHeatmapHasNoContentForUserItself(t *testing.T) {
   387  	defer tests.PrepareTestEnv(t)()
   388  	testPrivateActivityDoSomethingForActionEntries(t)
   389  	testPrivateActivityHelperEnablePrivateActivity(t)
   390  
   391  	session := loginUser(t, privateActivityTestUser)
   392  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   393  
   394  	assert.True(t, hasContent, "user should see their own heatmap content")
   395  }
   396  
   397  func TestPrivateActivityYesHeatmapHasNoContentForOtherUser(t *testing.T) {
   398  	defer tests.PrepareTestEnv(t)()
   399  	testPrivateActivityDoSomethingForActionEntries(t)
   400  	testPrivateActivityHelperEnablePrivateActivity(t)
   401  
   402  	session := loginUser(t, privateActivityTestOtherUser)
   403  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   404  
   405  	assert.False(t, hasContent, "other user should not see heatmap content")
   406  }
   407  
   408  func TestPrivateActivityYesHeatmapHasNoContentForAdmin(t *testing.T) {
   409  	defer tests.PrepareTestEnv(t)()
   410  	testPrivateActivityDoSomethingForActionEntries(t)
   411  	testPrivateActivityHelperEnablePrivateActivity(t)
   412  
   413  	session := loginUser(t, privateActivityTestAdmin)
   414  	hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
   415  
   416  	assert.True(t, hasContent, "heatmap should show content for admin")
   417  }