store_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. require 'test_helper'
  2. class StoreTest < ActiveSupport::TestCase
  3. test 'store fs - get_location' do
  4. sha = 'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
  5. location = Store::Provider::File.get_location(sha)
  6. assert_equal(Rails.root.join('storage', 'fs', 'ed70', '02b4', '39e9a', 'c845f', '22357d8', '22bac14', '44730fbdb6016d3ec9432297b9ec9f73').to_s, location)
  7. end
  8. test 'store fs - empty dir remove' do
  9. sha = 'ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73'
  10. content = 'content'
  11. location = Store::Provider::File.get_location(sha)
  12. result = Store::Provider::File.add(content, sha)
  13. assert(result)
  14. exists = File.exist?(location)
  15. assert(exists)
  16. Store::Provider::File.delete(sha)
  17. exists = File.exist?(location)
  18. assert_not(exists)
  19. exists = File.exist?(Rails.root.join('storage', 'fs', 'ed70', '02b4'))
  20. assert_not(exists)
  21. exists = File.exist?(Rails.root.join('storage', 'fs', 'ed70'))
  22. assert_not(exists)
  23. exists = File.exist?(Rails.root.join('storage', 'fs'))
  24. assert(exists)
  25. exists = File.exist?(Rails.root.join('storage'))
  26. assert(exists)
  27. end
  28. test 'store attachment and move it between backends' do
  29. files = [
  30. {
  31. data: 'hello world',
  32. filename: 'test.txt',
  33. o_id: 1,
  34. },
  35. {
  36. data: 'hello world äöüß',
  37. filename: 'testäöüß.txt',
  38. o_id: 2,
  39. },
  40. {
  41. data: File.binread(Rails.root.join('test', 'data', 'pdf', 'test1.pdf')),
  42. filename: 'test.pdf',
  43. o_id: 3,
  44. },
  45. {
  46. data: File.binread(Rails.root.join('test', 'data', 'pdf', 'test1.pdf')),
  47. filename: 'test-again.pdf',
  48. o_id: 4,
  49. },
  50. ]
  51. files.each do |file|
  52. sha = Digest::SHA256.hexdigest(file[:data])
  53. # add attachments
  54. store = Store.add(
  55. object: 'Test',
  56. o_id: file[:o_id],
  57. data: file[:data],
  58. filename: file[:filename],
  59. preferences: {},
  60. created_by_id: 1,
  61. )
  62. assert store
  63. # get list of attachments
  64. attachments = Store.list(
  65. object: 'Test',
  66. o_id: file[:o_id],
  67. )
  68. assert attachments
  69. # sha check
  70. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  71. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  72. # filename check
  73. assert_equal(file[:filename], attachments[0].filename)
  74. # provider check
  75. assert_equal('DB', attachments[0].provider)
  76. end
  77. success = Store::File.verify
  78. assert success, 'verify ok'
  79. Store::File.move('DB', 'File')
  80. files.each do |file|
  81. sha = Digest::SHA256.hexdigest(file[:data])
  82. # get list of attachments
  83. attachments = Store.list(
  84. object: 'Test',
  85. o_id: file[:o_id],
  86. )
  87. assert attachments
  88. # sha check
  89. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  90. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  91. # filename check
  92. assert_equal(file[:filename], attachments[0].filename)
  93. # provider check
  94. assert_equal('File', attachments[0].provider)
  95. end
  96. success = Store::File.verify
  97. assert success, 'verify ok'
  98. Store::File.move('File', 'DB')
  99. files.each do |file|
  100. sha = Digest::SHA256.hexdigest(file[:data])
  101. # get list of attachments
  102. attachments = Store.list(
  103. object: 'Test',
  104. o_id: file[:o_id],
  105. )
  106. assert(attachments)
  107. assert_equal(attachments.count, 1)
  108. # sha check
  109. sha_new = Digest::SHA256.hexdigest(attachments[0].content)
  110. assert_equal(sha, sha_new, "check file #{file[:filename]}")
  111. # filename check
  112. assert_equal(file[:filename], attachments[0].filename)
  113. # provider check
  114. assert_equal('DB', attachments[0].provider)
  115. # delete attachments
  116. success = Store.remove(
  117. object: 'Test',
  118. o_id: file[:o_id],
  119. )
  120. assert(success)
  121. # check attachments again
  122. attachments = Store.list(
  123. object: 'Test',
  124. o_id: file[:o_id],
  125. )
  126. assert_not(attachments[0])
  127. end
  128. end
  129. test 'test resizable' do
  130. # not possible
  131. store = Store.add(
  132. object: 'SomeObject1',
  133. o_id: rand(1_234_567_890),
  134. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  135. filename: 'test1.pdf',
  136. preferences: {
  137. content_type: 'text/plain',
  138. content_id: 234,
  139. },
  140. created_by_id: 1,
  141. )
  142. assert_not(store.preferences.key?(:resizable))
  143. assert_not(store.preferences.key?(:content_inline))
  144. assert_not(store.preferences.key?(:content_preview))
  145. assert_raises(RuntimeError) do
  146. store.content_inline
  147. end
  148. assert_raises(RuntimeError) do
  149. store.content_preview
  150. end
  151. # not possible
  152. store = Store.add(
  153. object: 'SomeObject2',
  154. o_id: rand(1_234_567_890),
  155. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  156. filename: 'test1.pdf',
  157. preferences: {
  158. content_type: 'image/jpg',
  159. content_id: 234,
  160. },
  161. created_by_id: 1,
  162. )
  163. assert_equal(store.preferences[:resizable], false)
  164. assert_not(store.preferences.key?(:content_inline))
  165. assert_not(store.preferences.key?(:content_preview))
  166. assert_raises(RuntimeError) do
  167. store.content_inline
  168. end
  169. assert_raises(RuntimeError) do
  170. store.content_preview
  171. end
  172. # possible (preview and inline)
  173. store = Store.add(
  174. object: 'SomeObject3',
  175. o_id: rand(1_234_567_890),
  176. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload2.jpg')),
  177. filename: 'test1.pdf',
  178. preferences: {
  179. content_type: 'image/jpg',
  180. content_id: 234,
  181. },
  182. created_by_id: 1,
  183. )
  184. assert_equal(store.preferences[:resizable], true)
  185. assert_equal(store.preferences[:content_inline], true)
  186. assert_equal(store.preferences[:content_preview], true)
  187. temp_file = ::Tempfile.new.path
  188. File.binwrite(temp_file, store.content_inline)
  189. image = Rszr::Image.load(temp_file)
  190. assert_equal(image.width, 1800)
  191. temp_file = ::Tempfile.new.path
  192. File.binwrite(temp_file, store.content_preview)
  193. image = Rszr::Image.load(temp_file)
  194. assert_equal(image.width, 200)
  195. # possible (preview only)
  196. store = Store.add(
  197. object: 'SomeObject4',
  198. o_id: rand(1_234_567_890),
  199. data: File.binread(Rails.root.join('test', 'data', 'image', '1000x1000.png')),
  200. filename: 'test1.png',
  201. preferences: {
  202. content_type: 'image/png',
  203. content_id: 234,
  204. },
  205. created_by_id: 1,
  206. )
  207. assert_equal(store.preferences[:resizable], true)
  208. assert_nil(store.preferences[:content_inline])
  209. assert_equal(store.preferences[:content_preview], true)
  210. assert_raises(RuntimeError) do
  211. store.content_inline
  212. end
  213. temp_file = ::Tempfile.new.path
  214. File.binwrite(temp_file, store.content_preview)
  215. image = Rszr::Image.load(temp_file)
  216. assert_equal(image.width, 200)
  217. # no preview or inline needed
  218. store = Store.add(
  219. object: 'SomeObject5',
  220. o_id: rand(1_234_567_890),
  221. data: File.binread(Rails.root.join('test', 'data', 'image', '1x1.png')),
  222. filename: 'test1.png',
  223. preferences: {
  224. content_type: 'image/png',
  225. content_id: 234,
  226. },
  227. created_by_id: 1,
  228. )
  229. assert_nil(store.preferences[:resizable])
  230. assert_nil(store.preferences[:content_inline])
  231. assert_nil(store.preferences[:content_preview])
  232. assert_raises(RuntimeError) do
  233. store.content_inline
  234. end
  235. assert_raises(RuntimeError) do
  236. store.content_preview
  237. end
  238. # no preview or inline needed
  239. store = Store.add(
  240. object: 'SomeObject6',
  241. o_id: rand(1_234_567_890),
  242. data: File.binread(Rails.root.join('test', 'data', 'image', '4000x1.jpg')),
  243. filename: 'test1.jpg',
  244. preferences: {
  245. content_type: 'image/jpg',
  246. content_id: 234,
  247. },
  248. created_by_id: 1,
  249. )
  250. assert_nil(store.preferences[:resizable])
  251. assert_nil(store.preferences[:content_inline])
  252. assert_nil(store.preferences[:content_preview])
  253. # possible (no preview or inline needed)
  254. store = Store.add(
  255. object: 'SomeObject7',
  256. o_id: rand(1_234_567_890),
  257. data: File.binread(Rails.root.join('test', 'data', 'image', '8000x25.jpg')),
  258. filename: 'test1.jpg',
  259. preferences: {
  260. content_type: 'image/jpg',
  261. content_id: 234,
  262. },
  263. created_by_id: 1,
  264. )
  265. assert_nil(store.preferences[:resizable])
  266. assert_nil(store.preferences[:content_inline])
  267. assert_nil(store.preferences[:content_preview])
  268. # possible (preview and inline)
  269. store = Store.add(
  270. object: 'SomeObject8',
  271. o_id: rand(1_234_567_890),
  272. data: File.binread(Rails.root.join('test', 'data', 'image', '8000x300.jpg')),
  273. filename: 'test1.jpg',
  274. preferences: {
  275. content_type: 'image/jpg',
  276. content_id: 234,
  277. },
  278. created_by_id: 1,
  279. )
  280. assert_equal(store.preferences[:resizable], true)
  281. assert_equal(store.preferences[:content_inline], true)
  282. assert_equal(store.preferences[:content_preview], true)
  283. # possible, but skipped (preview and inline)
  284. Setting.set('import_mode', true)
  285. store = Store.add(
  286. object: 'SomeObject3',
  287. o_id: rand(1_234_567_890),
  288. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload2.jpg')),
  289. filename: 'test1.pdf',
  290. preferences: {
  291. content_type: 'image/jpg',
  292. content_id: 234,
  293. },
  294. created_by_id: 1,
  295. )
  296. assert_nil(store.preferences[:resizable])
  297. assert_nil(store.preferences[:content_inline])
  298. assert_nil(store.preferences[:content_preview])
  299. end
  300. test 'test maximal preferences size check with one oversized content' do
  301. store = Store.add(
  302. object: 'SomeObject1',
  303. o_id: rand(1_234_567_890),
  304. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  305. filename: 'test1.pdf',
  306. preferences: {
  307. content_type: 'text/plain',
  308. content_id: 234,
  309. some_key: '0' * 2500,
  310. },
  311. created_by_id: 1,
  312. )
  313. assert_not(store.preferences.key?(:some_key))
  314. assert(store.preferences.key?(:content_id))
  315. assert(store.preferences.key?(:content_type))
  316. end
  317. test 'test maximal preferences size check with two oversized content' do
  318. store = Store.add(
  319. object: 'SomeObject1',
  320. o_id: rand(1_234_567_890),
  321. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  322. filename: 'test1.pdf',
  323. preferences: {
  324. content_type: 'text/plain',
  325. content_id: 234,
  326. some_key1: '0' * 2000,
  327. some_key2: '0' * 2000,
  328. },
  329. created_by_id: 1,
  330. )
  331. assert_not(store.preferences.key?(:some_key1))
  332. assert(store.preferences.key?(:some_key2))
  333. assert(store.preferences.key?(:content_id))
  334. assert(store.preferences.key?(:content_type))
  335. end
  336. test 'test maximal preferences size check with one oversized key' do
  337. preferences = {
  338. content_type: 'text/plain',
  339. content_id: 234,
  340. }
  341. some_key1 = '0' * 2500
  342. preferences[some_key1] = 'some content'
  343. store = Store.add(
  344. object: 'SomeObject1',
  345. o_id: rand(1_234_567_890),
  346. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  347. filename: 'test1.pdf',
  348. preferences: preferences,
  349. created_by_id: 1,
  350. )
  351. assert_not(store.preferences.key?(some_key1))
  352. assert(store.preferences.key?(:content_id))
  353. assert(store.preferences.key?(:content_type))
  354. end
  355. test 'test maximal preferences size check with two oversized key' do
  356. preferences = {
  357. content_type: 'text/plain',
  358. content_id: 234,
  359. }
  360. some_key1 = '0' * 1500
  361. preferences[some_key1] = 'some content'
  362. some_key2 = '1' * 1500
  363. preferences[some_key2] = 'some content'
  364. store = Store.add(
  365. object: 'SomeObject1',
  366. o_id: rand(1_234_567_890),
  367. data: File.binread(Rails.root.join('test', 'data', 'upload', 'upload1.txt')),
  368. filename: 'test1.pdf',
  369. preferences: preferences,
  370. created_by_id: 1,
  371. )
  372. assert_not(store.preferences.key?(some_key1))
  373. assert(store.preferences.key?(some_key2))
  374. assert(store.preferences.key?(:content_id))
  375. assert(store.preferences.key?(:content_type))
  376. end
  377. end