-
Notifications
You must be signed in to change notification settings - Fork 165
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
Implements input validation for the AplicationId #135
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2019 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as inputHelpers from '../lib/inputHelpers'; | ||
|
||
describe('inputHelpers', () => { | ||
describe('#notEmpty', () => { | ||
it('throws Error for empty strings', async () => { | ||
await expectAsync(inputHelpers.notEmpty('', 'Error')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.notEmpty(' ', 'Error')).toBeRejectedWithError(); | ||
}); | ||
|
||
it('returns true for non-empty input', async () => { | ||
expect(await inputHelpers.notEmpty('a', 'Error')).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('#validateKeyPassword', () => { | ||
it('throws Error for empty string', async () => { | ||
await expectAsync(inputHelpers.validateKeyPassword('')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.validateKeyPassword(' ')).toBeRejectedWithError(); | ||
}); | ||
|
||
it('throws Error input with less than 6 characters', async () => { | ||
await expectAsync(inputHelpers.validateKeyPassword('a')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.validateKeyPassword('abc')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.validateKeyPassword('abcde')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.validateKeyPassword('abcde ')).toBeRejectedWithError(); | ||
}); | ||
|
||
it('returns true for valid input', async () => { | ||
expect(await inputHelpers.validateKeyPassword('abcdef')).toBeTrue(); | ||
expect(await inputHelpers.validateKeyPassword('abcdef ')).toBeTrue(); | ||
}); | ||
}); | ||
|
||
describe('#validateColor', () => { | ||
it('returns true for valid colors', async () => { | ||
expect(await inputHelpers.validateColor('#FF0033')); | ||
expect(await inputHelpers.validateColor('blue')); | ||
expect(await inputHelpers.validateColor('rgb(255, 0, 30)')); | ||
}); | ||
|
||
it('throws Error for invalid colors', async () => { | ||
await expectAsync(inputHelpers.validateColor('')).toBeRejectedWithError(); | ||
await expectAsync(inputHelpers.validateColor('abc')).toBeRejectedWithError(); | ||
await expectAsync( | ||
inputHelpers.validateColor('rgb(23, 0 30')).toBeRejectedWithError(); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,4 +137,36 @@ describe('util', () => { | |
expect(result).toBe('com.appspot.pwa_directory_test.twa'); | ||
}); | ||
}); | ||
|
||
describe('#validatePackageId', () => { | ||
it('returns true for valid packages', () => { | ||
expect(util.validatePackageId('com.pwa_directory.appspot.com')).toBeTrue(); | ||
expect(util.validatePackageId('com.pwa1directory.appspot.com')).toBeTrue(); | ||
}); | ||
|
||
it('returns false for packages with invalid characters', () => { | ||
expect(util.validatePackageId('com.pwa-directory.appspot.com')).toBeFalse(); | ||
expect(util.validatePackageId('com.pwa@directory.appspot.com')).toBeFalse(); | ||
expect(util.validatePackageId('com.pwa*directory.appspot.com')).toBeFalse(); | ||
expect(util.validatePackageId('com․pwa-directory.appspot.com')).toBeFalse(); | ||
}); | ||
|
||
it('returns false for packages empty sections', () => { | ||
expect(util.validatePackageId('com.example.')).toBeFalse(); | ||
expect(util.validatePackageId('.com.example')).toBeFalse(); | ||
expect(util.validatePackageId('com..example')).toBeFalse(); | ||
}); | ||
|
||
it('packages with less than 2 sections return false', () => { | ||
expect(util.validatePackageId('com')).toBeFalse(); | ||
expect(util.validatePackageId('')).toBeFalse(); | ||
}); | ||
|
||
it('packages starting with non-letters return false', () => { | ||
expect(util.validatePackageId('com.1char.twa')).toBeFalse(); | ||
expect(util.validatePackageId('1com.char.twa')).toBeFalse(); | ||
expect(util.validatePackageId('com.char.1twa')).toBeFalse(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One that starts with an underscore? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
expect(util.validatePackageId('_com.char.1twa')).toBeFalse(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's worth adding an example with some weird unicode character (maybe a unicode character that looks like a full stop but isn't).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done