tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_records_wbo.js (3698B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { WBORecord, Collection } = ChromeUtils.importESModule(
      5  "resource://services-sync/record.sys.mjs"
      6 );
      7 const { Resource } = ChromeUtils.importESModule(
      8  "resource://services-sync/resource.sys.mjs"
      9 );
     10 const { Service } = ChromeUtils.importESModule(
     11  "resource://services-sync/service.sys.mjs"
     12 );
     13 
     14 add_test(function test_toJSON() {
     15  _("Create a record, for now without a TTL.");
     16  let wbo = new WBORecord("coll", "a_record");
     17  wbo.modified = 12345;
     18  wbo.sortindex = 42;
     19  wbo.payload = {};
     20 
     21  _(
     22    "Verify that the JSON representation contains the WBO properties, but not TTL."
     23  );
     24  let json = JSON.parse(JSON.stringify(wbo));
     25  Assert.equal(json.modified, 12345);
     26  Assert.equal(json.sortindex, 42);
     27  Assert.equal(json.payload, "{}");
     28  Assert.equal(false, "ttl" in json);
     29 
     30  _("Set a TTL, make sure it's present in the JSON representation.");
     31  wbo.ttl = 30 * 60;
     32  json = JSON.parse(JSON.stringify(wbo));
     33  Assert.equal(json.ttl, 30 * 60);
     34  run_next_test();
     35 });
     36 
     37 add_task(async function test_fetch() {
     38  let record = {
     39    id: "asdf-1234-asdf-1234",
     40    modified: 2454725.98283,
     41    payload: JSON.stringify({ cheese: "roquefort" }),
     42  };
     43  let record2 = {
     44    id: "record2",
     45    modified: 2454725.98284,
     46    payload: JSON.stringify({ cheese: "gruyere" }),
     47  };
     48  let coll = [
     49    {
     50      id: "record2",
     51      modified: 2454725.98284,
     52      payload: JSON.stringify({ cheese: "gruyere" }),
     53    },
     54  ];
     55 
     56  _("Setting up server.");
     57  let server = httpd_setup({
     58    "/record": httpd_handler(200, "OK", JSON.stringify(record)),
     59    "/record2": httpd_handler(200, "OK", JSON.stringify(record2)),
     60    "/coll": httpd_handler(200, "OK", JSON.stringify(coll)),
     61  });
     62 
     63  try {
     64    _("Fetching a WBO record");
     65    let rec = new WBORecord("coll", "record");
     66    await rec.fetch(Service.resource(server.baseURI + "/record"));
     67    Assert.equal(rec.id, "asdf-1234-asdf-1234"); // NOT "record"!
     68 
     69    Assert.equal(rec.modified, 2454725.98283);
     70    Assert.equal(typeof rec.payload, "object");
     71    Assert.equal(rec.payload.cheese, "roquefort");
     72 
     73    _("Fetching a WBO record using the record manager");
     74    let rec2 = await Service.recordManager.get(server.baseURI + "/record2");
     75    Assert.equal(rec2.id, "record2");
     76    Assert.equal(rec2.modified, 2454725.98284);
     77    Assert.equal(typeof rec2.payload, "object");
     78    Assert.equal(rec2.payload.cheese, "gruyere");
     79    Assert.equal(Service.recordManager.response.status, 200);
     80 
     81    // Testing collection extraction.
     82    _("Extracting collection.");
     83    let rec3 = new WBORecord("tabs", "foo"); // Create through constructor.
     84    Assert.equal(rec3.collection, "tabs");
     85  } finally {
     86    await promiseStopServer(server);
     87  }
     88 });
     89 
     90 class FakeService {
     91  constructor(storageURL) {
     92    this.storageURL = storageURL;
     93  }
     94  resource(uri) {
     95    // allow ease of ctor without auth.
     96    return new Resource(uri);
     97  }
     98 }
     99 
    100 add_task(async function test_collection_invalid_url_handling() {
    101  const service = new FakeService("http://example.com/storage/");
    102 
    103  // Invalid string: Resource will leave coll.uri === null
    104  const coll = new Collection("not-a-valid-url", WBORecord, service);
    105 
    106  // Verify the error reporting scenario is handled
    107  Assert.throws(() => {
    108    coll.full = true; // Triggers _rebuildURL
    109  }, /_rebuildURL called with null uri/);
    110 });
    111 
    112 add_task(async function test_wborecord_uri_makeURI_null() {
    113  let record = new WBORecord("tabs", "abc123");
    114 
    115  // Test that we get a helpful error when makeURI returns null
    116  Assert.throws(() => {
    117    record.uri("not-a-valid-base-url");
    118  }, /WBORecord\.uri\(\): makeURI returned null for base=/);
    119 });