summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/FilterAdult.test.js
blob: e5d15a3fb0ef66077d02a2180b838f05288e5e65 (plain)
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
74
75
76
77
78
79
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
import { FilterAdult } from "lib/FilterAdult.jsm";
import { GlobalOverrider } from "test/unit/utils";

describe("FilterAdult", () => {
  let hashStub;
  let hashValue;
  let globals;

  beforeEach(() => {
    globals = new GlobalOverrider();
    hashStub = {
      finish: sinon.stub().callsFake(() => hashValue),
      init: sinon.stub(),
      update: sinon.stub(),
    };
    globals.set("Cc", {
      "@mozilla.org/security/hash;1": {
        createInstance() {
          return hashStub;
        },
      },
    });
    globals.set("gFilterAdultEnabled", true);
  });

  afterEach(() => {
    hashValue = "";
    globals.restore();
  });

  describe("filter", () => {
    it("should default to include on unexpected urls", () => {
      const empty = {};

      const result = FilterAdult.filter([empty]);

      assert.equal(result.length, 1);
      assert.equal(result[0], empty);
    });
    it("should not filter out non-adult urls", () => {
      const link = { url: "https://mozilla.org/" };

      const result = FilterAdult.filter([link]);

      assert.equal(result.length, 1);
      assert.equal(result[0], link);
    });
    it("should filter out adult urls", () => {
      // Use a hash value that is in the adult set
      hashValue = "+/UCpAhZhz368iGioEO8aQ==";
      const link = { url: "https://some-adult-site/" };

      const result = FilterAdult.filter([link]);

      assert.equal(result.length, 0);
    });
    it("should not filter out adult urls if the preference is turned off", () => {
      // Use a hash value that is in the adult set
      hashValue = "+/UCpAhZhz368iGioEO8aQ==";
      globals.set("gFilterAdultEnabled", false);
      const link = { url: "https://some-adult-site/" };

      const result = FilterAdult.filter([link]);

      assert.equal(result.length, 1);
      assert.equal(result[0], link);
    });
  });

  describe("isAdultUrl", () => {
    it("should default to false on unexpected urls", () => {
      const result = FilterAdult.isAdultUrl("");

      assert.equal(result, false);
    });
    it("should return false for non-adult urls", () => {
      const result = FilterAdult.isAdultUrl("https://mozilla.org/");

      assert.equal(result, false);
    });
    it("should return true for adult urls", () => {
      // Use a hash value that is in the adult set
      hashValue = "+/UCpAhZhz368iGioEO8aQ==";
      const result = FilterAdult.isAdultUrl("https://some-adult-site/");

      assert.equal(result, true);
    });
    it("should return false for adult urls when the preference is turned off", () => {
      // Use a hash value that is in the adult set
      hashValue = "+/UCpAhZhz368iGioEO8aQ==";
      globals.set("gFilterAdultEnabled", false);
      const result = FilterAdult.isAdultUrl("https://some-adult-site/");

      assert.equal(result, false);
    });

    describe("test functions", () => {
      it("should add and remove a filter in the adult list", () => {
        // Use a hash value that is in the adult set
        FilterAdult.addDomainToList("https://some-adult-site/");
        let result = FilterAdult.isAdultUrl("https://some-adult-site/");

        assert.equal(result, true);

        FilterAdult.removeDomainFromList("https://some-adult-site/");
        result = FilterAdult.isAdultUrl("https://some-adult-site/");

        assert.equal(result, false);
      });
    });
  });
});