From acb3ab3e87e16e6f2fa5d7d03c43a7d0220e7775 Mon Sep 17 00:00:00 2001 From: Jaime Martinez Date: Tue, 15 Jun 2021 15:56:21 +1000 Subject: Use stub in acme_test.go --- test/acceptance/acme_test.go | 106 +++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 38 deletions(-) (limited to 'test/acceptance/acme_test.go') diff --git a/test/acceptance/acme_test.go b/test/acceptance/acme_test.go index eb8f2160..c7a33796 100644 --- a/test/acceptance/acme_test.go +++ b/test/acceptance/acme_test.go @@ -9,65 +9,95 @@ import ( "github.com/stretchr/testify/require" ) +const ( + existingAcmeTokenPath = "/.well-known/acme-challenge/existingtoken" + notExistingAcmeTokenPath = "/.well-known/acme-challenge/notexistingtoken" +) + func TestAcmeChallengesWhenItIsNotConfigured(t *testing.T) { skipUnlessEnabled(t) - teardown := RunPagesProcess(t, *pagesBinary, supportedListeners(), "", "") - defer teardown() - - t.Run("When domain folder contains requested acme challenge it responds with it", func(t *testing.T) { - rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", - existingAcmeTokenPath) + RunPagesProcessWithStubGitLabServer(t, + withListeners([]ListenSpec{httpListener}), + ) - defer rsp.Body.Close() - require.NoError(t, err) - require.Equal(t, http.StatusOK, rsp.StatusCode) - body, _ := ioutil.ReadAll(rsp.Body) - require.Equal(t, "this is token\n", string(body)) - }) + 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.", + }, + } - t.Run("When domain folder doesn't contains requested acme challenge it returns 404", - func(t *testing.T) { + for name, test := range tests { + t.Run(name, func(t *testing.T) { rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", - notExistingAcmeTokenPath) + test.token) defer rsp.Body.Close() require.NoError(t, err) - require.Equal(t, http.StatusNotFound, rsp.StatusCode) - }, - ) + 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) { skipUnlessEnabled(t) - teardown := RunPagesProcess(t, *pagesBinary, supportedListeners(), "", "-gitlab-server=https://gitlab-acme.com") - defer teardown() - - t.Run("When domain folder contains requested acme challenge it responds with it", func(t *testing.T) { - rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", - existingAcmeTokenPath) + RunPagesProcessWithStubGitLabServer(t, + withListeners([]ListenSpec{httpListener}), + withExtraArgument("gitlab-server", "https://gitlab-acme.com"), + ) - defer rsp.Body.Close() - require.NoError(t, err) - require.Equal(t, http.StatusOK, rsp.StatusCode) - body, _ := ioutil.ReadAll(rsp.Body) - require.Equal(t, "this is token\n", string(body)) - }) + 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", + }, + } - t.Run("When domain folder doesn't contains requested acme challenge it redirects to GitLab", - func(t *testing.T) { + for name, test := range tests { + t.Run(name, func(t *testing.T) { rsp, err := GetRedirectPage(t, httpListener, "withacmechallenge.domain.com", - notExistingAcmeTokenPath) + test.token) defer rsp.Body.Close() require.NoError(t, err) - require.Equal(t, http.StatusTemporaryRedirect, rsp.StatusCode) + require.Equal(t, test.expectedStatus, rsp.StatusCode) + body, err := ioutil.ReadAll(rsp.Body) + require.NoError(t, err) + + require.Contains(t, string(body), test.expectedContent) - url, err := url.Parse(rsp.Header.Get("Location")) + redirectURL, err := url.Parse(rsp.Header.Get("Location")) require.NoError(t, err) - require.Equal(t, url.String(), "https://gitlab-acme.com/-/acme-challenge?domain=withacmechallenge.domain.com&token=notexistingtoken") - }, - ) + require.Equal(t, redirectURL.String(), test.expectedLocation) + }) + } } -- cgit v1.2.3