open-vault/sdk/framework/path_struct_test.go

96 lines
1.7 KiB
Go
Raw Normal View History

2015-04-17 16:18:21 +00:00
package framework
import (
"context"
2015-04-17 16:18:21 +00:00
"testing"
"github.com/hashicorp/vault/sdk/logical"
2015-04-17 16:18:21 +00:00
)
func TestPathStruct(t *testing.T) {
p := &PathStruct{
Name: "foo",
Path: "bar",
Schema: map[string]*FieldSchema{
"value": {Type: TypeString},
2015-04-17 16:18:21 +00:00
},
Read: true,
}
storage := new(logical.InmemStorage)
var b logical.Backend = &Backend{Paths: p.Paths()}
ctx := context.Background()
2015-04-17 16:18:21 +00:00
// Write via HTTP
_, err := b.HandleRequest(ctx, &logical.Request{
2016-01-07 15:30:47 +00:00
Operation: logical.UpdateOperation,
2015-04-17 16:18:21 +00:00
Path: "bar",
Data: map[string]interface{}{
"value": "baz",
},
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
// Read via HTTP
resp, err := b.HandleRequest(ctx, &logical.Request{
2015-04-17 16:18:21 +00:00
Operation: logical.ReadOperation,
Path: "bar",
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
if resp.Data["value"] != "baz" {
t.Fatalf("bad: %#v", resp)
}
// Read via API
v, err := p.Get(ctx, storage)
2015-04-17 16:18:21 +00:00
if err != nil {
t.Fatalf("bad: %#v", err)
}
if v["value"] != "baz" {
t.Fatalf("bad: %#v", v)
}
// Delete via HTTP
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.DeleteOperation,
Path: "bar",
Data: nil,
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
if resp != nil {
t.Fatalf("bad: %#v", resp)
}
// Re-read via HTTP
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "bar",
Storage: storage,
})
if err != nil {
t.Fatalf("bad: %#v", err)
}
if _, ok := resp.Data["value"]; ok {
t.Fatalf("bad: %#v", resp)
}
// Re-read via API
v, err = p.Get(ctx, storage)
if err != nil {
t.Fatalf("bad: %#v", err)
}
if v != nil {
t.Fatalf("bad: %#v", v)
}
2015-04-17 16:18:21 +00:00
}