main
 1# frozen_string_literal: true
 2
 3RSpec.describe Saml::Kit::Builders::Metadata do
 4  describe '.build' do
 5    subject { Saml::Kit::Metadata }
 6
 7    let(:url) { FFaker::Internet.uri('https') }
 8
 9    it 'builds metadata for a service provider' do
10      result = subject.build do |builder|
11        builder.build_service_provider do |x|
12          x.add_assertion_consumer_service(url, binding: :http_post)
13        end
14      end
15
16      hash_result = Hash.from_xml(result.to_xml)
17      expect(hash_result['EntityDescriptor']).to be_present
18      expect(hash_result['EntityDescriptor']['SPSSODescriptor']).to be_present
19      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']).to be_present
20      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']['Location']).to eql(url)
21    end
22
23    it 'builds metadata for an identity provider' do
24      result = subject.build do |builder|
25        builder.build_identity_provider do |x|
26          x.add_single_sign_on_service(url, binding: :http_post)
27        end
28      end
29
30      hash_result = Hash.from_xml(result.to_xml)
31      expect(hash_result['EntityDescriptor']).to be_present
32      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']).to be_present
33      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']).to be_present
34      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']['Location']).to eql(url)
35    end
36
37    it 'builds metadata for both IDP and SP' do
38      result = subject.build do |builder|
39        builder.build_service_provider do |x|
40          x.add_assertion_consumer_service(url, binding: :http_post)
41        end
42        builder.build_identity_provider do |x|
43          x.add_single_sign_on_service(url, binding: :http_post)
44        end
45      end
46
47      hash_result = Hash.from_xml(result.to_xml)
48      expect(hash_result['EntityDescriptor']).to be_present
49      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']).to be_present
50      expect(hash_result['EntityDescriptor']['SPSSODescriptor']).to be_present
51
52      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']).to be_present
53      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']['Location']).to eql(url)
54      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']).to be_present
55      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']['Location']).to eql(url)
56    end
57
58    it 'generates signed idp and sp metadata' do
59      configuration = Saml::Kit::Configuration.new do |config|
60        3.times { config.generate_key_pair_for(use: :signing) }
61      end
62      metadata = Saml::Kit::Metadata.build(configuration: configuration) do |builder|
63        builder.entity_id = FFaker::Internet.uri('https')
64        builder.build_identity_provider do |x|
65          x.embed_signature = true
66          x.add_single_sign_on_service(url, binding: :http_post)
67        end
68        builder.build_service_provider do |x|
69          x.embed_signature = true
70          x.add_assertion_consumer_service(url, binding: :http_post)
71        end
72      end
73      expect(metadata).to be_present
74      expect(metadata).to be_valid
75    end
76  end
77
78  specify do
79    configuration = Saml::Kit::Configuration.new do |config|
80      config.entity_id = 'https://www.example.org/metadata'
81      config.generate_key_pair_for(use: :signing)
82      config.generate_key_pair_for(use: :encryption)
83    end
84    metadata = Saml::Kit::Metadata.build(configuration: configuration) do |x|
85      x.organization_name = 'Acme'
86      x.contact_email = 'acme@example.org'
87      x.organization_url = 'https://www.example.org'
88      x.build_service_provider do |xxx|
89        xxx.add_assertion_consumer_service('https://www.example.org/assertions', binding: :http_post)
90      end
91    end
92    expect(metadata.to_xml(pretty: true)).to be_present
93  end
94end