code.gitea.io/gitea@v1.22.3/tests/integration/pull_compare_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  	"fmt"
     8  	"net/http"
     9  	"net/url"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/models/db"
    13  	issues_model "code.gitea.io/gitea/models/issues"
    14  	repo_model "code.gitea.io/gitea/models/repo"
    15  	"code.gitea.io/gitea/models/unittest"
    16  	user_model "code.gitea.io/gitea/models/user"
    17  	repo_service "code.gitea.io/gitea/services/repository"
    18  	"code.gitea.io/gitea/tests"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestPullCompare(t *testing.T) {
    24  	defer tests.PrepareTestEnv(t)()
    25  
    26  	session := loginUser(t, "user2")
    27  	req := NewRequest(t, "GET", "/user2/repo1/pulls")
    28  	resp := session.MakeRequest(t, req, http.StatusOK)
    29  	htmlDoc := NewHTMLParser(t, resp.Body)
    30  	link, exists := htmlDoc.doc.Find(".new-pr-button").Attr("href")
    31  	assert.True(t, exists, "The template has changed")
    32  
    33  	req = NewRequest(t, "GET", link)
    34  	resp = session.MakeRequest(t, req, http.StatusOK)
    35  	assert.EqualValues(t, http.StatusOK, resp.Code)
    36  
    37  	// test the edit button in the PR diff view
    38  	req = NewRequest(t, "GET", "/user2/repo1/pulls/3/files")
    39  	resp = session.MakeRequest(t, req, http.StatusOK)
    40  	doc := NewHTMLParser(t, resp.Body)
    41  	editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
    42  	assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
    43  
    44  	onGiteaRun(t, func(t *testing.T, u *url.URL) {
    45  		defer tests.PrepareTestEnv(t)()
    46  
    47  		session := loginUser(t, "user1")
    48  		testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
    49  		testCreateBranch(t, session, "user1", "repo1", "branch/master", "master1", http.StatusSeeOther)
    50  		testEditFile(t, session, "user1", "repo1", "master1", "README.md", "Hello, World (Edited)\n")
    51  		testPullCreate(t, session, "user1", "repo1", false, "master", "master1", "This is a pull title")
    52  
    53  		repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
    54  		issueIndex := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueIndex{GroupID: repo1.ID}, unittest.OrderBy("group_id ASC"))
    55  		prFilesURL := fmt.Sprintf("/user2/repo1/pulls/%d/files", issueIndex.MaxIndex)
    56  		req = NewRequest(t, "GET", prFilesURL)
    57  		resp = session.MakeRequest(t, req, http.StatusOK)
    58  		doc := NewHTMLParser(t, resp.Body)
    59  		editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
    60  		assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
    61  
    62  		repoForked := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
    63  		user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    64  
    65  		// delete the head repository and revisit the PR diff view
    66  		err := repo_service.DeleteRepositoryDirectly(db.DefaultContext, user2, repoForked.ID)
    67  		assert.NoError(t, err)
    68  
    69  		req = NewRequest(t, "GET", prFilesURL)
    70  		resp = session.MakeRequest(t, req, http.StatusOK)
    71  		doc = NewHTMLParser(t, resp.Body)
    72  		editButtonCount = doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
    73  		assert.EqualValues(t, editButtonCount, 0, "Expected not to find a button to edit a file in the PR diff view because head repository has been deleted")
    74  	})
    75  }