1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-01 04:37:40 +02:00

More tests (#395)

* Test UtilsService.newGuid

* Replace UtilsService.extendObject with Object.assign.

* Test getBrowser.

* Replace var with const.
This commit is contained in:
Oscar Hinton 2017-11-25 00:14:03 +01:00 committed by Kyle Spearrin
parent 2b042da237
commit a45f2c2fd7
3 changed files with 60 additions and 13 deletions

View File

@ -27,7 +27,7 @@ const MaxPasswordsInHistory = 100;
export default class PasswordGenerationService {
static generatePassword(options: any): string {
// overload defaults with given options
const o = UtilsService.extendObject({}, DefaultOptions, options);
const o = Object.assign({}, DefaultOptions, options);
// sanitize
if (o.uppercase && o.minUppercase < 0) {

View File

@ -1,4 +1,5 @@
import UtilsService from './utils.service';
import { BrowserType } from '../enums/browserType.enum';
describe('Utils Service', () => {
describe('getDomain', () => {
@ -51,4 +52,62 @@ describe('Utils Service', () => {
expect(UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1');
});
});
describe('newGuid', () => {
it('should create a valid guid', () => {
const validGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
expect(UtilsService.newGuid()).toMatch(validGuid);
});
});
describe('getBrowser', () => {
const original = navigator.userAgent;
// Reset the userAgent.
afterAll(() => {
Object.defineProperty(navigator, 'userAgent', {
value: original
});
});
it('should detect chrome', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
});
const utilsService = new UtilsService();
expect(utilsService.getBrowser()).toBe(BrowserType.Chrome);
});
it('should detect firefox', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
});
const utilsService = new UtilsService();
expect(utilsService.getBrowser()).toBe(BrowserType.Firefox);
});
it('should detect opera', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3175.3 Safari/537.36 OPR/49.0.2695.0 (Edition developer)'
});
const utilsService = new UtilsService();
expect(utilsService.getBrowser()).toBe(BrowserType.Opera);
});
it('should detect edge', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
});
const utilsService = new UtilsService();
expect(utilsService.getBrowser()).toBe(BrowserType.Edge);
});
});
});

View File

@ -64,18 +64,6 @@ export default class UtilsService implements UtilsServiceInterface {
});
}
static extendObject(...objects: any[]): any {
for (let i = 1; i < objects.length; i++) {
for (const key in objects[i]) {
if (objects[i].hasOwnProperty(key)) {
objects[0][key] = objects[i][key];
}
}
}
return objects[0];
}
// EFForg/OpenWireless
// ref https://github.com/EFForg/OpenWireless/blob/master/app/js/diceware.js
static secureRandomNumber(min: number, max: number): number {