-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
136 lines (106 loc) · 2.95 KB
/
backend_test.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
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
package secretsengine
import (
"context"
"os"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
)
type testEnv struct {
Host string
OAuthToken string
Username string
Password string
OrgName string
DeveloperEmail string
AppName string
ApiProducts string
Keys []string
Backend logical.Backend
Context context.Context
Storage logical.Storage
}
func getTestBackend(tb testing.TB) (*apigeeBackend, logical.Storage) {
tb.Helper()
config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
config.Logger = hclog.NewNullLogger()
config.System = logical.TestSystemView()
b, err := Factory(context.Background(), config)
if err != nil {
tb.Fatal(err)
}
return b.(*apigeeBackend), config.StorageView
}
func (e *testEnv) CreateConfig(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "config",
Storage: e.Storage,
Data: map[string]interface{}{
"host": e.Host,
"oauth_token": e.OAuthToken,
"username": e.Username,
"password": e.Password,
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, err)
require.Nil(t, resp)
}
func (e *testEnv) CreateRole(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "roles/test",
Storage: e.Storage,
Data: map[string]interface{}{
"org_name": e.OrgName,
"developer_email": e.DeveloperEmail,
"app_name": e.AppName,
"api_products": e.ApiProducts,
"ttl": 86400,
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, err)
require.Nil(t, resp)
}
func (e *testEnv) ReadCred(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test",
Storage: e.Storage,
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.Data)
require.Contains(t, resp.Data["org_name"], os.Getenv(envVarApigeeOrgName))
require.Contains(t, resp.Data["developer_email"], os.Getenv(envVarApigeeDeveloperEmail))
require.Contains(t, resp.Data["app_name"], os.Getenv(envVarApigeeAppName))
require.Contains(t, resp.Data["api_products"], os.Getenv(envVarApigeeApiProducts))
require.NotEmpty(t, resp.Data["key"])
require.NotEmpty(t, resp.Data["secret"])
require.NotEmpty(t, resp.Data["credentials"])
k, ok := resp.Data["key"]
if ok {
e.Keys = append(e.Keys, k.(string))
}
}
func (e *testEnv) DeleteCreds(t *testing.T) {
if len(e.Keys) == 0 {
t.Fatalf("expected 3 keys, got: %d", len(e.Keys))
}
for _, key := range e.Keys {
b := e.Backend.(*apigeeBackend)
client, err := b.getClient(e.Context, e.Storage)
if err != nil {
t.Fatal("error getting client")
}
err = client.DeleteCredentials(e.OrgName, e.DeveloperEmail, e.AppName, key)
if err != nil {
t.Fatalf("error deleting credentials: %s", err)
}
}
}