[UI] Actions: Improve frontend testing

This commit is contained in:
Panagiotis "Ivory" Vasilopoulos 2024-02-22 22:12:44 +01:00
parent 1668904513
commit 785f336c12

View file

@ -22,6 +22,15 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func GetWorkflowRunRedirectURI(t *testing.T, repo_url string, workflow string) string {
t.Helper()
req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo_url, workflow))
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
return resp.Header().Get("Location")
}
func TestActionsWebRouteLatestWorkflowRun(t *testing.T) { func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) { onGiteaRun(t, func(t *testing.T, u *url.URL) {
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
@ -44,13 +53,7 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
) )
defer f() defer f()
// helpers repoURL := repo.HTMLURL()
getWorkflowRunRedirectURI := func(workflow string) string {
req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/%s/runs/latest", repo.HTMLURL(), workflow))
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
return resp.Header().Get("Location")
}
t.Run("valid workflows", func(t *testing.T) { t.Run("valid workflows", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
@ -59,8 +62,8 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
assert.Equal(t, 2, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID})) assert.Equal(t, 2, unittest.GetCount(t, &actions_model.ActionRun{RepoID: repo.ID}))
// Get the redirect URIs for both workflows // Get the redirect URIs for both workflows
workflowOneURI := getWorkflowRunRedirectURI("workflow-1.yml") workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-1.yml")
workflowTwoURI := getWorkflowRunRedirectURI("workflow-2.yml") workflowTwoURI := GetWorkflowRunRedirectURI(t, repoURL, "workflow-2.yml")
// Verify that the two are different. // Verify that the two are different.
assert.NotEqual(t, workflowOneURI, workflowTwoURI) assert.NotEqual(t, workflowOneURI, workflowTwoURI)
@ -82,7 +85,7 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
// Get the redirect URI // Get the redirect URI
workflow := "workflow-1.yml" workflow := "workflow-1.yml"
workflowOneURI := getWorkflowRunRedirectURI(workflow) workflowOneURI := GetWorkflowRunRedirectURI(t, repoURL, workflow)
// Fetch the page that shows information about the run initiated by "workflow-1.yml". // Fetch the page that shows information about the run initiated by "workflow-1.yml".
// routers/web/repo/actions/view.go: data-workflow-url is constructed using data-workflow-name. // routers/web/repo/actions/view.go: data-workflow-url is constructed using data-workflow-name.
@ -91,21 +94,21 @@ func TestActionsWebRouteLatestWorkflowRun(t *testing.T) {
htmlDoc := NewHTMLParser(t, resp.Body) htmlDoc := NewHTMLParser(t, resp.Body)
// Verify that URL of the workflow is shown correctly. // Verify that URL of the workflow is shown correctly.
rightURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow) expectedURL := fmt.Sprintf("/user2/actionsTestRepo/actions?workflow=%s", workflow)
htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", rightURL), true) htmlDoc.AssertElement(t, fmt.Sprintf("#repo-action-view[data-workflow-url=\"%s\"]", expectedURL), true)
}) })
t.Run("existing workflow, non-existent branch", func(t *testing.T) { t.Run("existing workflow, non-existent branch", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar", repo.HTMLURL())) req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-1.yml/runs/latest?branch=foobar", repoURL))
MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
}) })
t.Run("non-existing workflow", func(t *testing.T) { t.Run("non-existing workflow", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-3.yml/runs/latest", repo.HTMLURL())) req := NewRequest(t, "GET", fmt.Sprintf("%s/actions/workflows/workflow-3.yml/runs/latest", repoURL))
MakeRequest(t, req, http.StatusNotFound) MakeRequest(t, req, http.StatusNotFound)
}) })
}) })