exp-parser.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. describe('UNIT: Expression Parser', function () {
  2. var ExpParser = require('vue/src/exp-parser'),
  3. utils = require('vue/src/utils'),
  4. oldWarn = utils.warn
  5. var warnSpy = {
  6. warned: false,
  7. swapWarn: function () {
  8. utils.warn = function () {
  9. warnSpy.warned = true
  10. }
  11. },
  12. resetWarn: function () {
  13. utils.warn = oldWarn
  14. warnSpy.warned = false
  15. }
  16. }
  17. var testCases = [
  18. {
  19. // string concat
  20. exp: 'a + b',
  21. vm: {
  22. a: 'hello',
  23. b: 'world'
  24. },
  25. expectedValue: 'helloworld'
  26. },
  27. {
  28. // math
  29. exp: 'a - b * 2 + 45',
  30. vm: {
  31. a: 100,
  32. b: 23
  33. },
  34. expectedValue: 100 - 23 * 2 + 45
  35. },
  36. {
  37. // boolean logic
  38. exp: '(a && b) ? c : d || e',
  39. vm: {
  40. a: true,
  41. b: false,
  42. c: null,
  43. d: false,
  44. e: 'worked'
  45. },
  46. expectedValue: 'worked'
  47. },
  48. {
  49. // inline string
  50. exp: "a + 'hello'",
  51. vm: {
  52. a: 'inline '
  53. },
  54. expectedValue: 'inline hello'
  55. },
  56. {
  57. // complex with nested values
  58. exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
  59. paths: ['todo.title', 'todo.done'],
  60. vm: {
  61. todo: {
  62. title: 'write tests',
  63. done: false
  64. }
  65. },
  66. expectedValue: 'write tests : nope'
  67. },
  68. {
  69. // expression with no data variables
  70. exp: "'a' + 'b'",
  71. vm: {},
  72. expectedValue: 'ab'
  73. },
  74. {
  75. // values with same variable name inside strings
  76. exp: "'\"test\"' + test + \"'hi'\" + hi",
  77. vm: {
  78. test: 1,
  79. hi: 2
  80. },
  81. expectedValue: '"test"1\'hi\'2'
  82. }
  83. ]
  84. testCases.forEach(describeCase)
  85. // extra case for invalid expressions
  86. describe('invalid expression', function () {
  87. before(warnSpy.swapWarn)
  88. it('should capture the error and warn', function () {
  89. function noop () {}
  90. ExpParser.parse('a + "fsef', {
  91. createBinding: noop,
  92. hasKey: noop,
  93. vm: {
  94. $compiler: {
  95. bindings: {},
  96. createBinding: noop
  97. },
  98. $data: {}
  99. }
  100. })
  101. assert.ok(warnSpy.warned)
  102. })
  103. after(warnSpy.resetWarn)
  104. })
  105. describe('Basic XSS protection', function () {
  106. var cases = [{
  107. xss: true,
  108. exp: "constructor.constructor('alert(1)')()",
  109. vm: {},
  110. expectedValue: undefined
  111. },
  112. {
  113. xss: true,
  114. exp: "\"\".toString.constructor.constructor('alert(1)')()",
  115. vm: {},
  116. expectedValue: undefined
  117. }]
  118. cases.forEach(describeCase)
  119. })
  120. function describeCase (testCase) {
  121. describe(testCase.exp, function () {
  122. function createBinding (path) {
  123. caughtMissingPaths.push(path)
  124. }
  125. var caughtMissingPaths = [],
  126. compilerMock = {
  127. createBinding: createBinding,
  128. hasKey: function () {},
  129. vm:{
  130. $data: {},
  131. $compiler:{
  132. bindings:{},
  133. createBinding: createBinding
  134. }
  135. }
  136. },
  137. vm = testCase.vm,
  138. vars = testCase.paths || Object.keys(vm),
  139. getter
  140. if (testCase.xss) {
  141. before(warnSpy.swapWarn)
  142. after(warnSpy.resetWarn)
  143. }
  144. before(function () {
  145. getter = ExpParser.parse(testCase.exp, compilerMock)
  146. })
  147. if (!testCase.xss) {
  148. it('should get correct paths', function () {
  149. if (!vars.length) return
  150. assert.strictEqual(caughtMissingPaths.length, vars.length)
  151. for (var i = 0; i < vars.length; i++) {
  152. assert.strictEqual(vars[i], caughtMissingPaths[i])
  153. }
  154. })
  155. }
  156. it('getter function should return expected value', function () {
  157. var value = getter.call(vm)
  158. assert.strictEqual(value, testCase.expectedValue)
  159. })
  160. if (testCase.xss) {
  161. it('should have warned', function () {
  162. assert.ok(warnSpy.warned)
  163. })
  164. }
  165. })
  166. }
  167. })