Commit 203443a

mokha <mokha@cisco.com>
2019-01-12 17:18:11
render attributes based on client/server mode
1 parent a67f385
Changed files (4)
lib/scim/kit/v2/attribute.rb
@@ -32,6 +32,8 @@ module Scim
         end
 
         def renderable?
+          return false if type.mutability == Mutability::READ_ONLY && _resource.mode?(:client)
+          return false if type.mutability == Mutability::WRITE_ONLY && _resource.mode?(:server)
           true
         end
 
lib/scim/kit/v2/mutability.rb
@@ -10,10 +10,13 @@ module Scim
         IMMUTABLE = 'immutable'
         WRITE_ONLY = 'writeOnly'
         VALID = {
+          immutable: IMMUTABLE,
           read_only: READ_ONLY,
           read_write: READ_WRITE,
-          immutable: IMMUTABLE,
-          write_only: WRITE_ONLY
+          readonly: READ_ONLY,
+          readwrite: READ_WRITE,
+          write_only: WRITE_ONLY,
+          writeonly: WRITE_ONLY,
         }.freeze
 
         def self.find(value)
spec/scim/kit/v2/attribute_spec.rb
@@ -255,4 +255,47 @@ RSpec.describe Scim::Kit::V2::Attribute do
       specify { expect(subject.errors[:emails]).to be_present }
     end
   end
+
+  describe "#renderable?" do
+    let(:type) { Scim::Kit::V2::AttributeType.new(name: 'userName', type: :string) }
+    let(:resource) { instance_double(Scim::Kit::V2::Resource) }
+
+    context "when the resource is in server mode" do
+      before do
+        allow(resource).to receive(:mode?).with(:server).and_return(true)
+        allow(resource).to receive(:mode?).with(:client).and_return(false)
+      end
+
+      context "when the type is read only" do
+        before { type.mutability = :read_only }
+
+        specify { expect(subject).to be_renderable }
+      end
+
+      context "when the type is write only" do
+        before { type.mutability = :write_only }
+
+        specify { expect(subject).not_to be_renderable }
+      end
+    end
+
+    context "when the resource is in client mode" do
+      before do
+        allow(resource).to receive(:mode?).with(:server).and_return(false)
+        allow(resource).to receive(:mode?).with(:client).and_return(true)
+      end
+
+      context "when the type is read only" do
+        before { type.mutability = :read_only }
+
+        specify { expect(subject).not_to be_renderable }
+      end
+
+      context "when the type is write only" do
+        before { type.mutability = :write_only }
+
+        specify { expect(subject).to be_renderable }
+      end
+    end
+  end
 end
spec/scim/kit/v2/resource_spec.rb
@@ -33,7 +33,7 @@ RSpec.describe Scim::Kit::V2::Resource do
     describe '#as_json' do
       specify { expect(subject.as_json[:schemas]).to match_array([schema.id]) }
       specify { expect(subject.as_json[:id]).to eql(id) }
-      specify { expect(subject.as_json[:externalId]).to eql(external_id) }
+      specify { expect(subject.as_json[:externalId]).to be_nil } # only render in client mode
       specify { expect(subject.as_json[:meta][:resourceType]).to eql('User') }
       specify { expect(subject.as_json[:meta][:location]).to eql(resource_location) }
       specify { expect(subject.as_json[:meta][:created]).to eql(created_at.iso8601) }
@@ -251,13 +251,13 @@ RSpec.describe Scim::Kit::V2::Resource do
 
     context "when building in client mode" do
       subject { described_class.new(schemas: schemas) }
+      let(:external_id) { SecureRandom.uuid }
 
-      before do
-        subject.external_id = SecureRandom.uuid
-      end
+      before { subject.external_id = external_id }
 
       specify { expect(subject.to_h.key?(:id)).to be(false) }
       specify { expect(subject.to_h.key?(:externalId)).to be(true) }
+      specify { expect(subject.to_h[:externalId]).to eql(external_id) }
       specify { expect(subject.to_h.key?(:meta)).to be(false) }
       specify { expect(subject.to_h.key?(:userName)).to be(true) }
       specify { expect(subject.to_h[:name].key?(:formatted)).to be(false) }
@@ -267,7 +267,7 @@ RSpec.describe Scim::Kit::V2::Resource do
       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?(:password)).to be(true) }
       specify { expect(subject.to_h.key?(:emails)).to be(true) }
       specify { expect(subject.to_h.key?(:groups)).to be(false) }
     end
@@ -290,7 +290,7 @@ RSpec.describe Scim::Kit::V2::Resource do
       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(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(true) }
     end