Skip to content

Commit bc82d13

Browse files
authored
Gate getTopics on deviceAccess (#162)
1 parent 4626d23 commit bc82d13

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/addons/topics-api.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ import OptableSDK from "../sdk";
22
import { TEST_HOST, TEST_SITE } from "../test/mocks.ts";
33
import "./topics-api.ts";
44

5+
describe("OptableSDK - getTopics", () => {
6+
let SDK;
7+
8+
beforeEach(() => {
9+
SDK = new OptableSDK({ host: TEST_HOST, site: TEST_SITE });
10+
// Mock the profile method
11+
SDK.profile = jest.fn();
12+
});
13+
14+
test("fails when browsing topics is not supported", async () => {
15+
expect.assertions(1);
16+
const expected = "browsing-topics not supported";
17+
await expect(SDK.getTopics()).rejects.toBe(expected);
18+
});
19+
20+
test("fails when device access is not granted", async () => {
21+
expect.assertions(1);
22+
const expected = "consent not granted for reading browsing topics";
23+
document.browsingTopics = {};
24+
25+
SDK.dcn.consent.deviceAccess = false;
26+
await expect(SDK.getTopics()).rejects.toBe(expected);
27+
});
28+
});
29+
530
describe("OptableSDK - ingestTopics", () => {
631
let SDK;
732

lib/addons/topics-api.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ OptableSDK.prototype.getTopics = async function (): Promise<BrowsingTopic[]> {
2323
throw "browsing-topics not supported";
2424
}
2525

26+
if (!this.dcn.consent.deviceAccess) {
27+
throw "consent not granted for reading browsing topics";
28+
}
29+
2630
const siteConfig = await this.site();
2731
if (!siteConfig.getTopicsURL) {
2832
throw "origin not enabled for topics api";

0 commit comments

Comments
 (0)