rename to browser platform utils

This commit is contained in:
Kyle Spearrin 2018-01-05 16:38:50 -05:00
parent 4a08bf6b71
commit cbf8b00b33
5 changed files with 91 additions and 91 deletions

View File

@ -16,8 +16,8 @@ import ApiService from '../services/api.service';
import AppIdService from '../services/appId.service';
import AutofillService from '../services/autofill.service';
import BrowserMessagingService from '../services/browserMessaging.service';
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
import BrowserStorageService from '../services/browserStorage.service';
import BrowserUtilsService from '../services/browserUtils.service';
import CipherService from '../services/cipher.service';
import CollectionService from '../services/collection.service';
import ConstantsService from '../services/constants.service';
@ -83,7 +83,7 @@ export default class MainBackground {
constructor() {
// Services
this.utilsService = new UtilsService();
this.platformUtilsService = new BrowserUtilsService();
this.platformUtilsService = new BrowserPlatformUtilsService();
this.messagingService = new BrowserMessagingService(this.platformUtilsService);
this.storageService = new BrowserStorageService(this.platformUtilsService);
this.i18nService = i18nService(this.platformUtilsService);

View File

@ -11,7 +11,7 @@ import { Identity } from './identity';
import { Login } from './login';
import { SecureNote } from './secureNote';
import BrowserUtilsService from '../../services/browserUtils.service';
import BrowserPlatformUtilsService from '../../services/browserPlatformUtils.service';
class Cipher extends Domain {
id: string;
@ -117,7 +117,7 @@ class Cipher extends Domain {
model.login = await this.login.decrypt(this.organizationId);
model.subTitle = model.login.username;
if (model.login.uri) {
model.login.domain = BrowserUtilsService.getDomain(model.login.uri);
model.login.domain = BrowserPlatformUtilsService.getDomain(model.login.uri);
}
break;
case CipherType.SecureNote:

View File

@ -0,0 +1,84 @@
import BrowserPlatformUtilsService from './browserPlatformUtils.service';
import { DeviceType } from '../enums/deviceType.enum';
describe('Browser Utils Service', () => {
describe('getDomain', () => {
it('should fail for invalid urls', () => {
expect(BrowserPlatformUtilsService.getDomain(null)).toBeNull();
expect(BrowserPlatformUtilsService.getDomain(undefined)).toBeNull();
expect(BrowserPlatformUtilsService.getDomain(' ')).toBeNull();
expect(BrowserPlatformUtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(BrowserPlatformUtilsService.getDomain('bitwarden')).toBeNull();
});
it('should handle urls without protocol', () => {
expect(BrowserPlatformUtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
});
it('should handle valid urls', () => {
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
expect(BrowserPlatformUtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
});
it('should support localhost and IP', () => {
expect(BrowserPlatformUtilsService.getDomain('https://localhost')).toBe('localhost');
expect(BrowserPlatformUtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});
});
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 browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.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 browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.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 browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.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 browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Edge);
});
});
});

View File

@ -11,7 +11,7 @@ const AnalyticsIds = {
[DeviceType.Safari]: 'UA-81915606-16',
};
export default class BrowserUtilsService implements PlatformUtilsServiceInterface {
export default class BrowserPlatformUtilsService implements PlatformUtilsServiceInterface {
static getDomain(uriString: string): string {
if (uriString == null) {
return null;
@ -26,7 +26,7 @@ export default class BrowserUtilsService implements PlatformUtilsServiceInterfac
try {
const url = new URL(uriString);
if (url.hostname === 'localhost' || BrowserUtilsService.validIpAddress(url.hostname)) {
if (url.hostname === 'localhost' || BrowserPlatformUtilsService.validIpAddress(url.hostname)) {
return url.hostname;
}
@ -193,7 +193,7 @@ export default class BrowserUtilsService implements PlatformUtilsServiceInterfac
}
getDomain(uriString: string): string {
return BrowserUtilsService.getDomain(uriString);
return BrowserPlatformUtilsService.getDomain(uriString);
}
inSidebar(theWindow: Window): boolean {

View File

@ -1,84 +0,0 @@
import BrowserUtilsService from './browserUtils.service';
import { DeviceType } from '../enums/deviceType.enum';
describe('Browser Utils Service', () => {
describe('getDomain', () => {
it('should fail for invalid urls', () => {
expect(BrowserUtilsService.getDomain(null)).toBeNull();
expect(BrowserUtilsService.getDomain(undefined)).toBeNull();
expect(BrowserUtilsService.getDomain(' ')).toBeNull();
expect(BrowserUtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(BrowserUtilsService.getDomain('bitwarden')).toBeNull();
});
it('should handle urls without protocol', () => {
expect(BrowserUtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
expect(BrowserUtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
});
it('should handle valid urls', () => {
expect(BrowserUtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
expect(BrowserUtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserUtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
expect(BrowserUtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
expect(BrowserUtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
expect(BrowserUtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
});
it('should support localhost and IP', () => {
expect(BrowserUtilsService.getDomain('https://localhost')).toBe('localhost');
expect(BrowserUtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});
});
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 browserUtilsService = new BrowserUtilsService();
expect(browserUtilsService.getDevice()).toBe(DeviceType.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 browserUtilsService = new BrowserUtilsService();
expect(browserUtilsService.getDevice()).toBe(DeviceType.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 browserUtilsService = new BrowserUtilsService();
expect(browserUtilsService.getDevice()).toBe(DeviceType.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 browserUtilsService = new BrowserUtilsService();
expect(browserUtilsService.getDevice()).toBe(DeviceType.Edge);
});
});
});