Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-13266] Import from LogMeOnce not working 2.0 #11452

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions libs/importer/spec/logmeonce-csv-importer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { LogMeOnceCsvImporter } from "../src/importers/logmeonce-csv-importer";
import { ImportResult } from "../src/models/import-result";

import { invalidRowData } from "./test-data/logmeonce-csv/invalid-row.csv";
import { invalidUrlData } from "./test-data/logmeonce-csv/invalid-url.csv";
import { missingNameData } from "./test-data/logmeonce-csv/missing-name.csv";
import { mixedData } from "./test-data/logmeonce-csv/mixed-data.csv";
import { multipleEntriesData } from "./test-data/logmeonce-csv/multiple-entries.csv";
import { validData } from "./test-data/logmeonce-csv/valid-data.csv";

describe("LogMeOnceCsvImporter", () => {
let importer: LogMeOnceCsvImporter;

beforeEach(() => {
importer = new LogMeOnceCsvImporter();
});

it("should return success false if CSV data is null", async () => {
try {
const result: ImportResult = await importer.parse(null);
expect(result.success).toBe(false);
} catch (error) {
expect(error).toBeInstanceOf(TypeError);
expect(error.message).toMatch(/Cannot read properties of null/);
}
});

it("should return success false if CSV data is empty", async () => {
const result: ImportResult = await importer.parse("");
expect(result.success).toBe(false);
});

it("should parse valid CSV data correctly", async () => {
const result = await importer.parse(validData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.name).toBe("Example");
expect(cipher.login.uris[0].uri).toBe("https://example.com");
expect(cipher.notes).toBe("Some notes");
expect(cipher.login.username).toBe("[email protected]");
expect(cipher.login.password).toBe("password123");
});

it("should skip rows with insufficient columns", async () => {
const result = await importer.parse(invalidRowData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.name).toBe("Example");
});

it("should handle CSV data with multiple entries", async () => {
const result = await importer.parse(multipleEntriesData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(2);
expect(result.ciphers[0].name).toBe("Example1");
expect(result.ciphers[1].name).toBe("Example2");
});

it("should handle CSV data with multiple entries and invalid rows", async () => {
const result = await importer.parse(mixedData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(2);
expect(result.ciphers[0].name).toBe("Example1");
expect(result.ciphers[1].name).toBe("Example2");
});

it("should use default values for missing columns", async () => {
const result = await importer.parse(missingNameData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.name).toBe("--");
});

it("should handle invalid URLs gracefully", async () => {
const result = await importer.parse(invalidUrlData);
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.login.uris[0].uri).toBe("invalid-url");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
export const invalidRowData = `name,url,note,group,username,password,extra
Example,https://example.com,Some notes,,[email protected],password123,
InvalidRow,https://invalid.com`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
export const invalidUrlData = `name,url,note,group,username,password,extra
Example,invalid-url,Some notes,,[email protected],password123,`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
export const missingNameData = `name,url,note,group,username,password,extra
,,Some notes,,[email protected],password123,`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable */
export const mixedData = `name,url,note,group,username,password,extra
Example1,https://example1.com,Notes1,,[email protected],password1,
InvalidRow,https://invalid.com
Example2,https://example2.com,Notes2,,[email protected],password2,`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable */
export const multipleEntriesData = `name,url,note,group,username,password,extra
Example1,https://example1.com,Notes1,,[email protected],password1,
Example2,https://example2.com,Notes2,,[email protected],password2,`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
export const validData = `name,url,note,group,username,password,extra
Example,https://example.com,Some notes,,[email protected],password123,`;
23 changes: 14 additions & 9 deletions libs/importer/src/importers/logmeonce-csv-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ export class LogMeOnceCsvImporter extends BaseImporter implements Importer {
return Promise.resolve(result);
}

results.forEach((value) => {
if (value.length < 4) {
results.forEach((value, index) => {
if (value.length < 7) {
return;
}
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(value[0], "--");
cipher.login.username = this.getValueOrDefault(value[2]);
cipher.login.password = this.getValueOrDefault(value[3]);
cipher.login.uris = this.makeUriArray(value[1]);
this.cleanupCipher(cipher);
result.ciphers.push(cipher);

if (index !== 0) {
const cipher = this.initLoginCipher();
cipher.name = this.getValueOrDefault(value[0], "--");
cipher.login.uris = this.makeUriArray(value[1]);
cipher.notes = this.getValueOrDefault(value[2]);
cipher.login.username = this.getValueOrDefault(value[4]);
cipher.login.password = this.getValueOrDefault(value[5]);

this.cleanupCipher(cipher);
result.ciphers.push(cipher);
}
});

result.success = true;
Expand Down
Loading