Comparing changes

v0.2.14 v0.2.15
6 commits 6 files changed
lib/scim/kit/v2/attributable.rb
@@ -5,10 +5,11 @@ module Scim
     module V2
       # Represents a dynamic attribute that corresponds to a SCIM type
       module Attributable
-        attr_reader :dynamic_attributes
+        def dynamic_attributes
+          @dynamic_attributes ||= {}.with_indifferent_access
+        end
 
         def define_attributes_for(resource, types)
-          @dynamic_attributes ||= {}.with_indifferent_access
           types.each { |x| attribute(x, resource) }
         end
 
@@ -41,7 +42,10 @@ module Scim
           if value.is_a?(Hash)
             attribute_for(name)&.assign_attributes(value)
           else
-            attribute_for(name)&._value = value
+            attribute = attribute_for(name)
+            raise Scim::Kit::UnknownAttributeError, name unless attribute
+
+            attribute._value = value
           end
         end
 
lib/scim/kit/v2/meta.rb
@@ -12,7 +12,7 @@ module Scim
         attr_reader :resource_type
 
         def initialize(resource_type, location)
-          @resource_type = resource_type
+          @resource_type = resource_type || 'Unknown'
           @location = location
           @created = @last_modified = Time.now
           @version = @created.to_i
lib/scim/kit/v2/resource.rb
@@ -9,7 +9,6 @@ module Scim
         include Attributable
         include Templatable
 
-        attr_accessor :id, :external_id
         attr_reader :meta
         attr_reader :schemas
         attr_reader :raw_attributes
@@ -17,13 +16,13 @@ module Scim
         validate :schema_validations
 
         def initialize(schemas:, location: nil, attributes: {})
-          @meta = Meta.new(schemas[0].name, location)
+          @meta = Meta.new(schemas[0]&.name, location)
           @meta.disable_timestamps
           @schemas = schemas
           @raw_attributes = attributes
-          schemas.each do |schema|
-            define_attributes_for(self, schema.attributes)
-          end
+          schemas.each { |x| define_attributes_for(self, x.attributes) }
+          attribute(AttributeType.new(name: :id), self)
+          attribute(AttributeType.new(name: :external_id), self)
           assign_attributes(attributes)
           yield self if block_given?
         end
@@ -37,6 +36,10 @@ module Scim
           end
         end
 
+        def template_name
+          'resource.json.jbuilder'
+        end
+
         private
 
         def schema_validations
lib/scim/kit/version.rb
@@ -2,6 +2,6 @@
 
 module Scim
   module Kit
-    VERSION = '0.2.14'
+    VERSION = '0.2.15'
   end
 end
lib/scim/kit.rb
@@ -2,6 +2,8 @@
 
 require 'active_model'
 require 'active_support/core_ext/hash/indifferent_access'
+require 'json'
+require 'pathname'
 require 'tilt'
 require 'tilt/jbuilder'
 
@@ -14,5 +16,6 @@ require 'scim/kit/version'
 module Scim
   module Kit
     class Error < StandardError; end
+    class UnknownAttributeError < Error; end
   end
 end
spec/scim/kit/v2/resource_spec.rb
@@ -7,6 +7,13 @@ RSpec.describe Scim::Kit::V2::Resource do
   let(:schema) { Scim::Kit::V2::Schema.new(id: Scim::Kit::V2::Schemas::USER, name: 'User', location: FFaker::Internet.uri('https')) }
   let(:resource_location) { FFaker::Internet.uri('https') }
 
+  context 'when the schemas is empty' do
+    let(:schemas) { [] }
+
+    specify { expect { subject }.not_to raise_error }
+    specify { expect(subject.meta.resource_type).to eql('Unknown') }
+  end
+
   context 'with common attributes' do
     let(:id) { SecureRandom.uuid }
     let(:external_id) { SecureRandom.uuid }
@@ -469,10 +476,16 @@ RSpec.describe Scim::Kit::V2::Resource do
       specify { expect(subject.name.family_name).to eql('Garrett') }
       specify { expect(subject.emails[0][:value]).to eql(email) }
       specify { expect(subject.emails[0][:primary]).to be(true) }
+      specify do
+        attributes = { schemas: schemas.map(&:id), unknown: 'unknown' }
+        expect do
+          described_class.new(schemas: schemas, attributes: attributes)
+        end.to raise_error(Scim::Kit::UnknownAttributeError)
+      end
     end
   end
 
-  describe 'Errors' do
+  describe Scim::Kit::V2::Messages::ERROR do
     subject { described_class.new(schemas: schemas) }
 
     let(:schemas) { [Scim::Kit::V2::Error.default_schema] }