Skip to content

Commit

Permalink
issue + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfi committed Nov 18, 2024
1 parent e5ba4d5 commit 838f302
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 9 deletions.
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");
});
});
4 changes: 4 additions & 0 deletions libs/importer/spec/test-data/logmeonce-csv/invalid-row.csv.ts
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`;
3 changes: 3 additions & 0 deletions libs/importer/spec/test-data/logmeonce-csv/invalid-url.csv.ts
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,`;
5 changes: 5 additions & 0 deletions libs/importer/spec/test-data/logmeonce-csv/mixed-data.csv.ts
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,`;
3 changes: 3 additions & 0 deletions libs/importer/spec/test-data/logmeonce-csv/valid-data.csv.ts
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

0 comments on commit 838f302

Please sign in to comment.