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

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"code.gitea.io/gitea/modules/translation"
    12  	"code.gitea.io/gitea/tests"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestViewBranches(t *testing.T) {
    18  	defer tests.PrepareTestEnv(t)()
    19  
    20  	req := NewRequest(t, "GET", "/user2/repo1/branches")
    21  	resp := MakeRequest(t, req, http.StatusOK)
    22  
    23  	htmlDoc := NewHTMLParser(t, resp.Body)
    24  	_, exists := htmlDoc.doc.Find(".delete-branch-button").Attr("data-url")
    25  	assert.False(t, exists, "The template has changed")
    26  }
    27  
    28  func TestDeleteBranch(t *testing.T) {
    29  	defer tests.PrepareTestEnv(t)()
    30  
    31  	deleteBranch(t)
    32  }
    33  
    34  func TestUndoDeleteBranch(t *testing.T) {
    35  	onGiteaRun(t, func(t *testing.T, u *url.URL) {
    36  		deleteBranch(t)
    37  		htmlDoc, name := branchAction(t, ".restore-branch-button")
    38  		assert.Contains(t,
    39  			htmlDoc.doc.Find(".ui.positive.message").Text(),
    40  			translation.NewLocale("en-US").Tr("repo.branch.restore_success", name),
    41  		)
    42  	})
    43  }
    44  
    45  func deleteBranch(t *testing.T) {
    46  	htmlDoc, name := branchAction(t, ".delete-branch-button")
    47  	assert.Contains(t,
    48  		htmlDoc.doc.Find(".ui.positive.message").Text(),
    49  		translation.NewLocale("en-US").Tr("repo.branch.deletion_success", name),
    50  	)
    51  }
    52  
    53  func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
    54  	session := loginUser(t, "user2")
    55  	req := NewRequest(t, "GET", "/user2/repo1/branches")
    56  	resp := session.MakeRequest(t, req, http.StatusOK)
    57  
    58  	htmlDoc := NewHTMLParser(t, resp.Body)
    59  	link, exists := htmlDoc.doc.Find(button).Attr("data-url")
    60  	if !assert.True(t, exists, "The template has changed") {
    61  		t.Skip()
    62  	}
    63  
    64  	req = NewRequestWithValues(t, "POST", link, map[string]string{
    65  		"_csrf": htmlDoc.GetCSRF(),
    66  	})
    67  	session.MakeRequest(t, req, http.StatusOK)
    68  
    69  	url, err := url.Parse(link)
    70  	assert.NoError(t, err)
    71  	req = NewRequest(t, "GET", "/user2/repo1/branches")
    72  	resp = session.MakeRequest(t, req, http.StatusOK)
    73  
    74  	return NewHTMLParser(t, resp.Body), url.Query().Get("name")
    75  }