parent
6f33262e8c
commit
bf9b225cb9
9 changed files with 287 additions and 44 deletions
|
|
@ -24,13 +24,16 @@ func TestToCreatePayload(t *testing.T) {
|
|||
})
|
||||
regions := []attr.Value{types.StringValue("EU"), types.StringValue("US")}
|
||||
regionsFixture := types.ListValueMust(types.StringType, regions)
|
||||
blockedCountries := []attr.Value{types.StringValue("XX"), types.StringValue("YY"), types.StringValue("ZZ")}
|
||||
blockedCountriesFixture := types.ListValueMust(types.StringType, blockedCountries)
|
||||
optimizer := types.ObjectValueMust(optimizerTypes, map[string]attr.Value{
|
||||
"enabled": types.BoolValue(true),
|
||||
})
|
||||
config := types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
})
|
||||
modelFixture := func(mods ...func(*Model)) *Model {
|
||||
model := &Model{
|
||||
|
|
@ -55,17 +58,19 @@ func TestToCreatePayload(t *testing.T) {
|
|||
"testHeader0": "testHeaderValue0",
|
||||
"testHeader1": "testHeaderValue1",
|
||||
},
|
||||
OriginUrl: cdn.PtrString("https://www.mycoolapp.com"),
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
OriginUrl: cdn.PtrString("https://www.mycoolapp.com"),
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
|
||||
},
|
||||
IsValid: true,
|
||||
},
|
||||
"happy_path_with_optimizer": {
|
||||
Input: modelFixture(func(m *Model) {
|
||||
m.Config = types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
})
|
||||
}),
|
||||
Expected: &cdn.CreateDistributionPayload{
|
||||
|
|
@ -73,9 +78,10 @@ func TestToCreatePayload(t *testing.T) {
|
|||
"testHeader0": "testHeaderValue0",
|
||||
"testHeader1": "testHeaderValue1",
|
||||
},
|
||||
OriginUrl: cdn.PtrString("https://www.mycoolapp.com"),
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Optimizer: cdn.NewOptimizer(true),
|
||||
OriginUrl: cdn.PtrString("https://www.mycoolapp.com"),
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Optimizer: cdn.NewOptimizer(true),
|
||||
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
|
||||
},
|
||||
IsValid: true,
|
||||
},
|
||||
|
|
@ -127,11 +133,14 @@ func TestConvertConfig(t *testing.T) {
|
|||
})
|
||||
regions := []attr.Value{types.StringValue("EU"), types.StringValue("US")}
|
||||
regionsFixture := types.ListValueMust(types.StringType, regions)
|
||||
blockedCountries := []attr.Value{types.StringValue("XX"), types.StringValue("YY"), types.StringValue("ZZ")}
|
||||
blockedCountriesFixture := types.ListValueMust(types.StringType, blockedCountries)
|
||||
optimizer := types.ObjectValueMust(optimizerTypes, map[string]attr.Value{"enabled": types.BoolValue(true)})
|
||||
config := types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
})
|
||||
modelFixture := func(mods ...func(*Model)) *Model {
|
||||
model := &Model{
|
||||
|
|
@ -162,16 +171,18 @@ func TestConvertConfig(t *testing.T) {
|
|||
Type: cdn.PtrString("http"),
|
||||
},
|
||||
},
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
|
||||
},
|
||||
IsValid: true,
|
||||
},
|
||||
"happy_path_with_optimizer": {
|
||||
Input: modelFixture(func(m *Model) {
|
||||
m.Config = types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
})
|
||||
}),
|
||||
Expected: &cdn.Config{
|
||||
|
|
@ -185,8 +196,9 @@ func TestConvertConfig(t *testing.T) {
|
|||
Type: cdn.PtrString("http"),
|
||||
},
|
||||
},
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Optimizer: cdn.NewOptimizer(true),
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Optimizer: cdn.NewOptimizer(true),
|
||||
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
|
||||
},
|
||||
IsValid: true,
|
||||
},
|
||||
|
|
@ -237,13 +249,16 @@ func TestMapFields(t *testing.T) {
|
|||
})
|
||||
regions := []attr.Value{types.StringValue("EU"), types.StringValue("US")}
|
||||
regionsFixture := types.ListValueMust(types.StringType, regions)
|
||||
blockedCountries := []attr.Value{types.StringValue("XX"), types.StringValue("YY"), types.StringValue("ZZ")}
|
||||
blockedCountriesFixture := types.ListValueMust(types.StringType, blockedCountries)
|
||||
optimizer := types.ObjectValueMust(optimizerTypes, map[string]attr.Value{
|
||||
"enabled": types.BoolValue(true),
|
||||
})
|
||||
config := types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
"optimizer": types.ObjectNull(optimizerTypes),
|
||||
})
|
||||
|
||||
emtpyErrorsList := types.ListValueMust(types.StringType, []attr.Value{})
|
||||
|
|
@ -284,8 +299,9 @@ func TestMapFields(t *testing.T) {
|
|||
Type: cdn.PtrString("http"),
|
||||
},
|
||||
},
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
Optimizer: nil,
|
||||
Regions: &[]cdn.Region{"EU", "US"},
|
||||
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
|
||||
Optimizer: nil,
|
||||
},
|
||||
CreatedAt: &createdAt,
|
||||
Domains: &[]cdn.Domain{
|
||||
|
|
@ -318,9 +334,10 @@ func TestMapFields(t *testing.T) {
|
|||
"happy_path_with_optimizer": {
|
||||
Expected: expectedModel(func(m *Model) {
|
||||
m.Config = types.ObjectValueMust(configTypes, map[string]attr.Value{
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"backend": backend,
|
||||
"regions": regionsFixture,
|
||||
"optimizer": optimizer,
|
||||
"blocked_countries": blockedCountriesFixture,
|
||||
})
|
||||
}),
|
||||
Input: distributionFixture(func(d *cdn.Distribution) {
|
||||
|
|
@ -411,3 +428,108 @@ func TestMapFields(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateCountryCode tests the validateCountryCode function with a variety of inputs.
|
||||
func TestValidateCountryCode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
inputCountry string
|
||||
wantOutput string
|
||||
expectError bool
|
||||
expectedError string
|
||||
}{
|
||||
// Happy Path
|
||||
{
|
||||
name: "Valid lowercase",
|
||||
inputCountry: "us",
|
||||
wantOutput: "US",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Valid uppercase",
|
||||
inputCountry: "DE",
|
||||
wantOutput: "DE",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Valid mixed case",
|
||||
inputCountry: "cA",
|
||||
wantOutput: "CA",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Valid country code FR",
|
||||
inputCountry: "fr",
|
||||
wantOutput: "FR",
|
||||
expectError: false,
|
||||
},
|
||||
|
||||
// Error Scenarios
|
||||
{
|
||||
name: "Invalid length - too short",
|
||||
inputCountry: "a",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code must be exactly 2 characters long",
|
||||
},
|
||||
{
|
||||
name: "Invalid length - too long",
|
||||
inputCountry: "USA",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code must be exactly 2 characters long",
|
||||
},
|
||||
{
|
||||
name: "Invalid characters - contains number",
|
||||
inputCountry: "U1",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code 'U1' must consist of two alphabetical letters (A-Z or a-z)",
|
||||
},
|
||||
{
|
||||
name: "Invalid characters - contains symbol",
|
||||
inputCountry: "D!",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code 'D!' must consist of two alphabetical letters (A-Z or a-z)",
|
||||
},
|
||||
{
|
||||
name: "Invalid characters - both are numbers",
|
||||
inputCountry: "42",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code '42' must consist of two alphabetical letters (A-Z or a-z)",
|
||||
},
|
||||
{
|
||||
name: "Empty string",
|
||||
inputCountry: "",
|
||||
wantOutput: "",
|
||||
expectError: true,
|
||||
expectedError: "country code must be exactly 2 characters long",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
gotOutput, err := validateCountryCode(tc.inputCountry)
|
||||
|
||||
if tc.expectError {
|
||||
if err == nil {
|
||||
t.Errorf("expected an error for input '%s', but got none", tc.inputCountry)
|
||||
} else if err.Error() != tc.expectedError {
|
||||
t.Errorf("for input '%s', expected error '%s', but got '%s'", tc.inputCountry, tc.expectedError, err.Error())
|
||||
}
|
||||
if gotOutput != "" {
|
||||
t.Errorf("expected empty string on error, but got '%s'", gotOutput)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("did not expect an error for input '%s', but got: %v", tc.inputCountry, err)
|
||||
}
|
||||
if gotOutput != tc.wantOutput {
|
||||
t.Errorf("for input '%s', expected output '%s', but got '%s'", tc.inputCountry, tc.wantOutput, gotOutput)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue