@webda/core 2.0.0-beta.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/lib/application.d.ts +5 -5
  2. package/lib/application.js +71 -84
  3. package/lib/application.js.map +1 -1
  4. package/lib/application.spec.js +121 -140
  5. package/lib/application.spec.js.map +1 -1
  6. package/lib/core.d.ts +21 -25
  7. package/lib/core.js +233 -320
  8. package/lib/core.js.map +1 -1
  9. package/lib/core.spec.js +334 -406
  10. package/lib/core.spec.js.map +1 -1
  11. package/lib/index.d.ts +49 -48
  12. package/lib/index.js +53 -70
  13. package/lib/index.js.map +1 -1
  14. package/lib/models/aclmodel.d.ts +2 -2
  15. package/lib/models/aclmodel.js +63 -90
  16. package/lib/models/aclmodel.js.map +1 -1
  17. package/lib/models/aclmodel.spec.js +128 -152
  18. package/lib/models/aclmodel.spec.js.map +1 -1
  19. package/lib/models/coremodel.d.ts +33 -14
  20. package/lib/models/coremodel.js +160 -117
  21. package/lib/models/coremodel.js.map +1 -1
  22. package/lib/models/coremodel.spec.js +143 -107
  23. package/lib/models/coremodel.spec.js.map +1 -1
  24. package/lib/models/deployment.js +1 -2
  25. package/lib/models/ident.d.ts +3 -3
  26. package/lib/models/ident.js +4 -9
  27. package/lib/models/ident.js.map +1 -1
  28. package/lib/models/ident.spec.js +9 -11
  29. package/lib/models/ident.spec.js.map +1 -1
  30. package/lib/models/ownermodel.d.ts +2 -2
  31. package/lib/models/ownermodel.js +39 -62
  32. package/lib/models/ownermodel.js.map +1 -1
  33. package/lib/models/ownermodel.spec.js +126 -168
  34. package/lib/models/ownermodel.spec.js.map +1 -1
  35. package/lib/models/rolemodel.d.ts +1 -1
  36. package/lib/models/rolemodel.js +24 -40
  37. package/lib/models/rolemodel.js.map +1 -1
  38. package/lib/models/rolemodel.spec.js +44 -63
  39. package/lib/models/rolemodel.spec.js.map +1 -1
  40. package/lib/models/user.d.ts +9 -9
  41. package/lib/models/user.js +3 -8
  42. package/lib/models/user.js.map +1 -1
  43. package/lib/models/user.spec.js +14 -16
  44. package/lib/models/user.spec.js.map +1 -1
  45. package/lib/queues/filequeue.d.ts +2 -2
  46. package/lib/queues/filequeue.js +72 -93
  47. package/lib/queues/filequeue.js.map +1 -1
  48. package/lib/queues/filequeue.spec.js +53 -70
  49. package/lib/queues/filequeue.spec.js.map +1 -1
  50. package/lib/queues/memoryqueue.d.ts +2 -2
  51. package/lib/queues/memoryqueue.js +37 -61
  52. package/lib/queues/memoryqueue.js.map +1 -1
  53. package/lib/queues/memoryqueue.spec.js +142 -165
  54. package/lib/queues/memoryqueue.spec.js.map +1 -1
  55. package/lib/queues/pubsubservice.d.ts +2 -2
  56. package/lib/queues/pubsubservice.js +3 -7
  57. package/lib/queues/pubsubservice.js.map +1 -1
  58. package/lib/queues/queue.spec.d.ts +1 -1
  59. package/lib/queues/queue.spec.js +42 -56
  60. package/lib/queues/queue.spec.js.map +1 -1
  61. package/lib/queues/queueservice.d.ts +3 -3
  62. package/lib/queues/queueservice.js +51 -67
  63. package/lib/queues/queueservice.js.map +1 -1
  64. package/lib/router.d.ts +3 -3
  65. package/lib/router.js +46 -47
  66. package/lib/router.js.map +1 -1
  67. package/lib/router.spec.js +64 -61
  68. package/lib/router.spec.js.map +1 -1
  69. package/lib/services/asyncevents.d.ts +2 -2
  70. package/lib/services/asyncevents.js +44 -64
  71. package/lib/services/asyncevents.js.map +1 -1
  72. package/lib/services/asyncevents.spec.js +81 -98
  73. package/lib/services/asyncevents.spec.js.map +1 -1
  74. package/lib/services/authentication.d.ts +23 -13
  75. package/lib/services/authentication.js +540 -566
  76. package/lib/services/authentication.js.map +1 -1
  77. package/lib/services/authentication.spec.js +411 -447
  78. package/lib/services/authentication.spec.js.map +1 -1
  79. package/lib/services/binary.d.ts +8 -6
  80. package/lib/services/binary.js +212 -245
  81. package/lib/services/binary.js.map +1 -1
  82. package/lib/services/binary.spec.d.ts +3 -3
  83. package/lib/services/binary.spec.js +390 -443
  84. package/lib/services/binary.spec.js.map +1 -1
  85. package/lib/services/cloudbinary.d.ts +3 -3
  86. package/lib/services/cloudbinary.js +45 -70
  87. package/lib/services/cloudbinary.js.map +1 -1
  88. package/lib/services/cloudbinary.spec.d.ts +3 -3
  89. package/lib/services/cloudbinary.spec.js +133 -162
  90. package/lib/services/cloudbinary.spec.js.map +1 -1
  91. package/lib/services/configuration.d.ts +1 -1
  92. package/lib/services/configuration.js +78 -105
  93. package/lib/services/configuration.js.map +1 -1
  94. package/lib/services/configuration.spec.js +63 -83
  95. package/lib/services/configuration.spec.js.map +1 -1
  96. package/lib/services/cron.d.ts +3 -2
  97. package/lib/services/cron.js +9 -13
  98. package/lib/services/cron.js.map +1 -1
  99. package/lib/services/cron.spec.js +18 -21
  100. package/lib/services/cron.spec.js.map +1 -1
  101. package/lib/services/cryptoservice.d.ts +9 -4
  102. package/lib/services/cryptoservice.js +187 -221
  103. package/lib/services/cryptoservice.js.map +1 -1
  104. package/lib/services/cryptoservice.spec.js +88 -115
  105. package/lib/services/cryptoservice.spec.js.map +1 -1
  106. package/lib/services/debugmailer.d.ts +1 -1
  107. package/lib/services/debugmailer.js +13 -34
  108. package/lib/services/debugmailer.js.map +1 -1
  109. package/lib/services/debugmailer.spec.js +16 -29
  110. package/lib/services/debugmailer.spec.js.map +1 -1
  111. package/lib/services/echo.d.ts +2 -2
  112. package/lib/services/echo.js +15 -31
  113. package/lib/services/echo.js.map +1 -1
  114. package/lib/services/echo.spec.js +27 -40
  115. package/lib/services/echo.spec.js.map +1 -1
  116. package/lib/services/filebinary.d.ts +5 -5
  117. package/lib/services/filebinary.js +165 -210
  118. package/lib/services/filebinary.js.map +1 -1
  119. package/lib/services/filebinary.spec.d.ts +2 -1
  120. package/lib/services/filebinary.spec.js +106 -116
  121. package/lib/services/filebinary.spec.js.map +1 -1
  122. package/lib/services/fileconfiguration.d.ts +1 -1
  123. package/lib/services/fileconfiguration.js +25 -44
  124. package/lib/services/fileconfiguration.js.map +1 -1
  125. package/lib/services/fileconfiguration.spec.js +79 -109
  126. package/lib/services/fileconfiguration.spec.js.map +1 -1
  127. package/lib/services/invitationservice.d.ts +8 -8
  128. package/lib/services/invitationservice.js +274 -314
  129. package/lib/services/invitationservice.js.map +1 -1
  130. package/lib/services/invitationservice.spec.js +443 -449
  131. package/lib/services/invitationservice.spec.js.map +1 -1
  132. package/lib/services/kubernetesconfiguration.d.ts +1 -1
  133. package/lib/services/kubernetesconfiguration.js +41 -60
  134. package/lib/services/kubernetesconfiguration.js.map +1 -1
  135. package/lib/services/kubernetesconfiguration.spec.js +80 -98
  136. package/lib/services/kubernetesconfiguration.spec.js.map +1 -1
  137. package/lib/services/mailer.d.ts +4 -4
  138. package/lib/services/mailer.js +62 -86
  139. package/lib/services/mailer.js.map +1 -1
  140. package/lib/services/mailer.spec.js +86 -127
  141. package/lib/services/mailer.spec.js.map +1 -1
  142. package/lib/services/notificationservice.d.ts +3 -3
  143. package/lib/services/notificationservice.js +21 -42
  144. package/lib/services/notificationservice.js.map +1 -1
  145. package/lib/services/notificationservice.spec.js +47 -73
  146. package/lib/services/notificationservice.spec.js.map +1 -1
  147. package/lib/services/oauth.d.ts +4 -4
  148. package/lib/services/oauth.js +68 -83
  149. package/lib/services/oauth.js.map +1 -1
  150. package/lib/services/oauth.spec.js +130 -156
  151. package/lib/services/oauth.spec.js.map +1 -1
  152. package/lib/services/proxy.d.ts +2 -6
  153. package/lib/services/proxy.js +45 -58
  154. package/lib/services/proxy.js.map +1 -1
  155. package/lib/services/proxy.spec.js +34 -53
  156. package/lib/services/proxy.spec.js.map +1 -1
  157. package/lib/services/resource.d.ts +2 -2
  158. package/lib/services/resource.js +12 -18
  159. package/lib/services/resource.js.map +1 -1
  160. package/lib/services/resource.spec.js +67 -99
  161. package/lib/services/resource.spec.js.map +1 -1
  162. package/lib/services/service.d.ts +26 -3
  163. package/lib/services/service.js +68 -40
  164. package/lib/services/service.js.map +1 -1
  165. package/lib/services/service.spec.js +62 -67
  166. package/lib/services/service.spec.js.map +1 -1
  167. package/lib/services/version.d.ts +1 -1
  168. package/lib/services/version.js +3 -8
  169. package/lib/services/version.js.map +1 -1
  170. package/lib/services/version.spec.js +11 -24
  171. package/lib/services/version.spec.js.map +1 -1
  172. package/lib/stores/aggregator.d.ts +2 -2
  173. package/lib/stores/aggregator.js +37 -58
  174. package/lib/stores/aggregator.js.map +1 -1
  175. package/lib/stores/file.d.ts +3 -3
  176. package/lib/stores/file.js +93 -131
  177. package/lib/stores/file.js.map +1 -1
  178. package/lib/stores/file.spec.d.ts +2 -2
  179. package/lib/stores/file.spec.js +262 -289
  180. package/lib/stores/file.spec.js.map +1 -1
  181. package/lib/stores/mapper.d.ts +3 -3
  182. package/lib/stores/mapper.js +110 -137
  183. package/lib/stores/mapper.js.map +1 -1
  184. package/lib/stores/mapper.spec.js +133 -150
  185. package/lib/stores/mapper.spec.js.map +1 -1
  186. package/lib/stores/memory.d.ts +3 -3
  187. package/lib/stores/memory.js +98 -144
  188. package/lib/stores/memory.js.map +1 -1
  189. package/lib/stores/memory.spec.d.ts +2 -2
  190. package/lib/stores/memory.spec.js +124 -145
  191. package/lib/stores/memory.spec.js.map +1 -1
  192. package/lib/stores/store.d.ts +27 -17
  193. package/lib/stores/store.js +833 -849
  194. package/lib/stores/store.js.map +1 -1
  195. package/lib/stores/store.spec.d.ts +5 -4
  196. package/lib/stores/store.spec.js +625 -669
  197. package/lib/stores/store.spec.js.map +1 -1
  198. package/lib/stores/webdaql/WebdaQLLexer.d.ts +4 -4
  199. package/lib/stores/webdaql/WebdaQLLexer.js +9 -13
  200. package/lib/stores/webdaql/WebdaQLLexer.js.map +1 -1
  201. package/lib/stores/webdaql/WebdaQLParserLexer.d.ts +4 -4
  202. package/lib/stores/webdaql/WebdaQLParserLexer.js +9 -13
  203. package/lib/stores/webdaql/WebdaQLParserLexer.js.map +1 -1
  204. package/lib/stores/webdaql/WebdaQLParserListener.d.ts +2 -2
  205. package/lib/stores/webdaql/WebdaQLParserListener.js +1 -2
  206. package/lib/stores/webdaql/WebdaQLParserListener.js.map +1 -1
  207. package/lib/stores/webdaql/WebdaQLParserParser.d.ts +10 -10
  208. package/lib/stores/webdaql/WebdaQLParserParser.js +62 -91
  209. package/lib/stores/webdaql/WebdaQLParserParser.js.map +1 -1
  210. package/lib/stores/webdaql/WebdaQLParserVisitor.d.ts +2 -2
  211. package/lib/stores/webdaql/WebdaQLParserVisitor.js +1 -2
  212. package/lib/stores/webdaql/WebdaQLParserVisitor.js.map +1 -1
  213. package/lib/stores/webdaql/query.d.ts +4 -5
  214. package/lib/stores/webdaql/query.js +16 -19
  215. package/lib/stores/webdaql/query.js.map +1 -1
  216. package/lib/stores/webdaql/query.spec.js +30 -32
  217. package/lib/stores/webdaql/query.spec.js.map +1 -1
  218. package/lib/test.d.ts +3 -3
  219. package/lib/test.js +73 -100
  220. package/lib/test.js.map +1 -1
  221. package/lib/unpackedapplication.d.ts +1 -1
  222. package/lib/unpackedapplication.js +49 -39
  223. package/lib/unpackedapplication.js.map +1 -1
  224. package/lib/utils/abstractdeployer.js +1 -5
  225. package/lib/utils/abstractdeployer.js.map +1 -1
  226. package/lib/utils/abstractdeployer.spec.js +6 -8
  227. package/lib/utils/abstractdeployer.spec.js.map +1 -1
  228. package/lib/utils/context.d.ts +7 -5
  229. package/lib/utils/context.js +95 -103
  230. package/lib/utils/context.js.map +1 -1
  231. package/lib/utils/context.spec.js +139 -163
  232. package/lib/utils/context.spec.js.map +1 -1
  233. package/lib/utils/cookie.d.ts +3 -3
  234. package/lib/utils/cookie.js +42 -61
  235. package/lib/utils/cookie.js.map +1 -1
  236. package/lib/utils/cookie.spec.js +42 -64
  237. package/lib/utils/cookie.spec.js.map +1 -1
  238. package/lib/utils/esm.d.ts +4 -0
  239. package/lib/utils/esm.js +10 -0
  240. package/lib/utils/esm.js.map +1 -0
  241. package/lib/utils/httpcontext.d.ts +3 -0
  242. package/lib/utils/httpcontext.js +48 -65
  243. package/lib/utils/httpcontext.js.map +1 -1
  244. package/lib/utils/httpcontext.spec.js +37 -56
  245. package/lib/utils/httpcontext.spec.js.map +1 -1
  246. package/lib/utils/logger.d.ts +2 -2
  247. package/lib/utils/logger.js +15 -28
  248. package/lib/utils/logger.js.map +1 -1
  249. package/lib/utils/logger.spec.js +9 -11
  250. package/lib/utils/logger.spec.js.map +1 -1
  251. package/lib/utils/serializers.js +17 -20
  252. package/lib/utils/serializers.js.map +1 -1
  253. package/lib/utils/serializers.spec.js +43 -44
  254. package/lib/utils/serializers.spec.js.map +1 -1
  255. package/lib/utils/session.d.ts +6 -6
  256. package/lib/utils/session.js +37 -59
  257. package/lib/utils/session.js.map +1 -1
  258. package/lib/utils/session.spec.js +38 -51
  259. package/lib/utils/session.spec.js.map +1 -1
  260. package/lib/utils/waiter.d.ts +1 -1
  261. package/lib/utils/waiter.js +32 -52
  262. package/lib/utils/waiter.js.map +1 -1
  263. package/lib/utils/waiter.spec.js +75 -92
  264. package/lib/utils/waiter.spec.js.map +1 -1
  265. package/package.json +18 -13
  266. package/test/config-invitation.json +1 -3
  267. package/test/config.json +1 -2
  268. package/test/config.old.json +1 -2
  269. package/test/moddas/annotatedservice.js +5 -5
  270. package/test/moddas/debugmailer.js +2 -3
  271. package/test/moddas/fakeservice.js +2 -3
  272. package/test/moddas/voidstore.js +6 -4
  273. package/test/models/ident.js +2 -6
  274. package/test/models/task.js +3 -7
  275. package/test/package.json +2 -1
  276. package/webda.module.json +895 -190
@@ -1,34 +1,21 @@
1
- "use strict";
2
1
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
2
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
3
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
6
  };
8
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
- return new (P || (P = Promise))(function (resolve, reject) {
11
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
- step((generator = generator.apply(thisArg, _arguments || [])).next());
15
- });
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.BinaryTest = exports.ImageUser = void 0;
19
- const mocha_1 = require("@testdeck/mocha");
20
- const assert = require("assert");
21
- const axios_1 = require("axios");
22
- const events_1 = require("events");
23
- const fs = require("fs");
24
- const sinon = require("sinon");
25
- const __1 = require("..");
26
- const test_1 = require("../test");
27
- const binary_1 = require("./binary");
28
- class ImageUser extends __1.User {
7
+ import { suite, test } from "@testdeck/mocha";
8
+ import * as assert from "assert";
9
+ import axios from "axios";
10
+ import { EventEmitter } from "events";
11
+ import * as fs from "fs";
12
+ import * as sinon from "sinon";
13
+ import { Binary, User } from "../index.js";
14
+ import { WebdaTest } from "../test.js";
15
+ import { BinaryNotFoundError, LocalBinaryFile, MemoryBinaryFile } from "./binary.js";
16
+ export class ImageUser extends User {
29
17
  }
30
- exports.ImageUser = ImageUser;
31
- class TestBinaryService extends __1.Binary {
18
+ class TestBinaryService extends Binary {
32
19
  store(object, property, file, metadatas, index) {
33
20
  throw new Error("Method not implemented.");
34
21
  }
@@ -47,11 +34,9 @@ class TestBinaryService extends __1.Binary {
47
34
  _get() {
48
35
  return null;
49
36
  }
50
- cascadeDelete() {
51
- return __awaiter(this, void 0, void 0, function* () { });
52
- }
37
+ async cascadeDelete() { }
53
38
  }
54
- class BinaryTest extends test_1.WebdaTest {
39
+ class BinaryTest extends WebdaTest {
55
40
  getUserStore() {
56
41
  return this.getService("Users");
57
42
  }
@@ -67,202 +52,183 @@ class BinaryTest extends test_1.WebdaTest {
67
52
  getTestFile() {
68
53
  return process.cwd() + "/test/Dockerfile.txt";
69
54
  }
70
- before(init = true) {
71
- const _super = Object.create(null, {
72
- before: { get: () => super.before }
73
- });
74
- return __awaiter(this, void 0, void 0, function* () {
75
- yield _super.before.call(this, init);
76
- let userStore = this.getUserStore();
77
- let binary = this.getBinary();
78
- assert.notStrictEqual(userStore, undefined);
79
- assert.notStrictEqual(binary, undefined);
80
- yield userStore.__clean();
81
- yield binary.__clean();
82
- });
55
+ async before(init = true) {
56
+ await super.before(init);
57
+ let userStore = this.getUserStore();
58
+ let binary = this.getBinary();
59
+ assert.notStrictEqual(userStore, undefined);
60
+ assert.notStrictEqual(binary, undefined);
61
+ await userStore.__clean();
62
+ await binary.__clean();
83
63
  }
84
- normal() {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- let userStore = this.getUserStore();
87
- let binary = this.getBinary();
88
- let map = this.getMap();
89
- let exposePath = this.getExposePath();
90
- var eventFired = 0;
91
- var events = ["Binary.Get", "Binary.Create", "Binary.Delete"];
92
- for (let evt in events) {
93
- binary.on(events[evt], function () {
94
- eventFired++;
95
- });
96
- }
97
- // Check CREATE - READ
98
- var hash;
99
- var user1;
100
- var user2;
101
- var user;
102
- var ctx;
103
- this.log("DEBUG", "Initializing users");
104
- user1 = yield userStore.save({
105
- test: "plop"
106
- });
107
- user2 = yield userStore.save({
108
- test: "plop"
109
- });
110
- this.log("DEBUG", "Store a local file");
111
- yield binary.store(user1, map, new binary_1.LocalBinaryFile(this.getTestFile()), {});
112
- user1 = yield userStore.get(user1.uuid);
113
- assert.notStrictEqual(user1[map], undefined);
114
- assert.strictEqual(user1[map].length, 1);
115
- hash = user1[map][0].hash;
116
- this.log("DEBUG", "Get usage count");
117
- let value = yield binary.getUsageCount(hash);
118
- assert.strictEqual(value, 1);
119
- this.log("DEBUG", "Store same local file on separate object");
120
- yield binary.store(user2, map, new binary_1.LocalBinaryFile(this.getTestFile()), {});
121
- user = yield userStore.get(user2.uuid);
122
- assert.notStrictEqual(user[map], undefined);
123
- assert.strictEqual(user[map].length, 1);
124
- assert.strictEqual(user[map][0].constructor.name, "BinaryMap");
125
- hash = user[map][0].hash;
126
- this.log("DEBUG", "Verify usage count");
127
- value = yield binary.getUsageCount(hash);
128
- assert.strictEqual(value, 2);
129
- this.log("DEBUG", "Delete one attached object");
130
- yield binary.delete(user, map, 0);
131
- user = yield userStore.get(user2.uuid);
132
- assert.strictEqual(user[map].length, 0);
133
- this.log("DEBUG", "Verify usage count decreased");
134
- value = yield binary.getUsageCount(hash);
135
- assert.strictEqual(value, 1);
136
- // Try to get images on user1 as user2
137
- ctx = yield this.newContext({
138
- type: "CRUD",
139
- uuid: "PLOP"
64
+ async normal() {
65
+ let userStore = this.getUserStore();
66
+ let binary = this.getBinary();
67
+ let map = this.getMap();
68
+ let exposePath = this.getExposePath();
69
+ var eventFired = 0;
70
+ var events = ["Binary.Get", "Binary.Create", "Binary.Delete"];
71
+ for (let evt in events) {
72
+ binary.on(events[evt], function () {
73
+ eventFired++;
140
74
  });
141
- ctx.session.userId = user2.uuid;
142
- let executor = this.getExecutor(ctx, "test.webda.io", "GET", exposePath + "/users/" + user1.uuid + "/" + map + "/0");
143
- this.log("DEBUG", "Verify permission check on HTTP GET");
144
- yield assert.rejects(executor.execute(ctx), res => res == 403);
145
- ctx.session.userId = user1.uuid;
146
- this.log("DEBUG", "Verify valid permission check on HTTP GET");
147
- executor = this.getExecutor(ctx, "test.webda.io", "GET", exposePath + "/users/" + user1.uuid + "/" + map + "/0");
148
- yield executor.execute(ctx);
149
- // We dont check for result as FileBinary will return datas and S3 a redirect
150
- if (fs.existsSync("./downloadTo.tmp")) {
151
- fs.unlinkSync("./downloadTo.tmp");
152
- }
153
- this.log("DEBUG", "BinaryMap downloadTo");
154
- yield binary.downloadTo(user1[map][0], "./downloadTo.tmp");
155
- // Check the result is the same
156
- assert.strictEqual(fs.readFileSync("./downloadTo.tmp").toString(), fs.readFileSync(this.getTestFile()).toString());
157
- fs.unlinkSync("./downloadTo.tmp");
158
- this.log("DEBUG", "BinaryMap downloadTo from CoreModel");
159
- yield user1[map][0].downloadTo("./downloadTo.tmp");
160
- assert.strictEqual(fs.readFileSync("./downloadTo.tmp").toString(), fs.readFileSync(this.getTestFile()).toString());
75
+ }
76
+ // Check CREATE - READ
77
+ var hash;
78
+ var user1;
79
+ var user2;
80
+ var user;
81
+ var ctx;
82
+ this.log("DEBUG", "Initializing users");
83
+ user1 = await userStore.save({
84
+ test: "plop"
85
+ });
86
+ user2 = await userStore.save({
87
+ test: "plop"
88
+ });
89
+ this.log("DEBUG", "Store a local file");
90
+ await binary.store(user1, map, new LocalBinaryFile(this.getTestFile()), {});
91
+ user1 = await userStore.get(user1.uuid);
92
+ assert.notStrictEqual(user1[map], undefined);
93
+ assert.strictEqual(user1[map].length, 1);
94
+ hash = user1[map][0].hash;
95
+ this.log("DEBUG", "Get usage count");
96
+ let value = await binary.getUsageCount(hash);
97
+ assert.strictEqual(value, 1);
98
+ this.log("DEBUG", "Store same local file on separate object");
99
+ await binary.store(user2, map, new LocalBinaryFile(this.getTestFile()), {});
100
+ user = await userStore.get(user2.uuid);
101
+ assert.notStrictEqual(user[map], undefined);
102
+ assert.strictEqual(user[map].length, 1);
103
+ assert.strictEqual(user[map][0].constructor.name, "BinaryMap");
104
+ hash = user[map][0].hash;
105
+ this.log("DEBUG", "Verify usage count");
106
+ value = await binary.getUsageCount(hash);
107
+ assert.strictEqual(value, 2);
108
+ this.log("DEBUG", "Delete one attached object");
109
+ await binary.delete(user, map, 0);
110
+ user = await userStore.get(user2.uuid);
111
+ assert.strictEqual(user[map].length, 0);
112
+ this.log("DEBUG", "Verify usage count decreased");
113
+ value = await binary.getUsageCount(hash);
114
+ assert.strictEqual(value, 1);
115
+ // Try to get images on user1 as user2
116
+ ctx = await this.newContext({
117
+ type: "CRUD",
118
+ uuid: "PLOP"
119
+ });
120
+ ctx.session.userId = user2.uuid;
121
+ let executor = this.getExecutor(ctx, "test.webda.io", "GET", exposePath + "/users/" + user1.uuid + "/" + map + "/0");
122
+ this.log("DEBUG", "Verify permission check on HTTP GET");
123
+ await assert.rejects(executor.execute(ctx), res => res == 403);
124
+ ctx.session.userId = user1.uuid;
125
+ this.log("DEBUG", "Verify valid permission check on HTTP GET");
126
+ executor = this.getExecutor(ctx, "test.webda.io", "GET", exposePath + "/users/" + user1.uuid + "/" + map + "/0");
127
+ await executor.execute(ctx);
128
+ // We dont check for result as FileBinary will return datas and S3 a redirect
129
+ if (fs.existsSync("./downloadTo.tmp")) {
161
130
  fs.unlinkSync("./downloadTo.tmp");
162
- // Fake IO issue
163
- let stub = sinon.stub(binary, "_get").callsFake(() => {
164
- return {
165
- pipe: stream => {
166
- stream.emit("error", "bad read");
167
- }
168
- };
169
- });
170
- let stub2;
171
- try {
172
- this.log("DEBUG", "BinaryMap downloadTo with I/O issues");
173
- yield assert.rejects(() => binary.downloadTo(user1[map][0], "./downloadTo.tmp"));
174
- assert.ok(!fs.existsSync("./downloadTo.tmp"));
175
- stub2 = sinon.stub(fs, "unlinkSync").callsFake(() => {
176
- throw new Error();
177
- });
178
- yield assert.rejects(() => binary.downloadTo(user1[map][0], "./downloadTo.tmp"));
179
- }
180
- finally {
181
- stub.restore();
182
- if (stub2) {
183
- stub2.restore();
131
+ }
132
+ this.log("DEBUG", "BinaryMap downloadTo");
133
+ await binary.downloadTo(user1[map][0], "./downloadTo.tmp");
134
+ // Check the result is the same
135
+ assert.strictEqual(fs.readFileSync("./downloadTo.tmp").toString(), fs.readFileSync(this.getTestFile()).toString());
136
+ fs.unlinkSync("./downloadTo.tmp");
137
+ this.log("DEBUG", "BinaryMap downloadTo from CoreModel");
138
+ await user1[map][0].downloadTo("./downloadTo.tmp");
139
+ assert.strictEqual(fs.readFileSync("./downloadTo.tmp").toString(), fs.readFileSync(this.getTestFile()).toString());
140
+ fs.unlinkSync("./downloadTo.tmp");
141
+ // Fake IO issue
142
+ let stub = sinon.stub(binary, "_get").callsFake(() => {
143
+ return {
144
+ pipe: stream => {
145
+ stream.emit("error", "bad read");
184
146
  }
185
- }
186
- this.log("DEBUG", "Check get stream");
187
- let buf1 = yield __1.Binary.streamToBuffer(yield user1[map][0].get());
188
- // cov
189
- let buf2 = yield __1.Binary.streamToBuffer(yield binary._get(user1[map][0]));
190
- assert.strictEqual(buf1.toString(), buf2.toString());
191
- this.log("DEBUG", "Delete CoreModel and ensure usage count");
192
- yield userStore.delete(user1.uuid);
193
- value = yield binary.getUsageCount(hash);
194
- assert.strictEqual(value, 0);
147
+ };
195
148
  });
149
+ let stub2;
150
+ try {
151
+ this.log("DEBUG", "BinaryMap downloadTo with I/O issues");
152
+ await assert.rejects(() => binary.downloadTo(user1[map][0], "./downloadTo.tmp"));
153
+ assert.ok(!fs.existsSync("./downloadTo.tmp"));
154
+ }
155
+ finally {
156
+ stub.restore();
157
+ if (stub2) {
158
+ stub2.restore();
159
+ }
160
+ }
161
+ this.log("DEBUG", "Check get stream");
162
+ let buf1 = await Binary.streamToBuffer(await user1[map][0].get());
163
+ // cov
164
+ let buf2 = await Binary.streamToBuffer(await binary._get(user1[map][0]));
165
+ assert.strictEqual(buf1.toString(), buf2.toString());
166
+ this.log("DEBUG", "Delete CoreModel and ensure usage count");
167
+ await userStore.delete(user1.uuid);
168
+ value = await binary.getUsageCount(hash);
169
+ assert.strictEqual(value, 0);
196
170
  }
197
- notMapped() {
198
- return __awaiter(this, void 0, void 0, function* () {
199
- let userStore = this.getUserStore();
200
- let binary = this.getBinary();
201
- let user1 = yield userStore.save({
202
- test: "plop"
203
- });
204
- yield assert.rejects(() => binary.store(user1, "images2", new binary_1.LocalBinaryFile(this.getTestFile()), {}), err => true);
171
+ async notMapped() {
172
+ let userStore = this.getUserStore();
173
+ let binary = this.getBinary();
174
+ let user1 = await userStore.save({
175
+ test: "plop"
205
176
  });
177
+ await assert.rejects(() => binary.store(user1, "images2", new LocalBinaryFile(this.getTestFile()), {}), err => true);
206
178
  }
207
- update() {
208
- return __awaiter(this, void 0, void 0, function* () {
209
- let userStore = this.getUserStore();
210
- let binary = this.getBinary();
211
- let map = this.getMap();
212
- var user1;
213
- var user;
214
- let hash;
215
- user1 = yield userStore.save({
216
- test: "plop"
217
- });
218
- yield binary.store(user1, map, new binary_1.LocalBinaryFile(this.getTestFile()), {});
219
- user = yield userStore.get(user1.uuid);
220
- assert.notStrictEqual(user[map], undefined);
221
- assert.strictEqual(user[map].length, 1);
222
- let value = yield binary.getUsageCount(user[map][0].hash);
223
- assert.strictEqual(value, 1);
179
+ async update() {
180
+ let userStore = this.getUserStore();
181
+ let binary = this.getBinary();
182
+ let map = this.getMap();
183
+ var user1;
184
+ var user;
185
+ let hash;
186
+ user1 = await userStore.save({
187
+ test: "plop"
224
188
  });
189
+ await binary.store(user1, map, new LocalBinaryFile(this.getTestFile()), {});
190
+ user = await userStore.get(user1.uuid);
191
+ assert.notStrictEqual(user[map], undefined);
192
+ assert.strictEqual(user[map].length, 1);
193
+ let value = await binary.getUsageCount(user[map][0].hash);
194
+ assert.strictEqual(value, 1);
225
195
  }
226
196
  checkMap() {
227
197
  let binary = this.getBinary();
228
198
  // @ts-ignore
229
199
  assert.throws(() => binary.checkMap("plop", "pouf"), /Unknown mapping/);
230
200
  }
231
- httpCreate() {
232
- return __awaiter(this, void 0, void 0, function* () {
233
- let userStore = this.getUserStore();
234
- let user1 = yield userStore.save({
235
- test: "plop"
236
- });
237
- let ctx = yield this.newContext();
238
- let executor = this.getExecutor(ctx, "test.webda.io", "POST", `/binary/users/${user1.getUuid()}/images`, {});
239
- yield assert.rejects(() => executor.execute(ctx), /403/);
240
- /**
241
- * Two modes should be handled
242
- *
243
- * POST body + X-Webda-Metadatas header
244
- * POST multipart
245
- */
246
- ctx.getHttpContext().setBody("MY_IMAGE");
247
- ctx.getSession().login(user1.getUuid(), "fake");
248
- // Need to verify
249
- yield executor.execute(ctx);
201
+ async httpCreate() {
202
+ let userStore = this.getUserStore();
203
+ let user1 = await userStore.save({
204
+ test: "plop"
250
205
  });
206
+ let ctx = await this.newContext();
207
+ let executor = this.getExecutor(ctx, "test.webda.io", "POST", `/binary/users/${user1.getUuid()}/images`, {});
208
+ await assert.rejects(() => executor.execute(ctx), /403/);
209
+ /**
210
+ * Two modes should be handled
211
+ *
212
+ * POST body + X-Webda-Metadatas header
213
+ * POST multipart
214
+ */
215
+ ctx.getHttpContext().setBody("MY_IMAGE");
216
+ ctx.getSession().login(user1.getUuid(), "fake");
217
+ // Need to verify
218
+ await executor.execute(ctx);
251
219
  }
252
- httpDelete() {
253
- return __awaiter(this, void 0, void 0, function* () {
254
- let { user1, ctx } = yield this.setupDefault(false);
255
- let executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/myhash`, {});
256
- yield assert.rejects(() => executor.execute(ctx), /403/, "DELETE binary w/o permission");
257
- // Now login
258
- ctx.getSession().login(user1.getUuid(), "fake");
259
- executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/myhash`, {});
260
- yield assert.rejects(() => executor.execute(ctx), /412/);
261
- executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {});
262
- yield executor.execute(ctx);
263
- yield user1.refresh();
264
- assert.ok(user1.images.length === 0);
265
- });
220
+ async httpDelete() {
221
+ let { user1, ctx } = await this.setupDefault(false);
222
+ let executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/myhash`, {});
223
+ await assert.rejects(() => executor.execute(ctx), /403/, "DELETE binary w/o permission");
224
+ // Now login
225
+ ctx.getSession().login(user1.getUuid(), "fake");
226
+ executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/myhash`, {});
227
+ await assert.rejects(() => executor.execute(ctx), /412/);
228
+ executor = this.getExecutor(ctx, "test.webda.io", "DELETE", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {});
229
+ await executor.execute(ctx);
230
+ await user1.refresh();
231
+ assert.ok(user1.images.length === 0);
266
232
  }
267
233
  getOperationName() {
268
234
  let binary = this.getBinary();
@@ -273,288 +239,269 @@ class BinaryTest extends test_1.WebdaTest {
273
239
  // @ts-ignore
274
240
  assert.strictEqual(binary.getOperationName(), binary.getName());
275
241
  }
276
- httpGetError() {
277
- return __awaiter(this, void 0, void 0, function* () {
278
- let { binary, user1, ctx } = yield this.setupDefault();
279
- // @ts-ignore
280
- let stub = sinon.stub(binary, "get").callsFake(() => {
281
- return {
282
- pipe: stream => {
283
- stream.emit("error", new Error("I/O"));
284
- }
285
- };
286
- });
287
- yield this.execute(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
288
- yield assert.rejects(() => this.execute(ctx, "test.webda.io", "GET", ctx.getResponseHeaders().Location.substring("http://test.webda.io".length)), /500/, "GET binary with I/O");
289
- stub.restore();
242
+ async httpGetError() {
243
+ let { binary, user1, ctx } = await this.setupDefault();
244
+ // @ts-ignore
245
+ let stub = sinon.stub(binary, "get").callsFake(() => {
246
+ return {
247
+ pipe: stream => {
248
+ stream.emit("error", new Error("I/O"));
249
+ }
250
+ };
290
251
  });
252
+ await this.execute(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
253
+ await assert.rejects(() => this.execute(ctx, "test.webda.io", "GET", ctx.getResponseHeaders().Location.substring("http://test.webda.io".length)), /500/, "GET binary with I/O");
254
+ stub.restore();
291
255
  }
292
- httpGet() {
293
- return __awaiter(this, void 0, void 0, function* () {
294
- let { binary, user1, ctx } = yield this.setupDefault(false);
295
- let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
296
- // If not logged in
297
- yield assert.rejects(() => executor.execute(ctx), /403/, "GET binary w/o permission");
298
- executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/unknown/images/0`, {});
299
- yield assert.rejects(() => executor.execute(ctx), /404/, "GET binary on unknown object");
300
- // Login
301
- ctx.getSession().login(user1.getUuid(), "fake");
302
- executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
303
- yield executor.execute(ctx);
304
- yield binary.delete(user1, "images", 0);
305
- // Check 404 now it is gone
306
- executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
307
- yield assert.rejects(() => executor.execute(ctx), /404/);
308
- });
256
+ async httpGet() {
257
+ let { binary, user1, ctx } = await this.setupDefault(false);
258
+ let executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
259
+ // If not logged in
260
+ await assert.rejects(() => executor.execute(ctx), /403/, "GET binary w/o permission");
261
+ executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/unknown/images/0`, {});
262
+ await assert.rejects(() => executor.execute(ctx), /404/, "GET binary on unknown object");
263
+ // Login
264
+ ctx.getSession().login(user1.getUuid(), "fake");
265
+ executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
266
+ await executor.execute(ctx);
267
+ await binary.delete(user1, "images", 0);
268
+ // Check 404 now it is gone
269
+ executor = this.getExecutor(ctx, "test.webda.io", "GET", `/binary/users/${user1.getUuid()}/images/0`, {});
270
+ await assert.rejects(() => executor.execute(ctx), /404/);
309
271
  }
310
- httpMetadata() {
272
+ async httpMetadata() {
311
273
  var _a;
312
- var _b;
313
- return __awaiter(this, void 0, void 0, function* () {
314
- let { binary, user1, ctx } = yield this.setupDefault(false);
315
- let executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {}); // If not logged in
316
- yield assert.rejects(() => executor.execute(ctx), /403/, "PUT metadata w/o permission");
317
- executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/unknown/images/0/${user1.images[0].hash}`, {});
318
- yield assert.rejects(() => executor.execute(ctx), /404/, "GET binary on unknown object");
319
- // Login
320
- ctx.getSession().login(user1.getUuid(), "fake");
321
- executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {
322
- "my-metadata": "updated",
323
- "my-other-metadata": true
324
- });
325
- yield executor.execute(ctx);
326
- let user = yield ctx.getCurrentUser(true);
327
- (_a = (_b = user.images[0]).metadata) !== null && _a !== void 0 ? _a : (_b.metadata = {});
328
- assert.strictEqual(user.images[0].metadata["my-metadata"], "updated");
329
- assert.strictEqual(user.images[0].metadata["my-other-metadata"], true);
330
- executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {
331
- "my-metadata": "updated".repeat(4096),
332
- "my-other-metadata": true
333
- });
334
- yield assert.rejects(() => executor.execute(ctx), /400/, "PUT metadata with big content should fail");
274
+ let { binary, user1, ctx } = await this.setupDefault(false);
275
+ let executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {}); // If not logged in
276
+ await assert.rejects(() => executor.execute(ctx), /403/, "PUT metadata w/o permission");
277
+ executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/unknown/images/0/${user1.images[0].hash}`, {});
278
+ await assert.rejects(() => executor.execute(ctx), /404/, "GET binary on unknown object");
279
+ // Login
280
+ ctx.getSession().login(user1.getUuid(), "fake");
281
+ executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {
282
+ "my-metadata": "updated",
283
+ "my-other-metadata": true
335
284
  });
336
- }
337
- getNotFound() {
338
- return __awaiter(this, void 0, void 0, function* () {
339
- yield assert.rejects(() =>
340
- // @ts-ignore
341
- this.getBinary().get({
342
- hash: "none",
343
- mimetype: "",
344
- size: 10
345
- }), binary_1.BinaryNotFoundError);
285
+ await executor.execute(ctx);
286
+ let user = await ctx.getCurrentUser(true);
287
+ (_a = user.images[0]).metadata ?? (_a.metadata = {});
288
+ assert.strictEqual(user.images[0].metadata["my-metadata"], "updated");
289
+ assert.strictEqual(user.images[0].metadata["my-other-metadata"], true);
290
+ executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/users/${user1.getUuid()}/images/0/${user1.images[0].hash}`, {
291
+ "my-metadata": "updated".repeat(4096),
292
+ "my-other-metadata": true
346
293
  });
294
+ await assert.rejects(() => executor.execute(ctx), /400/, "PUT metadata with big content should fail");
347
295
  }
348
- setupDefault(withLogin = true) {
349
- return __awaiter(this, void 0, void 0, function* () {
350
- let userStore = this.getUserStore();
351
- let binary = this.getBinary();
352
- let user1 = yield userStore.save({
353
- test: "plop"
354
- });
355
- yield binary.store(user1, "images", new binary_1.LocalBinaryFile(this.getTestFile()));
356
- yield user1.refresh();
357
- let ctx = yield this.newContext();
358
- if (withLogin) {
359
- ctx.getSession().login(user1.getUuid(), "fake");
360
- }
361
- user1.setContext(ctx);
362
- return { userStore, binary, user1, ctx };
363
- });
296
+ async getNotFound() {
297
+ await assert.rejects(() =>
298
+ // @ts-ignore
299
+ this.getBinary().get({
300
+ hash: "none",
301
+ mimetype: "",
302
+ size: 10
303
+ }), BinaryNotFoundError);
364
304
  }
365
- testChallenge(remoteCheckHash = true) {
366
- return __awaiter(this, void 0, void 0, function* () {
367
- let { userStore, binary, user1, ctx } = yield this.setupDefault(false);
368
- let { hash, challenge } = yield new binary_1.MemoryBinaryFile(Buffer.from("PLOP"), {}).getHashes();
369
- let metadata = {
370
- plop: "test"
371
- };
372
- let executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user1.getUuid()}/images`, {
373
- hash,
374
- challenge,
375
- metadata
376
- });
377
- yield assert.rejects(() => executor.execute(ctx), /403/);
305
+ async setupDefault(withLogin = true) {
306
+ let userStore = this.getUserStore();
307
+ let binary = this.getBinary();
308
+ let user1 = await userStore.save({
309
+ test: "plop"
310
+ });
311
+ await binary.store(user1, "images", new LocalBinaryFile(this.getTestFile()));
312
+ await user1.refresh();
313
+ let ctx = await this.newContext();
314
+ if (withLogin) {
378
315
  ctx.getSession().login(user1.getUuid(), "fake");
379
- yield executor.execute(ctx);
380
- let info = JSON.parse(ctx.getResponseBody());
381
- assert.ok(!info.done, "should not be done yet");
382
- // Execute twice as we should still get an upload
383
- yield executor.execute(ctx);
384
- let url = new URL(info.url);
385
- if (url.host === "test.webda.io") {
386
- executor = this.getExecutor(ctx, "test.webda.io", info.method || "PUT", url.pathname + url.search, "PLOP2", {
387
- "Content-MD5": info.md5,
388
- "Content-Type": "application/octet-stream"
389
- });
390
- yield assert.rejects(() => executor.execute(ctx));
391
- executor = this.getExecutor(ctx, "test.webda.io", info.method || "PUT", url.pathname + url.search, "PLOP", {
392
- "Content-MD5": info.md5,
393
- "Content-Type": "application/octet-stream"
394
- });
395
- yield executor.execute(ctx);
396
- }
397
- else {
398
- if (remoteCheckHash) {
399
- yield assert.rejects(() => this.sendChallengeData(info, "PLOP2"), "Should check content");
400
- }
401
- yield this.sendChallengeData(info, "PLOP");
402
- }
403
- yield user1.refresh();
404
- assert.strictEqual(user1.images.length, 2);
405
- assert.strictEqual((yield __1.Binary.streamToBuffer(yield binary.get(user1.images[1]))).toString(), "PLOP");
406
- // If we try to re upload it should be already up
407
- executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user1.getUuid()}/images`, {
408
- hash,
409
- challenge,
410
- metadata
316
+ }
317
+ user1.setContext(ctx);
318
+ return { userStore, binary, user1, ctx };
319
+ }
320
+ async testChallenge(remoteCheckHash = true) {
321
+ let { userStore, binary, user1, ctx } = await this.setupDefault(false);
322
+ let { hash, challenge } = await new MemoryBinaryFile(Buffer.from("PLOP"), {}).getHashes();
323
+ let metadata = {
324
+ plop: "test"
325
+ };
326
+ let executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user1.getUuid()}/images`, {
327
+ hash,
328
+ challenge,
329
+ metadata
330
+ });
331
+ await assert.rejects(() => executor.execute(ctx), /403/);
332
+ ctx.getSession().login(user1.getUuid(), "fake");
333
+ await executor.execute(ctx);
334
+ let info = JSON.parse(ctx.getResponseBody());
335
+ assert.ok(!info.done, "should not be done yet");
336
+ // Execute twice as we should still get an upload
337
+ await executor.execute(ctx);
338
+ let url = new URL(info.url);
339
+ if (url.host === "test.webda.io") {
340
+ executor = this.getExecutor(ctx, "test.webda.io", info.method || "PUT", url.pathname + url.search, "PLOP2", {
341
+ "Content-MD5": info.md5,
342
+ "Content-Type": "application/octet-stream"
411
343
  });
412
- yield executor.execute(ctx);
413
- info = JSON.parse(ctx.getResponseBody());
414
- assert.ok(info.done, "should be done");
415
- assert.ok(info.url === undefined);
416
- // Use another user to try adding same binary
417
- let user2 = yield userStore.save({});
418
- ctx.getSession().login(user2.getUuid(), "fake");
419
- executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user2.getUuid()}/images`, {
420
- hash,
421
- challenge,
422
- metadata
344
+ await assert.rejects(() => executor.execute(ctx));
345
+ executor = this.getExecutor(ctx, "test.webda.io", info.method || "PUT", url.pathname + url.search, "PLOP", {
346
+ "Content-MD5": info.md5,
347
+ "Content-Type": "application/octet-stream"
423
348
  });
424
- yield executor.execute(ctx);
425
- info = JSON.parse(ctx.getResponseBody());
426
- assert.ok(info.done, "should be done");
427
- assert.ok(info.url === undefined);
349
+ await executor.execute(ctx);
350
+ }
351
+ else {
352
+ if (remoteCheckHash) {
353
+ await assert.rejects(() => this.sendChallengeData(info, "PLOP2"), "Should check content");
354
+ }
355
+ await this.sendChallengeData(info, "PLOP");
356
+ }
357
+ await user1.refresh();
358
+ assert.strictEqual(user1.images.length, 2);
359
+ assert.strictEqual((await Binary.streamToBuffer(await binary.get(user1.images[1]))).toString(), "PLOP");
360
+ // If we try to re upload it should be already up
361
+ executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user1.getUuid()}/images`, {
362
+ hash,
363
+ challenge,
364
+ metadata
428
365
  });
366
+ await executor.execute(ctx);
367
+ info = JSON.parse(ctx.getResponseBody());
368
+ assert.ok(info.done, "should be done");
369
+ assert.ok(info.url === undefined);
370
+ // Use another user to try adding same binary
371
+ let user2 = await userStore.save({});
372
+ ctx.getSession().login(user2.getUuid(), "fake");
373
+ executor = this.getExecutor(ctx, "test.webda.io", "PUT", `/binary/upload/users/${user2.getUuid()}/images`, {
374
+ hash,
375
+ challenge,
376
+ metadata
377
+ });
378
+ await executor.execute(ctx);
379
+ info = JSON.parse(ctx.getResponseBody());
380
+ assert.ok(info.done, "should be done");
381
+ assert.ok(info.url === undefined);
429
382
  }
430
- sendChallengeData(info, data) {
431
- return __awaiter(this, void 0, void 0, function* () {
432
- return axios_1.default.request({
433
- method: info.method,
434
- url: info.url,
435
- data,
436
- headers: {
437
- "Content-MD5": info.md5,
438
- "Content-Type": "application/octet-stream"
439
- }
440
- });
383
+ async sendChallengeData(info, data) {
384
+ return axios.request({
385
+ method: info.method,
386
+ url: info.url,
387
+ data,
388
+ headers: {
389
+ "Content-MD5": info.md5,
390
+ "Content-Type": "application/octet-stream"
391
+ }
441
392
  });
442
393
  }
443
394
  }
444
395
  __decorate([
445
- mocha_1.test
396
+ test
446
397
  ], BinaryTest.prototype, "normal", null);
447
398
  __decorate([
448
- mocha_1.test
399
+ test
449
400
  ], BinaryTest.prototype, "notMapped", null);
450
401
  __decorate([
451
- mocha_1.test
402
+ test
452
403
  ], BinaryTest.prototype, "update", null);
453
404
  __decorate([
454
- mocha_1.test
405
+ test
455
406
  ], BinaryTest.prototype, "checkMap", null);
456
407
  __decorate([
457
- mocha_1.test
408
+ test
458
409
  ], BinaryTest.prototype, "httpCreate", null);
459
410
  __decorate([
460
- mocha_1.test
411
+ test
461
412
  ], BinaryTest.prototype, "httpDelete", null);
462
413
  __decorate([
463
- mocha_1.test
414
+ test
464
415
  ], BinaryTest.prototype, "getOperationName", null);
465
416
  __decorate([
466
- mocha_1.test
417
+ test
467
418
  ], BinaryTest.prototype, "httpGetError", null);
468
419
  __decorate([
469
- mocha_1.test
420
+ test
470
421
  ], BinaryTest.prototype, "httpGet", null);
471
422
  __decorate([
472
- mocha_1.test
423
+ test
473
424
  ], BinaryTest.prototype, "httpMetadata", null);
474
425
  __decorate([
475
- mocha_1.test
426
+ test
476
427
  ], BinaryTest.prototype, "getNotFound", null);
477
- exports.BinaryTest = BinaryTest;
478
- let BinaryAbstractTest = class BinaryAbstractTest extends test_1.WebdaTest {
479
- cov() {
480
- return __awaiter(this, void 0, void 0, function* () {
481
- let service = new TestBinaryService(undefined, "plop", {});
482
- let ctx = yield this.newContext();
483
- yield assert.rejects(() => service.putRedirectUrl(undefined), /404/);
484
- ctx.getHttpContext().setBody({
485
- hash: "plop"
486
- });
487
- yield assert.rejects(() => service.httpChallenge(ctx), /400/);
488
- ctx = yield this.newContext();
489
- ctx.getHttpContext().setBody({
490
- challenge: "plop"
491
- });
492
- yield assert.rejects(() => service.httpChallenge(ctx), /400/);
493
- ctx = yield this.newContext();
494
- let binary = this.getService("binary");
495
- ctx.setPathParameters({
496
- property: "images",
497
- store: "users",
498
- uid: "notfound",
499
- index: 0
500
- });
501
- ctx.getHttpContext().setBody({
502
- hash: "p",
503
- challenge: "z"
504
- });
505
- yield assert.rejects(() => binary.httpChallenge(ctx), /404/);
506
- let stubs = [];
507
- try {
508
- let model = {};
509
- stubs.push(sinon.stub(binary, "_verifyMapAndStore").callsFake(() => {
510
- return {
511
- get: (uuid, ctx) => __awaiter(this, void 0, void 0, function* () {
512
- return model;
513
- })
514
- };
515
- }));
516
- yield assert.rejects(() => binary.httpRoute(ctx), /404/);
517
- model = {
518
- images: [
519
- {
520
- size: 10
521
- }
522
- ],
523
- canAct: () => __awaiter(this, void 0, void 0, function* () { return true; })
524
- };
525
- stubs.push(
526
- // @ts-ignore
527
- sinon.stub(binary, "get").callsFake(() => __awaiter(this, void 0, void 0, function* () {
528
- return {
529
- pipe: stream => {
530
- stream.emit("error", new Error("I/O"));
531
- }
532
- };
533
- })));
534
- yield assert.rejects(() => binary.httpRoute(ctx), /500/);
535
- }
536
- finally {
537
- stubs.forEach(stub => stub.restore());
538
- }
428
+ let BinaryAbstractTest = class BinaryAbstractTest extends WebdaTest {
429
+ async cov() {
430
+ let service = new TestBinaryService(undefined, "plop", {});
431
+ let ctx = await this.newContext();
432
+ await assert.rejects(() => service.putRedirectUrl(undefined), /404/);
433
+ ctx.getHttpContext().setBody({
434
+ hash: "plop"
539
435
  });
540
- }
541
- streamToBufferError() {
542
- return __awaiter(this, void 0, void 0, function* () {
543
- let stream = new events_1.EventEmitter();
544
- // @ts-ignore
545
- let p = assert.rejects(() => __1.Binary.streamToBuffer(stream), /Bad I\/O/);
546
- stream.emit("error", new Error("Bad I/O"));
547
- yield p;
436
+ await assert.rejects(() => service.httpChallenge(ctx), /400/);
437
+ ctx = await this.newContext();
438
+ ctx.getHttpContext().setBody({
439
+ challenge: "plop"
440
+ });
441
+ await assert.rejects(() => service.httpChallenge(ctx), /400/);
442
+ ctx = await this.newContext();
443
+ let binary = this.getService("binary");
444
+ ctx.setPathParameters({
445
+ property: "images",
446
+ store: "users",
447
+ uid: "notfound",
448
+ index: 0
548
449
  });
450
+ ctx.getHttpContext().setBody({
451
+ hash: "p",
452
+ challenge: "z"
453
+ });
454
+ await assert.rejects(() => binary.httpChallenge(ctx), /404/);
455
+ let stubs = [];
456
+ try {
457
+ let model = {};
458
+ stubs.push(sinon.stub(binary, "_verifyMapAndStore").callsFake(() => {
459
+ return {
460
+ get: async (uuid, ctx) => {
461
+ return model;
462
+ }
463
+ };
464
+ }));
465
+ await assert.rejects(() => binary.httpRoute(ctx), /404/);
466
+ model = {
467
+ images: [
468
+ {
469
+ size: 10
470
+ }
471
+ ],
472
+ canAct: async () => true
473
+ };
474
+ stubs.push(
475
+ // @ts-ignore
476
+ sinon.stub(binary, "get").callsFake(async () => {
477
+ return {
478
+ pipe: stream => {
479
+ stream.emit("error", new Error("I/O"));
480
+ }
481
+ };
482
+ }));
483
+ await assert.rejects(() => binary.httpRoute(ctx), /500/);
484
+ }
485
+ finally {
486
+ stubs.forEach(stub => stub.restore());
487
+ }
488
+ }
489
+ async streamToBufferError() {
490
+ let stream = new EventEmitter();
491
+ // @ts-ignore
492
+ let p = assert.rejects(() => Binary.streamToBuffer(stream), /Bad I\/O/);
493
+ stream.emit("error", new Error("Bad I/O"));
494
+ await p;
549
495
  }
550
496
  };
551
497
  __decorate([
552
- mocha_1.test
498
+ test
553
499
  ], BinaryAbstractTest.prototype, "cov", null);
554
500
  __decorate([
555
- mocha_1.test
501
+ test
556
502
  ], BinaryAbstractTest.prototype, "streamToBufferError", null);
557
503
  BinaryAbstractTest = __decorate([
558
- mocha_1.suite
504
+ suite
559
505
  ], BinaryAbstractTest);
506
+ export { BinaryTest };
560
507
  //# sourceMappingURL=binary.spec.js.map