github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/helpers/login.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"strings"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  func SetAPI() (string, bool) {
    16  	apiURL := GetAPI()
    17  	skipSSLValidation := skipSSLValidation()
    18  	if skipSSLValidation {
    19  		Eventually(CF("api", apiURL, "--skip-ssl-validation")).Should(Exit(0))
    20  	} else {
    21  		Eventually(CF("api", apiURL)).Should(Exit(0))
    22  	}
    23  	return apiURL, skipSSLValidation
    24  }
    25  
    26  func UnsetAPI() {
    27  	Eventually(CF("api", "--unset")).Should(Exit(0))
    28  }
    29  
    30  func skipSSLValidation() bool {
    31  	if skip, err := strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err == nil && !skip {
    32  		return false
    33  	}
    34  	return true
    35  }
    36  
    37  func GetAPI() string {
    38  	apiURL := os.Getenv("CF_INT_API")
    39  	if apiURL == "" {
    40  		return "https://api.bosh-lite.com"
    41  	}
    42  	if !strings.HasPrefix(apiURL, "http") {
    43  		apiURL = fmt.Sprintf("https://%s", apiURL)
    44  	}
    45  
    46  	return apiURL
    47  }
    48  
    49  func LoginAs(username, password string) {
    50  	env := map[string]string{
    51  		"CF_USERNAME": username,
    52  		"CF_PASSWORD": password,
    53  	}
    54  
    55  	for i := 0; i < 3; i++ {
    56  		session := CFWithEnv(env, "auth")
    57  		Eventually(session).Should(Exit())
    58  		if session.ExitCode() == 0 {
    59  			break
    60  		}
    61  		time.Sleep(3 * time.Second)
    62  	}
    63  }
    64  
    65  func LoginCF() string {
    66  	username, password := GetCredentials()
    67  	LoginAs(username, password)
    68  	return username
    69  }
    70  
    71  func LoginCFWithClientCredentials() string {
    72  	username, password := SkipIfClientCredentialsNotSet()
    73  	env := map[string]string{
    74  		"CF_USERNAME": username,
    75  		"CF_PASSWORD": password,
    76  	}
    77  	Eventually(CFWithEnv(env, "auth", "--client-credentials")).Should(Exit(0))
    78  
    79  	return username
    80  }
    81  
    82  // GetCredentials returns back the username and the password.
    83  func GetCredentials() (string, string) {
    84  	username := os.Getenv("CF_INT_USERNAME")
    85  	if username == "" {
    86  		username = "admin"
    87  	}
    88  	password := os.Getenv("CF_INT_PASSWORD")
    89  	if password == "" {
    90  		password = "admin"
    91  	}
    92  	return username, password
    93  }
    94  
    95  // SkipIfOIDCCredentialsNotSet returns back the username and the password for
    96  // OIDC origin, or skips the test if those values are not set.
    97  func SkipIfOIDCCredentialsNotSet() (string, string) {
    98  	oidcUsername := os.Getenv("CF_INT_OIDC_USERNAME")
    99  	oidcPassword := os.Getenv("CF_INT_OIDC_PASSWORD")
   100  
   101  	if oidcUsername == "" || oidcPassword == "" {
   102  		Skip("CF_INT_OIDC_USERNAME or CF_INT_OIDC_PASSWORD is not set")
   103  	}
   104  
   105  	return oidcUsername, oidcPassword
   106  }
   107  
   108  func LogoutCF() {
   109  	Eventually(CF("logout")).Should(Exit(0))
   110  }
   111  
   112  func TargetOrgAndSpace(org string, space string) {
   113  	Eventually(CF("target", "-o", org, "-s", space)).Should(Exit(0))
   114  }
   115  
   116  func TargetOrg(org string) {
   117  	Eventually(CF("target", "-o", org)).Should(Exit(0))
   118  }
   119  
   120  func ClearTarget() {
   121  	LogoutCF()
   122  	LoginCF()
   123  }
   124  
   125  func SetupCF(org string, space string) {
   126  	LoginCF()
   127  	CreateOrgAndSpace(org, space)
   128  	TargetOrgAndSpace(org, space)
   129  }
   130  
   131  func SwitchToNoRole() string {
   132  	username, password := CreateUser()
   133  	LogoutCF()
   134  	LoginAs(username, password)
   135  	return username
   136  }
   137  
   138  func SwitchToOrgRole(org, role string) string {
   139  	username, password := CreateUserInOrgRole(org, role)
   140  	LogoutCF()
   141  	LoginAs(username, password)
   142  	return username
   143  }
   144  
   145  func SwitchToSpaceRole(org, space, role string) string {
   146  	username, password := CreateUserInSpaceRole(org, space, role)
   147  	LogoutCF()
   148  	LoginAs(username, password)
   149  	return username
   150  }