Skip to content

Commit

Permalink
Update length subdomain names validation (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko authored Jun 6, 2024
1 parent 7b3a845 commit eccf20d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion configuration/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func addSubdomainToApiUrlEnvironment(environment Environment, subdomain string)

newEnvironment := apiUrl

regex := regexp.MustCompile("^[0-9a-z]{8}$")
regex := regexp.MustCompile("^[0-9a-z]{8,11}$")

if regex.MatchString(subdomain) {
merchantApiUrl, _ := url.Parse(apiUrl)
Expand Down
77 changes: 42 additions & 35 deletions test/configuration_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,52 @@ import (
"github.com/checkout/checkout-sdk-go/mocks"
)

func TestShouldCreateConfiguration(t *testing.T) {
func TestShouldCreateConfigurationWithSubdomain(t *testing.T) {
credentials := new(mocks.CredentialsMock)
environment := configuration.Production()
subdomain := configuration.NewEnvironmentSubdomain(environment, "123dmain")
subdomainBad := configuration.NewEnvironmentSubdomain(environment, "baddomain")

cases := []struct {
name string
configuration *configuration.Configuration
checker func(*configuration.Configuration, error)
environment := configuration.Sandbox()

testCases := []struct {
subdomain string
expectedUrl string
}{
{"123dmain", "https://123dmain.api.sandbox.checkout.com"},
{"123domain", "https://123domain.api.sandbox.checkout.com"},
{"1234domain", "https://1234domain.api.sandbox.checkout.com"},
{"12345domain", "https://12345domain.api.sandbox.checkout.com"},
}

for _, tc := range testCases {
t.Run("Should create configuration with subdomain "+tc.subdomain, func(t *testing.T) {
subdomain := configuration.NewEnvironmentSubdomain(environment, tc.subdomain)
config := configuration.NewConfigurationWithSubdomain(credentials, environment, subdomain, &http.Client{}, nil)

assert.NotNil(t, config)
assert.Equal(t, tc.expectedUrl, config.EnvironmentSubdomain.ApiUrl)
})
}
}

func TestShouldCreateConfigurationWithBadSubdomain(t *testing.T) {
credentials := new(mocks.CredentialsMock)
environment := configuration.Sandbox()

testCases := []struct {
subdomain string
expectedUrl string
}{
{
name: "should create a configuration object",
configuration: configuration.NewConfiguration(credentials, environment, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
},
},
{
name: "should create a configuration object with a valid subdomain",
configuration: configuration.NewConfigurationWithSubdomain(credentials, environment, subdomain, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
assert.Equal(t, "https://123dmain.api.checkout.com", configuration.EnvironmentSubdomain.ApiUrl)
},
},
{
name: "should create a configuration object with a invalid subdomain",
configuration: configuration.NewConfigurationWithSubdomain(credentials, environment, subdomainBad, &http.Client{}, nil),
checker: func(configuration *configuration.Configuration, err error) {
assert.NotNil(t, configuration)
assert.Equal(t, "https://api.checkout.com", configuration.EnvironmentSubdomain.ApiUrl)
},
},
{"", "https://api.sandbox.checkout.com"},
{"123", "https://api.sandbox.checkout.com"},
{"123bad", "https://api.sandbox.checkout.com"},
{"12345domainBad", "https://api.sandbox.checkout.com"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.checker(tc.configuration, nil)
for _, tc := range testCases {
t.Run("Should create configuration with bad subdomain "+tc.subdomain, func(t *testing.T) {
subdomain := configuration.NewEnvironmentSubdomain(environment, tc.subdomain)
config := configuration.NewConfigurationWithSubdomain(credentials, environment, subdomain, &http.Client{}, nil)

assert.NotNil(t, config)
assert.Equal(t, tc.expectedUrl, config.EnvironmentSubdomain.ApiUrl)
})
}
}

0 comments on commit eccf20d

Please sign in to comment.