This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_utils.go
73 lines (58 loc) · 1.68 KB
/
test_utils.go
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
package artifactory
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
}
func newTestClient(fn roundTripperFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
// Literally https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-CreateToken
const canonicalAccessToken = `{
"access_token": "eyXsdgbtybbeeyh...",
"expires_in": 0,
"scope": "api:* member-of-groups:example",
"token_type": "Bearer",
"refresh_token": "fgsfgsdugh8dgu9s8gy9hsg..."
}`
func tokenCreatedResponse(token string) roundTripperFunc {
return func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(token)),
}, nil
}
}
func makeBackend(t *testing.T) (*backend, *logical.BackendConfig) {
config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
b, err := Backend(config)
if err != nil {
t.Fatal(err)
}
if err := b.Setup(context.Background(), config); err != nil {
t.Fatal(err)
}
return b, config
}
func configuredBackend(t *testing.T, adminConfig map[string]interface{}) (*backend, *logical.BackendConfig) {
b, config := makeBackend(t)
_, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/admin",
Storage: config.StorageView,
Data: adminConfig,
})
assert.NoError(t, err)
return b, config
}