Skip to content

Commit

Permalink
Merge pull request #190 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul: user password change
  • Loading branch information
mleone87 authored Sep 3, 2022
2 parents f6b9051 + a8e3d64 commit 2d62d11
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 59 deletions.
82 changes: 58 additions & 24 deletions proxmox/config_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package proxmox

import (
"encoding/json"
"unicode/utf8"
"errors"
"fmt"
"log"
"unicode/utf8"
)

// User options for the Proxmox API
Expand All @@ -21,7 +21,7 @@ type ConfigUser struct {
Lastname string `json:"lastname,omitempty"`
}

func (config ConfigUser) MapUserValues()(params map[string]interface{}) {
func (config ConfigUser) MapUserValues() (params map[string]interface{}) {
params = map[string]interface{}{
"comment": config.Comment,
"email": config.Email,
Expand All @@ -35,18 +35,28 @@ func (config ConfigUser) MapUserValues()(params map[string]interface{}) {
return
}

func (config ConfigUser) SetUser(userid string, password string, client *Client) (err error) {
func (config *ConfigUser) SetUser(userid string, password string, client *Client) (err error) {
err = ValidateUserPassword(password)
if err != nil {return err}
if err != nil {
return err
}

config.UserID = userid
if config != nil {
config.UserID = userid
}

userExists, err := client.CheckUserExistance(userid)
if err != nil {return err}
if err != nil {
return err
}

if userExists {
err = config.UpdateUser(client)
if err != nil {return err}
if config != nil {
err = config.UpdateUser(client)
if err != nil {
return err
}
}
if password != "" {
err = client.UpdateUserPassword(userid, password)
}
Expand All @@ -68,7 +78,7 @@ func (config ConfigUser) CreateUser(password string, client *Client) (err error)
return
}

func (config ConfigUser) UpdateUser(client *Client) (err error) {
func (config *ConfigUser) UpdateUser(client *Client) (err error) {
params := config.MapUserValues()
err = client.UpdateUser(config.UserID, params)
if err != nil {
Expand All @@ -82,31 +92,55 @@ func NewConfigUserFromApi(userid string, client *Client) (config *ConfigUser, er
// prepare json map to receive the information from the api
var userConfig map[string]interface{}
userConfig, err = client.GetUserConfig(userid)
if err != nil {return nil, err}
if err != nil {
return nil, err
}
config = new(ConfigUser)

config.UserID = userid

if _, isSet := userConfig["comment"]; isSet {config.Comment = userConfig["comment"].(string)}
if _, isSet := userConfig["email"]; isSet {config.Email = userConfig["email"].(string)}
if _, isSet := userConfig["enable"]; isSet {config.Enable = Itob(int(userConfig["enable"].(float64)))}
if _, isSet := userConfig["expire"]; isSet {config.Expire = int(userConfig["expire"].(float64))}
if _, isSet := userConfig["firstname"]; isSet {config.Firstname = userConfig["firstname"].(string)}
if _, isSet := userConfig["keys"]; isSet {config.Keys = userConfig["keys"].(string)}
if _, isSet := userConfig["lastname"]; isSet {config.Lastname = userConfig["lastname"].(string)}
if _, isSet := userConfig["groups"]; isSet {config.Groups = ArrayToStringType(userConfig["groups"].(interface{}).([]interface{}))}
if _, isSet := userConfig["comment"]; isSet {
config.Comment = userConfig["comment"].(string)
}
if _, isSet := userConfig["email"]; isSet {
config.Email = userConfig["email"].(string)
}
if _, isSet := userConfig["enable"]; isSet {
config.Enable = Itob(int(userConfig["enable"].(float64)))
}
if _, isSet := userConfig["expire"]; isSet {
config.Expire = int(userConfig["expire"].(float64))
}
if _, isSet := userConfig["firstname"]; isSet {
config.Firstname = userConfig["firstname"].(string)
}
if _, isSet := userConfig["keys"]; isSet {
config.Keys = userConfig["keys"].(string)
}
if _, isSet := userConfig["lastname"]; isSet {
config.Lastname = userConfig["lastname"].(string)
}
if _, isSet := userConfig["groups"]; isSet {
config.Groups = ArrayToStringType(userConfig["groups"].(interface{}).([]interface{}))
}

return
}

func NewConfigUserFromJson(input []byte) (config *ConfigUser, err error) {
config = &ConfigUser{}
err = json.Unmarshal([]byte(input), config)
if err != nil {log.Fatal(err)}
if len(input) != 0 {
config = &ConfigUser{}
err = json.Unmarshal([]byte(input), config)
if err != nil {
log.Fatal(err)
}
}
return
}

func ValidateUserPassword(password string) error{
if utf8.RuneCountInString(password) >= 5 || password == ""{return nil}
func ValidateUserPassword(password string) error {
if utf8.RuneCountInString(password) >= 5 || password == "" {
return nil
}
return errors.New("error updating User: the minimum password length is 5")
}
}
52 changes: 36 additions & 16 deletions test/cli/Users/User_0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package cli_user_test

import (
"testing"

_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
cliTest "github.com/Telmate/proxmox-api-go/test/cli"
)

func Test_User_0_Cleanup(t *testing.T){
func Test_User_0_Cleanup(t *testing.T) {
Test := cliTest.Test{
ReqErr: true,
ReqErr: true,
ErrContains: "test-user0@pve",
Args: []string{"-i","delete","user","test-user0@pve"},
Args: []string{"-i", "delete", "user", "test-user0@pve"},
}
Test.StandardTest(t)
}

// Set groups

func Test_User_0_Set_Full_With_Password_Set(t *testing.T){
func Test_User_0_Set_Full_With_Password_Set(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"comment": "this is a comment",
"email": "[email protected]",
"enable": true,
"expire": 99999999,
"expire": 253370811600,
"firstname": "Bruce",
"lastname": "Wayne",
"groups": [
Expand All @@ -33,17 +34,36 @@ func Test_User_0_Set_Full_With_Password_Set(t *testing.T){
}`,
Expected: "(test-user0@pve)",
Contains: true,
Args: []string{"-i","set","user","test-user0@pve","Enter123!"},
Args: []string{"-i", "set", "user", "test-user0@pve", "Enter123!"},
}
Test.StandardTest(t)
}

func Test_User_0_Login_Password_Set(t *testing.T) {
cliTest.SetEnvironmentVariables()
Test := cliTest.LoginTest{
UserID: "test-user0@pve",
UserID: "test-user0@pve",
Password: "Enter123!",
ReqErr: false,
ReqErr: false,
}
Test.Login(t)
}

func Test_User_0_Change_Password(t *testing.T) {
Test := cliTest.Test{
Expected: "(test-user0@pve)",
Contains: true,
Args: []string{"-i", "set", "user", "test-user0@pve", "aBc123!"},
}
Test.StandardTest(t)
}

func Test_User_0_Login_Password_Changed(t *testing.T) {
cliTest.SetEnvironmentVariables()
Test := cliTest.LoginTest{
UserID: "test-user0@pve",
Password: "aBc123!",
ReqErr: false,
}
Test.Login(t)
}
Expand All @@ -57,17 +77,17 @@ func Test_User_0_Get_Full(t *testing.T) {
"userid": "test-user0@pve",
"email": "[email protected]",
"enable": true,
"expire": 99999999,
"expire": 253370811600,
"firstname": "Bruce",
"keys": "2fa key",
"lastname": "Wayne"
}`,
Args: []string{"-i","get","user","test-user0@pve"},
Args: []string{"-i", "get", "user", "test-user0@pve"},
}
Test.StandardTest(t)
}

func Test_User_0_Set_Empty(t *testing.T){
func Test_User_0_Set_Empty(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
Expand All @@ -83,7 +103,7 @@ func Test_User_0_Set_Empty(t *testing.T){
}`,
Expected: "(test-user0@pve)",
Contains: true,
Args: []string{"-i","set","user","test-user0@pve"},
Args: []string{"-i", "set", "user", "test-user0@pve"},
}
Test.StandardTest(t)
}
Expand All @@ -97,16 +117,16 @@ func Test_User_0_Get_Empty(t *testing.T) {
"enable": false,
"expire": 0
}`,
Args: []string{"-i","get","user","test-user0@pve"},
Args: []string{"-i", "get", "user", "test-user0@pve"},
}
Test.StandardTest(t)
}

func Test_User_0_Delete(t *testing.T){
func Test_User_0_Delete(t *testing.T) {
Test := cliTest.Test{
Expected: "",
ReqErr: false,
Args: []string{"-i","delete","user","test-user0@pve"},
ReqErr: false,
Args: []string{"-i", "delete", "user", "test-user0@pve"},
}
Test.StandardTest(t)
}
39 changes: 20 additions & 19 deletions test/cli/Users/User_1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package cli_user_test

import (
"testing"

_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
cliTest "github.com/Telmate/proxmox-api-go/test/cli"
)

func Test_User_1_Cleanup(t *testing.T){
func Test_User_1_Cleanup(t *testing.T) {
Test := cliTest.Test{
ReqErr: true,
ReqErr: true,
ErrContains: "test-user1@pve",
Args: []string{"-i","delete","user","test-user1@pve"},
Args: []string{"-i", "delete", "user", "test-user1@pve"},
}
Test.StandardTest(t)
}

func Test_User_1_Set_Empty_Without_Password(t *testing.T){
func Test_User_1_Set_Empty_Without_Password(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
Expand All @@ -24,17 +25,17 @@ func Test_User_1_Set_Empty_Without_Password(t *testing.T){
}`,
Expected: "(test-user1@pve)",
Contains: true,
Args: []string{"-i","set","user","test-user1@pve"},
Args: []string{"-i", "set", "user", "test-user1@pve"},
}
Test.StandardTest(t)
}

func Test_User_1_Login_Password_Not_Set(t *testing.T) {
cliTest.SetEnvironmentVariables()
Test := cliTest.LoginTest{
UserID: "test-user1@pve",
UserID: "test-user1@pve",
Password: "Enter123!",
ReqErr: true,
ReqErr: true,
}
Test.Login(t)
}
Expand All @@ -48,19 +49,19 @@ func Test_User_1_Get_Empty(t *testing.T) {
"enable": false,
"expire": 0
}`,
Args: []string{"-i","get","user","test-user1@pve"},
Args: []string{"-i", "get", "user", "test-user1@pve"},
}
Test.StandardTest(t)
}

func Test_User_1_Set_Full_With_Password(t *testing.T){
func Test_User_1_Set_Full_With_Password(t *testing.T) {
Test := cliTest.Test{
InputJson: `
{
"comment": "this is a comment",
"email": "[email protected]",
"enable": true,
"expire": 99999999,
"expire": 253370811600,
"firstname": "Bruce",
"lastname": "Wayne",
"groups": [
Expand All @@ -69,17 +70,17 @@ func Test_User_1_Set_Full_With_Password(t *testing.T){
}`,
Expected: "(test-user1@pve)",
Contains: true,
Args: []string{"-i","set","user","test-user1@pve","Enter123!"},
Args: []string{"-i", "set", "user", "test-user1@pve", "Enter123!"},
}
Test.StandardTest(t)
}

func Test_User_1_Login_Password_Set(t *testing.T) {
cliTest.SetEnvironmentVariables()
Test := cliTest.LoginTest{
UserID: "test-user1@pve",
UserID: "test-user1@pve",
Password: "Enter123!",
ReqErr: false,
ReqErr: false,
}
Test.Login(t)
}
Expand All @@ -93,21 +94,21 @@ func Test_User_1_Get_Full(t *testing.T) {
"userid": "test-user1@pve",
"email": "[email protected]",
"enable": true,
"expire": 99999999,
"expire": 253370811600,
"firstname": "Bruce",
"lastname": "Wayne",
"keys": "2fa key"
}`,
Args: []string{"-i","get","user","test-user1@pve"},
Args: []string{"-i", "get", "user", "test-user1@pve"},
}
Test.StandardTest(t)
}

func Test_User_1_Delete(t *testing.T){
func Test_User_1_Delete(t *testing.T) {
Test := cliTest.Test{
Expected: "",
ReqErr: false,
Args: []string{"-i","delete","user","test-user1@pve"},
ReqErr: false,
Args: []string{"-i", "delete", "user", "test-user1@pve"},
}
Test.StandardTest(t)
}
}

0 comments on commit 2d62d11

Please sign in to comment.