Skip to content

Commit

Permalink
test: improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gitahernandez committed Mar 8, 2024
1 parent 52e6cc9 commit b508bdf
Show file tree
Hide file tree
Showing 6 changed files with 1,222 additions and 32 deletions.
11 changes: 9 additions & 2 deletions TestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
managed_accounts "go-client-library-passwordsafe/api/managed_account"
"go-client-library-passwordsafe/api/secrets"
"go-client-library-passwordsafe/api/utils"
"time"

//"os"

backoff "github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
)

Expand All @@ -34,11 +36,16 @@ func main() {
retryMaxElapsedTimeMinutes := 2
maxFileSecretSizeBytes := 5000000

backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.InitialInterval = 1 * time.Second
backoffDefinition.MaxElapsedTime = time.Duration(retryMaxElapsedTimeMinutes) * time.Second
backoffDefinition.RandomizationFactor = 0.5

//certificate = os.Getenv("CERTIFICATE")
//certificateKey = os.Getenv("CERTIFICATE_KEY")

// validate inputs
errorsInInputs := utils.ValidateInputs(clientId, clientSecret, apiUrl, clientTimeOutInSeconds, &separator, verifyCa, zapLogger, certificate, certificateKey, &retryMaxElapsedTimeMinutes, &maxFileSecretSizeBytes)
errorsInInputs := utils.ValidateInputs(clientId, clientSecret, &apiUrl, clientTimeOutInSeconds, &separator, verifyCa, zapLogger, certificate, certificateKey, &retryMaxElapsedTimeMinutes, &maxFileSecretSizeBytes)

if errorsInInputs != nil {
return
Expand All @@ -48,7 +55,7 @@ func main() {
httpClientObj, _ := utils.GetHttpClient(clientTimeOutInSeconds, verifyCa, certificate, certificateKey, zapLogger)

// instantiating authenticate obj, injecting httpClient object
authenticate, _ := authentication.Authenticate(*httpClientObj, apiUrl, clientId, clientSecret, zapLogger, retryMaxElapsedTimeMinutes)
authenticate, _ := authentication.Authenticate(*httpClientObj, backoffDefinition, apiUrl, clientId, clientSecret, zapLogger, retryMaxElapsedTimeMinutes)

// authenticating
_, err := authenticate.GetPasswordSafeAuthentication()
Expand Down
22 changes: 18 additions & 4 deletions api/authentication/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"net/url"
"reflect"
"testing"
"time"

backoff "github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -42,7 +44,10 @@ func TestSignOut(t *testing.T) {

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := UserTestConfig{
name: "TestSignOut",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -69,7 +74,10 @@ func TestSignAppin(t *testing.T) {

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := UserTestConfig{
name: "TestSignAppin",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -103,7 +111,10 @@ func TestGetToken(t *testing.T) {

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetTokenConfig{
name: "TestGetToken",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -142,7 +153,10 @@ func TestGetPasswordSafeAuthentication(t *testing.T) {

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetPasswordSafeAuthenticationConfig{
name: "TestGetPasswordSafeAuthentication",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 1 addition & 6 deletions api/authentication/authetication.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"

"net/url"
"time"

backoff "github.com/cenkalti/backoff/v4"
)
Expand All @@ -28,12 +27,8 @@ type AuthenticationObj struct {

// Authenticate is responsible for Auth configuration.
// Prerequisites - use input validation methods before using this class.
func Authenticate(httpClient utils.HttpClientObj, endpointUrl string, clientId string, clientSecret string, logger logging.Logger, retryMaxElapsedTimeSeconds int) (*AuthenticationObj, error) {
func Authenticate(httpClient utils.HttpClientObj, backoffDefinition *backoff.ExponentialBackOff, endpointUrl string, clientId string, clientSecret string, logger logging.Logger, retryMaxElapsedTimeSeconds int) (*AuthenticationObj, error) {

backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.InitialInterval = 1 * time.Second
backoffDefinition.MaxElapsedTime = time.Duration(retryMaxElapsedTimeSeconds) * time.Second
backoffDefinition.RandomizationFactor = 0.5
apiUrl, _ := url.Parse(endpointUrl)
authenticationObj := &AuthenticationObj{
ApiUrl: *apiUrl,
Expand Down
Loading

0 comments on commit b508bdf

Please sign in to comment.