Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update length subdomain names validation #118

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Loading