files.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. require("../setup");
  2. import { expect } from "chai";
  3. import { Cassettes } from "mocha-cassettes";
  4. import { LokaliseApi } from "../../src/lokalise/lokalise_api";
  5. describe("Files", function () {
  6. const cassette = new Cassettes("./test/cassettes");
  7. const lokaliseApi = new LokaliseApi({ apiKey: process.env.API_KEY });
  8. const project_id = "803826145ba90b42d5d860.46800099";
  9. const process_id = "d217930e61f717a21f257372e7662a054f822d8e";
  10. cassette
  11. .createTest("list", async () => {
  12. const files = await lokaliseApi.files().list({ project_id: project_id });
  13. const file = files.items[0];
  14. expect(file.file_id).to.eq(81446);
  15. expect(file.filename).to.eq("%LANG_ISO%.yml");
  16. expect(file.key_count).to.eq(66);
  17. })
  18. .register(this);
  19. cassette
  20. .createTest("list_pagination", async () => {
  21. const files = await lokaliseApi.files().list({
  22. project_id: project_id,
  23. page: 2,
  24. limit: 1,
  25. });
  26. expect(files.items[0].filename).to.eq("my_filename.json");
  27. expect(files.totalResults).to.eq(5);
  28. expect(files.totalPages).to.eq(5);
  29. expect(files.resultsPerPage).to.eq(1);
  30. expect(files.currentPage).to.eq(2);
  31. })
  32. .register(this);
  33. cassette
  34. .createTest("upload", async () => {
  35. const data =
  36. "ewogICAgImZydWl0IjogIkFwcGxlIiwKICAgICJzaXplIjogIkxhcmdlIiwKICAgICJjb2xvciI6ICJSZWQiCn0=";
  37. const process = await lokaliseApi.files().upload(project_id, {
  38. data: data,
  39. filename: "test_async.json",
  40. lang_iso: "en",
  41. });
  42. expect(process.process_id).to.eq(process_id);
  43. expect(process.type).to.eq("file-import");
  44. expect(process.status).to.eq("queued");
  45. expect(process.message).to.eq("");
  46. expect(process.created_by).to.eq(20181);
  47. expect(process.created_by_email).to.eq("bodrovis@protonmail.com");
  48. expect(process.created_at).to.eq("2020-07-02 15:13:45 (Etc/UTC)");
  49. expect(process.created_at_timestamp).to.eq(1593702825);
  50. })
  51. .register(this);
  52. cassette
  53. .createTest("upload without automations", async () => {
  54. const data =
  55. "ewogICAgImZydWl0IjogIkFwcGxlIiwKICAgICJzaXplIjogIkxhcmdlIiwKICAgICJjb2xvciI6ICJSZWQiCn0=";
  56. const process = await lokaliseApi.files().upload(project_id, {
  57. data: data,
  58. filename: "test_async.json",
  59. lang_iso: "en",
  60. use_automations: false,
  61. });
  62. expect(process.type).to.eq("file-import");
  63. expect(process.status).to.eq("queued");
  64. })
  65. .register(this);
  66. cassette
  67. .createTest("upload asynchronous re-check", async () => {
  68. const process = await lokaliseApi.queuedProcesses().get(process_id, {
  69. project_id: project_id,
  70. });
  71. expect(process.process_id).to.eq(process_id);
  72. expect(process.status).to.eq("finished");
  73. expect(process.details["files"].length).to.eq(1);
  74. const file = process.details["files"][0];
  75. expect(file.name_original).to.eq("test_async.json");
  76. expect(file.word_count_total).to.eq(3);
  77. expect(file.status).to.eq("finished");
  78. })
  79. .register(this);
  80. cassette
  81. .createTest("download", async () => {
  82. const response = await lokaliseApi.files().download(project_id, {
  83. format: "json",
  84. original_filenames: true,
  85. });
  86. expect(response.project_id).to.eq(project_id);
  87. expect(response.bundle_url).to.include("s3-eu-west-1.amazonaws.com");
  88. })
  89. .register(this);
  90. cassette
  91. .createTest("delete", async () => {
  92. const file_id = "1163964";
  93. const docs_project_id = "507504186242fccb32f015.15252556";
  94. const response = await lokaliseApi
  95. .files()
  96. .delete(file_id, { project_id: docs_project_id });
  97. expect(response.project_id).to.eq(docs_project_id);
  98. expect(response.file_deleted).to.be.true;
  99. })
  100. .register(this);
  101. });