summaryrefslogtreecommitdiffstats
path: root/tests/integration/api_issue_templates_test.go
blob: d634329514d210c86fb2e7d76d40de7783e79573 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

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

	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/tests"

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

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

		t.Run("no templates", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
			resp := MakeRequest(t, req, http.StatusOK)
			var issueTemplates []*api.IssueTemplate
			DecodeJSON(t, resp, &issueTemplates)
			assert.Empty(t, issueTemplates)
		})

		t.Run("existing template", func(t *testing.T) {
			templateCandidates := []string{
				".forgejo/ISSUE_TEMPLATE/test.md",
				".forgejo/issue_template/test.md",
				".gitea/ISSUE_TEMPLATE/test.md",
				".gitea/issue_template/test.md",
				".github/ISSUE_TEMPLATE/test.md",
				".github/issue_template/test.md",
			}

			for _, template := range templateCandidates {
				t.Run(template, func(t *testing.T) {
					defer tests.PrintCurrentTest(t)()
					defer func() {
						deleteFileInBranch(user, repo, template, repo.DefaultBranch)
					}()

					err := createOrReplaceFileInBranch(user, repo, template, repo.DefaultBranch,
						`---
name: 'Template Name'
about: 'This template is for testing!'
title: '[TEST] '
ref: 'main'
---

This is the template!`)
					require.NoError(t, err)

					req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
					resp := MakeRequest(t, req, http.StatusOK)
					var issueTemplates []*api.IssueTemplate
					DecodeJSON(t, resp, &issueTemplates)
					assert.Len(t, issueTemplates, 1)
					assert.Equal(t, "Template Name", issueTemplates[0].Name)
					assert.Equal(t, "This template is for testing!", issueTemplates[0].About)
					assert.Equal(t, "refs/heads/main", issueTemplates[0].Ref)
					assert.Equal(t, template, issueTemplates[0].FileName)
				})
			}
		})

		t.Run("multiple templates", func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()
			templatePriority := []string{
				".forgejo/issue_template/test.md",
				".gitea/issue_template/test.md",
				".github/issue_template/test.md",
			}
			defer func() {
				for _, template := range templatePriority {
					deleteFileInBranch(user, repo, template, repo.DefaultBranch)
				}
			}()

			for _, template := range templatePriority {
				err := createOrReplaceFileInBranch(user, repo, template, repo.DefaultBranch,
					`---
name: 'Template Name'
about: 'This template is for testing!'
title: '[TEST] '
ref: 'main'
---

This is the template!`)
				require.NoError(t, err)
			}

			req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/issue_templates", repo.FullName()))
			resp := MakeRequest(t, req, http.StatusOK)
			var issueTemplates []*api.IssueTemplate
			DecodeJSON(t, resp, &issueTemplates)

			// If templates have the same filename and content, but in different
			// directories, they count as different templates, and all are
			// considered.
			assert.Len(t, issueTemplates, 3)
		})
	})
}