assert.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict'
  2. const OError = require('@overleaf/o-error')
  3. const { expect } = require('chai')
  4. const assert = require('../../../../storage/lib/assert')
  5. describe('assert', function () {
  6. describe('blobHash', function () {
  7. it('should not throw for valid blob hashes', function () {
  8. expect(() =>
  9. assert.blobHash(
  10. 'aad321caf77ca6c5ab09e6c638c237705f93b001',
  11. 'should be a blob hash'
  12. )
  13. ).to.not.throw()
  14. })
  15. it('should throw for invalid blob hashes', function () {
  16. try {
  17. assert.blobHash('invalid-hash', 'should be a blob hash')
  18. expect.fail()
  19. } catch (error) {
  20. expect(error).to.be.instanceOf(TypeError)
  21. expect(error.message).to.equal('should be a blob hash')
  22. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 'invalid-hash' })
  23. }
  24. })
  25. it('should throw for string integer blob hashes', function () {
  26. try {
  27. assert.blobHash('123', 'should be a blob hash')
  28. expect.fail()
  29. } catch (error) {
  30. expect(error).to.be.instanceOf(TypeError)
  31. expect(error.message).to.equal('should be a blob hash')
  32. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '123' })
  33. }
  34. })
  35. })
  36. describe('projectId', function () {
  37. it('should not throw for valid mongo project ids', function () {
  38. expect(() =>
  39. assert.projectId('507f1f77bcf86cd799439011', 'should be a project id')
  40. ).to.not.throw()
  41. })
  42. it('should not throw for valid postgres project ids', function () {
  43. expect(() =>
  44. assert.projectId('123456789', 'should be a project id')
  45. ).to.not.throw()
  46. })
  47. it('should throw for invalid project ids', function () {
  48. try {
  49. assert.projectId('invalid-id', 'should be a project id')
  50. expect.fail()
  51. } catch (error) {
  52. expect(error).to.be.instanceOf(TypeError)
  53. expect(error.message).to.equal('should be a project id')
  54. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 'invalid-id' })
  55. }
  56. })
  57. it('should throw for non-numeric project ids', function () {
  58. try {
  59. assert.projectId('12345x', 'should be a project id')
  60. expect.fail()
  61. } catch (error) {
  62. expect(error).to.be.instanceOf(TypeError)
  63. expect(error.message).to.equal('should be a project id')
  64. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '12345x' })
  65. }
  66. })
  67. it('should throw for postgres ids starting with 0', function () {
  68. try {
  69. assert.projectId('0123456', 'should be a project id')
  70. expect.fail()
  71. } catch (error) {
  72. expect(error).to.be.instanceOf(TypeError)
  73. expect(error.message).to.equal('should be a project id')
  74. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '0123456' })
  75. }
  76. })
  77. })
  78. describe('chunkId', function () {
  79. it('should not throw for valid mongo chunk ids', function () {
  80. expect(() =>
  81. assert.chunkId('507f1f77bcf86cd799439011', 'should be a chunk id')
  82. ).to.not.throw()
  83. })
  84. it('should not throw for valid postgres chunk ids', function () {
  85. expect(() =>
  86. assert.chunkId('123456789', 'should be a chunk id')
  87. ).to.not.throw()
  88. })
  89. it('should throw for invalid chunk ids', function () {
  90. try {
  91. assert.chunkId('invalid-id', 'should be a chunk id')
  92. expect.fail()
  93. } catch (error) {
  94. expect(error).to.be.instanceOf(TypeError)
  95. expect(error.message).to.equal('should be a chunk id')
  96. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 'invalid-id' })
  97. }
  98. })
  99. it('should throw for integer chunk ids', function () {
  100. try {
  101. assert.chunkId(12345, 'should be a chunk id')
  102. expect.fail()
  103. } catch (error) {
  104. expect(error).to.be.instanceOf(TypeError)
  105. expect(error.message).to.equal('should be a chunk id')
  106. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 12345 })
  107. }
  108. })
  109. })
  110. describe('mongoId', function () {
  111. it('should not throw for valid mongo ids', function () {
  112. expect(() =>
  113. assert.mongoId('507f1f77bcf86cd799439011', 'should be a mongo id')
  114. ).to.not.throw()
  115. })
  116. it('should throw for invalid mongo ids', function () {
  117. try {
  118. assert.mongoId('invalid-id', 'should be a mongo id')
  119. expect.fail()
  120. } catch (error) {
  121. expect(error).to.be.instanceOf(TypeError)
  122. expect(error.message).to.equal('should be a mongo id')
  123. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 'invalid-id' })
  124. }
  125. })
  126. it('should throw for numeric mongo ids', function () {
  127. try {
  128. assert.mongoId('12345', 'should be a mongo id')
  129. expect.fail()
  130. } catch (error) {
  131. expect(error).to.be.instanceOf(TypeError)
  132. expect(error.message).to.equal('should be a mongo id')
  133. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '12345' })
  134. }
  135. })
  136. it('should throw for mongo ids that are too short', function () {
  137. try {
  138. assert.mongoId('507f1f77bcf86cd79943901', 'should be a mongo id')
  139. expect.fail()
  140. } catch (error) {
  141. expect(error).to.be.instanceOf(TypeError)
  142. expect(error.message).to.equal('should be a mongo id')
  143. expect(OError.getFullInfo(error)).to.deep.equal({
  144. arg: '507f1f77bcf86cd79943901',
  145. })
  146. }
  147. })
  148. it('should throw for mongo ids that are too long', function () {
  149. try {
  150. assert.mongoId('507f1f77bcf86cd7994390111', 'should be a mongo id')
  151. expect.fail()
  152. } catch (error) {
  153. expect(error).to.be.instanceOf(TypeError)
  154. expect(error.message).to.equal('should be a mongo id')
  155. expect(OError.getFullInfo(error)).to.deep.equal({
  156. arg: '507f1f77bcf86cd7994390111',
  157. })
  158. }
  159. })
  160. })
  161. describe('postgresId', function () {
  162. it('should not throw for valid postgres ids', function () {
  163. expect(() =>
  164. assert.postgresId('123456789', 'should be a postgres id')
  165. ).to.not.throw()
  166. expect(() =>
  167. assert.postgresId('1', 'should be a postgres id')
  168. ).to.not.throw()
  169. })
  170. it('should throw for invalid postgres ids', function () {
  171. try {
  172. assert.postgresId('invalid-id', 'should be a postgres id')
  173. expect.fail()
  174. } catch (error) {
  175. expect(error).to.be.instanceOf(TypeError)
  176. expect(error.message).to.equal('should be a postgres id')
  177. expect(OError.getFullInfo(error)).to.deep.equal({ arg: 'invalid-id' })
  178. }
  179. })
  180. it('should throw for postgres ids starting with 0', function () {
  181. try {
  182. assert.postgresId('0123456', 'should be a postgres id')
  183. expect.fail()
  184. } catch (error) {
  185. expect(error).to.be.instanceOf(TypeError)
  186. expect(error.message).to.equal('should be a postgres id')
  187. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '0123456' })
  188. }
  189. })
  190. it('should throw for postgres ids that are too long', function () {
  191. try {
  192. assert.postgresId('12345678901', 'should be a postgres id')
  193. expect.fail()
  194. } catch (error) {
  195. expect(error).to.be.instanceOf(TypeError)
  196. expect(error.message).to.equal('should be a postgres id')
  197. expect(OError.getFullInfo(error)).to.deep.equal({ arg: '12345678901' })
  198. }
  199. })
  200. })
  201. describe('regex constants', function () {
  202. it('MONGO_ID_REGEXP should match valid mongo ids', function () {
  203. expect('507f1f77bcf86cd799439011').to.match(assert.MONGO_ID_REGEXP)
  204. expect('abcdef0123456789abcdef01').to.match(assert.MONGO_ID_REGEXP)
  205. })
  206. it('MONGO_ID_REGEXP should not match invalid mongo ids', function () {
  207. expect('invalid-id').to.not.match(assert.MONGO_ID_REGEXP)
  208. expect('507f1f77bcf86cd79943901').to.not.match(assert.MONGO_ID_REGEXP) // too short
  209. expect('507f1f77bcf86cd7994390111').to.not.match(assert.MONGO_ID_REGEXP) // too long
  210. expect('507F1F77BCF86CD799439011').to.not.match(assert.MONGO_ID_REGEXP) // uppercase
  211. })
  212. it('POSTGRES_ID_REGEXP should match valid postgres ids', function () {
  213. expect('123456789').to.match(assert.POSTGRES_ID_REGEXP)
  214. expect('1').to.match(assert.POSTGRES_ID_REGEXP)
  215. })
  216. it('POSTGRES_ID_REGEXP should not match invalid postgres ids', function () {
  217. expect('invalid-id').to.not.match(assert.POSTGRES_ID_REGEXP)
  218. expect('0123456').to.not.match(assert.POSTGRES_ID_REGEXP) // starts with 0
  219. expect('12345678901').to.not.match(assert.POSTGRES_ID_REGEXP) // too long (> 10 digits)
  220. })
  221. })
  222. })