UnsupportedBrowserTests.mjs 5.4 KB

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