|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewAttribute(t *testing.T) { |
| 12 | + attribute := NewAttribute("userName", TypeString, "A unique identifier for the user.") |
| 13 | + |
| 14 | + t.Run("describes the attribute it names", func(t *testing.T) { |
| 15 | + assert.Equal(t, "userName", attribute.Name) |
| 16 | + assert.Equal(t, TypeString, attribute.Type) |
| 17 | + assert.Equal(t, "A unique identifier for the user.", attribute.Description) |
| 18 | + }) |
| 19 | + |
| 20 | + t.Run("defaults to a readWrite attribute returned by default", func(t *testing.T) { |
| 21 | + assert.Equal(t, MutabilityReadWrite, attribute.Mutability) |
| 22 | + assert.Equal(t, ReturnedDefault, attribute.Returned) |
| 23 | + assert.Equal(t, UniquenessNone, attribute.Uniqueness) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("defaults to an optional, single valued, case insensitive attribute", func(t *testing.T) { |
| 27 | + assert.False(t, attribute.Required) |
| 28 | + assert.False(t, attribute.MultiValued) |
| 29 | + assert.False(t, attribute.CaseExact) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("serializes to JSON correctly", func(t *testing.T) { |
| 33 | + body, err := json.Marshal(attribute) |
| 34 | + |
| 35 | + require.NoError(t, err) |
| 36 | + require.JSONEq(t, `{ |
| 37 | + "name": "userName", |
| 38 | + "type": "string", |
| 39 | + "multiValued": false, |
| 40 | + "description": "A unique identifier for the user.", |
| 41 | + "required": false, |
| 42 | + "caseExact": false, |
| 43 | + "mutability": "readWrite", |
| 44 | + "returned": "default", |
| 45 | + "uniqueness": "none" |
| 46 | + }`, string(body)) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestAttribute(t *testing.T) { |
| 51 | + t.Run("marks the attribute the client must send", func(t *testing.T) { |
| 52 | + attribute := NewAttribute("userName", TypeString, "A unique identifier for the user.") |
| 53 | + |
| 54 | + require.Same(t, attribute, attribute.AsRequired()) |
| 55 | + assert.True(t, attribute.Required) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("marks the attribute that holds more than one value", func(t *testing.T) { |
| 59 | + attribute := NewAttribute("emails", TypeComplex, "The email addresses for the user.") |
| 60 | + |
| 61 | + require.Same(t, attribute, attribute.AsMultiValued()) |
| 62 | + assert.True(t, attribute.MultiValued) |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("marks the attribute whose value is compared case sensitively", func(t *testing.T) { |
| 66 | + attribute := NewAttribute("id", TypeString, "A unique identifier for the resource.") |
| 67 | + |
| 68 | + require.Same(t, attribute, attribute.AsCaseExact()) |
| 69 | + assert.True(t, attribute.CaseExact) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("states the scope the service provider enforces uniqueness over", func(t *testing.T) { |
| 73 | + attribute := NewAttribute("userName", TypeString, "A unique identifier for the user.") |
| 74 | + |
| 75 | + require.Same(t, attribute, attribute.UniqueOn(UniquenessServer)) |
| 76 | + |
| 77 | + body, err := json.Marshal(attribute) |
| 78 | + |
| 79 | + require.NoError(t, err) |
| 80 | + require.Contains(t, string(body), `"uniqueness":"server"`) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("suggests the canonical values a client may send", func(t *testing.T) { |
| 84 | + attribute := NewAttribute("type", TypeString, "A label indicating the attribute's function."). |
| 85 | + Suggesting("work", "home", "other") |
| 86 | + |
| 87 | + body, err := json.Marshal(attribute) |
| 88 | + |
| 89 | + require.NoError(t, err) |
| 90 | + require.Contains(t, string(body), `"canonicalValues":["work","home","other"]`) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("names the resource types a reference may point at", func(t *testing.T) { |
| 94 | + attribute := NewAttribute("$ref", TypeReference, "The URI of the corresponding resource.") |
| 95 | + |
| 96 | + require.Same(t, attribute, attribute.Referencing(string(KindUser.Name), ReferenceExternal, ReferenceURI)) |
| 97 | + |
| 98 | + body, err := json.Marshal(attribute) |
| 99 | + |
| 100 | + require.NoError(t, err) |
| 101 | + require.Contains(t, string(body), `"referenceTypes":["User","external","uri"]`) |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("nests the sub-attributes of a complex attribute", func(t *testing.T) { |
| 105 | + givenName := NewAttribute("givenName", TypeString, "The given name of the user.") |
| 106 | + name := NewAttribute("name", TypeComplex, "The components of the user's name.") |
| 107 | + |
| 108 | + require.Same(t, name, name.With(givenName)) |
| 109 | + require.Equal(t, []*Attribute{givenName}, name.SubAttributes) |
| 110 | + |
| 111 | + body, err := json.Marshal(name) |
| 112 | + |
| 113 | + require.NoError(t, err) |
| 114 | + require.JSONEq(t, `{ |
| 115 | + "name": "name", |
| 116 | + "type": "complex", |
| 117 | + "multiValued": false, |
| 118 | + "description": "The components of the user's name.", |
| 119 | + "required": false, |
| 120 | + "caseExact": false, |
| 121 | + "mutability": "readWrite", |
| 122 | + "returned": "default", |
| 123 | + "uniqueness": "none", |
| 124 | + "subAttributes": [{ |
| 125 | + "name": "givenName", |
| 126 | + "type": "string", |
| 127 | + "multiValued": false, |
| 128 | + "description": "The given name of the user.", |
| 129 | + "required": false, |
| 130 | + "caseExact": false, |
| 131 | + "mutability": "readWrite", |
| 132 | + "returned": "default", |
| 133 | + "uniqueness": "none" |
| 134 | + }] |
| 135 | + }`, string(body)) |
| 136 | + }) |
| 137 | + |
| 138 | + t.Run("composes every refinement in a chain", func(t *testing.T) { |
| 139 | + attribute := NewAttribute("emails", TypeComplex, "The email addresses for the user."). |
| 140 | + AsRequired(). |
| 141 | + AsMultiValued(). |
| 142 | + AsCaseExact(). |
| 143 | + UniqueOn(UniquenessGlobal). |
| 144 | + Suggesting("work", "home"). |
| 145 | + With(NewAttribute("value", TypeString, "The email address.")) |
| 146 | + |
| 147 | + assert.True(t, attribute.Required) |
| 148 | + assert.True(t, attribute.MultiValued) |
| 149 | + assert.True(t, attribute.CaseExact) |
| 150 | + assert.Equal(t, UniquenessGlobal, attribute.Uniqueness) |
| 151 | + assert.Equal(t, []string{"work", "home"}, attribute.CanonicalValues) |
| 152 | + assert.Len(t, attribute.SubAttributes, 1) |
| 153 | + |
| 154 | + body, err := json.Marshal(attribute) |
| 155 | + |
| 156 | + require.NoError(t, err) |
| 157 | + require.Contains(t, string(body), `"uniqueness":"global"`) |
| 158 | + }) |
| 159 | + |
| 160 | + t.Run("serializes an attribute the client can neither write nor read back", func(t *testing.T) { |
| 161 | + attribute := NewAttribute("password", TypeString, "The user's cleartext password.") |
| 162 | + attribute.Mutability = MutabilityWriteOnly |
| 163 | + attribute.Returned = ReturnedNever |
| 164 | + |
| 165 | + body, err := json.Marshal(attribute) |
| 166 | + |
| 167 | + require.NoError(t, err) |
| 168 | + require.Contains(t, string(body), `"mutability":"writeOnly"`) |
| 169 | + require.Contains(t, string(body), `"returned":"never"`) |
| 170 | + }) |
| 171 | +} |
0 commit comments