Skip to content

Commit

Permalink
fix: tests refactoring (#1950)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Dodon <[email protected]>
  • Loading branch information
adodon2go authored Oct 26, 2023
1 parent a3d8202 commit d2fbd27
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 504 deletions.
24 changes: 15 additions & 9 deletions pkg/api/authn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func TestAPIKeys(t *testing.T) {
conf := config.New()
conf.HTTP.Port = port

htpasswdPath := test.MakeHtpasswdFile()
username, seedUser := test.GenerateRandomString()
password, seedPass := test.GenerateRandomString()
htpasswdPath := test.MakeHtpasswdFileFromString(test.GetCredString(username, password))
defer os.Remove(htpasswdPath)

mockOIDCServer, err := authutils.MockOIDCRun()
Expand Down Expand Up @@ -125,6 +127,7 @@ func TestAPIKeys(t *testing.T) {
conf.Extensions.UI.Enable = &defaultVal

ctlr := api.NewController(conf)
ctlr.Log.Info().Int64("seedUser", seedUser).Int64("seedPass", seedPass).Msg("random seed for username & password")
dir := t.TempDir()

ctlr.Config.Storage.RootDirectory = dir
Expand All @@ -145,7 +148,7 @@ func TestAPIKeys(t *testing.T) {
Convey("API key retrieved with basic auth", func() {
resp, err := resty.R().
SetBody(reqBody).
SetBasicAuth("test", "test").
SetBasicAuth(username, password).
Post(baseURL + constants.APIKeyPath)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
Expand All @@ -162,15 +165,15 @@ func TestAPIKeys(t *testing.T) {
So(email, ShouldNotBeEmpty)

resp, err = resty.R().
SetBasicAuth("test", apiKeyResponse.APIKey).
SetBasicAuth(username, apiKeyResponse.APIKey).
Get(baseURL + "/v2/_catalog")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusOK)

// get API key list with basic auth
resp, err = resty.R().
SetBasicAuth("test", "test").
SetBasicAuth(username, password).
Get(baseURL + constants.APIKeyPath)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
Expand All @@ -189,7 +192,7 @@ func TestAPIKeys(t *testing.T) {
// add another one
resp, err = resty.R().
SetBody(reqBody).
SetBasicAuth("test", "test").
SetBasicAuth(username, password).
Post(baseURL + constants.APIKeyPath)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
Expand All @@ -199,15 +202,15 @@ func TestAPIKeys(t *testing.T) {
So(err, ShouldBeNil)

resp, err = resty.R().
SetBasicAuth("test", apiKeyResponse.APIKey).
SetBasicAuth(username, apiKeyResponse.APIKey).
Get(baseURL + "/v2/_catalog")
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusOK)

// get API key list with api key auth
resp, err = resty.R().
SetBasicAuth("test", apiKeyResponse.APIKey).
SetBasicAuth(username, apiKeyResponse.APIKey).
Get(baseURL + constants.APIKeyPath)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
Expand Down Expand Up @@ -600,7 +603,7 @@ func TestAPIKeys(t *testing.T) {
So(len(apiKeyListResponse.APIKeys), ShouldEqual, 0)

resp, err = client.R().
SetBasicAuth("test", "test").
SetBasicAuth(username, password).
SetQueryParam("id", apiKeyResponse.UUID).
Delete(baseURL + constants.APIKeyPath)
So(err, ShouldBeNil)
Expand Down Expand Up @@ -832,7 +835,9 @@ func TestAPIKeys(t *testing.T) {
func TestAPIKeysOpenDBError(t *testing.T) {
Convey("Test API keys - unable to create database", t, func() {
conf := config.New()
htpasswdPath := test.MakeHtpasswdFile()
username, seedUser := test.GenerateRandomString()
password, seedPass := test.GenerateRandomString()
htpasswdPath := test.MakeHtpasswdFileFromString(test.GetCredString(username, password))
defer os.Remove(htpasswdPath)

mockOIDCServer, err := authutils.MockOIDCRun()
Expand Down Expand Up @@ -871,6 +876,7 @@ func TestAPIKeysOpenDBError(t *testing.T) {
}

ctlr := api.NewController(conf)
ctlr.Log.Info().Int64("seedUser", seedUser).Int64("seedPass", seedPass).Msg("random seed for username & password")
dir := t.TempDir()

err = os.Chmod(dir, 0o000)
Expand Down
Loading

0 comments on commit d2fbd27

Please sign in to comment.