require 'rails_helper' describe Devise::Strategies::SamlAuthenticatable do subject(:strategy) { described_class.new(env, :user) } let(:env) { {} } let(:response) { double(:response, :settings= => nil, is_valid?: true, sessionindex: '123123123') } before do allow(OneLogin::RubySaml::Response).to receive(:new).and_return(response) end let(:saml_config) { OneLogin::RubySaml::Settings.new } before do allow(strategy).to receive(:saml_config).and_return(saml_config) end let(:mapping) { double(:mapping, to: user_class) } let(:user_class) { double(:user_class, authenticate_with_saml: user) } let(:user) { double(:user) } before do allow(strategy).to receive(:mapping).and_return(mapping) allow(user).to receive(:after_saml_authentication) end let(:params) { {} } before do allow(strategy).to receive(:params).and_return(params) end context "with a login SAMLResponse parameter" do let(:params) { {SAMLResponse: "PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWw9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIElEPSIxMjMxMjMxMjMxMjMxMjMiIFZlcnNpb249IjIuMCIgSXNzdWVJbnN0YW50PSIyMDE1LTA2LTMwVDE0OjQyOjI3WiIgRGVzdGluYXRpb249IntyZWNpcGllbnR9Ij48c2FtbDpJc3N1ZXI+aHR0cHM6Ly90ZXN0L3NhbWwvbWV0YWRhdGEvMTIzMTIzPC9zYW1sOklzc3Vlcj48c2FtbHA6U3RhdHVzPjxzYW1scDpTdGF0dXNDb2RlIFZhbHVlPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6c3RhdHVzOlN1Y2Nlc3MiLz48L3NhbWxwOlN0YXR1cz48c2FtbDpBc3NlcnRpb24geG1sbnM6c2FtbD0idXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFzc2VydGlvbiIgeG1sbnM6eHM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hIiB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3Jn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enticates with the response" do expect(OneLogin::RubySaml::Response).to receive(:new).with(params[:SAMLResponse], settings: saml_config) expect(user_class).to receive(:authenticate_with_saml).with(response) expect(user).to receive(:after_saml_authentication).with(response.sessionindex) expect(strategy).to receive(:success!).with(user) strategy.authenticate! end context "and the resource cannot does not exist" do let(:user) { nil } it "fails to authenticate" do expect(strategy).to receive(:fail!).with(:invalid) strategy.authenticate! end end context "and the SAML response is not valid" do before do allow(response).to receive(:is_valid?).and_return(false) end it "fails to authenticate" do expect(strategy).to receive(:fail!).with(:invalid) strategy.authenticate! end end end context "with a logout SAMLResponse parameter" do let(:params) { {SAMLResponse: "PHNhbWxwOkxvZ291dFJlc3BvbnNlIFZlcnNpb249JzIuMCcgSW5SZXNwb25zZVRvPSdfMTM0MjQzMjQzMjQzMicgeG1sbnM6c2FtbHA9J3VybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDpwcm90b2NvbCcgSXNzdWVJbnN0YW50PScyMDE1LTA2LTMwVDE0OjQzOjQ0JyBJRD0nXzY5OTc2OTc5Nzk4Nzk4Nzk3OTg3Jz48c2FtbDpJc3N1ZXIgeG1sbnM6c2FtbD0ndXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOmFzc2VydGlvbic+aHR0cHM6Ly90ZXN0L3NhbWwvbWV0YWRhdGEvMTQzMjQzMjwvc2FtbDpJc3N1ZXI+PHNhbWxwOlN0YXR1cz48c2FtbHA6U3RhdHVzQ29kZSBWYWx1ZT0ndXJuOm9hc2lzOm5hbWVzOnRjOlNBTUw6Mi4wOnN0YXR1czpTdWNjZXNzJy8+PHNhbWxwOlN0YXR1c01lc3NhZ2U+U3VjY2Vzc2Z1bGx5IGxvZ2dlZCBvdXQgZnJvbSBzZXJ2aWNlIDwvc2FtbHA6U3RhdHVzTWVzc2FnZT48L3NhbWxwOlN0YXR1cz48L3NhbWxwOkxvZ291dFJlc3BvbnNlPg=="} } it "is valid" do expect(strategy).not_to be_valid end end it "is not valid without a SAMLResponse parameter" do expect(strategy).not_to be_valid end it "is permanent" do expect(strategy).to be_store end end