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

gitlab_test.go « gitlab « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 095fc2eb36625183590bd7ca4fb3b22e50fcc22a (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gitlab

import (
	"context"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"sync"
	"testing"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/cache"
	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/client"
	"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/mock"
	"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)

type lookupPathTest struct {
	file                string
	target              string
	expectedPrefix      string
	expectedPath        string
	expectedSubPath     string
	expectedIsNamespace bool
}

func TestGetDomain(t *testing.T) {
	tests := map[string]struct {
		file          string
		domain        string
		mockLookup    *api.Lookup
		expectedError error
	}{
		"when the response is correct": {
			file:   "client/testdata/test.gitlab.io.json",
			domain: "test.gitlab.io",
		},
		"when the response is not valid": {
			file:          "/dev/null",
			domain:        "test.gitlab.io",
			expectedError: io.EOF,
		},
		"when the response is unauthorized": {
			mockLookup:    &api.Lookup{Error: client.ErrUnauthorizedAPI},
			domain:        "test",
			expectedError: client.ErrUnauthorizedAPI,
		},
	}

	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			mockClient := NewMockClient(t, tc.file, tc.mockLookup, false)
			source := Gitlab{client: mockClient}

			domain, err := source.GetDomain(context.Background(), tc.domain)
			if tc.expectedError == nil {
				require.NoError(t, err)
				require.Equal(t, tc.domain, domain.Name)
			} else {
				require.Error(t, err)
				require.Nil(t, domain)
			}
		})
	}
}

func TestResolve(t *testing.T) {
	tests := map[string]struct {
		file                string
		target              string
		expectedPrefix      string
		expectedPath        string
		expectedSubPath     string
		expectedIsNamespace bool
	}{
		"when requesting nested group project with root path": {
			file:                "client/testdata/test.gitlab.io.json",
			target:              "https://test.gitlab.io:443/my/pages/project/",
			expectedPrefix:      "/my/pages/project/",
			expectedPath:        "some/path/to/project/",
			expectedSubPath:     "",
			expectedIsNamespace: false,
		},
		"when requesting a nested group project with full path": {
			file:                "client/testdata/test.gitlab.io.json",
			target:              "https://test.gitlab.io:443/my/pages/project/path/index.html",
			expectedPrefix:      "/my/pages/project/",
			expectedPath:        "some/path/to/project/",
			expectedSubPath:     "path/index.html",
			expectedIsNamespace: false,
		},
		"when requesting the group root project with root path": {
			file:                "client/testdata/test.gitlab.io.json",
			target:              "https://test.gitlab.io:443/",
			expectedPrefix:      "/",
			expectedPath:        "some/path/to/project-3/",
			expectedSubPath:     "",
			expectedIsNamespace: true,
		},
		"when requesting the group root project with full path": {
			file:                "client/testdata/test.gitlab.io.json",
			target:              "https://test.gitlab.io:443/path/to/index.html",
			expectedPrefix:      "/",
			expectedPath:        "some/path/to/project-3/",
			expectedSubPath:     "path/to/index.html",
			expectedIsNamespace: true,
		},
		"when request path has not been sanitized": {
			file:            "client/testdata/test.gitlab.io.json",
			target:          "https://test.gitlab.io:443/something/../something/../my/pages/project/index.html",
			expectedPrefix:  "/my/pages/project/",
			expectedPath:    "some/path/to/project/",
			expectedSubPath: "index.html",
		},
	}

	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			mockClient := NewMockClient(t, tc.file, nil, false)
			source := Gitlab{client: mockClient, enableDisk: true}

			request := httptest.NewRequest(http.MethodGet, tc.target, nil)

			response, err := source.Resolve(request)
			require.NoError(t, err)

			require.Equal(t, tc.expectedPrefix, response.LookupPath.Prefix)
			require.Equal(t, tc.expectedPath, response.LookupPath.Path)
			require.Equal(t, tc.expectedSubPath, response.SubPath)
			require.Equal(t, tc.expectedIsNamespace, response.LookupPath.IsNamespaceProject)
		})
	}
}

// Test validates https://gitlab.com/gitlab-org/gitlab-pages/-/issues/646 with go test -race
func TestResolveLookupPathsConcurrentNetRequests(t *testing.T) {
	tests := map[string]lookupPathTest{
		"when requesting the group root project with root path": {
			file:                "client/testdata/group-first.gitlab.io.json",
			target:              "https://group-first.gitlab.io:443/",
			expectedPrefix:      "/",
			expectedPath:        "some/path/group/",
			expectedSubPath:     "",
			expectedIsNamespace: true,
		},
		"when requesting another project with path": {
			file:                "client/testdata/group-first.gitlab.io.json",
			target:              "https://group-first.gitlab.io:443/my/second-project/index.html",
			expectedPrefix:      "/my/second-project/",
			expectedPath:        "some/path/to/project-2/",
			expectedSubPath:     "index.html",
			expectedIsNamespace: false,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			wg := &sync.WaitGroup{}
			mockClient := NewMockClient(t, test.file, nil, true)
			cache := cache.NewCache(mockClient, &testhelpers.CacheConfig)

			for i := 0; i < 3; i++ {
				wg.Add(1)
				go sendResolveRequest(t, wg, cache, test)
			}

			wg.Wait()
		})
	}
}

// Test proves fix for https://gitlab.com/gitlab-org/gitlab-pages/-/issues/576
func TestResolveLookupPathsOrderDoesNotMatter(t *testing.T) {
	tests := map[string]lookupPathTest{
		"when requesting the group root project with root path": {
			file:                "client/testdata/group-first.gitlab.io.json",
			target:              "https://group-first.gitlab.io:443/",
			expectedPrefix:      "/",
			expectedPath:        "some/path/group/",
			expectedSubPath:     "",
			expectedIsNamespace: true,
		},
		"when requesting another project with path": {
			file:                "client/testdata/group-first.gitlab.io.json",
			target:              "https://group-first.gitlab.io:443/my/second-project/index.html",
			expectedPrefix:      "/my/second-project/",
			expectedPath:        "some/path/to/project-2/",
			expectedSubPath:     "index.html",
			expectedIsNamespace: false,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			mockClient := NewMockClient(t, test.file, nil, true)
			cache := cache.NewCache(mockClient, &testhelpers.CacheConfig)
			source := Gitlab{client: cache, enableDisk: true}

			request := httptest.NewRequest(http.MethodGet, test.target, nil)

			response, err := source.Resolve(request)
			require.NoError(t, err)

			require.Equal(t, test.expectedPrefix, response.LookupPath.Prefix)
			require.Equal(t, test.expectedPath, response.LookupPath.Path)
			require.Equal(t, test.expectedSubPath, response.SubPath)
			require.Equal(t, test.expectedIsNamespace, response.LookupPath.IsNamespaceProject)
		})
	}
}

func NewMockClient(t *testing.T, file string, mockedLookup *api.Lookup, useCache bool) *mock.MockClientStub {
	mockCtrl := gomock.NewController(t)

	mockClient := mock.NewMockClientStub(mockCtrl)
	if !useCache {
		mockClient.EXPECT().
			Resolve(gomock.Any(), gomock.Any()).
			DoAndReturn(func(ctx context.Context, domain string) *api.Lookup {
				lookup := mockClient.GetLookup(ctx, domain)
				return &lookup
			}).
			Times(1)
	}

	mockClient.EXPECT().
		GetLookup(gomock.Any(), gomock.Any()).
		DoAndReturn(func(ctx context.Context, domain string) api.Lookup {
			if mockedLookup != nil {
				return *mockedLookup
			}

			lookup := api.Lookup{Name: domain}

			f, err := os.Open(file)
			if err != nil {
				lookup.Error = err
				return lookup
			}
			defer f.Close()

			lookup.ParseDomain(f)

			return lookup
		}).
		Times(1)

	return mockClient
}

func sendResolveRequest(t *testing.T, wg *sync.WaitGroup, resolver api.Resolver, test lookupPathTest) {
	request := httptest.NewRequest(http.MethodGet, test.target, nil)

	source := Gitlab{client: resolver, enableDisk: true}

	response, err := source.Resolve(request)
	require.NoError(t, err)

	require.Equal(t, test.expectedPrefix, response.LookupPath.Prefix)
	require.Equal(t, test.expectedPath, response.LookupPath.Path)
	require.Equal(t, test.expectedSubPath, response.SubPath)
	require.Equal(t, test.expectedIsNamespace, response.LookupPath.IsNamespaceProject)

	wg.Done()
}