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: 1d6ea4ec0bdb667ee24ff1550dd7f5ec8c08439d (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
package acceptance_test

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

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

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)

			defer rsp.Body.Close()
			require.NoError(t, err)
			require.Equal(t, test.expectedStatus, rsp.StatusCode)
			body, err := ioutil.ReadAll(rsp.Body)
			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)

			defer rsp.Body.Close()
			require.NoError(t, err)
			require.Equal(t, test.expectedStatus, rsp.StatusCode)
			body, err := ioutil.ReadAll(rsp.Body)
			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)
		})
	}
}