Skip to content

Commit 63344e0

Browse files
committed
add external api tests
1 parent b511862 commit 63344e0

File tree

4 files changed

+180
-166
lines changed

4 files changed

+180
-166
lines changed

api/sys_auth_test.go

-88
This file was deleted.

api/sys_mounts_test.go

+2-78
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,8 @@ import (
99
"testing"
1010
)
1111

12-
func TestGetMount(t *testing.T) {
13-
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultGetMountHandler))
14-
defer mockVaultServer.Close()
15-
16-
cfg := DefaultConfig()
17-
cfg.Address = mockVaultServer.URL
18-
client, err := NewClient(cfg)
19-
if err != nil {
20-
t.Fatal(err)
21-
}
22-
23-
mount, err := client.Sys().GetMount("secret")
24-
if err != nil {
25-
t.Fatal(err)
26-
}
27-
28-
expected := struct {
29-
Type string
30-
Version string
31-
}{Type: "pki", Version: ""}
32-
33-
if expected.Type != mount.Type || expected.Version != mount.PluginVersion {
34-
t.Errorf("Mount did not match: expected %+v but got %+v", expected, mount)
35-
}
36-
}
37-
3812
func TestListMounts(t *testing.T) {
39-
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultListMountsHandler))
13+
mockVaultServer := httptest.NewServer(http.HandlerFunc(mockVaultMountsHandler))
4014
defer mockVaultServer.Close()
4115

4216
cfg := DefaultConfig()
@@ -84,60 +58,10 @@ func TestListMounts(t *testing.T) {
8458
}
8559
}
8660

87-
func mockVaultListMountsHandler(w http.ResponseWriter, _ *http.Request) {
61+
func mockVaultMountsHandler(w http.ResponseWriter, _ *http.Request) {
8862
_, _ = w.Write([]byte(listMountsResponse))
8963
}
9064

91-
func mockVaultGetMountHandler(w http.ResponseWriter, _ *http.Request) {
92-
_, _ = w.Write([]byte(getMountResponse))
93-
}
94-
95-
const getMountResponse = `{
96-
"uuid": "556fc6ef-208c-5ac1-3838-40b0c052eaa8",
97-
"plugin_version": "",
98-
"running_plugin_version": "v1.13.9+builtin.vault",
99-
"deprecation_status": "supported",
100-
"accessor": "pki_a6c43de6",
101-
"options": {},
102-
"local": false,
103-
"seal_wrap": false,
104-
"external_entropy_access": false,
105-
"running_sha256": "",
106-
"config": {
107-
"default_lease_ttl": 86400,
108-
"force_no_cache": false,
109-
"max_lease_ttl": 86400
110-
},
111-
"type": "pki",
112-
"description": "test",
113-
"request_id": "285b1a04-821c-686a-8325-cfc70517c3eb",
114-
"lease_id": "",
115-
"renewable": false,
116-
"lease_duration": 0,
117-
"data": {
118-
"accessor": "pki_a6c43de6",
119-
"config": {
120-
"default_lease_ttl": 86400,
121-
"force_no_cache": false,
122-
"max_lease_ttl": 86400
123-
},
124-
"deprecation_status": "supported",
125-
"description": "test",
126-
"external_entropy_access": false,
127-
"local": false,
128-
"options": {},
129-
"plugin_version": "",
130-
"running_plugin_version": "v1.13.9+builtin.vault",
131-
"running_sha256": "",
132-
"seal_wrap": false,
133-
"type": "pki",
134-
"uuid": "556fc6ef-208c-5ac1-3838-40b0c052eaa8"
135-
},
136-
"wrap_info": null,
137-
"warnings": null,
138-
"auth": null
139-
}`
140-
14165
const listMountsResponse = `{
14266
"request_id": "3cd881e9-ea50-2e06-90b2-5641667485fa",
14367
"lease_id": "",
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package api
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/vault/api"
10+
)
11+
12+
func TestGetAuth(t *testing.T) {
13+
t.Parallel()
14+
15+
testCases := []struct {
16+
name string
17+
mountName string
18+
authInput *api.EnableAuthOptions
19+
expected *api.AuthMount
20+
shouldMount bool
21+
expectErr bool
22+
}{
23+
{
24+
name: "get-default-auth-mount-success",
25+
mountName: "token",
26+
authInput: nil,
27+
expected: &api.AuthMount{
28+
Type: "token",
29+
},
30+
shouldMount: false,
31+
expectErr: false,
32+
},
33+
{
34+
name: "get-manual-auth-mount-success",
35+
mountName: "userpass",
36+
authInput: &api.EnableAuthOptions{
37+
Type: "userpass",
38+
},
39+
expected: &api.AuthMount{
40+
Type: "userpass",
41+
},
42+
shouldMount: true,
43+
expectErr: false,
44+
},
45+
{
46+
name: "error-not-found",
47+
mountName: "not-found",
48+
authInput: nil,
49+
expected: &api.AuthMount{
50+
Type: "not-found",
51+
},
52+
shouldMount: false,
53+
expectErr: true,
54+
},
55+
}
56+
57+
for _, tc := range testCases {
58+
tc := tc
59+
60+
t.Run(tc.name, func(t *testing.T) {
61+
t.Parallel()
62+
63+
client, closer := testVaultServer(t)
64+
defer closer()
65+
66+
if tc.shouldMount {
67+
err := client.Sys().EnableAuthWithOptions(tc.mountName, tc.authInput)
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
}
72+
73+
mount, err := client.Sys().GetAuth(tc.mountName)
74+
if !tc.expectErr && err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
if !tc.expectErr {
79+
if tc.expected.Type != mount.Type || tc.expected.PluginVersion != mount.PluginVersion {
80+
t.Errorf("mount did not match: expected %+v but got %+v", tc.expected, mount)
81+
}
82+
} else {
83+
if err == nil {
84+
t.Errorf("expected error but got nil")
85+
}
86+
}
87+
})
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package api
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/vault/api"
10+
)
11+
12+
func TestGetMount(t *testing.T) {
13+
t.Parallel()
14+
15+
testCases := []struct {
16+
name string
17+
mountName string
18+
mountInput *api.MountInput
19+
expected *api.MountOutput
20+
shouldMount bool
21+
expectErr bool
22+
}{
23+
{
24+
name: "get-default-mount-success",
25+
mountName: "secret",
26+
mountInput: nil,
27+
expected: &api.MountOutput{
28+
Type: "kv",
29+
},
30+
shouldMount: false,
31+
expectErr: false,
32+
},
33+
{
34+
name: "get-manual-mount-success",
35+
mountName: "pki",
36+
mountInput: &api.MountInput{
37+
Type: "pki",
38+
},
39+
expected: &api.MountOutput{
40+
Type: "pki",
41+
},
42+
shouldMount: true,
43+
expectErr: false,
44+
},
45+
{
46+
name: "error-not-found",
47+
mountName: "not-found",
48+
mountInput: nil,
49+
expected: &api.MountOutput{
50+
Type: "not-found",
51+
},
52+
shouldMount: false,
53+
expectErr: true,
54+
},
55+
}
56+
57+
for _, tc := range testCases {
58+
tc := tc
59+
60+
t.Run(tc.name, func(t *testing.T) {
61+
t.Parallel()
62+
63+
client, closer := testVaultServer(t)
64+
defer closer()
65+
66+
if tc.shouldMount {
67+
err := client.Sys().Mount(tc.mountName, tc.mountInput)
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
}
72+
73+
mount, err := client.Sys().GetMount(tc.mountName)
74+
if !tc.expectErr && err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
if !tc.expectErr {
79+
if tc.expected.Type != mount.Type || tc.expected.PluginVersion != mount.PluginVersion {
80+
t.Errorf("mount did not match: expected %+v but got %+v", tc.expected, mount)
81+
}
82+
} else {
83+
if err == nil {
84+
t.Errorf("expected error but got nil")
85+
}
86+
}
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)