Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaeta committed Aug 27, 2024
1 parent cc1ac7a commit cf8a971
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/baton-aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/spf13/viper"
)

const (
ExternalIDLengthMaximum = 65 // TODO(marcos): this might be a bug.
ExternalIDLengthMinimum = 32
RegionDefault = "us-east-1"
)

var (
ExternalIdField = field.StringField(
"external-id",
Expand All @@ -29,7 +35,7 @@ var (
GlobalAwsSsoRegionField = field.StringField(
"global-aws-sso-region",
field.WithDescription("The region for the sso identities"),
field.WithDefaultValue("us-east-1"),
field.WithDefaultValue(RegionDefault),
)
GlobalBindingExternalIdField = field.StringField(
"global-binding-external-id",
Expand Down Expand Up @@ -103,7 +109,7 @@ func ValidateExternalId(input string) error {
return fmt.Errorf("external id is missing")
}

if fieldLength < 32 || fieldLength > 65 {
if fieldLength < ExternalIDLengthMinimum || fieldLength > ExternalIDLengthMaximum {
return fmt.Errorf("aws_external_id must be between 32 and 64 bytes")
}
return nil
Expand Down
73 changes: 73 additions & 0 deletions cmd/baton-aws/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"context"
"fmt"
"testing"

"github.com/conductorone/baton-sdk/pkg/test"

Check failure on line 8 in cmd/baton-aws/config_test.go

View workflow job for this annotation

GitHub Actions / go-test (1.21.x, ubuntu-latest)

cannot find module providing package github.com/conductorone/baton-sdk/pkg/test: import lookup disabled by -mod=vendor
"github.com/conductorone/baton-sdk/pkg/ustrings"
"github.com/spf13/viper"
)

const (
exampleARN = "arn:aws:iam::123456789012:role/David"
exampleExternalID = "12345678901234567890123456789012"
s3ARN = "arn:aws:s3:::my_corporate_bucket/exampleobject.png"
)

func TestConfigs(t *testing.T) {
ctx := context.Background()
test.ExerciseTestCasesFromExpressions(
t,
Configuration,
func(viper *viper.Viper) error { return validateConfig(ctx, viper) },
ustrings.ParseFlags,
[]test.TestCaseFromExpression{
{
"",
true,
"empty",
},
{
"--use-assume",
false,
"externalID + ARN missing",
},
{
fmt.Sprintf("--use-assume --external-id %s", exampleExternalID),
false,
"ARN missing",
},
{
fmt.Sprintf("--use-assume --role-arn %s", exampleARN),
false,
"external ID missing",
},
{
fmt.Sprintf("--use-assume --external-id 1 --role-arn %s", exampleARN),
false,
"externalID too short",
},
{

fmt.Sprintf(
"--use-assume --external-id %s --role-arn %s",
exampleExternalID,
s3ARN,
),
false,
"ARN is not IAM",
},
{
fmt.Sprintf(
"--use-assume --external-id %s --role-arn %s",
exampleExternalID,
exampleARN,
),
true,
"all",
},
},
)
}

0 comments on commit cf8a971

Please sign in to comment.