github.com/go-chef/chef@v0.30.1/testapi/authenticate.go (about)

     1  // Test the go-chef/chef chef authenticate_user api endpoint against a live server
     2  package testapi
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/go-chef/chef"
     9  )
    10  
    11  // authenticate exercise the chef server api
    12  func Authenticate() {
    13  	// Create a client for access
    14  	client := Client(nil)
    15  
    16  	// Create a user
    17  	var usr chef.User
    18  	usr = chef.User{UserName: "usrauth",
    19  		Email:       "usrauth@domain.io",
    20  		FirstName:   "usrauth",
    21  		LastName:    "fullname",
    22  		DisplayName: "Userauth Fullname",
    23  		Password:    "Logn12ComplexPwd#",
    24  	}
    25  	createUser_auth(client, usr)
    26  
    27  	var ar chef.Authenticate
    28  	// Authenticate with a valid password
    29  	ar.UserName = "usrauth"
    30  	ar.Password = "Logn12ComplexPwd#"
    31  	err := client.AuthenticateUser.Authenticate(ar)
    32  	fmt.Printf("Authenticate with a valid password %+vauthenticate\n", err)
    33  
    34  	// Authenticate with an invalid password
    35  	ar.Password = "badpassword"
    36  	err = client.AuthenticateUser.Authenticate(ar)
    37  	fmt.Printf("Authenticate with an invalid password %+v\n", err)
    38  
    39  	// Cleanup
    40  	deleteUser_auth(client, "usrauth")
    41  }
    42  
    43  // createUser_auth uses the chef server api to create a single user
    44  func createUser_auth(client *chef.Client, user chef.User) chef.UserResult {
    45  	usrResult, err := client.Users.Create(user)
    46  	if err != nil {
    47  		fmt.Fprintln(os.Stderr, "Issue creating user:", err)
    48  	}
    49  	return usrResult
    50  }
    51  
    52  // deleteUser_auth uses the chef server api to delete a single user
    53  func deleteUser_auth(client *chef.Client, name string) (err error) {
    54  	err = client.Users.Delete(name)
    55  	if err != nil {
    56  		fmt.Fprintln(os.Stderr, "Issue deleting org:", err)
    57  	}
    58  	return
    59  }