Skip to content

Commit ecd23a7

Browse files
authored
Merge pull request #244 from gerardo-navarro/gerardo-navarro-refactor-saml-spec-small
Refactor helper method #post_xml from the global scope into the relevant test context
2 parents c8f0e97 + 440d6fa commit ecd23a7

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

spec/omniauth/strategies/saml_spec.rb

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
end
77
end
88

9-
def post_xml(xml = :example_response, opts = {})
10-
post "/auth/saml/callback", opts.merge({'SAMLResponse' => load_xml(xml)})
11-
end
12-
139
describe OmniAuth::Strategies::SAML, :type => :strategy do
1410
include OmniAuth::Test::StrategyTestCase
1511

@@ -118,24 +114,27 @@ def post_xml(xml = :example_response, opts = {})
118114
end
119115

120116
describe 'POST /auth/saml/callback' do
121-
subject { last_response }
122-
123117
let(:xml) { :example_response }
118+
let(:params) { { 'SAMLResponse' => load_xml(xml) } }
119+
120+
subject(:post_callback_response) do
121+
post "/auth/saml/callback", params
122+
end
124123

125124
before :each do
126125
allow(Time).to receive(:now).and_return(Time.utc(2012, 11, 8, 20, 40, 00))
127126
end
128127

129128
context "when the response is valid" do
130-
before :each do
131-
post_xml
132-
end
133-
134129
it "should set the uid to the nameID in the SAML response" do
130+
post_callback_response
131+
135132
expect(auth_hash['uid']).to eq '_1f6fcf6be5e13b08b1e3610e7ff59f205fbd814f23'
136133
end
137134

138135
it "should set the raw info to all attributes" do
136+
post_callback_response
137+
139138
expect(auth_hash['extra']['raw_info'].all.to_hash).to eq(
140139
'first_name' => ['Rajiv'],
141140
'last_name' => ['Manglani'],
@@ -146,6 +145,8 @@ def post_xml(xml = :example_response, opts = {})
146145
end
147146

148147
it "should set the response_object to the response object from ruby_saml response" do
148+
post_callback_response
149+
149150
expect(auth_hash['extra']['response_object']).to be_kind_of(OneLogin::RubySaml::Response)
150151
end
151152
end
@@ -154,24 +155,22 @@ def post_xml(xml = :example_response, opts = {})
154155
before :each do
155156
saml_options.delete(:assertion_consumer_service_url)
156157
OmniAuth.config.full_host = 'http://localhost:9080'
157-
post_xml
158158
end
159159

160160
it { is_expected.not_to fail_with(:invalid_ticket) }
161161
end
162162

163163
context "when there is no SAMLResponse parameter" do
164-
before :each do
165-
post '/auth/saml/callback'
166-
end
164+
let(:params) { {} }
167165

168166
it { is_expected.to fail_with(:invalid_ticket) }
169167
end
170168

171169
context "when there is no name id in the XML" do
170+
let(:xml) { :no_name_id }
171+
172172
before :each do
173173
allow(Time).to receive(:now).and_return(Time.utc(2012, 11, 8, 23, 55, 00))
174-
post_xml :no_name_id
175174
end
176175

177176
it { is_expected.to fail_with(:invalid_ticket) }
@@ -180,58 +179,55 @@ def post_xml(xml = :example_response, opts = {})
180179
context "when the fingerprint is invalid" do
181180
before :each do
182181
saml_options[:idp_cert_fingerprint] = "00:00:00:00:00:0C:6C:A9:41:0F:6E:83:F6:D1:52:25:45:58:89:FB"
183-
post_xml
184182
end
185183

186184
it { is_expected.to fail_with(:invalid_ticket) }
187185
end
188186

189187
context "when the digest is invalid" do
190-
before :each do
191-
post_xml :digest_mismatch
192-
end
188+
let(:xml) { :digest_mismatch }
193189

194190
it { is_expected.to fail_with(:invalid_ticket) }
195191
end
196192

197193
context "when the signature is invalid" do
198-
before :each do
199-
post_xml :invalid_signature
200-
end
194+
let(:xml) { :invalid_signature }
201195

202196
it { is_expected.to fail_with(:invalid_ticket) }
203197
end
204198

205199
context "when the response is stale" do
200+
let(:xml) { :example_response }
201+
206202
before :each do
207203
allow(Time).to receive(:now).and_return(Time.utc(2012, 11, 8, 20, 45, 00))
208204
end
209205

210206
context "without :allowed_clock_drift option" do
211-
before { post_xml :example_response }
212-
213207
it { is_expected.to fail_with(:invalid_ticket) }
214208
end
215209

216210
context "with :allowed_clock_drift option" do
217211
before :each do
218212
saml_options[:allowed_clock_drift] = 60
219-
post_xml :example_response
220213
end
221214

222215
it { is_expected.to_not fail_with(:invalid_ticket) }
223216
end
224217
end
225218

226219
context "when response has custom attributes" do
220+
let(:xml) { :custom_attributes }
221+
227222
before :each do
228223
saml_options[:idp_cert_fingerprint] = "3B:82:F1:F5:54:FC:A8:FF:12:B8:4B:B8:16:61:1D:E4:8E:9B:E2:3C"
229224
saml_options[:attribute_statements] = {
230225
email: ["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"],
231226
first_name: ["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"],
232227
last_name: ["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"]
233228
}
234-
post_xml :custom_attributes
229+
230+
post_callback_response
235231
end
236232

237233
it "should obey attribute statements mapping" do
@@ -245,10 +241,13 @@ def post_xml(xml = :example_response, opts = {})
245241
end
246242

247243
context "when using custom user id attribute" do
244+
let(:xml) { :custom_attributes }
245+
248246
before :each do
249247
saml_options[:idp_cert_fingerprint] = "3B:82:F1:F5:54:FC:A8:FF:12:B8:4B:B8:16:61:1D:E4:8E:9B:E2:3C"
250248
saml_options[:uid_attribute] = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
251-
post_xml :custom_attributes
249+
250+
post_callback_response
252251
end
253252

254253
it "should return user id attribute" do
@@ -259,11 +258,10 @@ def post_xml(xml = :example_response, opts = {})
259258
context "when using custom user id attribute, but it is missing" do
260259
before :each do
261260
saml_options[:uid_attribute] = "missing_attribute"
262-
post_xml
263261
end
264262

265263
it "should fail to authenticate" do
266-
should fail_with(:invalid_ticket)
264+
expect(post_callback_response).to fail_with(:invalid_ticket)
267265
expect(last_request.env['omniauth.error']).to be_instance_of(OmniAuth::Strategies::SAML::ValidationError)
268266
expect(last_request.env['omniauth.error'].message).to eq("SAML response missing 'missing_attribute' attribute")
269267
end

0 commit comments

Comments
 (0)