Skip to content

Commit

Permalink
Test existing Login URI icon retrieval behavior (#12845)
Browse files Browse the repository at this point in the history
  • Loading branch information
MGibson1 authored Jan 27, 2025
1 parent 682e62c commit 8577aae
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions libs/common/src/vault/icon/build-cipher-icon.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { CipherType } from "../enums";
import { CipherView } from "../models/view/cipher.view";

import { buildCipherIcon } from "./build-cipher-icon";

describe("buildCipherIcon", () => {
const iconServerUrl = "https://icons.example";
describe("Login cipher", () => {
const cipher = {
type: CipherType.Login,
login: {
uri: "https://test.example",
},
} as any as CipherView;

it.each([true, false])("handles android app URIs for showFavicon setting %s", (showFavicon) => {
setUri("androidapp://test.example");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, showFavicon);

expect(iconDetails).toEqual({
icon: "bwi-android",
image: null,
fallbackImage: "",
imageEnabled: showFavicon,
});
});

it("does not mark as an android app if the protocol is not androidapp", () => {
// This weird URI points to test.androidapp with a default port and path of /.example
setUri("https://test.androidapp://.example");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: "https://icons.example/test.androidapp/icon.png",
fallbackImage: "images/bwi-globe.png",
imageEnabled: true,
});
});

it.each([true, false])("handles ios app URIs for showFavicon setting %s", (showFavicon) => {
setUri("iosapp://test.example");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, showFavicon);

expect(iconDetails).toEqual({
icon: "bwi-apple",
image: null,
fallbackImage: "",
imageEnabled: showFavicon,
});
});

it("does not mark as an ios app if the protocol is not iosapp", () => {
// This weird URI points to test.iosapp with a default port and path of /.example
setUri("https://test.iosapp://.example");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: "https://icons.example/test.iosapp/icon.png",
fallbackImage: "images/bwi-globe.png",
imageEnabled: true,
});
});

const testUris = ["test.example", "https://test.example"];

it.each(testUris)("resolves favicon for %s", (uri) => {
setUri(uri);

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: "https://icons.example/test.example/icon.png",
fallbackImage: "images/bwi-globe.png",
imageEnabled: true,
});
});

it.each(testUris)("does not resolve favicon for %s if showFavicon is false", () => {
setUri("https://test.example");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, false);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: undefined,
fallbackImage: "",
imageEnabled: false,
});
});

it("does not resolve a favicon if the URI is missing a `.`", () => {
setUri("test");

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: undefined,
fallbackImage: "",
imageEnabled: true,
});
});

it.each(["test.onion", "test.i2p"])("does not resolve a favicon for %s", (uri) => {
setUri(`https://${uri}`);

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: null,
fallbackImage: "images/bwi-globe.png",
imageEnabled: true,
});
});

it.each([null, undefined])("does not resolve a favicon if there is no uri", (nullish) => {
setUri(nullish as any as string);

const iconDetails = buildCipherIcon(iconServerUrl, cipher, true);

expect(iconDetails).toEqual({
icon: "bwi-globe",
image: null,
fallbackImage: "",
imageEnabled: true,
});
});

function setUri(uri: string) {
(cipher.login as { uri: string }).uri = uri;
}
});
});

0 comments on commit 8577aae

Please sign in to comment.