import chai from "chai"; import sinon from "sinon"; import { CategoryProviderType, EmptyCategoryValue } from "../../../src/categories/categoryProvider"; import { BrandSafetyController } from "../../../src/controllers/BrandSafetyController"; import CategoryService from "../../../src/services/category.service"; const expect = chai.expect; describe("BrandSafetyController", () => { describe("checkBrandSafety", () => { let brandSafetyController: BrandSafetyController; let categoryService: CategoryService; beforeEach(() => { brandSafetyController = new BrandSafetyController(); categoryService = CategoryService.getInstance(); }); afterEach(() => { sinon.restore(); }); it("should return undefined if url list is empty list", async () => { const fakeGetBrandSafetyDebugPurpose = sinon.fake(() => new EmptyCategoryValue()); sinon.replace(categoryService, "getBrandSafetyDebugPurpose", fakeGetBrandSafetyDebugPurpose); const output = await brandSafetyController.checkBrandSafety({ url: [], }); expect(output).to.deep.equal({ url: undefined, android: undefined, ios: undefined, }); expect(fakeGetBrandSafetyDebugPurpose.callCount).to.equal(0); }); it("should return empty segment for a single url", async () => { const fakeGetBrandSafetyDebugPurpose = sinon.fake(() => new EmptyCategoryValue()); sinon.replace(categoryService, "getBrandSafetyDebugPurpose", fakeGetBrandSafetyDebugPurpose); const output = await brandSafetyController.checkBrandSafety({ url: ["http://www.ogury.com/update?status=safe"], categoryProvider: CategoryProviderType.Mock, fromCache: true, }); expect(output).to.deep.equal({ url: { "http://www.ogury.com/update?status=safe": [], }, android: undefined, ios: undefined, }); expect(fakeGetBrandSafetyDebugPurpose.callCount).to.equal(1); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[0]).to.equal( "http://www.ogury.com/update?status=safe" ); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[1]).to.equal(CategoryProviderType.Mock); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[2]).to.equal(true); }); it("should return good segments for a single url", async () => { const fakeGetBrandSafetyDebugPurpose = sinon.fake(() => createFakeBrandSafetyResult(["1"])); sinon.replace(categoryService, "getBrandSafetyDebugPurpose", fakeGetBrandSafetyDebugPurpose); const output = await brandSafetyController.checkBrandSafety({ url: ["http://www.ogury.com/update?status=safe"], categoryProvider: CategoryProviderType.Mock, fromCache: true, }); expect(output).to.deep.equal({ url: { "http://www.ogury.com/update?status=safe": ["1"], }, android: undefined, ios: undefined, }); expect(fakeGetBrandSafetyDebugPurpose.callCount).to.equal(1); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[0]).to.equal( "http://www.ogury.com/update?status=safe" ); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[1]).to.equal(CategoryProviderType.Mock); expect(fakeGetBrandSafetyDebugPurpose.firstCall.args[2]).to.equal(true); }); it("should return results for multiple urls", async () => { const expectedOutput = { url: { "http://www.ogury.com/update?status=safe": ["1"], "http://www.ogury.com/update?status=unsafe": ["2"], }, android: undefined, ios: undefined, }; const fakeGetBrandSafetyDebugPurpose = sinon.fake(async (cmd: any) => { const expected: any = expectedOutput; return createFakeBrandSafetyResult(expected.url[cmd]); }); sinon.replace(categoryService, "getBrandSafetyDebugPurpose", fakeGetBrandSafetyDebugPurpose); const output = await brandSafetyController.checkBrandSafety({ url: [ "http://www.ogury.com/update?status=safe", "http://www.ogury.com/update?status=unsafe", ], categoryProvider: CategoryProviderType.Mock, fromCache: true, }); expect(output).to.deep.equal(expectedOutput); }); it("should return good results for urls, android and ios", async () => { const expectedOutput = { url: { "http://www.ogury.com/update?status=safe": ["1"], "http://www.ogury.com/update?status=unsafe": ["2"], }, android: { "com.safe.android": ["3"], "com.unsafe.android": ["4"] }, ios: { "com.safe.ios": ["5"], "com.unsafe.ios": ["6"] }, }; const fakeGetBrandSafetyDebugPurpose = sinon.fake(async (cmd: any) => { const expected: any = { urlOrApp: { ...expectedOutput.url, ...expectedOutput.android, ...expectedOutput.ios }, }; return createFakeBrandSafetyResult(expected.urlOrApp[cmd]); }); sinon.replace(categoryService, "getBrandSafetyDebugPurpose", fakeGetBrandSafetyDebugPurpose); const output = await brandSafetyController.checkBrandSafety({ url: [ "http://www.ogury.com/update?status=safe", "http://www.ogury.com/update?status=unsafe", ], android: ["com.safe.android", "com.unsafe.android"], ios: ["com.safe.ios", "com.unsafe.ios"], categoryProvider: CategoryProviderType.Mock, fromCache: true, }); expect(output).to.deep.equal(expectedOutput); }); }); }); const createFakeBrandSafetyResult = (segments: string[]) => ({ toSegmentList(): string[] { return segments; }, containsSegment(stringSegment: string): boolean { return segments.includes(stringSegment); }, });