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
|
package acceptance_test
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/fixture"
)
// makeGitLabPagesAccessStub provides a stub *httptest.Server to check pages_access API call.
// the result is based on the project id.
//
// Project IDs must be 4 digit long and the following rules applies:
// 1000-1999: Ok
// 2000-2999: Unauthorized
// 3000-3999: Invalid token
func makeGitLabPagesAccessStub(t *testing.T) *httptest.Server {
t.Helper()
return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/oauth/token":
require.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{\"access_token\":\"abc\"}")
case "/api/v4/user":
require.Equal(t, "Bearer abc", r.Header.Get("Authorization"))
w.WriteHeader(http.StatusOK)
default:
if handleAccessControlArtifactRequests(t, w, r) {
return
}
handleAccessControlRequests(t, w, r)
}
}))
}
func CreateHTTPSFixtureFiles(t *testing.T) (key string, cert string) {
t.Helper()
keyfile, err := ioutil.TempFile("", "https-fixture")
require.NoError(t, err)
key = keyfile.Name()
keyfile.Close()
certfile, err := ioutil.TempFile("", "https-fixture")
require.NoError(t, err)
cert = certfile.Name()
certfile.Close()
require.NoError(t, ioutil.WriteFile(key, []byte(fixture.Key), 0644))
require.NoError(t, ioutil.WriteFile(cert, []byte(fixture.Certificate), 0644))
return keyfile.Name(), certfile.Name()
}
func CreateGitLabAPISecretKeyFixtureFile(t *testing.T) (filepath string) {
t.Helper()
secretfile, err := ioutil.TempFile("", "gitlab-api-secret")
require.NoError(t, err)
secretfile.Close()
require.NoError(t, ioutil.WriteFile(secretfile.Name(), []byte(fixture.GitLabAPISecretKey), 0644))
return secretfile.Name()
}
|