replicatestorage.tests.js 20.3 KB
Newer Older
1
/*jslint nomen: true*/
2 3
/*global Blob*/
(function (jIO, QUnit, Blob) {
4
  "use strict";
5 6 7 8 9 10 11 12
  var test = QUnit.test,
    stop = QUnit.stop,
    start = QUnit.start,
    ok = QUnit.ok,
    expect = QUnit.expect,
    deepEqual = QUnit.deepEqual,
    equal = QUnit.equal,
    module = QUnit.module,
13 14 15 16 17 18 19
    throws = QUnit.throws,
    big_string = "",
    j;

  for (j = 0; j < 30; j += 1) {
    big_string += "a";
  }
20 21 22 23 24 25

  /////////////////////////////////////////////////////////////////
  // Custom test substorage definition
  /////////////////////////////////////////////////////////////////
  function Storage200() {
    return this;
26
  }
27
  jIO.addStorage('replicatestorage200', Storage200);
28

29 30
  function Storage500() {
    return this;
31
  }
32 33
  jIO.addStorage('replicatestorage500', Storage500);

34 35 36 37 38
  function Storage2713() {
    return this;
  }
  jIO.addStorage('signaturestorage2713', Storage2713);

39 40 41 42 43 44 45 46 47 48 49 50 51 52
  /////////////////////////////////////////////////////////////////
  // replicateStorage.constructor
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.constructor");
  test("create substorage", function () {
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
53

54 55 56 57
    ok(jio.__storage._local_sub_storage instanceof jio.constructor);
    equal(jio.__storage._local_sub_storage.__type, "replicatestorage200");
    ok(jio.__storage._remote_sub_storage instanceof jio.constructor);
    equal(jio.__storage._remote_sub_storage.__type, "replicatestorage500");
58

59
    deepEqual(jio.__storage._query_options, {});
60
    equal(jio.__storage._use_remote_post, false);
61
    equal(jio.__storage._conflict_handling, 0);
62 63
    equal(jio.__storage._parallel_operation_attachment_amount, 1);
    equal(jio.__storage._parallel_operation_amount, 1);
64 65 66 67 68 69
    equal(jio.__storage._check_local_creation, true);
    equal(jio.__storage._check_local_deletion, true);
    equal(jio.__storage._check_local_modification, true);
    equal(jio.__storage._check_remote_creation, true);
    equal(jio.__storage._check_remote_deletion, true);
    equal(jio.__storage._check_remote_modification, true);
70 71 72 73 74 75
    equal(jio.__storage._check_local_attachment_creation, false);
    equal(jio.__storage._check_local_attachment_deletion, false);
    equal(jio.__storage._check_local_attachment_modification, false);
    equal(jio.__storage._check_remote_attachment_creation, false);
    equal(jio.__storage._check_remote_attachment_deletion, false);
    equal(jio.__storage._check_remote_attachment_modification, false);
76

77
    equal(jio.__storage._custom_signature_sub_storage, false);
78
    equal(jio.__storage._signature_hash,
79
          "_replicate_7209dfbcaff00f6637f939fdd71fa896793ed385");
80

81
    ok(jio.__storage._signature_sub_storage instanceof jio.constructor);
82
    equal(jio.__storage._signature_sub_storage.__type, "query");
83

84 85 86 87 88 89 90 91
    ok(jio.__storage._signature_sub_storage
          .__storage._sub_storage instanceof jio.constructor);
    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage.__type, "document");

    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage
             .__storage._document_id,
92
          jio.__storage._signature_hash);
93

94 95 96
    ok(jio.__storage._signature_sub_storage
          .__storage._sub_storage
          .__storage._sub_storage
97
       instanceof jio.constructor);
98 99 100
    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage
             .__storage._sub_storage.__type,
101
          "replicatestorage200");
102

103
  });
104

105
  test("accept parameters", function () {
106 107 108 109 110 111 112 113
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      },
114
      query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]},
115
      use_remote_post: true,
116
      conflict_handling: 3,
117 118
      parallel_operation_attachment_amount: 2713,
      parallel_operation_amount: 2711,
119 120 121 122 123
      check_local_creation: false,
      check_local_deletion: false,
      check_local_modification: false,
      check_remote_creation: false,
      check_remote_deletion: false,
124 125 126 127 128 129 130
      check_remote_modification: false,
      check_local_attachment_creation: true,
      check_local_attachment_deletion: true,
      check_local_attachment_modification: true,
      check_remote_attachment_creation: true,
      check_remote_attachment_deletion: true,
      check_remote_attachment_modification: true
131 132 133 134 135 136
    });

    deepEqual(
      jio.__storage._query_options,
      {query: 'portal_type: "Foo"', limit: [0, 1234567890]}
    );
137
    equal(jio.__storage._use_remote_post, true);
138
    equal(jio.__storage._conflict_handling, 3);
139 140
    equal(jio.__storage._parallel_operation_attachment_amount, 2713);
    equal(jio.__storage._parallel_operation_amount, 2711);
141 142 143 144 145 146
    equal(jio.__storage._check_local_creation, false);
    equal(jio.__storage._check_local_deletion, false);
    equal(jio.__storage._check_local_modification, false);
    equal(jio.__storage._check_remote_creation, false);
    equal(jio.__storage._check_remote_deletion, false);
    equal(jio.__storage._check_remote_modification, false);
147 148 149 150 151 152
    equal(jio.__storage._check_local_attachment_creation, true);
    equal(jio.__storage._check_local_attachment_deletion, true);
    equal(jio.__storage._check_local_attachment_modification, true);
    equal(jio.__storage._check_remote_attachment_creation, true);
    equal(jio.__storage._check_remote_attachment_deletion, true);
    equal(jio.__storage._check_remote_attachment_modification, true);
153

154
    equal(jio.__storage._custom_signature_sub_storage, false);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    ok(jio.__storage._signature_sub_storage instanceof jio.constructor);
    equal(jio.__storage._signature_sub_storage.__type, "query");

    ok(jio.__storage._signature_sub_storage
          .__storage._sub_storage instanceof jio.constructor);
    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage.__type, "document");

    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage
             .__storage._document_id,
          jio.__storage._signature_hash);

    ok(jio.__storage._signature_sub_storage
          .__storage._sub_storage
          .__storage._sub_storage
       instanceof jio.constructor);
    equal(jio.__storage._signature_sub_storage
             .__storage._sub_storage
             .__storage._sub_storage.__type,
175
          "replicatestorage200");
176

177
    equal(jio.__storage._signature_hash,
178
          "_replicate_11881e431308c0ec8c0e6430be98db380e1b92f8");
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
  test("reject unknow conflict resolution", function () {
    throws(
      function () {
        jIO.createJIO({
          type: "replicate",
          local_sub_storage: {
            type: "replicatestorage200"
          },
          remote_sub_storage: {
            type: "replicatestorage500"
          },
          query: {query: 'portal_type: "Foo"', limit: [0, 1234567890]},
          conflict_handling: 4
        });
      },
      function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.status_code, 400);
        equal(error.message,
              "Unsupported conflict handling: 4");
        return true;
      }
    );
  });

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
  test("signature storage database", function () {

    var jio = jIO.createJIO({
      type: "replicate",
      signature_sub_storage: {
        type: "memory"
      },
      local_sub_storage: {
        type: "uuid",
        sub_storage: {
          type: "memory"
        }
      },
      remote_sub_storage: {
        type: "uuid",
        sub_storage: {
          type: "memory"
        }
      }
    });

    equal(jio.__storage._custom_signature_sub_storage, true);
    ok(jio.__storage._signature_sub_storage instanceof jio.constructor);
    equal(jio.__storage._signature_sub_storage.__type, "memory");

    ok(!jio.__storage.hasOwnProperty('_signature_hash'),
       jio.__storage._signature_hash);
  });


236 237 238 239 240 241 242
  /////////////////////////////////////////////////////////////////
  // replicateStorage.get
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.get");
  test("get called substorage get", function () {
    stop();
    expect(2);
243

244 245 246 247 248 249 250 251 252
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
253

254 255 256
    Storage200.prototype.get = function (id) {
      equal(id, "bar", "get 200 called");
      return {title: "foo"};
257 258
    };

259 260 261 262 263 264 265 266 267 268 269 270 271
    jio.get("bar")
      .then(function (result) {
        deepEqual(result, {
          "title": "foo"
        }, "Check document");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });
272

273 274 275 276 277
  /////////////////////////////////////////////////////////////////
  // replicateStorage.post
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.post");
  test("post called substorage post", function () {
278
    stop();
279
    expect(2);
280

281 282 283 284 285 286 287 288 289
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
290

291 292 293 294
    Storage200.prototype.post = function (param) {
      deepEqual(param, {title: "bar"}, "post 200 called");
      return "foo";
    };
295

296 297 298 299 300 301 302 303 304 305 306
    jio.post({title: "bar"})
      .then(function (result) {
        equal(result, "foo", "Check id");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321
  /////////////////////////////////////////////////////////////////
  // replicateStorage.hasCapacity
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.hasCapacity");
  test("hasCapacity return substorage value", function () {
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
322

323
    delete Storage200.prototype.hasCapacity;
324

325 326 327 328 329 330 331 332 333 334
    throws(
      function () {
        jio.hasCapacity("foo");
      },
      function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.status_code, 501);
        equal(error.message,
              "Capacity 'foo' is not implemented on 'replicatestorage200'");
        return true;
Tristan Cavelier's avatar
Tristan Cavelier committed
335
      }
336 337
    );
  });
338

339 340 341 342
  /////////////////////////////////////////////////////////////////
  // replicateStorage.buildQuery
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.buildQuery");
343

344 345 346
  test("buildQuery return substorage buildQuery", function () {
    stop();
    expect(2);
347

348 349 350 351 352 353 354
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
Tristan Cavelier's avatar
Tristan Cavelier committed
355
      }
356
    });
357

358 359 360
    Storage200.prototype.hasCapacity = function () {
      return true;
    };
361

362 363 364 365 366 367 368 369 370 371
    Storage200.prototype.buildQuery = function (options) {
      deepEqual(options, {
        include_docs: false,
        sort_on: [["title", "ascending"]],
        limit: [5],
        select_list: ["title", "id"],
        replicate: 'title: "two"'
      }, "allDocs parameter");
      return "bar";
    };
372

373 374 375 376 377 378 379 380 381 382 383 384 385
    jio.allDocs({
      include_docs: false,
      sort_on: [["title", "ascending"]],
      limit: [5],
      select_list: ["title", "id"],
      replicate: 'title: "two"'
    })
      .then(function (result) {
        deepEqual(result, {
          data: {
            rows: "bar",
            total_rows: 3
          }
386
        });
387 388 389 390 391 392 393
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
394 395
  });

396 397 398 399 400 401 402
  /////////////////////////////////////////////////////////////////
  // replicateStorage.put
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.put");
  test("put called substorage put", function () {
    stop();
    expect(3);
403

404 405 406 407
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
408
      },
409 410 411 412 413 414 415 416
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
    Storage200.prototype.put = function (id, param) {
      equal(id, "bar", "put 200 called");
      deepEqual(param, {"title": "foo"}, "put 200 called");
      return id;
417 418
    };

419 420 421 422 423 424 425 426 427 428 429
    jio.put("bar", {"title": "foo"})
      .then(function (result) {
        equal(result, "bar");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });
430

431 432 433
  test("put can not modify the signature", function () {
    stop();
    expect(3);
434

435 436 437 438 439 440 441 442
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
443
    });
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    delete Storage200.prototype.put;

    jio.put(jio.__storage._signature_hash, {"title": "foo"})
      .then(function () {
        ok(false);
      })
      .fail(function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.message, jio.__storage._signature_hash + " is frozen");
        equal(error.status_code, 403);
      })
      .always(function () {
        start();
      });
  });
459

460 461 462 463 464
  /////////////////////////////////////////////////////////////////
  // replicateStorage.remove
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.remove");
  test("remove called substorage remove", function () {
465
    stop();
466
    expect(2);
467

468 469 470 471 472 473 474 475 476 477 478 479 480
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
    Storage200.prototype.remove = function (id) {
      equal(id, "bar", "remove 200 called");
      return id;
    };
481

482 483 484 485 486 487 488 489 490 491 492
    jio.remove("bar", {"title": "foo"})
      .then(function (result) {
        equal(result, "bar");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });
Tristan Cavelier's avatar
Tristan Cavelier committed
493

494 495 496
  test("remove can not modify the signature", function () {
    stop();
    expect(3);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });
    delete Storage200.prototype.remove;

    jio.remove(jio.__storage._signature_hash)
      .then(function () {
        ok(false);
      })
      .fail(function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.message, jio.__storage._signature_hash + " is frozen");
        equal(error.status_code, 403);
      })
      .always(function () {
        start();
520
      });
521
  });
522

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 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 693 694 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 730 731 732
  /////////////////////////////////////////////////////////////////
  // replicateStorage.getAttachment
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.getAttachment");
  test("called substorage getAttachment", function () {
    stop();
    expect(3);

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    }),
      blob = new Blob([big_string]);

    Storage200.prototype.getAttachment = function (id, name) {
      equal(id, "bar", "getAttachment 200 called");
      equal(name, "foo", "getAttachment 200 called");
      return blob;
    };

    jio.getAttachment("bar", "foo")
      .then(function (result) {
        equal(result, blob);
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });

  /////////////////////////////////////////////////////////////////
  // replicateStorage.putAttachment
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.putAttachment");
  test("putAttachment called substorage putAttachment", function () {
    stop();
    expect(4);

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    }),
      blob = new Blob([""]);

    Storage200.prototype.putAttachment = function (id, name, blob2) {
      equal(id, "bar", "putAttachment 200 called");
      equal(name, "foo", "putAttachment 200 called");
      deepEqual(blob2, blob,
                "putAttachment 200 called");
      return "OK";
    };

    jio.putAttachment("bar", "foo", blob)
      .then(function (result) {
        equal(result, "OK");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });

  test("putAttachment can not modify the signature", function () {
    stop();
    expect(3);

    delete Storage200.prototype.putAttachment;

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    }),
      blob = new Blob([""]);

    jio.putAttachment(jio.__storage._signature_hash, "Foo", blob)
      .then(function () {
        ok(false);
      })
      .fail(function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.message, jio.__storage._signature_hash + " is frozen");
        equal(error.status_code, 403);
      })
      .always(function () {
        start();
      });
  });

  /////////////////////////////////////////////////////////////////
  // replicateStorage.removeAttachment
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.removeAttachment");
  test("removeAttachment called substorage removeAttachment", function () {
    stop();
    expect(3);

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });

    Storage200.prototype.removeAttachment = function (id, name) {
      equal(id, "bar", "removeAttachment 200 called");
      equal(name, "foo", "removeAttachment 200 called");
      return "OK";
    };

    jio.removeAttachment("bar", "foo")
      .then(function (result) {
        equal(result, "OK");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });

  test("removeAttachment can not modify the signature", function () {
    stop();
    expect(3);

    delete Storage200.prototype.removeAttachment;

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });

    jio.removeAttachment(jio.__storage._signature_hash, "Foo")
      .then(function () {
        ok(false);
      })
      .fail(function (error) {
        ok(error instanceof jIO.util.jIOError);
        equal(error.message, jio.__storage._signature_hash + " is frozen");
        equal(error.status_code, 403);
      })
      .always(function () {
        start();
      });
  });

  /////////////////////////////////////////////////////////////////
  // replicateStorage.allAttachments
  /////////////////////////////////////////////////////////////////
  module("replicateStorage.allAttachments");
  test("allAttachments called substorage allAttachments", function () {
    stop();
    expect(2);

    var jio = jIO.createJIO({
      type: "replicate",
      local_sub_storage: {
        type: "replicatestorage200"
      },
      remote_sub_storage: {
        type: "replicatestorage500"
      }
    });

    Storage200.prototype.allAttachments = function (id) {
      equal(id, "bar", "allAttachments, 200 called");
      return {attachmentname: {}};
    };

    jio.allAttachments("bar")
      .then(function (result) {
        deepEqual(result, {
          attachmentname: {}
        }, "Check document");
      })
      .fail(function (error) {
        ok(false, error);
      })
      .always(function () {
        start();
      });
  });

733
}(jIO, QUnit, Blob));