UnsupportedBrowserTests.mjs 5.2 KB

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