added admin console command tests

This commit is contained in:
Gani Georgiev
2023-04-20 23:39:48 +03:00
parent 4d94673839
commit faa0a9f7dc
5 changed files with 266 additions and 43 deletions

View File

@@ -1,7 +1,8 @@
package cmd
import (
"os"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/go-ozzo/ozzo-validation/v4/is"
@@ -30,20 +31,20 @@ func adminCreateCommand(app core.App) *cobra.Command {
Use: "create",
Example: "admin create test@example.com 1234567890",
Short: "Creates a new admin account",
Run: func(command *cobra.Command, args []string) {
// prevents printing the error log twice
SilenceErrors: true,
SilenceUsage: true,
RunE: func(command *cobra.Command, args []string) error {
if len(args) != 2 {
color.Red("Missing email and password arguments.")
os.Exit(1)
return errors.New("Missing email and password arguments.")
}
if is.EmailFormat.Validate(args[0]) != nil {
color.Red("Invalid email address.")
os.Exit(1)
if args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
return errors.New("Missing or invalid email address.")
}
if len(args[1]) < 8 {
color.Red("The password must be at least 8 chars long.")
os.Exit(1)
return errors.New("The password must be at least 8 chars long.")
}
admin := &models.Admin{}
@@ -51,11 +52,11 @@ func adminCreateCommand(app core.App) *cobra.Command {
admin.SetPassword(args[1])
if err := app.Dao().SaveAdmin(admin); err != nil {
color.Red("Failed to create new admin account: %v", err)
os.Exit(1)
return fmt.Errorf("Failed to create new admin account: %v", err)
}
color.Green("Successfully created new admin %s!", admin.Email)
return nil
},
}
@@ -67,36 +68,35 @@ func adminUpdateCommand(app core.App) *cobra.Command {
Use: "update",
Example: "admin update test@example.com 1234567890",
Short: "Changes the password of a single admin account",
Run: func(command *cobra.Command, args []string) {
// prevents printing the error log twice
SilenceErrors: true,
SilenceUsage: true,
RunE: func(command *cobra.Command, args []string) error {
if len(args) != 2 {
color.Red("Missing email and password arguments.")
os.Exit(1)
return errors.New("Missing email and password arguments.")
}
if is.EmailFormat.Validate(args[0]) != nil {
color.Red("Invalid email address.")
os.Exit(1)
if args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
return errors.New("Missing or invalid email address.")
}
if len(args[1]) < 8 {
color.Red("The new password must be at least 8 chars long.")
os.Exit(1)
return errors.New("The new password must be at least 8 chars long.")
}
admin, err := app.Dao().FindAdminByEmail(args[0])
if err != nil {
color.Red("Admin with email %s doesn't exist.", args[0])
os.Exit(1)
return fmt.Errorf("Admin with email %s doesn't exist.", args[0])
}
admin.SetPassword(args[1])
if err := app.Dao().SaveAdmin(admin); err != nil {
color.Red("Failed to change admin %s password: %v", admin.Email, err)
os.Exit(1)
return fmt.Errorf("Failed to change admin %s password: %v", admin.Email, err)
}
color.Green("Successfully changed admin %s password!", admin.Email)
return nil
},
}
@@ -108,24 +108,26 @@ func adminDeleteCommand(app core.App) *cobra.Command {
Use: "delete",
Example: "admin delete test@example.com",
Short: "Deletes an existing admin account",
Run: func(command *cobra.Command, args []string) {
if len(args) == 0 || is.EmailFormat.Validate(args[0]) != nil {
color.Red("Invalid or missing email address.")
os.Exit(1)
// prevents printing the error log twice
SilenceErrors: true,
SilenceUsage: true,
RunE: func(command *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" || is.EmailFormat.Validate(args[0]) != nil {
return errors.New("Invalid or missing email address.")
}
admin, err := app.Dao().FindAdminByEmail(args[0])
if err != nil {
color.Yellow("Admin %s is already deleted.", args[0])
return
return nil
}
if err := app.Dao().DeleteAdmin(admin); err != nil {
color.Red("Failed to delete admin %s: %v", admin.Email, err)
os.Exit(1)
return fmt.Errorf("Failed to delete admin %s: %v", admin.Email, err)
}
color.Green("Successfully deleted admin %s!", admin.Email)
return nil
},
}