fix: add app's subdomain & add test case

This commit is contained in:
Long Nguyen
2021-05-05 10:19:51 +07:00
parent 109de8cac8
commit 28889039b7
3 changed files with 75 additions and 30 deletions

View File

@@ -274,39 +274,13 @@ class ServiceManager {
return Promise.resolve()
.then(function () {
const rootDomain = self.dataStore.getRootDomain()
const dotRootDomain = `.${rootDomain}`
const appDomain = appName + dotRootDomain
if (!customDomain || !/^[a-z0-9\-\.]+$/.test(customDomain)) {
try {
Utils.checkCustomDomain(customDomain, appName, rootDomain)
} catch (error) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_BAD_NAME,
'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca'
)
}
if (customDomain.length > 80) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_BAD_NAME,
'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.'
)
}
if (customDomain.indexOf('..') >= 0) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_BAD_NAME,
'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca'
)
}
if (
customDomain.indexOf(dotRootDomain) >= 0 &&
customDomain.indexOf(dotRootDomain) +
dotRootDomain.length ===
appDomain.length
) {
throw ApiStatusCodes.createError(
ApiStatusCodes.STATUS_ERROR_BAD_NAME,
'Domain name is not accepted. Custom domain cannot be subdomain of root domain.'
error
)
}
})

View File

@@ -120,4 +120,34 @@ export default class Utils {
return Promise.resolve()
}
static checkCustomDomain(
customDomain: string,
appName: string,
rootDomain: string
) {
const dotRootDomain = `.${rootDomain}`
const dotAppDomain = `.${appName}${dotRootDomain}`
if (!customDomain || !/^[a-z0-9\-\.]+$/.test(customDomain)) {
throw 'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca'
}
if (customDomain.length > 80) {
throw 'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.'
}
if (customDomain.indexOf('..') >= 0) {
throw 'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca'
}
if (
customDomain.indexOf(dotAppDomain) === -1 &&
customDomain.indexOf(dotRootDomain) >= 0 &&
customDomain.indexOf(dotRootDomain) + dotRootDomain.length ===
customDomain.length
) {
throw 'Domain name is not accepted. Custom domain cannot be subdomain of root domain.'
}
}
}

View File

@@ -83,3 +83,44 @@ test('Testing filter in place - remove 2nd', () => {
expect(originalArray[0].val1).toBe('e-1-1')
expect(originalArray[0].val2).toBe('e-2-1')
})
test('Testing code to check custom domain', () => {
const rootDomain = 'google123.ca'
const appName = 'app1'
expect(() => Utils.checkCustomDomain('', appName, rootDomain)).toThrow(
'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca'
)
expect(() =>
Utils.checkCustomDomain('x'.repeat(81), appName, rootDomain)
).toThrow(
'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.'
)
expect(() =>
Utils.checkCustomDomain('..google321.ca', appName, rootDomain)
).toThrow(
'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca'
)
expect(() =>
Utils.checkCustomDomain(`app2.${rootDomain}`, appName, rootDomain)
).toThrow(
'Domain name is not accepted. Custom domain cannot be subdomain of root domain.'
)
expect(() =>
Utils.checkCustomDomain(
`api.${appName}.${rootDomain}`,
appName,
rootDomain
)
).not.toThrow(
'Domain name is not accepted. Custom domain cannot be subdomain of root domain.'
)
expect(() =>
Utils.checkCustomDomain(`caprover123.ca`, appName, rootDomain)
).not.toThrow()
})