github.com/hashicorp/vault/sdk@v0.11.0/helper/authmetadata/auth_metadata_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package authmetadata 5 6 import ( 7 "fmt" 8 "reflect" 9 "sort" 10 "testing" 11 12 "github.com/hashicorp/vault/sdk/framework" 13 "github.com/hashicorp/vault/sdk/logical" 14 ) 15 16 var testFields = &Fields{ 17 FieldName: "some-field-name", 18 Default: []string{"fizz", "buzz"}, 19 AvailableToAdd: []string{"foo", "bar"}, 20 } 21 22 func TestFieldSchema(t *testing.T) { 23 schema := FieldSchema(testFields) 24 if schema.Type != framework.TypeCommaStringSlice { 25 t.Fatal("expected TypeCommaStringSlice") 26 } 27 if schema.Description != `The metadata to include on the aliases and audit logs generated by this plugin. When set to 'default', includes: fizz, buzz. These fields are available to add: foo, bar. Not editing this field means the 'default' fields are included. Explicitly setting this field to empty overrides the 'default' and means no metadata will be included. If not using 'default', explicit fields must be sent like: 'field1,field2'.` { 28 t.Fatal("received unexpected description: " + schema.Description) 29 } 30 if schema.DisplayAttrs == nil { 31 t.Fatal("expected display attributes") 32 } 33 if schema.DisplayAttrs.Name != testFields.FieldName { 34 t.Fatalf("expected name of %s", testFields.FieldName) 35 } 36 if schema.DisplayAttrs.Value != "field1,field2" { 37 t.Fatal("expected field1,field2") 38 } 39 if !reflect.DeepEqual(schema.Default, []string{"default"}) { 40 t.Fatal("expected default") 41 } 42 } 43 44 func TestGetAuthMetadata(t *testing.T) { 45 h := NewHandler(testFields) 46 expected := []string{"fizz", "buzz"} 47 sort.Strings(expected) 48 actual := h.AuthMetadata() 49 sort.Strings(actual) 50 if !reflect.DeepEqual(expected, actual) { 51 t.Fatalf("expected %s but received %s", expected, actual) 52 } 53 } 54 55 func TestParseAuthMetadata(t *testing.T) { 56 h := NewHandler(testFields) 57 data := &framework.FieldData{ 58 Raw: map[string]interface{}{ 59 testFields.FieldName: []string{"default"}, 60 }, 61 Schema: map[string]*framework.FieldSchema{ 62 testFields.FieldName: FieldSchema(testFields), 63 }, 64 } 65 if err := h.ParseAuthMetadata(data); err != nil { 66 t.Fatal(err) 67 } 68 expected := []string{"fizz", "buzz"} 69 sort.Strings(expected) 70 actual := h.AuthMetadata() 71 sort.Strings(actual) 72 if !reflect.DeepEqual(expected, actual) { 73 t.Fatalf("expected %s but received %s", expected, actual) 74 } 75 } 76 77 func TestPopulateDesiredAuthMetadata(t *testing.T) { 78 h := NewHandler(testFields) 79 data := &framework.FieldData{ 80 Raw: map[string]interface{}{ 81 testFields.FieldName: []string{"foo"}, 82 }, 83 Schema: map[string]*framework.FieldSchema{ 84 testFields.FieldName: FieldSchema(testFields), 85 }, 86 } 87 if err := h.ParseAuthMetadata(data); err != nil { 88 t.Fatal(err) 89 } 90 auth := &logical.Auth{ 91 Alias: &logical.Alias{ 92 Name: "foo", 93 }, 94 } 95 if err := h.PopulateDesiredMetadata(auth, map[string]string{ 96 "fizz": "fizzval", 97 "buzz": "buzzval", 98 "foo": "fooval", 99 }); err != nil { 100 t.Fatal(err) 101 } 102 if len(auth.Alias.Metadata) != 1 { 103 t.Fatal("expected only 1 configured field to be populated") 104 } 105 if auth.Alias.Metadata["foo"] != "fooval" { 106 t.Fatal("expected foova;") 107 } 108 } 109 110 func TestMarshalJSON(t *testing.T) { 111 h := NewHandler(&Fields{}) 112 h.authMetadata = []string{"fizz", "buzz"} 113 b, err := h.MarshalJSON() 114 if err != nil { 115 t.Fatal(err) 116 } 117 if string(b) != `{"auth_metadata":["fizz","buzz"]}` { 118 t.Fatal(`expected {"auth_metadata":["fizz","buzz"]}`) 119 } 120 } 121 122 func TestUnmarshalJSON(t *testing.T) { 123 h := NewHandler(&Fields{}) 124 if err := h.UnmarshalJSON([]byte(`{"auth_metadata":["fizz","buzz"]}`)); err != nil { 125 t.Fatal(err) 126 } 127 if fmt.Sprintf("%s", h.authMetadata) != `[fizz buzz]` { 128 t.Fatal(`expected [fizz buzz]`) 129 } 130 }