move_cart.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { default: axios } = require("axios");
  2. const { expect } = require("chai");
  3. const move = require("../coverage_models/move");
  4. const TestFactory = require("../lib/TestFactory");
  5. /*
  6. CARTESIAN TEST FOR /move
  7. NOTE: This test is very similar to the test for /copy,
  8. but DRYing it would add too much complexity.
  9. It is best to have both tests open side-by-side
  10. when making changes to either one.
  11. */
  12. const PREFIX = 'move_cart_';
  13. module.exports = TestFactory.cartesian('Cartesian Test for /move', move, {
  14. each: async (t, state, i) => {
  15. // 1. Common setup for all states
  16. await t.mkdir(`${PREFIX}${i}`);
  17. const dir = `/${t.cwd}/${PREFIX}${i}`;
  18. await t.mkdir(`${PREFIX}${i}/a`);
  19. await t.write(`${PREFIX}${i}/a/a_file.txt`, 'file a contents\n');
  20. // 2. Situation setup for this state
  21. if ( state['conditions.destinationIsFile'] ) {
  22. await t.write(`${PREFIX}${i}/b`, 'placeholder\n');
  23. } else {
  24. await t.mkdir(`${PREFIX}${i}/b`);
  25. await t.write(`${PREFIX}${i}/b/b_file.txt`, 'file b contents\n');
  26. }
  27. const srcUID = (await t.stat(`${PREFIX}${i}/a/a_file.txt`)).uid;
  28. const dstUID = (await t.stat(`${PREFIX}${i}/b`)).uid;
  29. // 3. Parameter setup for this state
  30. const data = {};
  31. data.source = state['source.format'] === 'uid'
  32. ? srcUID : `${dir}/a/a_file.txt` ;
  33. data.destination = state['destination.format'] === 'uid'
  34. ? dstUID : `${dir}/b` ;
  35. if ( state.name === 'specified' ) {
  36. data.new_name = 'x_file.txt';
  37. }
  38. if ( state.overwrite ) {
  39. data[state.overwrite] = true;
  40. }
  41. // 4. Request
  42. let e = null;
  43. let resp;
  44. try {
  45. resp = await axios.request({
  46. method: 'post',
  47. httpsAgent: t.httpsAgent,
  48. url: t.getURL('move'),
  49. data,
  50. headers: {
  51. ...t.headers_,
  52. 'Content-Type': 'application/json'
  53. }
  54. });
  55. } catch (e_) {
  56. e = e_;
  57. }
  58. // 5. Check Response
  59. let error_expected = null;
  60. if (
  61. state['conditions.destinationIsFile'] &&
  62. state.name === 'specified'
  63. ) {
  64. error_expected = {
  65. code: 'dest_is_not_a_directory',
  66. message: `Destination must be a directory.`,
  67. };
  68. }
  69. else if (
  70. state['conditions.destinationIsFile'] &&
  71. ! state.overwrite
  72. ) {
  73. error_expected = {
  74. code: 'item_with_same_name_exists',
  75. message: 'An item with name `b` already exists.',
  76. entry_name: 'b',
  77. }
  78. }
  79. if ( error_expected ) {
  80. expect(e).to.exist;
  81. const data = e.response.data;
  82. expect(data).deep.equal(error_expected);
  83. } else {
  84. if ( e ) throw e;
  85. }
  86. }
  87. })