summaryrefslogtreecommitdiffstats
path: root/tests/integration/repo_starwatch_test.go
blob: a8bad30109f120417a710b9c24c166fd176efc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"strings"
	"testing"

	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/test"
	"code.gitea.io/gitea/routers"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/assert"
)

func testRepoStarringOrWatching(t *testing.T, action, listURI string) {
	t.Helper()

	defer tests.PrepareTestEnv(t)()

	oppositeAction := "un" + action
	session := loginUser(t, "user5")

	// Star/Watch the repo as user5
	req := NewRequestWithValues(t, "POST", fmt.Sprintf("/user2/repo1/action/%s", action), map[string]string{
		"_csrf": GetCSRF(t, session, "/user2/repo1"),
	})
	session.MakeRequest(t, req, http.StatusOK)

	// Load the repo home as user5
	req = NewRequest(t, "GET", "/user2/repo1")
	resp := session.MakeRequest(t, req, http.StatusOK)

	// Verify that the star/watch button is now the opposite
	htmlDoc := NewHTMLParser(t, resp.Body)
	actionButton := htmlDoc.Find(fmt.Sprintf("form[action='/user2/repo1/action/%s']", oppositeAction))
	assert.Equal(t, 1, actionButton.Length())
	text := strings.ToLower(actionButton.Find("button span.text").Text())
	assert.Equal(t, oppositeAction, text)

	// Load stargazers/watchers as user5
	req = NewRequestf(t, "GET", "/user2/repo1/%s", listURI)
	resp = session.MakeRequest(t, req, http.StatusOK)

	// Verify that "user5" is among the stargazers/watchers
	htmlDoc = NewHTMLParser(t, resp.Body)
	htmlDoc.AssertElement(t, ".user-cards .list .card > a[href='/user5']", true)

	// Unstar/unwatch the repo as user5
	req = NewRequestWithValues(t, "POST", fmt.Sprintf("/user2/repo1/action/%s", oppositeAction), map[string]string{
		"_csrf": GetCSRF(t, session, "/user2/repo1"),
	})
	session.MakeRequest(t, req, http.StatusOK)

	// Load the repo home as user5
	req = NewRequest(t, "GET", "/user2/repo1")
	resp = session.MakeRequest(t, req, http.StatusOK)

	// Verify that the star/watch button is now back to its default
	htmlDoc = NewHTMLParser(t, resp.Body)
	actionButton = htmlDoc.Find(fmt.Sprintf("form[action='/user2/repo1/action/%s']", action))
	assert.Equal(t, 1, actionButton.Length())
	text = strings.ToLower(actionButton.Find("button span.text").Text())
	assert.Equal(t, action, text)

	// Load stargazers/watchers as user5
	req = NewRequestf(t, "GET", "/user2/repo1/%s", listURI)
	resp = session.MakeRequest(t, req, http.StatusOK)

	// Verify that "user5" is not among the stargazers/watchers
	htmlDoc = NewHTMLParser(t, resp.Body)
	htmlDoc.AssertElement(t, ".user-cards .list .item.ui.segment > a[href='/user5']", false)
}

func TestRepoStarUnstarUI(t *testing.T) {
	testRepoStarringOrWatching(t, "star", "stars")
}

func TestRepoWatchUnwatchUI(t *testing.T) {
	testRepoStarringOrWatching(t, "watch", "watchers")
}

func TestDisabledStars(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	defer test.MockVariableValue(&setting.Repository.DisableStars, true)()
	defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()

	t.Run("repo star, unstar", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "POST", "/user2/repo1/action/star")
		MakeRequest(t, req, http.StatusNotFound)

		req = NewRequest(t, "POST", "/user2/repo1/action/unstar")
		MakeRequest(t, req, http.StatusNotFound)
	})

	t.Run("repo stargazers", func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		req := NewRequest(t, "GET", "/user2/repo1/stars")
		MakeRequest(t, req, http.StatusNotFound)
	})
}