|
| 1 | +// |
| 2 | +// PasswordHidingTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | +// The single source of truth for "does this connection's auth mode replace the |
| 6 | +// password" feeds both the connection form (whether to show the prompt toggle) |
| 7 | +// and the runtime connect/reconnect paths (whether to prompt at all). |
| 8 | +// |
| 9 | + |
| 10 | +import Foundation |
| 11 | +import TableProPluginKit |
| 12 | +import Testing |
| 13 | + |
| 14 | +@testable import TablePro |
| 15 | + |
| 16 | +@Suite("Password hiding from connection fields") |
| 17 | +struct PasswordHidingTests { |
| 18 | + private func dropdown(default defaultValue: String, _ values: [String]) -> ConnectionField { |
| 19 | + ConnectionField( |
| 20 | + id: "awsAuth", |
| 21 | + label: "Authentication", |
| 22 | + defaultValue: defaultValue, |
| 23 | + fieldType: .dropdown(options: values.map { .init(value: $0, label: $0) }), |
| 24 | + section: .authentication, |
| 25 | + hidesPassword: true |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + private let pgpassToggle = ConnectionField( |
| 30 | + id: "usePgpass", |
| 31 | + label: "Use Password File", |
| 32 | + defaultValue: "false", |
| 33 | + fieldType: .toggle, |
| 34 | + section: .authentication, |
| 35 | + hidesPassword: true |
| 36 | + ) |
| 37 | + |
| 38 | + private let secretField = ConnectionField( |
| 39 | + id: "serviceAccountJson", |
| 40 | + label: "Service Account", |
| 41 | + fieldType: .secure, |
| 42 | + section: .authentication, |
| 43 | + hidesPassword: true |
| 44 | + ) |
| 45 | + |
| 46 | + @Test("A dropdown hides the password only when set off its default") |
| 47 | + func dropdownAwayFromDefault() { |
| 48 | + let fields = [dropdown(default: "off", ["off", "accessKey", "profile"])] |
| 49 | + #expect(fields.hidesPassword(forValues: [:]) == false) |
| 50 | + #expect(fields.hidesPassword(forValues: ["awsAuth": "off"]) == false) |
| 51 | + #expect(fields.hidesPassword(forValues: ["awsAuth": "accessKey"]) == true) |
| 52 | + #expect(fields.hidesPassword(forValues: ["awsAuth": "profile"]) == true) |
| 53 | + } |
| 54 | + |
| 55 | + @Test("A toggle hides the password only when on") |
| 56 | + func toggleOn() { |
| 57 | + let fields = [pgpassToggle] |
| 58 | + #expect(fields.hidesPassword(forValues: [:]) == false) |
| 59 | + #expect(fields.hidesPassword(forValues: ["usePgpass": "false"]) == false) |
| 60 | + #expect(fields.hidesPassword(forValues: ["usePgpass": "true"]) == true) |
| 61 | + } |
| 62 | + |
| 63 | + @Test("A secure field that always replaces the password hides it unconditionally") |
| 64 | + func secureFieldAlwaysHides() { |
| 65 | + #expect([secretField].hidesPassword(forValues: [:]) == true) |
| 66 | + } |
| 67 | + |
| 68 | + @Test("Fields without the hidesPassword flag never hide the password") |
| 69 | + func plainFieldsDoNotHide() { |
| 70 | + let plain = ConnectionField(id: "region", label: "Region", section: .authentication) |
| 71 | + #expect([plain].hidesPassword(forValues: ["region": "us-east-1"]) == false) |
| 72 | + #expect([ConnectionField]().hidesPassword(forValues: [:]) == false) |
| 73 | + } |
| 74 | + |
| 75 | + @Test("Only authentication-section fields are considered") |
| 76 | + func ignoresNonAuthenticationFields() { |
| 77 | + let advanced = ConnectionField( |
| 78 | + id: "advancedToggle", |
| 79 | + label: "Advanced", |
| 80 | + defaultValue: "false", |
| 81 | + fieldType: .toggle, |
| 82 | + section: .advanced, |
| 83 | + hidesPassword: true |
| 84 | + ) |
| 85 | + #expect([advanced].hidesPassword(forValues: ["advancedToggle": "true"]) == false) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +@Suite("Password hiding resolved from plugin metadata") |
| 90 | +@MainActor |
| 91 | +struct PluginManagerPasswordHidingTests { |
| 92 | + private func connection(type: DatabaseType, fields: [String: String]) -> DatabaseConnection { |
| 93 | + var connection = DatabaseConnection(name: "test", type: type) |
| 94 | + connection.additionalFields = fields |
| 95 | + return connection |
| 96 | + } |
| 97 | + |
| 98 | + @Test("AWS IAM modes hide the password for relational types") |
| 99 | + func iamHidesPassword() { |
| 100 | + let manager = PluginManager.shared |
| 101 | + #expect(manager.hidesPassword(for: connection(type: .mysql, fields: ["awsAuth": "accessKey"]))) |
| 102 | + #expect(manager.hidesPassword(for: connection(type: .postgresql, fields: ["awsAuth": "profile"]))) |
| 103 | + } |
| 104 | + |
| 105 | + @Test("Password auth does not hide the password") |
| 106 | + func passwordModeDoesNotHide() { |
| 107 | + let manager = PluginManager.shared |
| 108 | + #expect(!manager.hidesPassword(for: connection(type: .mysql, fields: ["awsAuth": "off"]))) |
| 109 | + #expect(!manager.hidesPassword(for: connection(type: .mysql, fields: [:]))) |
| 110 | + } |
| 111 | +} |
0 commit comments