krz/keycask

Password manager: Swift core library, CLI for macOS/Linux/Windows, iOS/macOS app. cli password-manager swift

Tests/KeycaskCLITests/LsFindTests.swift

72 lines · 2600 bytes

 1import Foundation
 2import Testing
 3
 4@Suite struct LsFindTests {
 5    func seeded() throws -> CLI {
 6        let cli = try CLI.initialized()
 7        try cli.run([
 8            "add", "github", "-u", "cmc", "--url", "https://github.com", "--tag", "Dev",
 9            "--generate",
10        ])
11        try cli.run([
12            "add", "bank", "--url", "https://bank.example", "--notes", "downtown branch",
13            "--generate",
14        ])
15        try cli.run(["add", "Alpha", "--tag", "dev", "--generate"])
16        return cli
17    }
18
19    @Test func lsSortsByNameAndShowsColumns() throws {
20        let cli = try seeded()
21        let r = try cli.run(["ls"])
22        #expect(r.status == 0)
23        let names = r.lines.map {
24            String($0.split(separator: " ", omittingEmptySubsequences: true)[1])
25        }
26        #expect(names == ["Alpha", "bank", "github"])
27        #expect(r.stdout.contains("cmc"))
28        #expect(r.stdout.contains("https://github.com"))
29    }
30
31    @Test func lsTagFilterIsCaseInsensitive() throws {
32        let cli = try seeded()
33        let r = try cli.run(["ls", "--tag", "DEV"])
34        #expect(r.lines.count == 2)
35        #expect(!r.stdout.contains("bank"))
36    }
37
38    @Test func lsJsonIsAnArrayWithMaskedPasswords() throws {
39        let cli = try seeded()
40        let r = try cli.run(["ls", "--json"])
41        let arr = try JSONSerialization.jsonObject(with: Data(r.stdout.utf8)) as! [[String: Any]]
42        #expect(arr.count == 3)
43        #expect(arr.allSatisfy { $0["password"] as? String == "********" })
44    }
45
46    @Test func emptyVaultListsNothing() throws {
47        let cli = try CLI.initialized()
48        let r = try cli.run(["ls"])
49        #expect(r.status == 0)
50        #expect(r.stdout == "")
51        let j = try cli.run(["ls", "--json"])
52        #expect(j.stdout.trimmingCharacters(in: .whitespacesAndNewlines) == "[]")
53    }
54
55    @Test func findMatchesNotesURLTagsCaseInsensitively() throws {
56        let cli = try seeded()
57        #expect(try cli.run(["find", "DOWNTOWN"]).lines.count == 1)
58        #expect(try cli.run(["find", "github.com"]).lines.count == 1)
59        #expect(try cli.run(["find", "dev"]).lines.count == 2)
60        let none = try cli.run(["find", "zzz"])
61        #expect(none.status == 0)
62        #expect(none.stdout == "")
63    }
64
65    @Test func findJson() throws {
66        let cli = try seeded()
67        let r = try cli.run(["find", "bank", "--json"])
68        let arr = try JSONSerialization.jsonObject(with: Data(r.stdout.utf8)) as! [[String: Any]]
69        #expect(arr.count == 1)
70        #expect(arr[0]["name"] as? String == "bank")
71    }
72}