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

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

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
)

type responseFn func(*testing.T, string) api.VirtualDomain

// DomainResponses holds the predefined API responses for certain domains
// that can be used with the GitLab API stub in acceptance tests
var DomainResponses = map[string]responseFn{
	"zip-from-disk.gitlab.io":           ZipFromFile,
	"zip-from-disk-not-found.gitlab.io": ZipFromFileNotFound,
	"zip-not-allowed-path.gitlab.io":    ZipFromNotAllowedPath,
	// test assume the working dir is inside shared/pages/
	"group.gitlab-example.com":        GenerateVirtualDomainFromDir("group", "group.gitlab-example.com"),
	"CapitalGroup.gitlab-example.com": GenerateVirtualDomainFromDir("CapitalGroup", "CapitalGroup.gitlab-example.com"),
	"group.404.gitlab-example.com":    GenerateVirtualDomainFromDir("group.404", "group.404.gitlab-example.com"),
	"domain.404.com":                  domain404,
	// NOTE: before adding more domains here, generate the zip archive by running (per project)
	// make zip PROJECT_SUBDIR=group/serving
	// make zip PROJECT_SUBDIR=group/project2
}

// ZipFromFile response for zip.gitlab.io
func ZipFromFile(t *testing.T, wd string) api.VirtualDomain {
	t.Helper()

	return api.VirtualDomain{
		Certificate: "",
		Key:         "",
		LookupPaths: []api.LookupPath{
			{
				ProjectID:     123,
				AccessControl: false,
				HTTPSOnly:     false,
				Prefix:        "/",
				Source: api.Source{
					Type: "zip",
					Path: fmt.Sprintf("file://%s/@hashed/67/06/670671cd97404156226e507973f2ab8330d3022ca96e0c93bdbdb320c41adcaf/pages_deployments/01/artifacts.zip", wd),
				},
			},
		},
	}
}

// ZipFromFileNotFound response for zip-from-disk-not-found.gitlab.io
func ZipFromFileNotFound(t *testing.T, wd string) api.VirtualDomain {
	t.Helper()

	return api.VirtualDomain{
		Certificate: "",
		Key:         "",
		LookupPaths: []api.LookupPath{
			{
				ProjectID:     123,
				AccessControl: false,
				HTTPSOnly:     false,
				Prefix:        "/",
				Source: api.Source{
					Type: "zip",
					Path: fmt.Sprintf("file://%s/@hashed/67/06/670671cd97404156226e507973f2ab8330d3022ca96e0c93bdbdb320c41adcaf/pages_deployments/01/unknown.zip", wd),
				},
			},
		},
	}
}

// ZipFromNotAllowedPath response for zip-not-allowed-path.gitlab.io
func ZipFromNotAllowedPath(t *testing.T, wd string) api.VirtualDomain {
	t.Helper()

	return api.VirtualDomain{
		Certificate: "",
		Key:         "",
		LookupPaths: []api.LookupPath{
			{
				ProjectID:     123,
				AccessControl: false,
				HTTPSOnly:     false,
				Prefix:        "/",
				Source: api.Source{
					Type: "zip",
					// path outside of `pages-root`
					Path: "file:///some/random/path/public.zip",
				},
			},
		},
	}
}

// GenerateVirtualDomainFromDir walks the subdirectory inside of shared/pages/ to find any zip archives.
// It works for subdomains of pages-domain but not for custom domains (yet)
func GenerateVirtualDomainFromDir(dir, rootDomain string) responseFn {
	return func(t *testing.T, wd string) api.VirtualDomain {
		t.Helper()

		var foundZips []string

		// walk over dir and save any paths containing a `.zip` file
		// $(GITLAB_PAGES_DIR)/shared/pages + "/" + group

		cleanDir := filepath.Join(wd, dir)

		// make sure resolved path inside dir is under wd to avoid https://securego.io/docs/rules/g304.html
		require.Truef(t, strings.HasPrefix(cleanDir, wd), "path %q outside of wd %q", cleanDir, wd)

		filepath.Walk(cleanDir, func(path string, info os.FileInfo, err error) error {
			require.NoError(t, err)

			if strings.HasSuffix(info.Name(), ".zip") {
				project := strings.TrimPrefix(path, wd+"/"+dir)
				foundZips = append(foundZips, project)
			}

			return nil
		})

		lookupPaths := make([]api.LookupPath, 0, len(foundZips))
		// generate lookup paths
		for _, project := range foundZips {
			// if project = "group/subgroup/project/public.zip
			// trim prefix group and suffix /public.zip
			// so prefix = "/subgroup/project"
			prefix := strings.TrimPrefix(project, dir)
			prefix = strings.TrimSuffix(prefix, "/"+filepath.Base(project))

			// use / as prefix when the current prefix matches the rootDomain, e.g.
			// if request is group.gitlab-example.com/ and group/group.gitlab-example.com/public.zip exists
			if prefix == "/"+rootDomain {
				prefix = "/"
			}

			lookupPath := api.LookupPath{
				// TODO: allow configuring response
				// Related MR in progress https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498
				ProjectID:     123,
				AccessControl: false,
				HTTPSOnly:     false,
				Prefix:        prefix,
				Source: api.Source{
					Type: "zip",
					Path: fmt.Sprintf("file://%s", wd+"/"+dir+project),
				},
			}

			lookupPaths = append(lookupPaths, lookupPath)
		}

		return api.VirtualDomain{
			LookupPaths: lookupPaths,
		}
	}
}

// domain404 hardcoding for now, will implement a better solution in a follow up MR
//TODO: remove hardcoded custom domains: https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/498
func domain404(t *testing.T, wd string) api.VirtualDomain {
	t.Helper()

	return api.VirtualDomain{
		Certificate: "",
		Key:         "",
		LookupPaths: []api.LookupPath{
			{
				ProjectID:     123,
				AccessControl: false,
				HTTPSOnly:     false,
				Prefix:        "/",
				Source: api.Source{
					Type: "zip",
					Path: fmt.Sprintf("file://%s/group.404/domain.404/public.zip", wd),
				},
			},
		},
	}
}