github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/user.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gexec"
     9  )
    10  
    11  type User struct {
    12  	GUID      string
    13  	Username  string
    14  	CreatedAt time.Time
    15  }
    16  
    17  // GetUsers returns all the users in the targeted environment.
    18  func GetUsers() []User {
    19  	var userPagesResponse struct {
    20  		NextURL   *string `json:"next_url"`
    21  		Resources []struct {
    22  			Metadata struct {
    23  				GUID      string    `json:"guid"`
    24  				CreatedAt time.Time `json:"created_at"`
    25  			} `json:"metadata"`
    26  			Entity struct {
    27  				Username string `json:"username"`
    28  			} `json:"entity"`
    29  		} `json:"resources"`
    30  	}
    31  
    32  	var allUsers []User
    33  	nextURL := "/v2/users?results-per-page=50"
    34  
    35  	for {
    36  		session := CF("curl", nextURL)
    37  		Eventually(session).Should(Exit(0))
    38  
    39  		err := json.Unmarshal(session.Out.Contents(), &userPagesResponse)
    40  		Expect(err).NotTo(HaveOccurred())
    41  		for _, resource := range userPagesResponse.Resources {
    42  			allUsers = append(allUsers, User{
    43  				GUID:      resource.Metadata.GUID,
    44  				CreatedAt: resource.Metadata.CreatedAt,
    45  				Username:  resource.Entity.Username,
    46  			})
    47  		}
    48  
    49  		if userPagesResponse.NextURL == nil {
    50  			break
    51  		}
    52  		nextURL = *userPagesResponse.NextURL
    53  	}
    54  
    55  	return allUsers
    56  }
    57  
    58  // CreateUser creates a user with a random username and password and returns both.
    59  func CreateUser() (string, string) {
    60  	username := NewUsername()
    61  	password := RandomName()
    62  
    63  	// env := map[string]string{
    64  	// 	"NEW_USER_PASSWORD": password,
    65  	// }
    66  
    67  	// session := CFWithEnv(env, "create-user", username, "$NEW_USER_PASSWORD")
    68  	session := CF("create-user", username, password)
    69  	Eventually(session).Should(Exit(0))
    70  
    71  	return username, password
    72  }
    73  
    74  // DeleteUser deletes the user specified by username.
    75  func DeleteUser(username string) {
    76  	session := CF("delete-user", username, "-f")
    77  	Eventually(session).Should(Exit(0))
    78  }
    79  
    80  // CreateUserInOrgRole creates a user with a random username and password and gives them the specified role within
    81  // a specific org. The new user's username and password are returned.
    82  func CreateUserInOrgRole(org, role string) (string, string) {
    83  	username, password := CreateUser()
    84  
    85  	session := CF("set-org-role", username, org, role)
    86  	Eventually(session).Should(Exit(0))
    87  
    88  	return username, password
    89  }
    90  
    91  // CreateUserInSpaceRole creates a user with a random username and password and gives them the specified role within
    92  // a specific space. The new user's username and password are returned.
    93  func CreateUserInSpaceRole(org, space, role string) (string, string) {
    94  	username, password := CreateUser()
    95  
    96  	session := CF("set-space-role", username, org, space, role)
    97  	Eventually(session).Should(Exit(0))
    98  
    99  	return username, password
   100  }