summaryrefslogtreecommitdiffstats
path: root/tests/integration/opengraph_test.go
blob: 89e2a85acdb7da759922564e49e612d1ccc34339 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"image"
	"net/http"
	"testing"

	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/tests"

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

func TestOpenGraphProperties(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	siteName := "Forgejo: Beyond coding. We Forge."

	cases := []struct {
		name     string
		url      string
		expected map[string]string
	}{
		{
			name: "website root",
			url:  "/",
			expected: map[string]string{
				"og:title":       siteName,
				"og:url":         setting.AppURL,
				"og:description": "Forgejo is a self-hosted lightweight software forge. Easy to install and low maintenance, it just does the job.",
				"og:type":        "website",
				"og:image":       "/assets/img/logo.png",
				"og:site_name":   siteName,
			},
		},
		{
			name: "profile page without description",
			url:  "/user30",
			expected: map[string]string{
				"og:title":     "User Thirty",
				"og:url":       setting.AppURL + "user30",
				"og:type":      "profile",
				"og:image":     setting.AppURL + "assets/img/avatar_default.png",
				"og:site_name": siteName,
			},
		},
		{
			name: "profile page with description",
			url:  "/the_34-user.with.all.allowedchars",
			expected: map[string]string{
				"og:title":       "the_1-user.with.all.allowedChars",
				"og:url":         setting.AppURL + "the_34-user.with.all.allowedChars",
				"og:description": "some [commonmark](https://commonmark.org/)!",
				"og:type":        "profile",
				"og:image":       setting.AppURL + "assets/img/avatar_default.png",
				"og:site_name":   siteName,
			},
		},
		{
			name: "issue",
			url:  "/user2/repo1/issues/1",
			expected: map[string]string{
				"og:title":        "issue1",
				"og:url":          setting.AppURL + "user2/repo1/issues/1",
				"og:description":  "content for the first issue",
				"og:type":         "object",
				"og:image":        setting.AppURL + "user2/repo1/issues/1/summary-card",
				"og:image:alt":    "Summary card of an issue titled \"issue1\" in repository user2/repo1",
				"og:image:width":  "1200",
				"og:image:height": "600",
				"og:site_name":    siteName,
			},
		},
		{
			name: "pull request",
			url:  "/user2/repo1/pulls/2",
			expected: map[string]string{
				"og:title":        "issue2",
				"og:url":          setting.AppURL + "user2/repo1/pulls/2",
				"og:description":  "content for the second issue",
				"og:type":         "object",
				"og:image":        setting.AppURL + "user2/repo1/pulls/2/summary-card",
				"og:image:alt":    "Summary card of an issue titled \"issue2\" in repository user2/repo1",
				"og:image:width":  "1200",
				"og:image:height": "600",
				"og:site_name":    siteName,
			},
		},
		{
			name: "file in repo",
			url:  "/user27/repo49/src/branch/master/test/test.txt",
			expected: map[string]string{
				"og:title":     "repo49/test/test.txt at master",
				"og:url":       setting.AppURL + "/user27/repo49/src/branch/master/test/test.txt",
				"og:type":      "object",
				"og:image":     setting.AppURL + "user27/repo49/-/summary-card",
				"og:site_name": siteName,
			},
		},
		{
			name: "wiki page for repo without description",
			url:  "/user2/repo1/wiki/Page-With-Spaced-Name",
			expected: map[string]string{
				"og:title":     "Page With Spaced Name",
				"og:url":       setting.AppURL + "/user2/repo1/wiki/Page-With-Spaced-Name",
				"og:type":      "object",
				"og:image":     setting.AppURL + "user2/repo1/-/summary-card",
				"og:site_name": siteName,
			},
		},
		{
			name: "index page for repo without description",
			url:  "/user2/repo1",
			expected: map[string]string{
				"og:title":     "repo1",
				"og:url":       setting.AppURL + "user2/repo1",
				"og:type":      "object",
				"og:image":     setting.AppURL + "user2/repo1/-/summary-card",
				"og:site_name": siteName,
			},
		},
		{
			name: "index page for repo with description",
			url:  "/user27/repo49",
			expected: map[string]string{
				"og:title":       "repo49",
				"og:url":         setting.AppURL + "user27/repo49",
				"og:description": "A wonderful repository with more than just a README.md",
				"og:type":        "object",
				"og:image":       setting.AppURL + "user27/repo49/-/summary-card",
				"og:site_name":   siteName,
			},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", tc.url)
			resp := MakeRequest(t, req, http.StatusOK)
			doc := NewHTMLParser(t, resp.Body)

			foundProps := make(map[string]string)
			doc.Find("head meta[property^=\"og:\"]").Each(func(_ int, selection *goquery.Selection) {
				prop, foundProp := selection.Attr("property")
				assert.True(t, foundProp)
				content, foundContent := selection.Attr("content")
				assert.True(t, foundContent, "opengraph meta tag without a content property")
				foundProps[prop] = content
			})

			assert.EqualValues(t, tc.expected, foundProps, "mismatching opengraph properties")
		})
	}
}

func TestOpenGraphSummaryCard(t *testing.T) {
	defer tests.PrepareTestEnv(t)()

	cases := []struct {
		name string
		url  string
	}{
		{
			name: "repo",
			url:  "/user2/repo1/-/summary-card",
		},
		{
			name: "issue",
			url:  "/user2/repo1/issues/1/summary-card",
		},
		{
			name: "pull request",
			url:  "/user2/repo1/pulls/2/summary-card",
		},
		{
			name: "release",
			url:  "/user2/repo1/releases/summary-card/1",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			req := NewRequest(t, "GET", tc.url)
			resp := MakeRequest(t, req, http.StatusOK)

			assert.Equal(t, "image/png", resp.Header().Get("Content-Type"))
			img, imgType, err := image.Decode(resp.Body)
			require.NoError(t, err)
			assert.Equal(t, "png", imgType)
			assert.Equal(t, 1200, img.Bounds().Dx())
			assert.Equal(t, 600, img.Bounds().Dy())
		})
	}
}