|
1 | 1 | require "ldclient-rb/context"
|
| 2 | +require "json" |
2 | 3 |
|
3 | 4 | module LaunchDarkly
|
4 | 5 | describe LDContext do
|
@@ -148,6 +149,64 @@ module LaunchDarkly
|
148 | 149 | expect(org_first.fully_qualified_key).to eq("org:b-org-key:user:a-user-key")
|
149 | 150 | end
|
150 | 151 | end
|
| 152 | + |
| 153 | + describe "converts back to JSON format" do |
| 154 | + it "single kind contexts" do |
| 155 | + contextHash = { |
| 156 | + key: "launchdarkly", |
| 157 | + kind: "org", |
| 158 | + address: { |
| 159 | + street: "1999 Harrison St Suite 1100", |
| 160 | + city: "Oakland", |
| 161 | + state: "CA", |
| 162 | + zip: "94612", |
| 163 | + _meta: { |
| 164 | + privateAttributes: ["city"], |
| 165 | + }, |
| 166 | + }, |
| 167 | + } |
| 168 | + context = subject.create(contextHash) |
| 169 | + contextJson = context.to_json |
| 170 | + backToHash = JSON.parse(contextJson, symbolize_names: true) |
| 171 | + |
| 172 | + expect(backToHash).to eq(contextHash) |
| 173 | + end |
| 174 | + |
| 175 | + it "multi kind contexts" do |
| 176 | + contextHash = { |
| 177 | + kind: "multi", |
| 178 | + "org": { |
| 179 | + key: "launchdarkly", |
| 180 | + address: { |
| 181 | + street: "1999 Harrison St Suite 1100", |
| 182 | + city: "Oakland", |
| 183 | + state: "CA", |
| 184 | + zip: "94612", |
| 185 | + }, |
| 186 | + _meta: { |
| 187 | + privateAttributes: ["address/city"], |
| 188 | + }, |
| 189 | + }, |
| 190 | + "user": { |
| 191 | + key: "user-key", |
| 192 | + name: "Ruby", |
| 193 | + anonymous: true, |
| 194 | + }, |
| 195 | + } |
| 196 | + context = subject.create(contextHash) |
| 197 | + contextJson = context.to_json |
| 198 | + backToHash = JSON.parse(contextJson, symbolize_names: true) |
| 199 | + |
| 200 | + expect(backToHash).to eq(contextHash) |
| 201 | + end |
| 202 | + |
| 203 | + it "invalid context returns error" do |
| 204 | + context = subject.create({ key: "", kind: "user", name: "testing" }) |
| 205 | + expect(context.valid?).to be false |
| 206 | + expect(context.to_h).to eq({ error: "context key must not be empty" }) |
| 207 | + expect(context.to_json).to eq({ error: "context key must not be empty" }.to_json) |
| 208 | + end |
| 209 | + end |
151 | 210 | end
|
152 | 211 |
|
153 | 212 | describe "context counts" do
|
|
0 commit comments