|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +package open |
| 5 | + |
| 6 | +import ( |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig" |
| 11 | + "github.com/microsoft/go-sqlcmd/internal/cmdparser" |
| 12 | +) |
| 13 | + |
| 14 | +// TestCopyPasswordToClipboardWithNoUser tests that clipboard copy returns false when no user |
| 15 | +func TestCopyPasswordToClipboardWithNoUser(t *testing.T) { |
| 16 | + if runtime.GOOS == "linux" { |
| 17 | + t.Skip("Skipping on Linux due to ADS tool initialization issue in tools factory") |
| 18 | + } |
| 19 | + |
| 20 | + cmdparser.TestSetup(t) |
| 21 | + |
| 22 | + // Test with nil user |
| 23 | + result := copyPasswordToClipboard(nil, nil) |
| 24 | + if result { |
| 25 | + t.Error("Expected false when user is nil") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// TestCopyPasswordToClipboardWithNonBasicAuth tests clipboard copy with non-basic auth |
| 30 | +func TestCopyPasswordToClipboardWithNonBasicAuth(t *testing.T) { |
| 31 | + if runtime.GOOS == "linux" { |
| 32 | + t.Skip("Skipping on Linux due to ADS tool initialization issue in tools factory") |
| 33 | + } |
| 34 | + |
| 35 | + cmdparser.TestSetup(t) |
| 36 | + |
| 37 | + // Test with non-basic auth type |
| 38 | + user := &sqlconfig.User{ |
| 39 | + AuthenticationType: "windows", |
| 40 | + Name: "test-user", |
| 41 | + } |
| 42 | + |
| 43 | + result := copyPasswordToClipboard(user, nil) |
| 44 | + if result { |
| 45 | + t.Error("Expected false when auth type is not 'basic'") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// TestCopyPasswordToClipboardWithEmptyPassword tests clipboard copy logic with empty password |
| 50 | +// Note: We can't call copyPasswordToClipboard directly with an empty password because |
| 51 | +// config.GetCurrentContextInfo() will panic on empty password decode. Instead we test |
| 52 | +// the logic conditions. |
| 53 | +func TestCopyPasswordToClipboardWithEmptyPassword(t *testing.T) { |
| 54 | + // The empty password case is handled by GetCurrentContextInfo returning empty string, |
| 55 | + // which causes copyPasswordToClipboard to return false before attempting clipboard copy. |
| 56 | + // This test verifies the logic in userShouldCopyPassword. |
| 57 | + |
| 58 | + // Even with basic auth, if there's no password configured, the function returns false |
| 59 | + // because GetCurrentContextInfo returns empty string for password |
| 60 | + user := &sqlconfig.User{ |
| 61 | + AuthenticationType: "basic", |
| 62 | + BasicAuth: &sqlconfig.BasicAuthDetails{ |
| 63 | + Username: "sa", |
| 64 | + PasswordEncryption: "", |
| 65 | + Password: "", // Empty password |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // The function should return true for copying (it checks auth type) |
| 70 | + // but will return false when GetCurrentContextInfo returns empty password |
| 71 | + if !userShouldCopyPassword(user) { |
| 72 | + t.Error("userShouldCopyPassword should return true for basic auth user") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestCopyPasswordToClipboardLogic tests the logic flow of clipboard copy |
| 77 | +func TestCopyPasswordToClipboardLogic(t *testing.T) { |
| 78 | + // Test the logic conditions without actually calling clipboard |
| 79 | + |
| 80 | + // Condition 1: user is nil |
| 81 | + if userShouldCopyPassword(nil) { |
| 82 | + t.Error("Should not copy password when user is nil") |
| 83 | + } |
| 84 | + |
| 85 | + // Condition 2: auth type is not basic |
| 86 | + user := &sqlconfig.User{ |
| 87 | + AuthenticationType: "integrated", |
| 88 | + } |
| 89 | + if userShouldCopyPassword(user) { |
| 90 | + t.Error("Should not copy password when auth type is not basic") |
| 91 | + } |
| 92 | + |
| 93 | + // Condition 3: auth type is basic |
| 94 | + user = &sqlconfig.User{ |
| 95 | + AuthenticationType: "basic", |
| 96 | + BasicAuth: &sqlconfig.BasicAuthDetails{ |
| 97 | + Username: "sa", |
| 98 | + Password: "test", |
| 99 | + }, |
| 100 | + } |
| 101 | + if !userShouldCopyPassword(user) { |
| 102 | + t.Error("Should copy password when auth type is basic") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// userShouldCopyPassword is a helper that tests the condition logic |
| 107 | +func userShouldCopyPassword(user *sqlconfig.User) bool { |
| 108 | + if user == nil || user.AuthenticationType != "basic" { |
| 109 | + return false |
| 110 | + } |
| 111 | + return true |
| 112 | +} |
0 commit comments