Welcome to mirror list, hosted at ThFree Co, Russian Federation.

acme_test.go « acceptance « test - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89c57c96f0e0d3006e559901138cf75697baeeb0 (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
package acceptance_test

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

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)

const (
	existingAcmeTokenPath    = "/.well-known/acme-challenge/existingtoken"
	notExistingAcmeTokenPath = "/.well-known/acme-challenge/notexistingtoken"
)

func TestAcmeChallengesWhenItIsNotConfigured(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
	)

	tests := map[string]struct {
		token           string
		expectedStatus  int
		expectedContent string
	}{
		"When domain folder contains requested acme challenge it responds with it": {
			token:           existingAcmeTokenPath,
			expectedStatus:  http.StatusOK,
			expectedContent: "this is token\n",
		},
		"When domain folder does not contain requested acme challenge it returns 404": {
			token:           notExistingAcmeTokenPath,
			expectedStatus:  http.StatusNotFound,
			expectedContent: "The page you're looking for could not be found.",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com",
				test.token)

			require.NoError(t, err)
			require.Equal(t, test.expectedStatus, rsp.StatusCode)
			body, err := io.ReadAll(rsp.Body)
			require.NoError(t, rsp.Body.Close())
			require.NoError(t, err)

			require.Contains(t, string(body), test.expectedContent)
		})
	}
}

func TestAcmeChallengesWhenItIsConfigured(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
		withExtraArgument("gitlab-server", "https://gitlab-acme.com"),
	)

	tests := map[string]struct {
		token            string
		expectedStatus   int
		expectedContent  string
		expectedLocation string
	}{
		"When domain folder contains requested acme challenge it responds with it": {
			token:           existingAcmeTokenPath,
			expectedStatus:  http.StatusOK,
			expectedContent: "this is token\n",
		},
		"When domain folder doesn't contains requested acme challenge it redirects to GitLab": {
			token:            notExistingAcmeTokenPath,
			expectedStatus:   http.StatusTemporaryRedirect,
			expectedContent:  "",
			expectedLocation: "https://gitlab-acme.com/-/acme-challenge?domain=withacmechallenge.domain.com&token=notexistingtoken",
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com",
				test.token)

			require.NoError(t, err)
			require.Equal(t, test.expectedStatus, rsp.StatusCode)
			body, err := io.ReadAll(rsp.Body)
			require.NoError(t, rsp.Body.Close())
			require.NoError(t, err)

			require.Contains(t, string(body), test.expectedContent)

			redirectURL, err := url.Parse(rsp.Header.Get("Location"))
			require.NoError(t, err)

			require.Equal(t, redirectURL.String(), test.expectedLocation)
		})
	}
}

func TestAcmeChallengesNoRedirection(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
		withExtraArgument("gitlab-server", "https://gitlab-acme.com"),
	)

	tests := map[string]struct {
		path             string
		expectedStatus   int
		expectedContent  string
		expectedLocation string
	}{
		"wildcard redirect should not redirect acme challenge": {
			path:            existingAcmeTokenPath,
			expectedStatus:  http.StatusOK,
			expectedContent: "this is token\n",
		},
		"non-acme paths should be redirected": {
			path: "/example",
			// rule inside _redirects is a 200 rewrite of /index.html
			expectedStatus:  http.StatusOK,
			expectedContent: "acme-challenge-project\n",
		},
		"When domain folder doesn't contain requested acme challenge it redirects to GitLab": {
			path:             notExistingAcmeTokenPath,
			expectedStatus:   http.StatusTemporaryRedirect,
			expectedContent:  "",
			expectedLocation: "https://gitlab-acme.com/-/acme-challenge?domain=acmewithredirects.domain.com&token=notexistingtoken",
		},
	}

	for tn, tt := range tests {
		t.Run(tn, func(t *testing.T) {
			rsp, err := GetRedirectPage(t, httpListener, "acmewithredirects.domain.com", tt.path)
			require.NoError(t, err)
			testhelpers.Close(t, rsp.Body)

			require.Equal(t, tt.expectedStatus, rsp.StatusCode)
			body, err := io.ReadAll(rsp.Body)
			require.NoError(t, rsp.Body.Close())
			require.NoError(t, err)

			require.Contains(t, string(body), tt.expectedContent)

			redirectURL, err := url.Parse(rsp.Header.Get("Location"))
			require.NoError(t, err)

			require.Equal(t, redirectURL.String(), tt.expectedLocation)
		})
	}
}