copy_cart.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const { default: axios } = require("axios");
  2. const { expect } = require("chai");
  3. const copy = require("../coverage_models/copy");
  4. const TestFactory = require("../lib/TestFactory");
  5. /*
  6. CARTESIAN TEST FOR /copy
  7. NOTE: This test is very similar to the test for /move,
  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 = 'copy_cart_';
  13. module.exports = TestFactory.cartesian('Cartesian Test for /copy', copy, {
  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. let pathOfThingToCopy = '';
  20. if ( state.subject === 'file' ) {
  21. await t.write(`${PREFIX}${i}/a/a_file.txt`, 'file a contents\n');
  22. pathOfThingToCopy = `/a/a_file.txt`;
  23. } else {
  24. await t.mkdir(`${PREFIX}${i}/a/a_directory`);
  25. pathOfThingToCopy = `/a/a_directory`;
  26. // for test purposes, a "full" directory has each of three classes:
  27. // - a file
  28. // - an empty directory
  29. // - a directory with a file in it
  30. if ( state.subject === 'directory-full' ) {
  31. // add a file
  32. await t.write(`${PREFIX}${i}/a/a_directory/a_file.txt`, 'file a contents\n');
  33. // add a directory with a file inside of it
  34. await t.mkdir(`${PREFIX}${i}/a/a_directory/b_directory`);
  35. await t.write(`${PREFIX}${i}/a/a_directory/b_directory/b_file.txt`, 'file a contents\n');
  36. // add an empty directory
  37. await t.mkdir(`${PREFIX}${i}/a/a_directory/c_directory`);
  38. }
  39. }
  40. // 2. Situation setup for this state
  41. if ( state['conditions.destinationIsFile'] ) {
  42. await t.write(`${PREFIX}${i}/b`, 'placeholder\n');
  43. } else {
  44. await t.mkdir(`${PREFIX}${i}/b`);
  45. await t.write(`${PREFIX}${i}/b/b_file.txt`, 'file b contents\n');
  46. }
  47. const srcUID = (await t.stat(`${PREFIX}${i}${pathOfThingToCopy}`)).uid;
  48. const dstUID = (await t.stat(`${PREFIX}${i}/b`)).uid;
  49. // 3. Parameter setup for this state
  50. const data = {};
  51. data.source = state['source.format'] === 'uid'
  52. ? srcUID : `${dir}${pathOfThingToCopy}` ;
  53. data.destination = state['destination.format'] === 'uid'
  54. ? dstUID : `${dir}/b` ;
  55. if ( state.name === 'specified' ) {
  56. data.new_name = 'x_renamed';
  57. }
  58. if ( state.overwrite ) {
  59. data[state.overwrite] = true;
  60. }
  61. // 4. Request
  62. let e = null;
  63. let resp;
  64. try {
  65. resp = await axios.request({
  66. method: 'post',
  67. httpsAgent: t.httpsAgent,
  68. url: t.getURL('copy'),
  69. data,
  70. headers: {
  71. ...t.headers_,
  72. 'Content-Type': 'application/json'
  73. }
  74. });
  75. } catch (e_) {
  76. e = e_;
  77. }
  78. // 5. Check Response
  79. let error_expected = null;
  80. if (
  81. state['conditions.destinationIsFile'] &&
  82. state.name === 'specified'
  83. ) {
  84. error_expected = {
  85. code: 'dest_is_not_a_directory',
  86. message: `Destination must be a directory.`,
  87. };
  88. }
  89. else if (
  90. state['conditions.destinationIsFile'] &&
  91. ! state.overwrite &&
  92. ! state.dedupe_name
  93. ) {
  94. console.log('AN ERROR IS EXPECTED');
  95. error_expected = {
  96. code: 'item_with_same_name_exists',
  97. message: 'An item with name `b` already exists.',
  98. entry_name: 'b',
  99. }
  100. }
  101. if ( error_expected ) {
  102. expect(e).to.exist;
  103. const data = e.response.data;
  104. expect(data).deep.equal(error_expected);
  105. } else {
  106. if ( e ) throw e;
  107. }
  108. }
  109. })