-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use "DESCRIBE USER" in ReadUser, UserExists (#769)
- Loading branch information
Showing
6 changed files
with
115 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package resources_test | |
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
sqlmock "github.com/DATA-DOG/go-sqlmock" | ||
|
@@ -43,30 +44,63 @@ func TestUserCreate(t *testing.T) { | |
r.NotNil(d) | ||
|
||
WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { | ||
mock.ExpectExec(`^CREATE USER "good_name" COMMENT='great comment' DEFAULT_NAMESPACE='mynamespace' DEFAULT_ROLE='bestrole' DEFAULT_WAREHOUSE='mywarehouse' DISPLAY_NAME='Display Name' EMAIL='[email protected]' FIRST_NAME='Marcin' LAST_NAME='Zukowski' LOGIN_NAME='gname' PASSWORD='awesomepassword' RSA_PUBLIC_KEY='asdf' RSA_PUBLIC_KEY_2='asdf2' DISABLED=true MUST_CHANGE_PASSWORD=true$`).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
expectReadUser(mock) | ||
name := "good_name" | ||
q := fmt.Sprintf(`^CREATE USER "%s" COMMENT='great comment' DEFAULT_NAMESPACE='mynamespace' DEFAULT_ROLE='bestrole' DEFAULT_WAREHOUSE='mywarehouse' DISPLAY_NAME='Display Name' EMAIL='[email protected]' FIRST_NAME='Marcin' LAST_NAME='Zukowski' LOGIN_NAME='gname' PASSWORD='awesomepassword' RSA_PUBLIC_KEY='asdf' RSA_PUBLIC_KEY_2='asdf2' DISABLED=true MUST_CHANGE_PASSWORD=true$`, name) | ||
mock.ExpectExec(q).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
expectReadUser(mock, name) | ||
err := resources.CreateUser(d, db) | ||
r.NoError(err) | ||
}) | ||
} | ||
|
||
func expectReadUser(mock sqlmock.Sqlmock) { | ||
rows := sqlmock.NewRows([]string{ | ||
"name", "created_on", "login_name", "display_name", "first_name", "last_name", "email", "mins_to_unlock", | ||
"days_to_expiry", "comment", "disabled", "must_change_password", "snowflake_lock", "default_warehouse", | ||
"default_namespace", "default_role", "ext_authn_duo", "ext_authn_uid", "mins_to_bypass_mfa", "owner", | ||
"last_success_login", "expires_at_time", "locked_until_time", "has_password", "has_rsa_public_key"}, | ||
).AddRow("good_name", "created_on", "myloginname", "display_name", "first_name", "last_name", "email", "mins_to_unlock", "days_to_expiry", "mock comment", false, true, "snowflake_lock", "default_warehouse", "default_namespace", "default_role", "ext_authn_duo", "ext_authn_uid", "mins_to_bypass_mfa", "owner", "last_success_login", "expires_at_time", "locked_until_time", "has_password", false) | ||
mock.ExpectQuery(`^SHOW USERS LIKE 'good_name'$`).WillReturnRows(rows) | ||
func expectReadUser(mock sqlmock.Sqlmock, name string) { | ||
rowsmap := map[string]string{ | ||
"NAME": name, | ||
"CREATED_ON": "created_on", | ||
"LOGIN_NAME": "myloginname", | ||
"DISPLAY_NAME": "display_name", | ||
"FIRST_NAME": "first_name", | ||
"LAST_NAME": "last_name", | ||
"EMAIL": "email", | ||
"MINS_TO_UNLOCK": "mins_to_unlock", | ||
"DAYS_TO_EXPIRY": "days_to_expiry", | ||
"COMMENT": "mock comment", | ||
"DISABLED": "false", | ||
"MUST_CHANGE_PASSWORD": "true", | ||
"SNOWFLAKE_LOCK": "snowflake_lock", | ||
"DEFAULT_WAREHOUSE": "default_warehouse", | ||
"DEFAULT_NAMESPACE": "default_namespace", | ||
"DEFAULT_ROLE": "default_role", | ||
"EXT_AUTHN_DUO": "ext_authn_duo", | ||
"EXT_AUTHN_UID": "ext_authn_uid", | ||
"MINS_TO_BYPASS_MFA": "mins_to_bypass_mfa", | ||
"OWNER": "owner", | ||
"LAST_SUCCESS_LOGIN": "last_success_login", | ||
"EXPIRES_AT_TIME": "expires_at_time", | ||
"LOCKED_UNTIL_TIME": "locked_until_time", | ||
"HAS_PASSWORD": "has_password", | ||
"HAS_RSA_PUBLIC_KEY": "false", | ||
} | ||
|
||
rows := sqlmock.NewRows( | ||
[]string{"property", "value", "default", "description"}, | ||
) | ||
|
||
for k, v := range rowsmap { | ||
rows.AddRow(k, v, "", "") | ||
} | ||
|
||
q := fmt.Sprintf(`^DESCRIBE USER "%s"$`, name) | ||
mock.ExpectQuery(q).WillReturnRows(rows) | ||
} | ||
|
||
func TestUserRead(t *testing.T) { | ||
r := require.New(t) | ||
|
||
d := user(t, "good_name", map[string]interface{}{"name": "good_name"}) | ||
name := "good_name" | ||
d := user(t, name, map[string]interface{}{"name": name}) | ||
|
||
WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { | ||
expectReadUser(mock) | ||
expectReadUser(mock, name) | ||
err := resources.ReadUser(d, db) | ||
r.NoError(err) | ||
r.Equal("mock comment", d.Get("comment").(string)) | ||
|
@@ -75,7 +109,7 @@ func TestUserRead(t *testing.T) { | |
|
||
// Test when resource is not found, checking if state will be empty | ||
r.NotEmpty(d.State()) | ||
q := snowflake.User(d.Id()).Show() | ||
q := snowflake.User(d.Id()).Describe() | ||
mock.ExpectQuery(q).WillReturnError(sql.ErrNoRows) | ||
err2 := resources.ReadUser(d, db) | ||
r.Empty(d.State()) | ||
|
@@ -85,11 +119,11 @@ func TestUserRead(t *testing.T) { | |
|
||
func TestUserExists(t *testing.T) { | ||
r := require.New(t) | ||
|
||
d := user(t, "good_name", map[string]interface{}{"name": "good_name"}) | ||
name := "good_name" | ||
d := user(t, name, map[string]interface{}{"name": name}) | ||
|
||
WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { | ||
expectReadUser(mock) | ||
expectReadUser(mock, name) | ||
b, err := resources.UserExists(d, db) | ||
r.NoError(err) | ||
r.True(b) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters