github.com/aiven/aiven-go-client@v1.36.0/accounts_acc_test.go (about)

     1  package aiven
     2  
     3  import (
     4  	"math/rand"
     5  	"strconv"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Accounts", func() {
    12  	Context("Check all possible accounts interactions", func() {
    13  		// Account
    14  		var (
    15  			accountName string
    16  			account     *AccountResponse
    17  			err         error
    18  		)
    19  
    20  		It("Account creation should not error", func() {
    21  			accountName = "test-acc-account-" + strconv.Itoa(rand.Int())
    22  			account, err = client.Accounts.Create(Account{Name: accountName})
    23  			Expect(err).NotTo(HaveOccurred())
    24  		})
    25  
    26  		It("Account creation should populate fields properly", func() {
    27  			Expect(account.Account.Id).NotTo(BeEmpty())
    28  			Expect(account.Account.Name).To(Equal(accountName))
    29  			Expect(account.Account.CreateTime).NotTo(BeNil())
    30  			Expect(account.Account.UpdateTime).NotTo(BeNil())
    31  			Expect(account.Account.IsAccountOwner).To(Equal(true))
    32  			Expect(account.Account.PrimaryBillingGroupId).NotTo(BeNil())
    33  			Expect(account.APIResponse.Message).To(BeEmpty())
    34  			Expect(account.APIResponse.Errors).To(BeEmpty())
    35  		})
    36  
    37  		// AccountTeams
    38  		var (
    39  			teamName string
    40  			team     *AccountTeamResponse
    41  			errT     error
    42  		)
    43  
    44  		It("Create account team", func() {
    45  			if account == nil {
    46  				Fail("account is nil")
    47  			}
    48  			teamName = "test-acc-team" + strconv.Itoa(rand.Int())
    49  			team, errT = client.AccountTeams.Create(account.Account.Id, AccountTeam{
    50  				Name: teamName,
    51  			})
    52  
    53  			Expect(errT).NotTo(HaveOccurred())
    54  
    55  			Expect(team.Team.Id).NotTo(BeEmpty())
    56  			Expect(team.Team.Name).To(Equal(teamName))
    57  			Expect(team.Team.UpdateTime).NotTo(BeNil())
    58  			Expect(team.Team.CreateTime).NotTo(BeNil())
    59  			Expect(team.APIResponse.Message).To(BeEmpty())
    60  			Expect(team.APIResponse.Errors).To(BeEmpty())
    61  		})
    62  
    63  		// Account members
    64  		var (
    65  			memberEmail string
    66  			errM        error
    67  		)
    68  
    69  		It("should not error", func() {
    70  			memberEmail = "ivan.savciuc+" + strconv.Itoa(rand.Int()) + "@aiven.io"
    71  			errM = client.AccountTeamMembers.Invite(account.Account.Id, team.Team.Id, memberEmail)
    72  
    73  			Expect(memberEmail).NotTo(BeEmpty())
    74  			Expect(errM).NotTo(HaveOccurred())
    75  		})
    76  
    77  		It("should send invite", func() {
    78  			invites, errI := client.AccountTeamInvites.List(account.Account.Id, team.Team.Id)
    79  			Expect(errI).NotTo(HaveOccurred())
    80  
    81  			var found bool
    82  			for _, invite := range invites.Invites {
    83  				if invite.UserEmail == memberEmail {
    84  					found = true
    85  				}
    86  			}
    87  
    88  			Expect(found).To(Equal(true), "cannot find invitation for newly created member")
    89  
    90  			if errD := client.AccountTeamInvites.Delete(account.Account.Id, team.Team.Id, memberEmail); errD != nil {
    91  				Fail("cannot delete an invitation :" + errD.Error())
    92  			}
    93  		})
    94  
    95  		// Account project
    96  		var (
    97  			projectName string
    98  			projectType = "admin"
    99  		)
   100  
   101  		It("Account projects creation ", func() {
   102  			projectName = "test-acc-pr" + strconv.Itoa(rand.Int())
   103  
   104  			By("Create project")
   105  			if _, errP := client.Projects.Create(CreateProjectRequest{
   106  				Project:   projectName,
   107  				AccountId: ToStringPointer(account.Account.Id),
   108  				Tags:      map[string]string{},
   109  			}); errP != nil {
   110  				Fail("cannot create project :" + errP.Error())
   111  			}
   112  
   113  			By("Create account team project association")
   114  			if errTP := client.AccountTeamProjects.Create(
   115  				account.Account.Id,
   116  				team.Team.Id,
   117  				AccountTeamProject{ProjectName: projectName, TeamType: "developer"}); errTP != nil {
   118  				Fail("cannot create account team project association:" + errTP.Error())
   119  			}
   120  
   121  			By("Update account team project")
   122  			if errTPu := client.AccountTeamProjects.Update(
   123  				account.Account.Id,
   124  				team.Team.Id,
   125  				AccountTeamProject{ProjectName: projectName, TeamType: projectType}); errTPu != nil {
   126  				Fail("cannot update account team project:" + errTPu.Error())
   127  			}
   128  		})
   129  
   130  		It("Account team project association should be in the list", func() {
   131  			projects, errL := client.AccountTeamProjects.List(account.Account.Id, team.Team.Id)
   132  			Expect(errL).NotTo(HaveOccurred())
   133  
   134  			if projects != nil {
   135  				var found bool
   136  				for _, p := range projects.Projects {
   137  					if p.ProjectName == projectName {
   138  						Expect(p.TeamType).To(Equal(projectType))
   139  						found = true
   140  					}
   141  				}
   142  
   143  				Expect(found).To(Equal(true), "cannot find project in the account team projects list")
   144  			}
   145  		})
   146  
   147  		It("AccountAuthentications check authentication methods", func() {
   148  			list, errL := client.AccountAuthentications.List(account.Account.Id)
   149  			Expect(errL).NotTo(HaveOccurred())
   150  			Expect(list.AuthenticationMethods).NotTo(BeEmpty(), "default auth methods should be created automatically")
   151  		})
   152  
   153  		It("AccountAuthentications  add new one", func() {
   154  			resp, errL := client.AccountAuthentications.Create(account.Account.Id, AccountAuthenticationMethodCreate{
   155  				AuthenticationMethodName: "test-auth",
   156  				AuthenticationMethodType: "saml",
   157  			})
   158  			Expect(errL).NotTo(HaveOccurred())
   159  			Expect(resp.AuthenticationMethod.AuthenticationMethodID).NotTo(BeEmpty())
   160  			Expect(resp.AuthenticationMethod.SAMLMetadataURL).NotTo(BeEmpty())
   161  			Expect(resp.AuthenticationMethod.SAMLAcsURL).NotTo(BeEmpty())
   162  			Expect(resp.AuthenticationMethod.AccountID).To(Equal(account.Account.Id))
   163  			Expect(resp.APIResponse.Message).To(BeEmpty())
   164  			Expect(resp.APIResponse.Errors).To(BeEmpty())
   165  		})
   166  
   167  		It("remove all the stuff", func() {
   168  			if errD := client.AccountTeamProjects.Delete(account.Account.Id, team.Team.Id, projectName); errD != nil {
   169  				Fail("cannot delete account team project association :" + errD.Error())
   170  			}
   171  
   172  			if errD := client.Projects.Delete(projectName); errD != nil {
   173  				Fail("cannot delete project :" + errD.Error())
   174  			}
   175  
   176  			if list, errL := client.AccountTeamMembers.List(account.Account.Id, team.Team.Id); errL != nil {
   177  				Fail("cannot get a list of account team members:" + errL.Error())
   178  			} else {
   179  				for _, m := range list.Members {
   180  					if errD := client.AccountTeamMembers.Delete(account.Account.Id, team.Team.Id, m.UserEmail); errD != nil {
   181  						Fail("cannot delete account team member:" + errD.Error())
   182  					}
   183  				}
   184  			}
   185  			if errD := client.AccountTeams.Delete(account.Account.Id, team.Team.Id); errD != nil {
   186  				Fail("cannot delete account team :" + errD.Error())
   187  			}
   188  
   189  			if errD := client.Accounts.Delete(account.Account.Id); errD != nil {
   190  				Fail("cannot delete account :" + errD.Error())
   191  			}
   192  		})
   193  	})
   194  })