open-vault/http/logical_test.go

69 lines
1.4 KiB
Go
Raw Normal View History

package http
import (
"net/http"
"reflect"
"testing"
2015-04-05 00:42:19 +00:00
"time"
"github.com/hashicorp/vault/vault"
)
func TestLogical(t *testing.T) {
2015-03-29 23:14:54 +00:00
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := TestServer(t, core)
defer ln.Close()
2015-03-29 23:14:54 +00:00
TestServerAuth(t, addr, token)
2015-04-07 18:04:06 +00:00
// WRITE
resp := testHttpPut(t, addr+"/v1/secret/foo", map[string]interface{}{
"data": "bar",
})
testResponseStatus(t, resp, 204)
2015-04-07 18:04:06 +00:00
// READ
resp, err := http.Get(addr + "/v1/secret/foo")
if err != nil {
t.Fatalf("err: %s", err)
}
var actual map[string]interface{}
expected := map[string]interface{}{
2015-03-16 20:29:51 +00:00
"renewable": false,
2015-04-05 00:42:19 +00:00
"lease_duration": float64((30 * 24 * time.Hour) / time.Second),
"data": map[string]interface{}{
"data": "bar",
},
2015-04-05 00:42:19 +00:00
"auth": nil,
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
delete(actual, "lease_id")
if !reflect.DeepEqual(actual, expected) {
2015-04-05 00:42:19 +00:00
t.Fatalf("bad: %#v %#v", actual, expected)
}
2015-04-07 18:04:06 +00:00
// DELETE
resp = testHttpDelete(t, addr+"/v1/secret/foo")
testResponseStatus(t, resp, 204)
resp, err = http.Get(addr + "/v1/secret/foo")
if err != nil {
t.Fatalf("err: %s", err)
}
testResponseStatus(t, resp, 404)
}
func TestLogical_noExist(t *testing.T) {
2015-03-29 23:14:54 +00:00
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := TestServer(t, core)
defer ln.Close()
2015-03-29 23:14:54 +00:00
TestServerAuth(t, addr, token)
resp, err := http.Get(addr + "/v1/secret/foo")
if err != nil {
t.Fatalf("err: %s", err)
}
testResponseStatus(t, resp, 404)
}