UnsupportedBrowserTests.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { expect } from 'chai'
  2. import User from './helpers/User.js'
  3. const botUserAgents = new Map([
  4. [
  5. 'Googlebot',
  6. 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
  7. ],
  8. ])
  9. const unsupportedUserAgents = new Map([
  10. ['IE 11', 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko'],
  11. [
  12. 'Safari 13',
  13. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_12_2) AppleWebKit/629.24.7 (KHTML, like Gecko) Version/13.0.26 Safari/629.24.7',
  14. ],
  15. ])
  16. const supportedUserAgents = new Map([
  17. [
  18. 'Chrome 90',
  19. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36',
  20. ],
  21. [
  22. 'Chrome 121',
  23. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
  24. ],
  25. [
  26. 'Firefox 122',
  27. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:122.0) Gecko/20100101 Firefox/122.0',
  28. ],
  29. [
  30. 'Safari 14',
  31. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_5_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15',
  32. ],
  33. [
  34. 'Safari 17',
  35. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15',
  36. ],
  37. ])
  38. describe('UnsupportedBrowsers', function () {
  39. beforeEach(function () {
  40. this.user = new User()
  41. })
  42. describe('allows bots', function () {
  43. const url = '/login'
  44. for (const [name, userAgent] of botUserAgents) {
  45. it(name, function (done) {
  46. this.user.request(
  47. {
  48. url,
  49. headers: {
  50. 'user-agent': userAgent,
  51. },
  52. },
  53. (error, response) => {
  54. expect(error).to.not.exist
  55. expect(response.statusCode).to.equal(200)
  56. done()
  57. }
  58. )
  59. })
  60. }
  61. })
  62. describe('allows supported browsers', function () {
  63. const url = '/login'
  64. for (const [name, userAgent] of supportedUserAgents) {
  65. it(name, function (done) {
  66. this.user.request(
  67. {
  68. url,
  69. headers: {
  70. 'user-agent': userAgent,
  71. },
  72. },
  73. (error, response) => {
  74. expect(error).to.not.exist
  75. expect(response.statusCode).to.equal(200)
  76. done()
  77. }
  78. )
  79. })
  80. }
  81. })
  82. describe('redirects unsupported browsers to unsupported page', function () {
  83. const url = '/login'
  84. for (const [name, userAgent] of unsupportedUserAgents) {
  85. it(name, function (done) {
  86. this.user.request(
  87. {
  88. url,
  89. headers: {
  90. 'user-agent': userAgent,
  91. },
  92. },
  93. (error, response) => {
  94. expect(error).to.not.exist
  95. expect(response.statusCode).to.equal(302)
  96. expect(response.headers.location).to.equal(
  97. '/unsupported-browser?fromURL=' + encodeURIComponent(url)
  98. )
  99. done()
  100. }
  101. )
  102. })
  103. }
  104. })
  105. it('redirects unsupported browsers from any page', function (done) {
  106. const url = '/foo/bar/baz'
  107. this.user.request(
  108. {
  109. url,
  110. headers: {
  111. 'user-agent': unsupportedUserAgents.get('IE 11'),
  112. },
  113. },
  114. (error, response) => {
  115. expect(error).to.not.exist
  116. expect(response.statusCode).to.equal(302)
  117. expect(response.headers.location).to.equal(
  118. '/unsupported-browser?fromURL=' + encodeURIComponent(url)
  119. )
  120. done()
  121. }
  122. )
  123. })
  124. it('should render the unsupported browser page for unsupported browser', function (done) {
  125. const url =
  126. '/unsupported-browser?fromURL=' + encodeURIComponent('/foo/bar/baz')
  127. this.user.request(
  128. {
  129. url,
  130. headers: {
  131. 'user-agent': unsupportedUserAgents.get('IE 11'),
  132. },
  133. },
  134. (error, response) => {
  135. expect(error).to.not.exist
  136. expect(response.statusCode).to.equal(200)
  137. done()
  138. }
  139. )
  140. })
  141. it('shows the previous URL', function (done) {
  142. const url = '/project/60867f47174dfd13f1e00000'
  143. this.user.request(
  144. {
  145. url: '/unsupported-browser?fromURL=' + encodeURIComponent(url),
  146. headers: {
  147. 'user-agent': unsupportedUserAgents.get('IE 11'),
  148. },
  149. },
  150. (error, response, body) => {
  151. expect(error).to.not.exist
  152. expect(response.statusCode).to.equal(200)
  153. expect(body).to.include('URL:')
  154. expect(body).to.include(url)
  155. done()
  156. }
  157. )
  158. })
  159. it('shows a sanitized URL', function (done) {
  160. const url = 'https://evil.com/the/pathname'
  161. this.user.request(
  162. {
  163. url: '/unsupported-browser?fromURL=' + encodeURIComponent(url),
  164. headers: {
  165. 'user-agent': unsupportedUserAgents.get('IE 11'),
  166. },
  167. },
  168. (error, response, body) => {
  169. expect(error).to.not.exist
  170. expect(response.statusCode).to.equal(200)
  171. expect(body).to.include('URL:')
  172. expect(body).to.not.include('evil.com')
  173. expect(body).to.include('/the/pathname')
  174. done()
  175. }
  176. )
  177. })
  178. })