github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/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  const NonUAAOrigin = "cli-oidc-provider"
    12  
    13  type User struct {
    14  	GUID      string
    15  	Username  string
    16  	Origin    string
    17  	CreatedAt time.Time
    18  }
    19  
    20  // ListUsers returns all the users in the targeted environment.
    21  func GetUsers() []User {
    22  	var userPagesResponse struct {
    23  		NextURL   *string `json:"next_url"`
    24  		Resources []struct {
    25  			Metadata struct {
    26  				GUID      string    `json:"guid"`
    27  				CreatedAt time.Time `json:"created_at"`
    28  			} `json:"metadata"`
    29  			Entity struct {
    30  				Username string `json:"username"`
    31  			} `json:"entity"`
    32  		} `json:"resources"`
    33  	}
    34  
    35  	var allUsers []User
    36  	nextURL := "/v2/users?results-per-page=50"
    37  
    38  	for {
    39  		session := CF("curl", nextURL)
    40  		Eventually(session).Should(Exit(0))
    41  
    42  		err := json.Unmarshal(session.Out.Contents(), &userPagesResponse)
    43  		Expect(err).NotTo(HaveOccurred())
    44  		for _, resource := range userPagesResponse.Resources {
    45  			allUsers = append(allUsers, User{
    46  				GUID:      resource.Metadata.GUID,
    47  				CreatedAt: resource.Metadata.CreatedAt,
    48  				Username:  resource.Entity.Username,
    49  			})
    50  		}
    51  
    52  		if userPagesResponse.NextURL == nil {
    53  			break
    54  		}
    55  		nextURL = *userPagesResponse.NextURL
    56  	}
    57  
    58  	return allUsers
    59  }
    60  
    61  func GetUsersV3() []User {
    62  	var userPagesResponse struct {
    63  		Pagination struct {
    64  			NextURL *string `json:"next_url"`
    65  		} `json:"pagination"`
    66  		Resources []struct {
    67  			GUID     string `json:"guid"`
    68  			Origin   string `json:"origin"`
    69  			Username string `json:"username"`
    70  		} `json:"resources"`
    71  	}
    72  
    73  	var allUsers []User
    74  	nextURL := "/v3/users?per_page=50"
    75  
    76  	for {
    77  		session := CF("curl", nextURL)
    78  		Eventually(session).Should(Exit(0))
    79  
    80  		err := json.Unmarshal(session.Out.Contents(), &userPagesResponse)
    81  		Expect(err).NotTo(HaveOccurred())
    82  		for _, resource := range userPagesResponse.Resources {
    83  			allUsers = append(allUsers, User{
    84  				Origin:   resource.Origin,
    85  				Username: resource.Username,
    86  			})
    87  		}
    88  
    89  		if userPagesResponse.Pagination.NextURL == nil {
    90  			break
    91  		}
    92  		nextURL = *userPagesResponse.Pagination.NextURL
    93  	}
    94  
    95  	return allUsers
    96  }
    97  
    98  // CreateUser creates a user with a random username and password and returns both.
    99  func CreateUser() (string, string) {
   100  	username := NewUsername()
   101  	password := RandomName()
   102  
   103  	// env := map[string]string{
   104  	// 	"NEW_USER_PASSWORD": password,
   105  	// }
   106  
   107  	// session := CFWithEnv(env, "create-user", username, "$NEW_USER_PASSWORD")
   108  	session := CF("create-user", username, password)
   109  	Eventually(session).Should(Exit(0))
   110  
   111  	return username, password
   112  }
   113  
   114  // DeleteUser deletes the user specified by username.
   115  func DeleteUser(username string) {
   116  	session := CF("delete-user", username, "-f")
   117  	Eventually(session).Should(Exit(0))
   118  }
   119  
   120  // CreateUserInOrgRole creates a user with a random username and password and gives them the specified role within
   121  // a specific org. The new user's username and password are returned.
   122  func CreateUserInOrgRole(org, role string) (string, string) {
   123  	username, password := CreateUser()
   124  
   125  	session := CF("set-org-role", username, org, role)
   126  	Eventually(session).Should(Exit(0))
   127  
   128  	return username, password
   129  }
   130  
   131  // CreateUserInSpaceRole creates a user with a random username and password and gives them the specified role within
   132  // a specific space. The new user's username and password are returned.
   133  func CreateUserInSpaceRole(org, space, role string) (string, string) {
   134  	username, password := CreateUser()
   135  
   136  	session := CF("set-space-role", username, org, space, role)
   137  	Eventually(session).Should(Exit(0))
   138  
   139  	return username, password
   140  }