summaryrefslogtreecommitdiffstats
path: root/tests/integration/repo_citation_test.go
blob: 4f7e24ee5122292005ae5c5691ef050e4f46f2a3 (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
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later

package integration

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

	repo_model "code.gitea.io/gitea/models/repo"
	unit_model "code.gitea.io/gitea/models/unit"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	files_service "code.gitea.io/gitea/services/repository/files"
	"code.gitea.io/gitea/tests"

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

func TestCitation(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, u *url.URL) {
		user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})

		session := loginUser(t, user.LoginName)

		t.Run("No citation", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			repo, _, f := tests.CreateDeclarativeRepo(t, user, "citation-no-citation", []unit_model.Type{unit_model.TypeCode}, nil, nil)
			defer f()

			testCitationButtonExists(t, session, repo, "", false)
		})

		t.Run("cff citation", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			repo, f := createRepoWithEmptyFile(t, user, "citation-cff", "CITATION.cff")
			defer f()

			testCitationButtonExists(t, session, repo, "CITATION.cff", true)
		})

		t.Run("bib citation", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			repo, f := createRepoWithEmptyFile(t, user, "citation-bib", "CITATION.bib")
			defer f()

			testCitationButtonExists(t, session, repo, "CITATION.bib", true)
		})
	})
}

func testCitationButtonExists(t *testing.T, session *TestSession, repo *repo_model.Repository, file string, exists bool) {
	req := NewRequest(t, "GET", repo.HTMLURL())
	resp := session.MakeRequest(t, req, http.StatusOK)
	doc := NewHTMLParser(t, resp.Body)

	doc.AssertElement(t, "#cite-repo-button", exists)

	if exists {
		href, exists := doc.doc.Find("#goto-citation-btn").Attr("href")
		assert.True(t, exists)

		assert.True(t, strings.HasSuffix(href, file))
	}
}

func createRepoWithEmptyFile(t *testing.T, user *user_model.User, repoName, fileName string) (*repo_model.Repository, func()) {
	repo, _, f := tests.CreateDeclarativeRepo(t, user, repoName, []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{
		{
			Operation: "create",
			TreePath:  fileName,
		},
	})

	return repo, f
}