-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac56d19
commit b242187
Showing
2 changed files
with
115 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -19,25 +20,18 @@ func Test_UserCRUD(t *testing.T) { | |
UserId: opslevel.UserId{Email: "[email protected]"}, | ||
Name: userName, | ||
} | ||
cliArgs := []string{expectedUser.Email, expectedUser.Name} | ||
cmd.RootCmd.SetArgs(cliArgs) | ||
|
||
// Create User | ||
createOutput, err := execCmd(Create, "user", cliArgs...) | ||
userId, err := createUser(expectedUser) | ||
if err != nil { | ||
t.Errorf("Create 'user' failed, got error: %v", err) | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
userId := asString(createOutput) | ||
|
||
// Get User | ||
getOutput, err := execCmd(Get, "user", userId) | ||
createdUser, err := getUser(userId) | ||
if err != nil { | ||
t.Errorf("Get 'user' failed, got error: %v", err) | ||
} | ||
|
||
createdUser, err := jsonToResource[opslevel.User](getOutput) | ||
if err != nil { | ||
t.Error("Failed to convert JSON from API to 'opslevel.User' struct") | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
if createdUser.Name != expectedUser.Name || | ||
createdUser.Email != expectedUser.Email || | ||
|
@@ -46,28 +40,67 @@ func Test_UserCRUD(t *testing.T) { | |
t.Errorf("Create 'user' failed, expected user '%+v' but got '%+v'", expectedUser, createdUser) | ||
} | ||
|
||
// Update User | ||
expectedUpdatedUser := opslevel.User{ | ||
UserId: createdUser.UserId, | ||
Name: createdUser.Name, | ||
Role: opslevel.UserRoleTeamMember, | ||
} | ||
if err := writeToYaml(userFileName, expectedUpdatedUser); err != nil { | ||
t.Errorf("Error while writing '%v' to file '%s': %v", expectedUpdatedUser, userFileName, err) | ||
} | ||
|
||
// Store Update User stuff to "file" | ||
cliArgs = []string{expectedUser.Email, "-f", userFileName} | ||
updateOutput, err := execCmd(Update, "user", cliArgs...) | ||
updatedUserId, err := updateUser(string(createdUser.Id), expectedUpdatedUser) | ||
if err != nil { | ||
t.Errorf("Update 'user' failed, got error: %v", err) | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
updatedUserId := asString(updateOutput) | ||
if string(createdUser.Id) != updatedUserId { | ||
t.Errorf("Update 'user' failed, expected returned ID '%s' but got '%s'", string(createdUser.Id), updatedUserId) | ||
} | ||
|
||
// Delete User | ||
if _, err = execCmd(Delete, "user", string(createdUser.Id)); err != nil { | ||
if err = deleteUser(string(createdUser.Id)); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func createUser(expectedUser opslevel.User) (string, error) { | ||
cliArgs := []string{expectedUser.Email, expectedUser.Name} | ||
cmd.RootCmd.SetArgs(cliArgs) | ||
|
||
// Create User | ||
createOutput, err := execCmd(Create, "user", cliArgs...) | ||
if err != nil { | ||
return "", fmt.Errorf("Create 'user' failed, got error: %v", err) | ||
} | ||
return asString(createOutput), nil | ||
} | ||
|
||
func getUser(userId string) (*opslevel.User, error) { | ||
getOutput, err := execCmd(Get, "user", userId) | ||
if err != nil { | ||
return nil, fmt.Errorf("Get 'user' failed, got error: %v", err) | ||
} | ||
|
||
createdUser, err := jsonToResource[opslevel.User](getOutput) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to convert JSON from API to 'opslevel.User' struct") | ||
} | ||
return createdUser, err | ||
} | ||
|
||
func updateUser(userId string, userToUpdate opslevel.User) (string, error) { | ||
if err := writeToYaml(userFileName, userToUpdate); err != nil { | ||
return "", fmt.Errorf("Error while writing '%v' to file '%s': %v", userToUpdate, userFileName, err) | ||
} | ||
|
||
// Store Update User stuff to "file" | ||
cliArgs := []string{userId, "-f", userFileName} | ||
updateOutput, err := execCmd(Update, "user", cliArgs...) | ||
if err != nil { | ||
return "", fmt.Errorf("Update 'user' failed, got error: %v", err) | ||
} | ||
return asString(updateOutput), nil | ||
} | ||
|
||
func deleteUser(userId string) error { | ||
_, err := execCmd(Delete, "user", userId) | ||
return err | ||
} |