Commit c84928e

mokha <mokha@cisco.com>
2019-01-12 16:37:17
detect which mode the resource is in.
1 parent 89213cb
Changed files (3)
lib
spec
scim
lib/scim/kit/v2/templates/resource.json.jbuilder
@@ -1,23 +1,25 @@
 # frozen_string_literal: true
 
 json.key_format! camelize: :lower
-if meta.location
+if mode?(:server)
   json.meta do
     render meta, json: json
   end
 end
 json.schemas schemas.map(&:id)
-json.id id if id
-json.external_id external_id if external_id
+json.id id if mode?(:server)
+json.external_id external_id if mode?(:server) && external_id
 schemas.each do |schema|
   if schema.core?
     schema.attributes.each do |type|
-      render dynamic_attributes[type.name], json: json
+      attribute = dynamic_attributes[type.name]
+      render attribute, json: json
     end
   else
     json.set! schema.id do
       schema.attributes.each do |type|
-        render dynamic_attributes[type.name], json: json
+        attribute = dynamic_attributes[type.name]
+        render attribute, json: json
       end
     end
   end
lib/scim/kit/v2/resource.rb
@@ -26,6 +26,15 @@ module Scim
           yield self if block_given?
         end
 
+        def mode?(type)
+          case type.to_sym
+          when :server
+            meta&.location
+          else
+            meta&.location.nil?
+          end
+        end
+
         private
 
         def schema_validations
spec/scim/kit/v2/resource_spec.rb
@@ -224,5 +224,80 @@ RSpec.describe Scim::Kit::V2::Resource do
       specify { expect(resource.to_h.key?(:external_id)).to be(false) }
       specify { puts resource.to_h }
     end
+
+    context "when building in client mode" do
+      subject { described_class.new(schemas: schemas) }
+
+      before do
+        schema.add_attribute(name: 'userName') do |attribute|
+          attribute.required = true
+          attribute.uniqueness = :server
+        end
+        schema.add_attribute(name: 'name') do | attribute|
+          attribute.add_attribute(name: 'formatted') do |x|
+            x.mutability = :read_only
+          end
+          attribute.add_attribute(name: 'familyName')
+          attribute.add_attribute(name: 'givenName')
+        end
+        schema.add_attribute(name: 'displayName') do |attribute|
+          attribute.mutability = :read_only
+        end
+        schema.add_attribute(name: 'locale')
+        schema.add_attribute(name: 'timezone')
+        schema.add_attribute(name: 'active', type: :boolean)
+        schema.add_attribute(name: 'password') do |attribute|
+          attribute.mutability = :write_only
+          attribute.returned  = :never
+        end
+        schema.add_attribute(name: 'emails') do |attribute|
+          attribute.multi_valued = true
+          attribute.add_attribute(name: 'value')
+          attribute.add_attribute(name: 'primary', type: :boolean)
+        end
+        schema.add_attribute(name: 'groups') do |attribute|
+          attribute.multi_valued = true
+          attribute.mutability = :read_only
+          attribute.add_attribute(name: 'value') do |x|
+            x.mutability = :read_only
+          end
+          attribute.add_attribute(name: '$ref') do |x|
+            x.reference_types = ['User', 'Group']
+            x.mutability = :read_only
+          end
+          attribute.add_attribute(name: 'display') do |x|
+            x.mutability = :read_only
+          end
+        end
+      end
+
+      specify { expect(subject.to_h.key?(:userName)).to be(true) }
+      specify { expect(subject.to_h[:name].key?(:formatted)).to be(false) }
+      specify { expect(subject.to_h[:name].key?(:familyName)).to be(true) }
+      specify { expect(subject.to_h[:name].key?(:givenName)).to be(true) }
+      specify { expect(subject.to_h[:name].key?(:displayName)).to be(false) }
+      specify { expect(subject.to_h.key?(:locale)).to be(true) }
+      specify { expect(subject.to_h.key?(:timezone)).to be(true) }
+      specify { expect(subject.to_h.key?(:active)).to be(true) }
+      specify { expect(subject.to_h.key?(:password)).to be(false) }
+      specify { expect(subject.to_h.key?(:emails)).to be(true) }
+      specify { expect(subject.to_h.key?(:groups)).to be(false) }
+    end
+  end
+
+  describe "#mode?" do
+    context "when server mode" do
+      subject { described_class.new(schemas: schemas, location: resource_location) }
+
+      specify { expect(subject).to be_mode(:server) }
+      specify { expect(subject).not_to be_mode(:client) }
+    end
+
+    context "when client mode" do
+      subject { described_class.new(schemas: schemas) }
+
+      specify { expect(subject).not_to be_mode(:server) }
+      specify { expect(subject).to be_mode(:client) }
+    end
   end
 end