Commit 0a7df9d

mokha <mokha@cisco.com>
2019-01-07 17:30:02
emit json for complex attributes
1 parent be448b4
lib/scim/kit/v2/templates/attribute.json.jbuilder
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+json.key_format! camelize: :lower
+if type.complex?
+  json.set! type.name do
+    dynamic_attributes.values.each do |attribute|
+      render attribute, json: json
+    end
+  end
+else
+  json.set! type.name, value
+end
lib/scim/kit/v2/templates/resource.json.jbuilder
@@ -6,3 +6,6 @@ json.external_id external_id
 json.meta do
   render meta, json: json
 end
+dynamic_attributes.values.each do |attribute|
+  render attribute, json: json
+end
lib/scim/kit/v2/attributable.rb
@@ -4,6 +4,8 @@ module Scim
   module Kit
     module V2
       module Attributable
+        attr_reader :dynamic_attributes
+
         def define_attributes_for(types)
           @dynamic_attributes = Hash[
             types.map do |x|
lib/scim/kit/v2/attribute.rb
@@ -6,6 +6,7 @@ module Scim
       # Represents a SCIM Attribute
       class Attribute
         include Attributable
+        include Templatable
         attr_reader :type
         attr_reader :value
 
spec/scim/kit/v2/attribute_spec.rb
@@ -12,6 +12,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = user_name }
 
       specify { expect(subject.value).to eql(user_name) }
+      specify { expect(subject.as_json[:userName]).to eql(user_name) }
     end
 
     context 'when integer' do
@@ -53,12 +54,14 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = true }
 
       specify { expect(subject.value).to be(true) }
+      specify { expect(subject.as_json[:hungry]).to be(true) }
     end
 
     context 'when false' do
       before { subject.value = false }
 
       specify { expect(subject.value).to be(false) }
+      specify { expect(subject.as_json[:hungry]).to be(false) }
     end
 
     context 'when string' do
@@ -73,12 +76,14 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = Math::PI }
 
       specify { expect(subject.value).to eql(Math::PI) }
+      specify { expect(subject.as_json[:measurement]).to be(Math::PI) }
     end
 
     context 'when given an integer' do
       before { subject.value = 42 }
 
       specify { expect(subject.value).to eql(42.to_f) }
+      specify { expect(subject.as_json[:measurement]).to be(42.to_f) }
     end
   end
 
@@ -89,6 +94,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = 34 }
 
       specify { expect(subject.value).to be(34) }
+      specify { expect(subject.as_json[:age]).to be(34) }
     end
 
     context 'when given float' do
@@ -106,6 +112,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = datetime }
 
       specify { expect(subject.value).to eql(datetime) }
+      specify { expect(subject.as_json[:birthdate]).to eql(datetime.iso8601) }
     end
 
     context 'when given a string' do
@@ -123,6 +130,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
       before { subject.value = photo }
 
       specify { expect(subject.value).to eql(Base64.strict_encode64(photo)) }
+      specify { expect(subject.as_json[:photo]).to eql(Base64.strict_encode64(photo)) }
     end
   end
 
@@ -133,6 +141,7 @@ RSpec.describe Scim::Kit::V2::Attribute do
     before { subject.value = uri }
 
     specify { expect(subject.value).to eql(uri) }
+    specify { expect(subject.as_json[:group]).to eql(uri) }
   end
 
   context 'with complex type' do
@@ -144,11 +153,14 @@ RSpec.describe Scim::Kit::V2::Attribute do
     end
 
     before do
-      subject.family_name = 'mo'
-      subject.given_name = 'khan'
+      subject.family_name = 'Garrett'
+      subject.given_name = 'Tsuyoshi'
+      puts subject.as_json
     end
 
-    specify { expect(subject.family_name).to eql('mo') }
-    specify { expect(subject.given_name).to eql('khan') }
+    specify { expect(subject.family_name).to eql('Garrett') }
+    specify { expect(subject.given_name).to eql('Tsuyoshi') }
+    specify { expect(subject.as_json[:name][:familyName]).to eql('Garrett') }
+    specify { expect(subject.as_json[:name][:givenName]).to eql('Tsuyoshi') }
   end
 end
spec/scim/kit/v2/resource_spec.rb
@@ -57,11 +57,16 @@ RSpec.describe Scim::Kit::V2::Resource do
         x.add_attribute(name: 'familyName')
         x.add_attribute(name: 'givenName')
       end
-      subject.name.family_name = 'khan'
-      subject.name.given_name = 'mo'
+      subject.name.family_name = 'Garrett'
+      subject.name.given_name = 'Tsuyoshi'
     end
 
-    specify { expect(subject.name.family_name).to eql('khan') }
-    specify { expect(subject.name.given_name).to eql('mo') }
+    specify { expect(subject.name.family_name).to eql('Garrett') }
+    specify { expect(subject.name.given_name).to eql('Tsuyoshi') }
+
+    describe '#as_json' do
+      specify { expect(subject.as_json[:name][:familyName]).to eql('Garrett') }
+      specify { expect(subject.as_json[:name][:givenName]).to eql('Tsuyoshi') }
+    end
   end
 end