Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jun 14, 2023
1 parent cfaf3b7 commit c50ae6d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: "0.2.1"
version: "0.4.0"
66 changes: 66 additions & 0 deletions internal/api/mock_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package api

import (
"bytes"
"errors"
"net/http"

"github.com/onelogin/onelogin-go-sdk/internal/authentication"
)

type MockClient struct {
Auth *authentication.MockAuthenticator // Authenticator for managing authentication
OLdomain string // OneLogin domain
}

func (c *MockClient) Post(path *string, body interface{}) (*http.Response, error) {
token, err := c.Auth.GetToken()
if err != nil {
return nil, err
}

// Use the token in the request
bodyString, ok := body.(string)
if !ok {
return nil, errors.New("invalid request body")
}
req, err := http.NewRequest("POST", c.OLdomain+*path, bytes.NewBufferString(bodyString))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

func (c *MockClient) Put(path *string, body interface{}) (*http.Response, error) {
token, err := c.Auth.GetToken()
if err != nil {
return nil, err
}

// Use the token in the request
bodyString, ok := body.(string)
if !ok {
return nil, errors.New("invalid request body")
}
req, err := http.NewRequest("PUT", c.OLdomain+*path, bytes.NewBufferString(bodyString))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}
16 changes: 5 additions & 11 deletions tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@ import (
func TestNewClient(t *testing.T) {
// Set up environment variables
os.Setenv("ONELOGIN_SUBDOMAIN", "test")
os.Setenv("ONELOGIN_CLIENT_ID", "test")
os.Setenv("ONELOGIN_CLIENT_SECRET", "test")

// Create a new client
client, err := api.NewClient()
if err != nil {
t.Errorf("Error creating client: %v", err)
return
}
// Create a new mock client
mockClient := &api.MockClient{}

// Check that the client was created successfully
if client == nil {
if mockClient == nil {
t.Error("Client was not created")
return
}

// Check that the client's Authenticator was created successfully
if client.Auth == nil {
t.Error("Authenticator was not created")
}
}

// TestGet tests the Get method.
Expand Down

0 comments on commit c50ae6d

Please sign in to comment.