github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/saml_auth_test.go (about)

     1  //go:build auth || functional || ALL
     2  
     3  /*
     4   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	. "gopkg.in/check.v1"
    11  )
    12  
    13  // Test_SamlAdfsAuth checks if SAML ADFS login works using WS-TRUST endpoint
    14  //
    15  //	"/adfs/services/trust/13/usernamemixed".
    16  //
    17  // Credential variables must be specified in test configuration for it to work
    18  // The steps of this test are:
    19  // * Query object using test framework vCD connection
    20  // * Create a new client with SAML authentication using specified org and query the same object
    21  // using it to make sure access is granted
    22  // * Compare results to ensure that it worked as it should
    23  //
    24  // Note. This test requires real environment setup to work. Unit testing is also available in
    25  // `saml_auth_unit_test.go`
    26  func (vcd *TestVCD) Test_SamlAdfsAuth(check *C) {
    27  	cfg := vcd.config
    28  	if cfg.Provider.SamlUser == "" || cfg.Provider.SamlPassword == "" || cfg.VCD.Org == "" {
    29  		check.Skip("Skipping test because no Org, SamlUser, SamlPassword and was specified")
    30  	}
    31  	vcd.checkSkipWhenApiToken(check)
    32  
    33  	// Get vDC details using existing vCD client
    34  	org, err := vcd.client.GetOrgByName(cfg.VCD.Org)
    35  	check.Assert(err, IsNil)
    36  
    37  	vdc, err := org.GetVDCByName(cfg.VCD.Vdc, true)
    38  	check.Assert(err, IsNil)
    39  
    40  	// Get new vCD session and client using specifically SAML credentials
    41  	samlVcdCli := NewVCDClient(vcd.client.Client.VCDHREF, true,
    42  		WithSamlAdfs(true, cfg.Provider.SamlCustomRptId))
    43  	err = samlVcdCli.Authenticate(cfg.Provider.SamlUser, cfg.Provider.SamlPassword, cfg.VCD.Org)
    44  	check.Assert(err, IsNil)
    45  
    46  	samlOrg, err := vcd.client.GetOrgByName(cfg.VCD.Org)
    47  	check.Assert(err, IsNil)
    48  
    49  	samlVdc, err := samlOrg.GetVDCByName(cfg.VCD.Vdc, true)
    50  	check.Assert(err, IsNil)
    51  
    52  	check.Assert(samlVdc, DeepEquals, vdc)
    53  
    54  	// If SamlCustomRptId was not specified - try to feed VCD entity ID manually (this is usually
    55  	// done automatically, but doing it to test this path is not broken)
    56  	if cfg.Provider.SamlCustomRptId == "" {
    57  		samlEntityId, err := getSamlEntityId(vcd.client, cfg.VCD.Org)
    58  		check.Assert(err, IsNil)
    59  
    60  		samlCustomRptVcdCli := NewVCDClient(vcd.client.Client.VCDHREF, true,
    61  			WithSamlAdfs(true, samlEntityId))
    62  		err = samlCustomRptVcdCli.Authenticate(cfg.Provider.SamlUser, cfg.Provider.SamlPassword, cfg.VCD.Org)
    63  		check.Assert(err, IsNil)
    64  
    65  		samlCustomRptOrg, err := vcd.client.GetOrgByName(cfg.VCD.Org)
    66  		check.Assert(err, IsNil)
    67  
    68  		samlCustomRptVdc, err := samlCustomRptOrg.GetVDCByName(cfg.VCD.Vdc, true)
    69  		check.Assert(err, IsNil)
    70  
    71  		check.Assert(samlCustomRptVdc, DeepEquals, samlVdc)
    72  	}
    73  
    74  }