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