From 7e6fe41389cd04a1e4c5688560a9c67ccbdf5d67 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 26 Feb 2024 09:14:11 +0100 Subject: [PATCH] Add tests for the star/unstar & watch/unwatch UI Signed-off-by: Gergely Nagy --- tests/integration/repo_starwatch_test.go | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/integration/repo_starwatch_test.go diff --git a/tests/integration/repo_starwatch_test.go b/tests/integration/repo_starwatch_test.go new file mode 100644 index 000000000..4b4f96772 --- /dev/null +++ b/tests/integration/repo_starwatch_test.go @@ -0,0 +1,82 @@ +// 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/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.True(t, actionButton.Length() == 1) + 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 .item.ui.segment > 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.True(t, actionButton.Length() == 1) + 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") +}