fix(mongodb): User role should be updatable (#731)

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
This commit is contained in:
Alexander Dahmen 2025-03-26 14:19:28 +01:00 committed by GitHub
parent 91903f5a60
commit a2c25bede2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 168 additions and 3 deletions

View file

@ -377,3 +377,91 @@ func TestToCreatePayload(t *testing.T) {
})
}
}
func TestToUpdatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
inputRoles []string
expected *mongodbflex.UpdateUserPayload
isValid bool
}{
{
"default_values",
&Model{},
[]string{},
&mongodbflex.UpdateUserPayload{
Roles: &[]string{},
Database: nil,
},
true,
},
{
"simple values",
&Model{
Username: types.StringValue("username"),
Database: types.StringValue("database"),
},
[]string{
"role_1",
"role_2",
},
&mongodbflex.UpdateUserPayload{
Roles: &[]string{
"role_1",
"role_2",
},
Database: utils.Ptr("database"),
},
true,
},
{
"null_fields",
&Model{
Username: types.StringNull(),
Database: types.StringNull(),
},
[]string{
"",
},
&mongodbflex.UpdateUserPayload{
Roles: &[]string{
"",
},
Database: nil,
},
true,
},
{
"nil_model",
nil,
[]string{},
nil,
false,
},
{
"nil_roles",
&Model{},
nil,
nil,
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toUpdatePayload(tt.input, tt.inputRoles)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}