krz/keycask

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

Tests/KeycaskCLITests/EditRmTests.swift

v0.1.0
keycask/Tests/KeycaskCLITests/EditRmTests.swift history · blame · raw

75 lines · 3098 bytes

 1import Foundation
 2import Testing
 3
 4@Suite struct EditRmTests {
 5    @Test func editChangesFieldsAndBumpsUpdated() throws {
 6        let cli = try CLI.initialized()
 7        try cli.run(["add", "gh", "--tag", "a", "--generate"])
 8        let before =
 9            try JSONSerialization.jsonObject(
10                with: Data(try cli.run(["show", "gh", "--json"]).stdout.utf8)) as! [String: Any]
11        let r = try cli.run([
12            "edit", "gh", "--name", "github", "-u", "cmc", "--url", "https://x", "--notes", "n",
13            "--tag", "b", "--untag", "a",
14        ])
15        #expect(r.status == 0)
16        let after =
17            try JSONSerialization.jsonObject(
18                with: Data(try cli.run(["show", "github", "--json"]).stdout.utf8)) as! [String: Any]
19        #expect(after["name"] as? String == "github")
20        #expect(after["username"] as? String == "cmc")
21        #expect(after["url"] as? String == "https://x")
22        #expect(after["notes"] as? String == "n")
23        #expect(after["tags"] as? [String] == ["b"])
24        #expect(after["created"] as? String == before["created"] as? String)
25        #expect(after["id"] as? String == before["id"] as? String)
26    }
27
28    @Test func editPasswordFromStdinAndGenerate() throws {
29        let cli = try CLI.initialized()
30        try cli.run(["add", "gh", "--generate"])
31        try cli.run(["edit", "gh", "--password"], stdin: "newpass\n")
32        #expect(try cli.run(["show", "gh", "--field", "password"]).stdout == "newpass\n")
33        try cli.run(["edit", "gh", "--generate", "--length", "30"])
34        #expect(try cli.run(["show", "gh", "--field", "password"]).stdout.count == 31)
35    }
36
37    @Test func editWithNoChangesIsUsageError() throws {
38        let cli = try CLI.initialized()
39        try cli.run(["add", "gh", "--generate"])
40        let r = try cli.run(["edit", "gh"])
41        #expect(r.status == 2)
42    }
43
44    @Test func editUnknownIsNotFound() throws {
45        let cli = try CLI.initialized()
46        #expect(try cli.run(["edit", "nope", "--url", "x"]).status == 3)
47    }
48
49    @Test func rmWithYesRemoves() throws {
50        let cli = try CLI.initialized()
51        let id = try cli.run(["add", "gh", "--generate"]).stdout.trimmingCharacters(in: .newlines)
52        let r = try cli.run(["rm", id, "--yes"])
53        #expect(r.status == 0)
54        #expect(try cli.run(["show", id]).status == 3)
55        #expect(try cli.run(["ls"]).stdout == "")
56    }
57
58    @Test func rmWithoutYesAndWithoutTTYIsUsageError() throws {
59        let cli = try CLI.initialized()
60        try cli.run(["add", "gh", "--generate"])
61        let r = try cli.run(["rm", "gh"], stdin: "y\n")
62        #expect(r.status == 2)
63        #expect(r.stderr.contains("--yes"))
64        #expect(try cli.run(["ls"]).lines.count == 1)
65    }
66
67    @Test func rmAmbiguousNameLists() throws {
68        let cli = try CLI.initialized()
69        try cli.run(["add", "gh", "--generate"])
70        try cli.run(["add", "gh", "--generate"])
71        let r = try cli.run(["rm", "gh", "--yes"])
72        #expect(r.status == 5)
73        #expect(try cli.run(["ls"]).lines.count == 2)
74    }
75}