-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to switch user during test execution (#7164)
feat: ability to switch user during test execution (#7164) Signed-off-by: pashavictorovich <pavel@codefresh.io>
- Loading branch information
1 parent
be884d2
commit c64e8df
Showing
5 changed files
with
216 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package project | ||
|
||
import ( | ||
"github.com/argoproj/argo-cd/v2/test/e2e/fixture" | ||
) | ||
|
||
// this implements the "when" part of given/when/then | ||
// | ||
// none of the func implement error checks, and that is complete intended, you should check for errors | ||
// using the Then() | ||
type Actions struct { | ||
context *Context | ||
ignoreErrors bool | ||
} | ||
|
||
func (a *Actions) IgnoreErrors() *Actions { | ||
a.ignoreErrors = true | ||
return a | ||
} | ||
|
||
func (a *Actions) DoNotIgnoreErrors() *Actions { | ||
a.ignoreErrors = false | ||
return a | ||
} | ||
|
||
func (a *Actions) prepareSetPasswordArgs(account string) []string { | ||
a.context.t.Helper() | ||
return []string{ | ||
"account", "update-password", "--account", account, "--current-password", fixture.AdminPassword, "--new-password", fixture.DefaultTestUserPassword, "--plaintext", | ||
} | ||
} | ||
|
||
func (a *Actions) Create() *Actions { | ||
fixture.SetAccounts(map[string][]string{ | ||
a.context.name: {"login"}, | ||
}) | ||
_, _ = fixture.RunCli(a.prepareSetPasswordArgs(a.context.name)...) | ||
return a | ||
} | ||
|
||
func (a *Actions) Login() *Actions { | ||
fixture.LoginAs(a.context.name) | ||
return a | ||
} | ||
|
||
func (a *Actions) Then() *Consequences { | ||
a.context.t.Helper() | ||
return &Consequences{a.context, a} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package project | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/argoproj/argo-cd/v2/pkg/apiclient/session" | ||
|
||
"github.com/argoproj/argo-cd/v2/pkg/apiclient/account" | ||
"github.com/argoproj/argo-cd/v2/test/e2e/fixture" | ||
. "github.com/argoproj/argo-cd/v2/util/errors" | ||
"github.com/argoproj/argo-cd/v2/util/io" | ||
) | ||
|
||
// this implements the "then" part of given/when/then | ||
type Consequences struct { | ||
context *Context | ||
actions *Actions | ||
} | ||
|
||
func (c *Consequences) And(block func(account *account.Account, err error)) *Consequences { | ||
c.context.t.Helper() | ||
block(c.get()) | ||
return c | ||
} | ||
|
||
func (c *Consequences) CurrentUser(block func(user *session.GetUserInfoResponse, err error)) *Consequences { | ||
c.context.t.Helper() | ||
block(c.getCurrentUser()) | ||
return c | ||
} | ||
|
||
func (c *Consequences) get() (*account.Account, error) { | ||
_, accountClient, _ := fixture.ArgoCDClientset.NewAccountClient() | ||
accList, err := accountClient.ListAccounts(context.Background(), &account.ListAccountRequest{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, acc := range accList.Items { | ||
if acc.Name == c.context.name { | ||
return acc, nil | ||
} | ||
} | ||
return nil, errors.New("account not found") | ||
} | ||
|
||
func (c *Consequences) getCurrentUser() (*session.GetUserInfoResponse, error) { | ||
closer, client, err := fixture.ArgoCDClientset.NewSessionClient() | ||
CheckError(err) | ||
defer io.Close(closer) | ||
return client.GetUserInfo(context.Background(), &session.GetUserInfoRequest{}) | ||
} | ||
|
||
func (c *Consequences) Given() *Context { | ||
return c.context | ||
} | ||
|
||
func (c *Consequences) When() *Actions { | ||
return c.actions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package project | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/argoproj/argo-cd/v2/test/e2e/fixture" | ||
"github.com/argoproj/argo-cd/v2/util/env" | ||
) | ||
|
||
// this implements the "given" part of given/when/then | ||
type Context struct { | ||
t *testing.T | ||
// seconds | ||
timeout int | ||
name string | ||
} | ||
|
||
func Given(t *testing.T) *Context { | ||
fixture.EnsureCleanState(t) | ||
// ARGOCE_E2E_DEFAULT_TIMEOUT can be used to override the default timeout | ||
// for any context. | ||
timeout := env.ParseNumFromEnv("ARGOCD_E2E_DEFAULT_TIMEOUT", 10, 0, 180) | ||
return &Context{t: t, name: fixture.Name(), timeout: timeout} | ||
} | ||
|
||
func (c *Context) GetName() string { | ||
return c.name | ||
} | ||
|
||
func (c *Context) Name(name string) *Context { | ||
c.name = name | ||
return c | ||
} | ||
|
||
func (c *Context) And(block func()) *Context { | ||
block() | ||
return c | ||
} | ||
|
||
func (c *Context) When() *Actions { | ||
// in case any settings have changed, pause for 1s, not great, but fine | ||
time.Sleep(1 * time.Second) | ||
return &Actions{context: c} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters