2021-05-26 12:44:09 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
2021-07-27 08:02:20 +02:00
|
|
|
import {createIntl} from 'react-intl'
|
|
|
|
|
2021-05-26 12:44:09 +02:00
|
|
|
import {Utils} from './utils'
|
|
|
|
|
2021-06-15 09:34:59 +02:00
|
|
|
describe('utils', () => {
|
|
|
|
describe('assureProtocol', () => {
|
|
|
|
test('should passthrough on valid short protocol', () => {
|
|
|
|
expect(Utils.ensureProtocol('https://focalboard.com')).toBe('https://focalboard.com')
|
|
|
|
})
|
|
|
|
test('should passthrough on valid long protocol', () => {
|
|
|
|
expect(Utils.ensureProtocol('somecustomprotocol://focalboard.com')).toBe('somecustomprotocol://focalboard.com')
|
|
|
|
})
|
2021-05-26 12:44:09 +02:00
|
|
|
|
2021-06-15 09:34:59 +02:00
|
|
|
test('should passthrough on valid short protocol', () => {
|
|
|
|
expect(Utils.ensureProtocol('x://focalboard.com')).toBe('x://focalboard.com')
|
|
|
|
})
|
2021-05-26 12:44:09 +02:00
|
|
|
|
2021-06-15 09:34:59 +02:00
|
|
|
test('should add a https for empty protocol', () => {
|
|
|
|
expect(Utils.ensureProtocol('focalboard.com')).toBe('https://focalboard.com')
|
|
|
|
})
|
|
|
|
})
|
2021-05-26 12:44:09 +02:00
|
|
|
|
2021-06-15 09:34:59 +02:00
|
|
|
describe('htmlFromMarkdown', () => {
|
|
|
|
test('should not allow XSS on links href on the webapp', () => {
|
|
|
|
expect(Utils.htmlFromMarkdown('[]("xss-attack="true"other="whatever)')).toBe('<p><a href="%22xss-attack=%22true%22other=%22whatever"></a></p>')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should not allow XSS on links href on the desktop app', () => {
|
|
|
|
const windowAsAny = window as any
|
|
|
|
windowAsAny.openInNewBrowser = () => null
|
2021-06-18 16:46:57 +02:00
|
|
|
const expectedHtml = '<p><a target="_blank" rel="noreferrer" href="%22xss-attack=%22true%22other=%22whatever" title="" onclick="event.stopPropagation(); openInNewBrowser && openInNewBrowser(event.target.href);"></a></p>'
|
2021-06-15 09:34:59 +02:00
|
|
|
expect(Utils.htmlFromMarkdown('[]("xss-attack="true"other="whatever)')).toBe(expectedHtml)
|
|
|
|
windowAsAny.openInNewBrowser = null
|
|
|
|
})
|
|
|
|
})
|
2021-06-22 17:26:00 +02:00
|
|
|
|
|
|
|
describe('test - buildURL', () => {
|
|
|
|
test('buildURL, no base', () => {
|
|
|
|
expect(Utils.buildURL('test', true)).toBe('http://localhost/test')
|
|
|
|
expect(Utils.buildURL('/test', true)).toBe('http://localhost/test')
|
|
|
|
|
|
|
|
expect(Utils.buildURL('test')).toBe('/test')
|
|
|
|
expect(Utils.buildURL('/test')).toBe('/test')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('buildURL, base no slash', () => {
|
|
|
|
const windowAsAny = window as any
|
|
|
|
windowAsAny.baseURL = 'base'
|
|
|
|
|
|
|
|
expect(Utils.buildURL('test', true)).toBe('http://localhost/base/test')
|
|
|
|
expect(Utils.buildURL('/test', true)).toBe('http://localhost/base/test')
|
|
|
|
|
|
|
|
expect(Utils.buildURL('test')).toBe('base/test')
|
|
|
|
expect(Utils.buildURL('/test')).toBe('base/test')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('buildUrl, base with slash', () => {
|
|
|
|
const windowAsAny = window as any
|
|
|
|
windowAsAny.baseURL = '/base/'
|
|
|
|
|
|
|
|
expect(Utils.buildURL('test', true)).toBe('http://localhost/base/test')
|
|
|
|
expect(Utils.buildURL('/test', true)).toBe('http://localhost/base/test')
|
|
|
|
|
|
|
|
expect(Utils.buildURL('test')).toBe('base/test')
|
|
|
|
expect(Utils.buildURL('/test')).toBe('base/test')
|
|
|
|
})
|
|
|
|
})
|
2021-07-27 08:02:20 +02:00
|
|
|
|
|
|
|
describe('display date', () => {
|
|
|
|
const intl = createIntl({locale: 'en-us'})
|
|
|
|
|
|
|
|
it('should show month and day for current year', () => {
|
|
|
|
const currentYear = new Date().getFullYear()
|
|
|
|
const date = new Date(currentYear, 6, 9)
|
|
|
|
expect(Utils.displayDate(date, intl)).toBe('July 09')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should show month, day and year for previous year', () => {
|
|
|
|
const currentYear = new Date().getFullYear()
|
|
|
|
const previousYear = currentYear - 1
|
|
|
|
const date = new Date(previousYear, 6, 9)
|
|
|
|
expect(Utils.displayDate(date, intl)).toBe(`July 09, ${previousYear}`)
|
|
|
|
})
|
|
|
|
})
|
2021-05-26 12:44:09 +02:00
|
|
|
})
|