markdown.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React from "react"
  2. import { render } from "enzyme"
  3. import Markdown from "components/providers/markdown"
  4. import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.jsx"
  5. describe("Markdown Link Anchor Safety", function () {
  6. describe("Swagger 2.0", function () {
  7. it("sanitizes Markdown links", function () {
  8. const str = `Hello, [here](http://google.com/) is my link`
  9. const wrapper = render(<Markdown source={str} />)
  10. const anchor = wrapper.find("a")
  11. expect(anchor.attr("href")).toEqual("http://google.com/")
  12. expect(anchor.attr("target")).toEqual("_blank")
  13. expect(anchor.attr("rel") || "").toMatch("noopener")
  14. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  15. })
  16. it("sanitizes raw HTML links", function () {
  17. const str = `Hello, <a href="http://google.com/">here</a> is my link`
  18. const wrapper = render(<Markdown source={str} />)
  19. const anchor = wrapper.find("a")
  20. expect(anchor.attr("href")).toEqual("http://google.com/")
  21. expect(anchor.attr("rel") || "").toMatch("noopener")
  22. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  23. })
  24. })
  25. describe("OAS 3", function () {
  26. it("sanitizes Markdown links", function () {
  27. const str = `Hello, [here](http://google.com/) is my link`
  28. const wrapper = render(<OAS3Markdown source={str} />)
  29. const anchor = wrapper.find("a")
  30. expect(anchor.attr("href")).toEqual("http://google.com/")
  31. expect(anchor.attr("target")).toEqual("_blank")
  32. expect(anchor.attr("rel") || "").toMatch("noopener")
  33. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  34. })
  35. it("sanitizes raw HTML links", function () {
  36. const str = `Hello, <a href="http://google.com/">here</a> is my link`
  37. const wrapper = render(<OAS3Markdown source={str} />)
  38. const anchor = wrapper.find("a")
  39. expect(anchor.attr("href")).toEqual("http://google.com/")
  40. expect(anchor.attr("rel") || "").toMatch("noopener")
  41. expect(anchor.attr("rel") || "").toMatch("noreferrer")
  42. })
  43. })
  44. })
  45. function withMarkdownWrapper(str, { isOAS3 = false } = {}) {
  46. if(isOAS3) {
  47. return `<div class="renderedMarkdown"><p>${str}</p></div>`
  48. }
  49. return `<div class="markdown"><p>${str}</p>\n</div>`
  50. }