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

Function TestIdentityProviderOnError

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

Source from the content-addressed store, hash-verified

453}
454
455func TestIdentityProviderOnError(t *testing.T) {
456 type call struct {
457 status int
458 err error
459 }
460 setup := func(t *testing.T) (*IdentityProviderTest, *[]call) {
461 test := idpcovNewTest(t)
462 test.IDP.SessionProvider = &mockSessionProvider{GetSessionFunc: idpcovAliceSession}
463 var calls []call
464 test.IDP.OnError = func(w http.ResponseWriter, _ *http.Request, status int, err error) {
465 calls = append(calls, call{status, err})
466 w.WriteHeader(http.StatusTeapot)
467 }
468 return test, &calls
469 }
470 spMetadata := func(test *IdentityProviderTest, mutate func(*EntityDescriptor)) {
471 test.IDP.ServiceProviderProvider = &mockServiceProviderProvider{
472 GetServiceProviderFunc: func(_ *http.Request, id string) (*EntityDescriptor, error) {
473 if id != test.SP.MetadataURL.String() {
474 return nil, os.ErrNotExist
475 }
476 md := test.SP.Metadata()
477 mutate(md)
478 return md, nil
479 },
480 }
481 }
482 badEncryptionCert := func(md *EntityDescriptor) {
483 md.SPSSODescriptors[0].KeyDescriptors = []KeyDescriptor{idpcovCertKeyDescriptor("encryption", "!!!")}
484 }
485
486 t.Run("ServeSSO unparseable request", func(t *testing.T) {
487 test, calls := setup(t)
488 w := httptest.NewRecorder()
489 test.IDP.ServeSSO(w, httptest.NewRequest("PUT", "https://idp.example.com/saml/sso", nil))
490 assert.Equal(t, http.StatusTeapot, w.Code)
491 assert.Assert(t, len(*calls) == 1)
492 assert.Equal(t, http.StatusBadRequest, (*calls)[0].status)
493 assert.Error(t, (*calls)[0].err, "failed to parse request: method not allowed")
494 assert.Check(t, errors.Is((*calls)[0].err, ErrUnsupported))
495 })
496
497 t.Run("ServeSSO invalid request", func(t *testing.T) {
498 test, calls := setup(t)
499 r := idpcovSSORequest(t, test)
500 test.IDP.ServiceProviderProvider = &mockServiceProviderProvider{
501 GetServiceProviderFunc: func(*http.Request, string) (*EntityDescriptor, error) { return nil, os.ErrNotExist },
502 }
503 test.IDP.ServeSSO(httptest.NewRecorder(), r)
504 assert.Assert(t, len(*calls) == 1)
505 assert.Equal(t, http.StatusBadRequest, (*calls)[0].status)
506 assert.Check(t, errors.Is((*calls)[0].err, ErrUnknownServiceProvider))
507 })
508
509 t.Run("ServeSSO bad SP encryption certificate", func(t *testing.T) {
510 test, calls := setup(t)
511 spMetadata(test, badEncryptionCert)
512 test.IDP.ServeSSO(httptest.NewRecorder(), idpcovSSORequest(t, test))

Callers

nothing calls this directly

Calls 15

idpcovNewTestFunction · 0.85
idpcovCertKeyDescriptorFunction · 0.85
idpcovSSORequestFunction · 0.85
idpcovAssertionMakerFuncFuncType · 0.85
StringMethod · 0.80
ServeSSOMethod · 0.80
ServeIDPInitiatedMethod · 0.80
DefaultOnErrorMethod · 0.80
NewMethod · 0.65
GetMethod · 0.65
mustParsePrivateKeyFunction · 0.50
WriteHeaderMethod · 0.45

Tested by

no test coverage detected