Skip to content

Commit

Permalink
fix: fix tests, passing token as pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tomazpu committed Oct 18, 2024
1 parent 8ea9973 commit 3a052d8
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 115 deletions.
47 changes: 35 additions & 12 deletions cmd/monaco/download/download_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestDownloadConfigsBehaviour(t *testing.T) {
tt.givenOpts.downloadOptionsShared = downloadOptionsShared{
environmentURL: "testurl.com",
auth: manifest.Auth{
Token: manifest.AuthSecret{
Token: &manifest.AuthSecret{
Name: "TEST_TOKEN_VAR",
Value: "test.token",
},
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestDownload_Options(t *testing.T) {
"download all if options are not limiting",
downloadConfigsOptions{
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{OAuth: &manifest.OAuth{}}, // OAuth required to be defined for platform types
auth: manifest.Auth{Token: &manifest.AuthSecret{}, OAuth: &manifest.OAuth{}}, // OAuth and Token required to download whole config
},
},
wantDownload{
Expand All @@ -188,38 +188,56 @@ func TestDownload_Options(t *testing.T) {
},
{
"only settings requested",
downloadConfigsOptions{onlySettings: true},
downloadConfigsOptions{
onlySettings: true,
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{Token: &manifest.AuthSecret{}},
}},
wantDownload{settings: true},
},
{
"specific settings requested",
downloadConfigsOptions{specificSchemas: []string{"some:schema"}},
downloadConfigsOptions{
specificSchemas: []string{"some:schema"},
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{Token: &manifest.AuthSecret{}},
}},
wantDownload{settings: true},
},
{
"only documents requested",
downloadConfigsOptions{onlyDocuments: true,
downloadConfigsOptions{
onlyDocuments: true,
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{OAuth: &manifest.OAuth{}},
}},
wantDownload{document: true},
},
{
"only openpipeline requested",
downloadConfigsOptions{onlyOpenPipeline: true,
downloadConfigsOptions{
onlyOpenPipeline: true,
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{OAuth: &manifest.OAuth{}},
}},
wantDownload{openpipeline: true},
},
{
"only apis requested",
downloadConfigsOptions{onlyAPIs: true},
downloadConfigsOptions{
onlyAPIs: true,
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{Token: &manifest.AuthSecret{}},
}},
wantDownload{config: true},
},
{
"specific config apis requested",
downloadConfigsOptions{specificAPIs: []string{"alerting-profile"}},
downloadConfigsOptions{
specificAPIs: []string{"alerting-profile"},
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{Token: &manifest.AuthSecret{}},
}},
wantDownload{config: true},
},
{
Expand All @@ -234,7 +252,12 @@ func TestDownload_Options(t *testing.T) {
},
{
"specific APIs and schemas",
downloadConfigsOptions{specificAPIs: []string{"alerting-profile"}, specificSchemas: []string{"some:schema"}},
downloadConfigsOptions{
specificAPIs: []string{"alerting-profile"},
specificSchemas: []string{"some:schema"},
downloadOptionsShared: downloadOptionsShared{
auth: manifest.Auth{Token: &manifest.AuthSecret{}},
}},
wantDownload{config: true, settings: true},
},
}
Expand Down Expand Up @@ -375,7 +398,7 @@ func TestDownloadConfigsExitsEarlyForUnknownSettingsSchema(t *testing.T) {
downloadOptionsShared: downloadOptionsShared{
environmentURL: "testurl.com",
auth: manifest.Auth{
Token: manifest.AuthSecret{
Token: &manifest.AuthSecret{
Name: "TEST_TOKEN_VAR",
Value: "test.token",
},
Expand All @@ -397,7 +420,7 @@ func TestMapToAuth(t *testing.T) {
t.Run("Best case scenario only with token", func(t *testing.T) {
t.Setenv("TOKEN", "token_value")

expected := &manifest.Auth{Token: manifest.AuthSecret{Name: "TOKEN", Value: "token_value"}}
expected := &manifest.Auth{Token: &manifest.AuthSecret{Name: "TOKEN", Value: "token_value"}}

actual, errs := auth{token: "TOKEN"}.mapToAuth()

Expand All @@ -410,7 +433,7 @@ func TestMapToAuth(t *testing.T) {
t.Setenv("CLIENT_SECRET", "client_secret_value")

expected := &manifest.Auth{
Token: manifest.AuthSecret{Name: "TOKEN", Value: "token_value"},
Token: &manifest.AuthSecret{Name: "TOKEN", Value: "token_value"},
OAuth: &manifest.OAuth{
ClientID: manifest.AuthSecret{Name: "CLIENT_ID", Value: "client_id_value"},
ClientSecret: manifest.AuthSecret{Name: "CLIENT_SECRET", Value: "client_secret_value"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/monaco/download/download_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ func setupTestingDownloadOptions(t *testing.T, server *httptest.Server, projectN
downloadOptionsShared: downloadOptionsShared{
environmentURL: server.URL,
auth: manifest.Auth{
Token: manifest.AuthSecret{
Token: &manifest.AuthSecret{
Name: "TOKEN_ENV_VAR",
Value: "token",
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/monaco/dynatrace/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func VerifyEnvironmentGeneration(envs manifest.Environments) bool {
}

func isValidEnvironment(env manifest.EnvironmentDefinition) bool {
if env.Auth.Token == nil && env.Auth.OAuth == nil {
log.Error("no token and oAuth provided in manifest")
return false
}

if env.Auth.OAuth == nil {
return isClassicEnvironment(env)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/monaco/dynatrace/dynatrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestVerifyEnvironmentGen(t *testing.T) {
Name: "URL",
Value: server.URL,
},
Auth: manifest.Auth{Token: manifest.AuthSecret{Name: "DT_API_TOKEN", Value: "some token"}},
Auth: manifest.Auth{Token: &manifest.AuthSecret{Name: "DT_API_TOKEN", Value: "some token"}},
},
})
assert.True(t, ok)
Expand Down
30 changes: 15 additions & 15 deletions pkg/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestConvertParameters(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -181,7 +181,7 @@ func TestConvertConvertRemovesEscapeCharsFromParameters(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -350,7 +350,7 @@ func TestLoadPropertiesForEnvironment(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: groupName,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -403,7 +403,7 @@ func TestConvertConfig(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -471,7 +471,7 @@ func TestConvertDeprecatedConfigToLatest(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -545,7 +545,7 @@ func TestConvertConfigWithEnvNameCollisionShouldFail(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -590,7 +590,7 @@ func TestConvertSkippedConfig(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
}

Expand Down Expand Up @@ -643,15 +643,15 @@ func TestConvertConfigs(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: environmentGroup,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
},
environmentName2: manifest.EnvironmentDefinition{
Name: environmentName2,
URL: createSimpleUrlDefinition(),
Group: environmentGroup2,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
},
}
Expand Down Expand Up @@ -755,7 +755,7 @@ func TestConvertWithMissingName(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: "development",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
},
}
Expand Down Expand Up @@ -828,15 +828,15 @@ func TestConvertProjects(t *testing.T) {
URL: createSimpleUrlDefinition(),
Group: environmentGroup,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
},
environmentName2: manifest.EnvironmentDefinition{
Name: environmentName2,
URL: createSimpleUrlDefinition(),
Group: environmentGroup2,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "token"},
Token: &manifest.AuthSecret{Name: "token"},
},
},
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ func TestNewEnvironmentDefinitionFromV1(t *testing.T) {
URL: newUrlDefinitionFromV1(tt.args.env),
Group: tt.args.group,
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: tt.args.env.GetTokenName()},
Token: &manifest.AuthSecret{Name: tt.args.env.GetTokenName()},
},
}); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewEnvironmentDefinitionFromV1() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -1434,7 +1434,7 @@ func createEnvEnvironmentDefinition() manifest.EnvironmentDefinition {
},
Group: "group",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "NAME"},
Token: &manifest.AuthSecret{Name: "NAME"},
},
}
}
Expand All @@ -1448,7 +1448,7 @@ func createValueEnvironmentDefinition() manifest.EnvironmentDefinition {
},
Group: "group",
Auth: manifest.Auth{
Token: manifest.AuthSecret{Name: "NAME"},
Token: &manifest.AuthSecret{Name: "NAME"},
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/download/download_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestWriteToDisk(t *testing.T) {
proj := CreateProjectData(tt.args.downloadedConfigs, tt.args.projectName) //using CreateProject data to simplify test struct setup
writerContext := WriterContext{
ProjectToWrite: proj,
Auth: manifest.Auth{Token: manifest.AuthSecret{
Auth: manifest.Auth{Token: &manifest.AuthSecret{
Name: tt.args.tokenEnvVarName,
}},
EnvironmentUrl: tt.args.environmentUrl,
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestWriteToDisk_OverwritesManifestIfForced(t *testing.T) {
proj := CreateProjectData(downloadedConfigs, projectName) //using CreateProject data to simplify test struct setup
writerContext := WriterContext{
ProjectToWrite: proj,
Auth: manifest.Auth{Token: manifest.AuthSecret{
Auth: manifest.Auth{Token: &manifest.AuthSecret{
Name: tokenEnvVarName,
}},
EnvironmentUrl: environmentUrl,
Expand Down
Loading

0 comments on commit 3a052d8

Please sign in to comment.