Commit 5ef4d24

mo <mo.khan@gmail.com>
2019-01-04 23:16:29
move defaults to authentication scheme class
1 parent ac6b929
lib/scim/kit/v2/authentication_scheme.rb
@@ -3,7 +3,23 @@
 module Scim
   module Kit
     module V2
+      # Represents the available Authentication Schemes.
       class AuthenticationScheme
+        DEFAULTS = {
+          httpbasic: {
+            description: 'Authentication scheme using the HTTP Basic Standard',
+            documentation_uri: 'http://example.com/help/httpBasic.html',
+            name: 'HTTP Basic',
+            spec_uri: 'http://www.rfc-editor.org/info/rfc2617'
+          },
+          oauthbearertoken: {
+            description:
+            'Authentication scheme using the OAuth Bearer Token Standard',
+            documentation_uri: 'http://example.com/help/oauth.html',
+            name: 'OAuth Bearer Token',
+            spec_uri: 'http://www.rfc-editor.org/info/rfc6750'
+          }
+        }.freeze
         include Templatable
         attr_accessor :name
         attr_accessor :description
@@ -14,6 +30,17 @@ module Scim
         def initialize
           yield self if block_given?
         end
+
+        def self.build_for(type)
+          defaults = DEFAULTS[type.to_sym] || {}
+          new do |x|
+            x.type = type
+            x.description = defaults[:description]
+            x.documentation_uri = defaults[:documentation_uri]
+            x.name = defaults[:name]
+            x.spec_uri = defaults[:spec_uri]
+          end
+        end
       end
     end
   end
lib/scim/kit/v2/schema.rb
@@ -6,11 +6,13 @@ module Scim
       # Represents a SCIM Schema
       class Schema
         include Templatable
+
+        CORE = 'urn:ietf:params:scim:schemas:core:2.0'
         ERROR = 'urn:ietf:params:scim:api:messages:2.0:Error'
-        GROUP = 'urn:ietf:params:scim:schemas:core:2.0:Group'
-        RESOURCE_TYPE = 'urn:ietf:params:scim:schemas:core:2.0:ResourceType'
-        SERVICE_PROVIDER_CONFIGURATION = 'urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig'
-        USER = 'urn:ietf:params:scim:schemas:core:2.0:User'
+        GROUP = "#{CORE}:Group"
+        RESOURCE_TYPE = "#{CORE}:ResourceType"
+        SERVICE_PROVIDER_CONFIGURATION = "#{CORE}:ServiceProviderConfig"
+        USER = "#{CORE}:User"
 
         attr_reader :id, :name, :location, :attributes
         attr_accessor :description
lib/scim/kit/v2/service_provider_configuration.rb
@@ -18,28 +18,7 @@ module Scim
         end
 
         def add_authentication(type)
-          authentication_scheme = case type.to_sym
-          when :oauthbearertoken
-            AuthenticationScheme.new do |x|
-              x.description = 'Authentication scheme using the OAuth Bearer Token Standard'
-              x.documentation_uri = 'http://example.com/help/oauth.html'
-              x.name = 'OAuth Bearer Token'
-              x.spec_uri = 'http://www.rfc-editor.org/info/rfc6750'
-              x.type = type
-            end
-          when :httpbasic
-            AuthenticationScheme.new do |x|
-              x.description = 'Authentication scheme using the HTTP Basic Standard'
-              x.documentation_uri = 'http://example.com/help/httpBasic.html'
-              x.name = 'HTTP Basic'
-              x.spec_uri = 'http://www.rfc-editor.org/info/rfc2617'
-              x.type = type
-            end
-          else
-            AuthenticationScheme.new do |x|
-              x.type = type
-            end
-          end
+          authentication_scheme = AuthenticationScheme.build_for(type)
           yield authentication_scheme if block_given?
           @authentication_schemes << authentication_scheme
         end
spec/scim/kit/v2/service_provider_configuration_spec.rb
@@ -2,13 +2,14 @@
 
 RSpec.describe Scim::Kit::V2::ServiceProviderConfiguration do
   subject { described_class.new(location: location) }
+
   let(:location) { FFaker::Internet.uri('https') }
   let(:now) { Time.now }
 
-  describe "#to_json" do
+  describe '#to_json' do
     let(:result) { JSON.parse(subject.to_json, symbolize_names: true) }
 
-    specify { expect(result[:schemas]).to match_array(["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"]) }
+    specify { expect(result[:schemas]).to match_array(['urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig']) }
     specify { expect(result[:documentationUri]).to be_blank }
     specify { expect(result[:patch][:supported]).to be(false) }
     specify { expect(result[:bulk][:supported]).to be(false) }
@@ -23,7 +24,7 @@ RSpec.describe Scim::Kit::V2::ServiceProviderConfiguration do
     specify { expect(result[:meta][:lastModified]).to eql(now.iso8601) }
     specify { expect(result[:meta][:version]).not_to be_nil }
 
-    context "with documentation uri" do
+    context 'with documentation uri' do
       before do
         subject.documentation_uri = FFaker::Internet.uri('https')
       end
@@ -31,35 +32,27 @@ RSpec.describe Scim::Kit::V2::ServiceProviderConfiguration do
       specify { expect(result[:documentationUri]).to eql(subject.documentation_uri) }
     end
 
-    context "with OAuth Bearer Token" do
+    context 'with OAuth Bearer Token' do
       before { subject.add_authentication(:oauthbearertoken) }
 
-      specify do
-        expect(result[:authenticationSchemes]).to match_array([{
-          name: 'OAuth Bearer Token',
-          description: 'Authentication scheme using the OAuth Bearer Token Standard',
-          specUri: 'http://www.rfc-editor.org/info/rfc6750',
-          documentationUri: 'http://example.com/help/oauth.html',
-          type: 'oauthbearertoken',
-        }])
-      end
+      specify { expect(result[:authenticationSchemes][0][:name]).to eql('OAuth Bearer Token') }
+      specify { expect(result[:authenticationSchemes][0][:description]).to eql('Authentication scheme using the OAuth Bearer Token Standard') }
+      specify { expect(result[:authenticationSchemes][0][:specUri]).to eql('http://www.rfc-editor.org/info/rfc6750') }
+      specify { expect(result[:authenticationSchemes][0][:documentationUri]).to eql('http://example.com/help/oauth.html') }
+      specify { expect(result[:authenticationSchemes][0][:type]).to eql('oauthbearertoken') }
     end
 
-    context "with http basic" do
+    context 'with http basic' do
       before { subject.add_authentication(:httpbasic) }
 
-      specify do
-        expect(result[:authenticationSchemes]).to match_array([{
-          name: 'HTTP Basic',
-          description: 'Authentication scheme using the HTTP Basic Standard',
-          specUri: 'http://www.rfc-editor.org/info/rfc2617',
-          documentationUri: 'http://example.com/help/httpBasic.html',
-          type: 'httpbasic',
-        }])
-      end
+      specify { expect(result[:authenticationSchemes][0][:name]).to eql('HTTP Basic') }
+      specify { expect(result[:authenticationSchemes][0][:description]).to eql('Authentication scheme using the HTTP Basic Standard') }
+      specify { expect(result[:authenticationSchemes][0][:specUri]).to eql('http://www.rfc-editor.org/info/rfc2617') }
+      specify { expect(result[:authenticationSchemes][0][:documentationUri]).to eql('http://example.com/help/httpBasic.html') }
+      specify { expect(result[:authenticationSchemes][0][:type]).to eql('httpbasic') }
     end
 
-    context "with custom scheme" do
+    context 'with custom scheme' do
       before do
         subject.add_authentication(:custom) do |x|
           x.name = 'custom'