s3storage.tests.js 29.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global module, test, stop, start, expect, ok, deepEqual, location, sinon,
  davstorage_spec, RSVP, jIO, test_util, dav_storage, btoa */

(function () {
  "use strict";

  var spec, use_fake_server = true;
  if (typeof s3storage_spec === 'object') {
    use_fake_server = false;
    spec = s3storage_spec;
    console.log(spec);
  } else {
    spec = dav_storage.createDescription(
      "http://localhost",
      "none"
    );
  }

  module("S3 Storage");

  function success(promise) {
    return new RSVP.Promise(function (resolve, reject, notify) {
      /*jslint unparam: true*/
      promise.then(resolve, resolve, notify);
    }, function () {
      promise.cancel();
    });
  }

  test("Scenario", function () {

    var server, responses = [], shared = {}, jio = jIO.createJIO(spec, {
      "workspace": {},
      "max_retry": 2
    });

    stop();

    if (use_fake_server) {
      /*jslint regexp: true */
      server = sinon.fakeServer.create();
      server.autoRespond = true;
      server.autoRespondAfter = 5;
      server.respondWith(/.*/, function (xhr) {
        var response = responses.shift();
        if (response) {
          return xhr.respond.apply(xhr, response);
        }
        ok(false, "No response associated to the latest request!");
      });
    } else {
      responses.push = function () {
        return;
      };
      server = {restore: function () {
        return;
      }};
    }

    function postNewDocument() {
      responses.push([404, {}, '']); // GET
      responses.push([201, {}, '']); // PUT
      return jio.post({"title": "Unique ID"});
    }

    function postNewDocumentTest(answer) {
      var uuid = answer.id;
      answer.id = "<uuid>";
      deepEqual(answer, {
        "id": "<uuid>",
        "method": "post",
        "result": "success",
74 75
        "status": 204,
        "statusText": "No Content"
76
      }, "Post a new document");
77 78
      ok(/^no_document_id_[0-9]+$/.test(uuid), "New document id should look like " +
         "no_document_id_479658600408584 : " + uuid);
79
      shared.created_document_id = uuid;
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    }

    function getCreatedDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": shared.created_document_id,
        "title": "Unique ID"
      })]); // GET
      return jio.get({"_id": shared.created_document_id});
    }

    function getCreatedDocumentTest(answer) {
      deepEqual(answer, {
        "data": {
          "_id": shared.created_document_id,
          "title": "Unique ID"
        },
        "id": shared.created_document_id,
        "method": "get",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get new document");
    }

    function postSpecificDocument() {
      responses.push([404, {}, '']); // GET
      responses.push([201, {}, '']); // PUT
      return jio.post({"_id": "b", "title": "Bee"});
    }

    function postSpecificDocumentTest(answer) {
      deepEqual(answer, {
        "id": "b",
        "method": "post",
        "result": "success",
118 119
        "status": 204,
        "statusText": "No Content"
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
      }, "Post specific document");
    }

    function listDocuments() {
      responses.push([
        207,
        {"Content-Type": "text/xml"},
        '<?xml version="1.0" encoding="utf-8"?>' +
          '<D:multistatus xmlns:D="DAV:">' +
          '<D:response xmlns:lp2="http://apache.' +
          'org/dav/props/" xmlns:lp1="DAV:">' +
          '<D:href>/uploads/</D:href>' +
          '<D:propstat>' +
          '<D:prop>' +
          '<lp1:resourcetype><D:collection/></lp1:resourcetype>' +
          '<lp1:creationdate>2013-09-19T11:54:43Z</lp1:creationdate>' +
          '<lp1:getlastmodified>Thu, 19 Sep 2013 11:54:43 GMT' +
          '</lp1:getlastmodified>' +
          '<lp1:getetag>"240be-1000-4e6bb383e5fbb"</lp1:getetag>' +
          '<D:supportedlock>' +
          '<D:lockentry>' +
          '<D:lockscope><D:exclusive/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '<D:lockentry>' +
          '<D:lockscope><D:shared/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '</D:supportedlock>' +
          '<D:lockdiscovery/>' +
          '<D:getcontenttype>httpd/unix-directory</D:getcontenttype>' +
          '</D:prop>' +
          '<D:status>HTTP/1.1 200 OK</D:status>' +
          '</D:propstat>' +
          '</D:response>' +
          '<D:response xmlns:lp2="http://apache.org/dav/props/" ' +
          'xmlns:lp1="DAV:">' +
          '<D:href>/uploads/' + shared.created_document_id + '</D:href>' +
          '<D:propstat>' +
          '<D:prop>' +
          '<lp1:resourcetype/>' +
          '<lp1:creationdate>2013-09-19T11:54:43Z</lp1:creationdate>' +
          '<lp1:getcontentlength>66</lp1:getcontentlength>' +
          '<lp1:getlastmodified>Thu, 19 Sep 2013 11:54:43 GMT' +
          '</lp1:getlastmodified>' +
          '<lp1:getetag>"20529-42-4e6bb383d0d30"</lp1:getetag>' +
          '<lp2:executable>F</lp2:executable>' +
          '<D:supportedlock>' +
          '<D:lockentry>' +
          '<D:lockscope><D:exclusive/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '<D:lockentry>' +
          '<D:lockscope><D:shared/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '</D:supportedlock>' +
          '<D:lockdiscovery/>' +
          '</D:prop>' +
          '<D:status>HTTP/1.1 200 OK</D:status>' +
          '</D:propstat>' +
          '</D:response>' +
          '<D:response xmlns:lp2="http://apache.org/dav/props/" ' +
          'xmlns:lp1="DAV:">' +
          '<D:href>/uploads/b</D:href>' +
          '<D:propstat>' +
          '<D:prop>' +
          '<lp1:resourcetype/>' +
          '<lp1:creationdate>2013-09-19T11:54:43Z</lp1:creationdate>' +
          '<lp1:getcontentlength>25</lp1:getcontentlength>' +
          '<lp1:getlastmodified>Thu, 19 Sep 2013 11:54:43 GMT' +
          '</lp1:getlastmodified>' +
          '<lp1:getetag>"20da3-19-4e6bb383e5fbb"</lp1:getetag>' +
          '<lp2:executable>F</lp2:executable>' +
          '<D:supportedlock>' +
          '<D:lockentry>' +
          '<D:lockscope><D:exclusive/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '<D:lockentry>' +
          '<D:lockscope><D:shared/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '</D:supportedlock>' +
          '<D:lockdiscovery/>' +
          '</D:prop>' +
          '<D:status>HTTP/1.1 200 OK</D:status>' +
          '</D:propstat>' +
          '</D:response>' +
          '</D:multistatus>'
      ]); // PROPFIND
      return jio.allDocs();
    }

    function list2DocumentsTest(answer) {
      if (answer && answer.data && Array.isArray(answer.data.rows)) {
        answer.data.rows.sort(function (a) {
          return a.id === "b" ? 1 : 0;
        });
      }
      deepEqual(answer, {
        "data": {
          "total_rows": 2,
          "rows": [{
            "id": shared.created_document_id,
            "value": {}
          }, {
            "id": "b",
            "value": {}
          }]
        },
        "method": "allDocs",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "List 2 documents");
    }

    function removeCreatedDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": shared.created_document_id,
        "title": "Unique ID"
      })]); // GET
      responses.push([204, {}, '']); // DELETE
      return jio.remove({"_id": shared.created_document_id});
    }

    function removeCreatedDocumentTest(answer) {
      deepEqual(answer, {
        "id": shared.created_document_id,
        "method": "remove",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Remove first document.");
    }

    function removeSpecificDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "b",
        "title": "Bee"
      })]); // GET
      responses.push([204, {}, '']); // DELETE
      return jio.remove({"_id": "b"});
    }

    function removeSpecificDocumentTest(answer) {
      deepEqual(answer, {
        "id": "b",
        "method": "remove",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Remove second document.");
    }

    function listEmptyStorage() {
      responses.push([
        207,
        {"Content-Type": "text/xml"},
        '<?xml version="1.0" encoding="utf-8"?>' +
          '<D:multistatus xmlns:D="DAV:">' +
          '<D:response xmlns:lp2="http://apache.org/dav/props/" ' +
          'xmlns:lp1="DAV:">' +
          '<D:href>/uploads/</D:href>' +
          '<D:propstat>' +
          '<D:prop>' +
          '<lp1:resourcetype><D:collection/></lp1:resourcetype>' +
          '<lp1:creationdate>2013-09-19T11:54:43Z</lp1:creationdate>' +
          '<lp1:getlastmodified>Thu, 19 Sep 2013 11:54:43 GMT' +
          '</lp1:getlastmodified>' +
          '<lp1:getetag>"240be-1000-4e6bb3840a9ac"</lp1:getetag>' +
          '<D:supportedlock>' +
          '<D:lockentry>' +
          '<D:lockscope><D:exclusive/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '<D:lockentry>' +
          '<D:lockscope><D:shared/></D:lockscope>' +
          '<D:locktype><D:write/></D:locktype>' +
          '</D:lockentry>' +
          '</D:supportedlock>' +
          '<D:lockdiscovery/>' +
          '<D:getcontenttype>httpd/unix-directory</D:getcontenttype>' +
          '</D:prop>' +
          '<D:status>HTTP/1.1 200 OK</D:status>' +
          '</D:propstat>' +
          '</D:response>' +
          '</D:multistatus>'
      ]); // PROPFIND
      return jio.allDocs();
    }

    function listEmptyStorageTest(answer) {
      deepEqual(answer, {
        "data": {
          "total_rows": 0,
          "rows": []
        },
        "method": "allDocs",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "List empty storage");
    }

    function putNewDocument() {
      responses.push([404, {}, '']); // GET
      responses.push([200, {}, '']); // PUT
      return jio.put({"_id": "a", "title": "Hey"});
    }

    function putNewDocumentTest(answer) {
      deepEqual(answer, {
        "id": "a",
        "method": "put",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Put new document");
    }

    function getCreatedDocument2() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey"
      })]); // GET
      return jio.get({"_id": "a"});
    }

    function getCreatedDocument2Test(answer) {
      deepEqual(answer, {
        "data": {
          "_id": "a",
          "title": "Hey"
        },
        "id": "a",
        "method": "get",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get new document");
    }

    function postSameDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey"
      })]); // GET
      return success(jio.post({"_id": "a", "title": "Hoo"}));
    }

    function postSameDocumentTest(answer) {
      deepEqual(answer, {
        "error": "conflict",
        "id": "a",
384
        "message": "Cannot create document",
385
        "method": "post",
386
        "reason": "Document already exists",
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        "result": "error",
        "status": 409,
        "statusText": "Conflict"
      }, "Unable to post the same document (conflict)");
    }

    function createAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey"
      })]); // GET
      responses.push([201, {}, '']); // PUT (attachment)
      responses.push([204, {}, '']); // PUT (metadata)
      return jio.putAttachment({
        "_id": "a",
        "_attachment": "aa",
        "_data": "aaa",
        "_content_type": "text/plain"
      });
    }

    function createAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "aa",
413 414
        "digest":"sha256-9834876dcfb05cb167a5c24953eba58c4"+
        "ac89b1adf57f28f2f9d09af107ee8f0",
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        "id": "a",
        "method": "putAttachment",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Create new attachment");
    }

    function updateAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
432 433
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            "length": 3
          }
        }
      })]); // GET
      responses.push([204, {}, '']); // PUT (attachment)
      responses.push([204, {}, '']); // PUT (metadata)
      return jio.putAttachment({
        "_id": "a",
        "_attachment": "aa",
        "_data": "aab",
        "_content_type": "text/plain"
      });
    }

    function updateAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "aa",
451 452
        "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
        "0728e095ff24218119d51bd22475363",
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
        "id": "a",
        "method": "putAttachment",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Update last attachment");
    }

    function createAnotherAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
470 471
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
        "0728e095ff24218119d51bd22475363",
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
            "length": 3
          }
        }
      })]); // GET
      responses.push([201, {}, '']); // PUT (attachment)
      responses.push([204, {}, '']); // PUT (metadata)
      return jio.putAttachment({
        "_id": "a",
        "_attachment": "ab",
        "_data": "aba",
        "_content_type": "text/plain"
      });
    }

    function createAnotherAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "ab",
        "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
          "ed343e6c739e54131fcb3a56e4bc1bd",
        "id": "a",
        "method": "putAttachment",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Create another attachment");
    }

    function updateLastDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hey",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
508 509
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
            "length": 3
          },
          "ab": {
            "content_type": "text/plain",
            "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
              "ed343e6c739e54131fcb3a56e4bc1bd",
            "length": 3
          }
        }
      })]); // GET
      responses.push([204, {}, '']); // PUT
      return jio.put({"_id": "a", "title": "Hoo"});
    }

    function updateLastDocumentTest(answer) {
      deepEqual(answer, {
        "id": "a",
        "method": "put",
        "result": "success",
529 530
        "status": 200,
        "statusText": "Ok"
531 532 533 534 535 536 537 538 539 540 541 542
      }, "Update document metadata");
    }

    function getFirstAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
543 544
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
            "length": 3
          },
          "ab": {
            "content_type": "text/plain",
            "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
              "ed343e6c739e54131fcb3a56e4bc1bd",
            "length": 3
          }
        }
      })]); // GET
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, "aab"]); // GET
      return jio.getAttachment({"_id": "a", "_attachment": "aa"});
    }

    function getFirstAttachmentTest(answer) {
      var blob = answer.data;
      answer.data = "<blob>";
      deepEqual(answer, {
        "attachment": "aa",
        "data": "<blob>",
567 568
        "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
        "0728e095ff24218119d51bd22475363",
569 570 571 572 573 574 575 576
        "id": "a",
        "method": "getAttachment",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get first attachment");
      return jIO.util.readBlobAsText(blob).then(function (e) {
        deepEqual(blob.type, "text/plain", "Check blob type");
577
        deepEqual(e.target.result, "aab", "Check blob text content");
578 579 580 581 582 583 584 585 586 587 588 589 590 591
      }, function (err) {
        deepEqual(err, "no error", "Check blob text content");
      });
    }

    function getSecondAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
592 593
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
            "length": 3
          },
          "ab": {
            "content_type": "text/plain",
            "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
              "ed343e6c739e54131fcb3a56e4bc1bd",
            "length": 3
          }
        }
      })]); // GET
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, "aba"]); // GET
      return jio.getAttachment({"_id": "a", "_attachment": "ab"});
    }

    function getSecondAttachmentTest(answer) {
      var blob = answer.data;
      answer.data = "<blob>";
      deepEqual(answer, {
        "attachment": "ab",
        "data": "<blob>",
        "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
          "ed343e6c739e54131fcb3a56e4bc1bd",
        "id": "a",
        "method": "getAttachment",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get first attachment");
      return jIO.util.readBlobAsText(blob).then(function (e) {
        deepEqual(blob.type, "text/plain", "Check blob type");
        deepEqual(e.target.result, "aba", "Check blob text content");
      }, function (err) {
        deepEqual(err, "no error", "Check blob text content");
      });
    }

    function getLastDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
641 642
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
            "length": 3
          },
          "ab": {
            "content_type": "text/plain",
            "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
              "ed343e6c739e54131fcb3a56e4bc1bd",
            "length": 3
          }
        }
      })]); // GET
      return jio.get({"_id": "a"});
    }

    function getLastDocumentTest(answer) {
      deepEqual(answer, {
        "data": {
          "_id": "a",
          "title": "Hoo",
          "_attachments": {
            "aa": {
              "content_type": "text/plain",
664 665
              "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
              "0728e095ff24218119d51bd22475363",
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
              "length": 3
            },
            "ab": {
              "content_type": "text/plain",
              "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
                "ed343e6c739e54131fcb3a56e4bc1bd",
              "length": 3
            }
          }
        },
        "id": "a",
        "method": "get",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get last document metadata");
    }

    function removeSecondAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
693 694
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
            "length": 3
          },
          "ab": {
            "content_type": "text/plain",
            "digest": "sha256-e124adcce1fb2f88e1ea799c3d0820845" +
              "ed343e6c739e54131fcb3a56e4bc1bd",
            "length": 3
          }
        }
      })]); // GET
      responses.push([204, {}, '']); // PUT
      responses.push([204, {}, '']); // DELETE
      return jio.removeAttachment({"_id": "a", "_attachment": "ab"});
    }

    function removeSecondAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "ab",
        "id": "a",
        "method": "removeAttachment",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Remove second document");
    }

    function getInexistentSecondAttachment() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
730 731
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5"+
            "0728e095ff24218119d51bd22475363",
732 733 734 735 736 737 738 739 740 741 742 743
            "length": 3
          }
        }
      })]); // GET
      return success(jio.getAttachment({"_id": "a", "_attachment": "ab"}));
    }

    function getInexistentSecondAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "ab",
        "error": "not_found",
        "id": "a",
744
        "message": "File does not exist",
745
        "method": "getAttachment",
746
        "reason": "Not Found",
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
        "result": "error",
        "status": 404,
        "statusText": "Not Found"
      }, "Get inexistent second attachment");
    }

    function getOneAttachmentDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
              "0728e095ff24218119d51bd22475363",
            "length": 3
          }
        }
      })]); // GET
      return jio.get({"_id": "a"});
    }

    function getOneAttachmentDocumentTest(answer) {
      deepEqual(answer, {
        "data": {
          "_attachments": {
            "aa": {
              "content_type": "text/plain",
              "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
                "0728e095ff24218119d51bd22475363",
              "length": 3
            }
          },
          "_id": "a",
          "title": "Hoo"
        },
        "id": "a",
        "method": "get",
        "result": "success",
        "status": 200,
        "statusText": "Ok"
      }, "Get document metadata");
    }

    function removeSecondAttachmentAgain() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
              "0728e095ff24218119d51bd22475363",
            "length": 3
          }
        }
      })]); // GET
      return success(jio.removeAttachment({"_id": "a", "_attachment": "ab"}));
    }

    function removeSecondAttachmentAgainTest(answer) {
      deepEqual(answer, {
        "attachment": "ab",
        "error": "not_found",
        "id": "a",
816
        "message": "This Attachment does not exist",
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
        "method": "removeAttachment",
        "reason": "missing attachment",
        "result": "error",
        "status": 404,
        "statusText": "Not Found"
      }, "Remove inexistent attachment");
    }

    function removeDocument() {
      responses.push([200, {
        "Content-Type": "application/octet-stream"
      }, JSON.stringify({
        "_id": "a",
        "title": "Hoo",
        "_attachments": {
          "aa": {
            "content_type": "text/plain",
            "digest": "sha256-38760eabb666e8e61ee628a17c4090cc5" +
              "0728e095ff24218119d51bd22475363",
            "length": 3
          }
        }
      })]); // GET
      responses.push([204, {}, '']); // DELETE (metadata)
      responses.push([204, {}, '']); // DELETE (attachment aa)
      return jio.remove({"_id": "a"});
    }

    function removeDocumentTest(answer) {
      deepEqual(answer, {
        "id": "a",
        "method": "remove",
        "result": "success",
        "status": 204,
        "statusText": "No Content"
      }, "Remove document and its attachments");
    }

    function getInexistentFirstAttachment() {
      responses.push([404, {}, '']); // GET
      return success(jio.getAttachment({"_id": "a", "_attachment": "aa"}));
    }

    function getInexistentFirstAttachmentTest(answer) {
      deepEqual(answer, {
        "attachment": "aa",
        "error": "not_found",
        "id": "a",
865
        "message": "File does not exist",
866
        "method": "getAttachment",
867
        "reason": "Not Found",
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
        "result": "error",
        "status": 404,
        "statusText": "Not Found"
      }, "Get inexistent first attachment");
    }

    function getInexistentDocument() {
      responses.push([404, {}, '']); // GET
      return success(jio.get({"_id": "a"}));
    }

    function getInexistentDocumentTest(answer) {
      deepEqual(answer, {
        "error": "not_found",
        "id": "a",
883
        "message": "File does not exist",
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
        "method": "get",
        "reason": "Not Found",
        "result": "error",
        "status": 404,
        "statusText": "Not Found"
      }, "Get inexistent document");
    }

    function removeInexistentDocument() {
      responses.push([404, {}, '']); // GET
      return success(jio.remove({"_id": "a"}));
    }

    function removeInexistentDocumentTest(answer) {
      deepEqual(answer, {
        "error": "not_found",
        "id": "a",
901
        "message": "File does not exist",
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
        "method": "remove",
        "reason": "Not Found",
        "result": "error",
        "status": 404,
        "statusText": "Not Found"
      }, "Remove already removed document");
    }

    function unexpectedError(error) {
      if (error instanceof Error) {
        deepEqual([
          error.name + ": " + error.message,
          error
        ], "UNEXPECTED ERROR", "Unexpected error");
      } else {
        deepEqual(error, "UNEXPECTED ERROR", "Unexpected error");
      }
    }

    // # Post new documents, list them and remove them
    // post a 201
923
      postNewDocument().then(postNewDocumentTest).
924
      // get 200
925
      then(getCreatedDocument).then(getCreatedDocumentTest).
926
      // post b 201
927
      then(postSpecificDocument).then(postSpecificDocumentTest).
928
      // allD 200 2 documents
929
      then(listDocuments).then(list2DocumentsTest).
930
      // remove a 204
931
      then(removeCreatedDocument).then(removeCreatedDocumentTest).
932
      // remove b 204
933
      then(removeSpecificDocument).then(removeSpecificDocumentTest).
934
      // allD 200 empty storage
935
      then(listEmptyStorage).then(listEmptyStorageTest).
936 937
      // # Create and update documents, and some attachment and remove them
      // put 201
938
      then(putNewDocument).then(putNewDocumentTest).
939
      // get 200
Jonathan Rivalan's avatar
Jonathan Rivalan committed
940
      then(getCreatedDocument2).then(getCreatedDocument2Test).
941
      // post 409
942
      then(postSameDocument).then(postSameDocumentTest).
943
      // putA a 204
944
      then(createAttachment).then(createAttachmentTest).
945
      // putA a 204
946
      then(updateAttachment).then(updateAttachmentTest).
947
      // putA b 204
948
      then(createAnotherAttachment).then(createAnotherAttachmentTest).
949
      // put 204
950
      then(updateLastDocument).then(updateLastDocumentTest).
951
      // getA a 200
952
      then(getFirstAttachment).then(getFirstAttachmentTest).
953
      // getA b 200
954
      then(getSecondAttachment).then(getSecondAttachmentTest).
955
      // get 200
956
      then(getLastDocument).then(getLastDocumentTest).
957
      // removeA b 204
958
      then(removeSecondAttachment).then(removeSecondAttachmentTest).
959
      // getA b 404
960 961
      then(getInexistentSecondAttachment).
      then(getInexistentSecondAttachmentTest).
962
      // get 200
963
      then(getOneAttachmentDocument).then(getOneAttachmentDocumentTest).
964
      // removeA b 404
965
      then(removeSecondAttachmentAgain).then(removeSecondAttachmentAgainTest).
966
      // remove 204
967
      then(removeDocument).then(removeDocumentTest).
968
      // getA a 404
969
      then(getInexistentFirstAttachment).then(getInexistentFirstAttachmentTest).
970
      // get 404
971
      then(getInexistentDocument).then(getInexistentDocumentTest).
972
      // remove 404
973
      then(removeInexistentDocument).then(removeInexistentDocumentTest).
974 975 976 977 978 979 980 981 982 983
      // check 204
      //then(checkDocument).done(checkDocumentTest).
      //then(checkStorage).done(checkStorageTest).
      fail(unexpectedError).
      always(start).
      always(server.restore.bind(server));

  });

}());