MCPcopy Create free account
hub / github.com/crewjam/saml / TestIdpAuthnRequestErrorKinds

Function TestIdpAuthnRequestErrorKinds

errors_test.go:272–370  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

270}
271
272func TestIdpAuthnRequestErrorKinds(t *testing.T) {
273 test := idpcovNewTest(t)
274
275 validate := func(t *testing.T, mutate func(r *AuthnRequest)) error {
276 t.Helper()
277 authnRequest, err := test.SP.MakeAuthenticationRequest(test.SP.GetSSOBindingLocation(HTTPRedirectBinding), HTTPRedirectBinding, HTTPPostBinding)
278 assert.NilError(t, err)
279 mutate(authnRequest)
280 buf, err := xml.Marshal(authnRequest)
281 assert.NilError(t, err)
282 req := IdpAuthnRequest{IDP: &test.IDP, Now: TimeNow(), RequestBuffer: buf,
283 HTTPRequest: httptest.NewRequest("POST", "https://idp.example.com/saml/sso", nil)}
284 return req.Validate()
285 }
286 assert.NilError(t, validate(t, func(*AuthnRequest) {}))
287
288 tests := []struct {
289 name string
290 mutate func(r *AuthnRequest)
291 want error
292 }{
293 {"Destination", func(r *AuthnRequest) { r.Destination = "https://elsewhere.example.com/sso" }, ErrDestinationMismatch},
294 {"IssueInstant", func(r *AuthnRequest) { r.IssueInstant = TimeNow().Add(-time.Hour) }, ErrExpired},
295 {"Version", func(r *AuthnRequest) { r.Version = "1.1" }, ErrUnsupported},
296 {"no Issuer", func(r *AuthnRequest) { r.Issuer = nil }, ErrMissingElement},
297 {"unknown Issuer", func(r *AuthnRequest) { r.Issuer.Value = "https://other-sp.example.com/metadata" }, ErrUnknownServiceProvider},
298 {"unknown AssertionConsumerServiceURL", func(r *AuthnRequest) {
299 r.AssertionConsumerServiceURL = "https://sp.example.com/elsewhere"
300 }, ErrUnknownAssertionConsumerService},
301 }
302 for _, tc := range tests {
303 t.Run(tc.name, func(t *testing.T) {
304 checkOnlyKind(t, validate(t, tc.mutate), tc.want)
305 })
306 }
307
308 t.Run("unknown Issuer matches os.ErrNotExist", func(t *testing.T) {
309 err := validate(t, func(r *AuthnRequest) { r.Issuer.Value = "https://other-sp.example.com/metadata" })
310 assert.Check(t, errors.Is(err, os.ErrNotExist))
311 })
312
313 t.Run("invalid XML", func(t *testing.T) {
314 req := IdpAuthnRequest{IDP: &test.IDP, Now: TimeNow(), RequestBuffer: []byte("<a>")}
315 checkOnlyKind(t, req.Validate(), ErrMalformedMessage)
316 })
317
318 t.Run("wrong root element", func(t *testing.T) {
319 req := IdpAuthnRequest{IDP: &test.IDP, Now: TimeNow(), RequestBuffer: []byte("<NotAnAuthnRequest/>")}
320 checkOnlyKind(t, req.Validate(), ErrMalformedMessage)
321 })
322
323 t.Run("service provider lookup error is wrapped", func(t *testing.T) {
324 lookupErr := errors.New("database on fire")
325 old := test.IDP.ServiceProviderProvider
326 t.Cleanup(func() { test.IDP.ServiceProviderProvider = old })
327 test.IDP.ServiceProviderProvider = &mockServiceProviderProvider{
328 GetServiceProviderFunc: func(*http.Request, string) (*EntityDescriptor, error) { return nil, lookupErr },
329 }

Callers

nothing calls this directly

Calls 9

ValidateMethod · 0.95
PostBindingMethod · 0.95
idpcovNewTestFunction · 0.85
checkOnlyKindFunction · 0.85
NewIdpAuthnRequestFunction · 0.85
idpcovRequestFunction · 0.85
GetSSOBindingLocationMethod · 0.80
NewMethod · 0.65

Tested by

no test coverage detected