|
9 | 9 | "testing" |
10 | 10 |
|
11 | 11 | "github.com/alecthomas/kong" |
| 12 | + "github.com/microsoft/go-mssqldb/azuread" |
12 | 13 | "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" |
13 | 14 | "github.com/stretchr/testify/assert" |
14 | 15 | "github.com/stretchr/testify/require" |
@@ -327,6 +328,54 @@ func TestMissingInputFile(t *testing.T) { |
327 | 328 | assert.Equal(t, 1, exitCode, "exitCode") |
328 | 329 | } |
329 | 330 |
|
| 331 | +func TestConditionsForPasswordPrompt(t *testing.T) { |
| 332 | + |
| 333 | + type test struct { |
| 334 | + authenticationMethod string |
| 335 | + inputFile []string |
| 336 | + username string |
| 337 | + pwd string |
| 338 | + expectedResult bool |
| 339 | + } |
| 340 | + tests := []test{ |
| 341 | + // Positive Testcases |
| 342 | + {sqlcmd.SqlPassword, []string{""}, "someuser", "", true}, |
| 343 | + {sqlcmd.NotSpecified, []string{"testdata/someFile.sql"}, "someuser", "", true}, |
| 344 | + {azuread.ActiveDirectoryPassword, []string{""}, "someuser", "", true}, |
| 345 | + {azuread.ActiveDirectoryPassword, []string{"testdata/someFile.sql"}, "someuser", "", true}, |
| 346 | + {azuread.ActiveDirectoryServicePrincipal, []string{""}, "someuser", "", true}, |
| 347 | + {azuread.ActiveDirectoryServicePrincipal, []string{"testdata/someFile.sql"}, "someuser", "", true}, |
| 348 | + {azuread.ActiveDirectoryApplication, []string{""}, "someuser", "", true}, |
| 349 | + {azuread.ActiveDirectoryApplication, []string{"testdata/someFile.sql"}, "someuser", "", true}, |
| 350 | + |
| 351 | + //Negative Testcases |
| 352 | + {sqlcmd.NotSpecified, []string{""}, "", "", false}, |
| 353 | + {sqlcmd.NotSpecified, []string{"testdata/someFile.sql"}, "", "", false}, |
| 354 | + {azuread.ActiveDirectoryDefault, []string{""}, "someuser", "", false}, |
| 355 | + {azuread.ActiveDirectoryDefault, []string{"testdata/someFile.sql"}, "someuser", "", false}, |
| 356 | + {azuread.ActiveDirectoryInteractive, []string{""}, "someuser", "", false}, |
| 357 | + {azuread.ActiveDirectoryInteractive, []string{"testdata/someFile.sql"}, "someuser", "", false}, |
| 358 | + {azuread.ActiveDirectoryManagedIdentity, []string{""}, "someuser", "", false}, |
| 359 | + {azuread.ActiveDirectoryManagedIdentity, []string{"testdata/someFile.sql"}, "someuser", "", false}, |
| 360 | + } |
| 361 | + |
| 362 | + for _, testcase := range tests { |
| 363 | + t.Log(testcase.authenticationMethod, testcase.inputFile, testcase.username, testcase.pwd, testcase.expectedResult) |
| 364 | + args := newArguments() |
| 365 | + args.DisableCmdAndWarn = true |
| 366 | + args.InputFile = testcase.inputFile |
| 367 | + args.UserName = testcase.username |
| 368 | + vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn) |
| 369 | + setVars(vars, &args) |
| 370 | + var connectConfig sqlcmd.ConnectSettings |
| 371 | + setConnect(&connectConfig, &args, vars) |
| 372 | + connectConfig.AuthenticationMethod = testcase.authenticationMethod |
| 373 | + connectConfig.Password = testcase.pwd |
| 374 | + assert.Equal(t, testcase.expectedResult, isConsoleInitializationRequired(&connectConfig, &args), "Unexpected test result encountered for console initialization") |
| 375 | + assert.Equal(t, testcase.expectedResult, connectConfig.RequiresPassword() && connectConfig.Password == "", "Unexpected test result encountered for password prompt conditions") |
| 376 | + } |
| 377 | +} |
| 378 | + |
330 | 379 | // Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set |
331 | 380 | func canTestAzureAuth() bool { |
332 | 381 | server := os.Getenv(sqlcmd.SQLCMDSERVER) |
|
0 commit comments