2016-03-29 16:41:24 +08:00
|
|
|
React = require "react"
|
|
|
|
ReactTestUtils = require('react-addons-test-utils')
|
2015-08-20 01:20:41 +08:00
|
|
|
EventedIFrame = require '../../src/components/evented-iframe'
|
|
|
|
|
|
|
|
describe 'EventedIFrame', ->
|
|
|
|
describe 'link clicking behavior', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@frame = ReactTestUtils.renderIntoDocument(
|
|
|
|
<EventedIFrame src="about:blank" />
|
|
|
|
)
|
|
|
|
|
|
|
|
@setAttributeSpy = jasmine.createSpy('setAttribute')
|
|
|
|
@preventDefaultSpy = jasmine.createSpy('preventDefault')
|
|
|
|
@openLinkSpy = jasmine.createSpy("openLink")
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
@oldOpenLink = NylasEnv.windowEventHandler.openLink
|
|
|
|
NylasEnv.windowEventHandler.openLink = @openLinkSpy
|
2015-08-20 01:20:41 +08:00
|
|
|
|
|
|
|
@fakeEvent = (href) =>
|
|
|
|
stopPropagation: ->
|
|
|
|
preventDefault: @preventDefaultSpy
|
|
|
|
target:
|
|
|
|
getAttribute: (attr) -> return href
|
|
|
|
setAttribute: @setAttributeSpy
|
|
|
|
|
|
|
|
afterEach ->
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.windowEventHandler.openLink = @oldOpenLink
|
2015-08-20 01:20:41 +08:00
|
|
|
|
|
|
|
it 'works for acceptable link types', ->
|
|
|
|
hrefs = [
|
|
|
|
"http://nylas.com"
|
|
|
|
"https://www.nylas.com"
|
|
|
|
"mailto:evan@nylas.com"
|
|
|
|
"tel:8585311718"
|
|
|
|
"custom:www.nylas.com"
|
|
|
|
]
|
|
|
|
for href, i in hrefs
|
|
|
|
@frame._onIFrameClick(@fakeEvent(href))
|
|
|
|
expect(@setAttributeSpy).not.toHaveBeenCalled()
|
|
|
|
expect(@openLinkSpy).toHaveBeenCalled()
|
|
|
|
target = @openLinkSpy.calls[i].args[0].target
|
2016-04-26 08:28:07 +08:00
|
|
|
targetHref = @openLinkSpy.calls[i].args[0].href
|
|
|
|
expect(target).not.toBeDefined()
|
|
|
|
expect(targetHref).toBe href
|
2015-08-20 01:20:41 +08:00
|
|
|
|
|
|
|
it 'corrects relative uris', ->
|
|
|
|
hrefs = [
|
|
|
|
"nylas.com"
|
|
|
|
"www.nylas.com"
|
|
|
|
]
|
|
|
|
for href, i in hrefs
|
|
|
|
@frame._onIFrameClick(@fakeEvent(href))
|
|
|
|
expect(@setAttributeSpy).toHaveBeenCalled()
|
|
|
|
modifiedHref = @setAttributeSpy.calls[i].args[1]
|
|
|
|
expect(modifiedHref).toBe "http://#{href}"
|
|
|
|
|
|
|
|
it 'corrects protocol-relative uris', ->
|
|
|
|
hrefs = [
|
|
|
|
"//nylas.com"
|
|
|
|
"//www.nylas.com"
|
|
|
|
]
|
|
|
|
for href, i in hrefs
|
|
|
|
@frame._onIFrameClick(@fakeEvent(href))
|
|
|
|
expect(@setAttributeSpy).toHaveBeenCalled()
|
|
|
|
modifiedHref = @setAttributeSpy.calls[i].args[1]
|
|
|
|
expect(modifiedHref).toBe "https:#{href}"
|
|
|
|
|
|
|
|
it 'disallows malicious uris', ->
|
|
|
|
hrefs = [
|
|
|
|
"file://usr/bin/bad"
|
|
|
|
]
|
|
|
|
for href in hrefs
|
|
|
|
@frame._onIFrameClick(@fakeEvent(href))
|
|
|
|
expect(@preventDefaultSpy).toHaveBeenCalled()
|
|
|
|
expect(@openLinkSpy).not.toHaveBeenCalled()
|
|
|
|
|