From cbf8b00b33b0693761e94ce2bac125bef21f30ce Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 5 Jan 2018 16:38:50 -0500 Subject: [PATCH] rename to browser platform utils --- src/background/main.background.ts | 4 +- src/models/domain/cipher.ts | 4 +- .../browserPlatformUtils.service.spec.ts | 84 +++++++++++++++++++ ...ice.ts => browserPlatformUtils.service.ts} | 6 +- src/services/browserUtils.service.spec.ts | 84 ------------------- 5 files changed, 91 insertions(+), 91 deletions(-) create mode 100644 src/services/browserPlatformUtils.service.spec.ts rename src/services/{browserUtils.service.ts => browserPlatformUtils.service.ts} (96%) delete mode 100644 src/services/browserUtils.service.spec.ts diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 31870b98a3..98a6875ec6 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -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); diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index e916f24242..5c2a4f5b04 100644 --- a/src/models/domain/cipher.ts +++ b/src/models/domain/cipher.ts @@ -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: diff --git a/src/services/browserPlatformUtils.service.spec.ts b/src/services/browserPlatformUtils.service.spec.ts new file mode 100644 index 0000000000..84b42d421f --- /dev/null +++ b/src/services/browserPlatformUtils.service.spec.ts @@ -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); + }); + }); +}); diff --git a/src/services/browserUtils.service.ts b/src/services/browserPlatformUtils.service.ts similarity index 96% rename from src/services/browserUtils.service.ts rename to src/services/browserPlatformUtils.service.ts index 9338a82adb..0db30957eb 100644 --- a/src/services/browserUtils.service.ts +++ b/src/services/browserPlatformUtils.service.ts @@ -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 { diff --git a/src/services/browserUtils.service.spec.ts b/src/services/browserUtils.service.spec.ts deleted file mode 100644 index e06cd115d9..0000000000 --- a/src/services/browserUtils.service.spec.ts +++ /dev/null @@ -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); - }); - }); -});