Skip to content

Commit

Permalink
broken testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jun 13, 2023
1 parent e998ffd commit 320ed3d
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 118 deletions.
2 changes: 1 addition & 1 deletion internal/authentication/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
olError "github.com/onelogin/onelogin-go-sdk/internal/error"
)

const (
var (
TokenPath string = "/auth/oauth2/v2/token"
RevokePath string = "/auth/oauth2/revoke"
)
Expand Down
40 changes: 40 additions & 0 deletions internal/authentication/mock_authenticator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package authentication

import (
"errors"
)

type MockAuthenticator struct {
GetTokenFunc func() (string, error)
NewAuthenticatorFunc func() Authenticator
GenerateTokenFunc func() error
RevokeTokenFunc func(token, domain *string) error
}

func (a *MockAuthenticator) GetToken() (string, error) {
if a.GetTokenFunc != nil {
return a.GetTokenFunc()
}
return "", errors.New("GetTokenFunc is not implemented")
}

func (a *MockAuthenticator) NewAuthenticatorF() Authenticator {
if a.NewAuthenticatorFunc != nil {
return a.NewAuthenticatorFunc()
}
return *NewAuthenticator()
}

func (a *MockAuthenticator) GenerateToken() error {
if a.GenerateTokenFunc != nil {
return a.GenerateTokenFunc()
}
return errors.New("GenerateTokenFunc is not implemented")
}

func (a *MockAuthenticator) RevokeToken(token, domain *string) error {
if a.RevokeTokenFunc != nil {
return a.RevokeTokenFunc(token, domain)
}
return errors.New("RevokeTokenFunc is not implemented")
}
277 changes: 195 additions & 82 deletions tests/api_test.go
Original file line number Diff line number Diff line change
@@ -1,116 +1,229 @@
package tests

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

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

type MockDoType func(req *http.Request) (*http.Response, error)
// TestNewClient tests the NewClient function.
func TestNewClient(t *testing.T) {
// Set up environment variables
os.Setenv("ONELOGIN_SUBDOMAIN", "test")

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

// Check that the client was created successfully
if client == 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")
}
}

func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return m.MockDo(req)
// TestGet tests the Get method.
func TestGet(t *testing.T) {
// Create a new client
client := &api.Client{
HttpClient: http.DefaultClient,
Auth: authentication.NewAuthenticator(),
OLdomain: "https://test.onelogin.com",
}

// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Write a response
w.WriteHeader(http.StatusOK)
w.Write([]byte("test"))
}))
defer ts.Close()

// Send a test request
path := "/test"
queryParams := models.UserQuery{}
resp, err := client.Get(&path, &queryParams)
if err != nil {
t.Errorf("Error sending request: %v", err)
}

// Check that the response was received successfully
if resp == nil {
t.Error("Response was not received")
}

// Check that the response's body was read successfully
if resp == nil || resp.Body == nil {
t.Error("Response body is nil")
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
if string(body) != "test" {
t.Errorf("Response body was %s, expected test", string(body))
}
}

func TestNewRequest(t *testing.T) {
httpmock := MockClient{
MockDo: func(req *http.Request) (*http.Response, error) {
resp := http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
}
return &resp, nil
},
// TestDelete tests the Delete method.
func TestDelete(t *testing.T) {
// Create a new client
client := &api.Client{
HttpClient: http.DefaultClient,
Auth: authentication.NewAuthenticator(),
OLdomain: "https://test.onelogin.com",
}

authenticator := authentication.NewAuthenticator()
// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Write a response
w.WriteHeader(http.StatusOK)
w.Write([]byte("test"))
}))
defer ts.Close()

// Send a test request
path := "/test"
resp, err := client.Delete(&path)
if err != nil {
t.Errorf("Error sending request: %v", err)
}

client := api.Client{
HttpClient: &httpmock,
Auth: authenticator,
OLdomain: "http://localhost",
// Check that the response was received successfully
if resp == nil || resp.Body == nil {
t.Error("Response was not received")
}
test := "/test"
resp, err := client.Get(&test, nil)

// Check that the response's body was read successfully
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
t.Errorf("Error reading response body: %v", err)
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
if string(body) != "test" {
t.Errorf("Response body was %s, expected test", string(body))
}
}

func TestGet(t *testing.T) {
// Create a mock HTTP client
httpmock := MockClient{
MockDo: func(req *http.Request) (*http.Response, error) {
// Modify the response based on the test case
if req.URL.Path == "/test" && req.Method == "GET" {
// Successful case
resp := http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
}
return &resp, nil
} else if req.URL.Path == "/test" && req.Method == "POST" {
// Unsuccessful case - incorrect method
resp := http.Response{
StatusCode: http.StatusMethodNotAllowed,
Body: ioutil.NopCloser(bytes.NewBufferString(`Method not allowed`)),
}
return &resp, nil
} else {
// Unsuccessful case - unexpected request
resp := http.Response{
StatusCode: http.StatusNotFound,
Body: ioutil.NopCloser(bytes.NewBufferString(`Not found`)),
}
return &resp, nil
}
},
}

// Create an API client with the mock HTTP client
authenticator := authentication.NewAuthenticator()
client := api.Client{
HttpClient: &httpmock,
Auth: authenticator,
OLdomain: "http://localhost",
}

// Test successful GET request
test := "/test"
resp, err := client.Get(&test, nil)
// TestPost tests the Post method.
func TestPost(t *testing.T) {
// Create a new client
client := &api.Client{
HttpClient: http.DefaultClient,
Auth: authentication.NewAuthenticator(),
OLdomain: "https://test.onelogin.com",
}

// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check that the request's body was read correctly
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if string(body) != "test" {
t.Errorf("Request body was %s, expected test", string(body))
}

// Write a response
w.WriteHeader(http.StatusOK)
w.Write([]byte("test"))
}))
defer ts.Close()

// Send a test request
path := "/test"
body := "test"
resp, err := client.Post(&path, &body)
if err != nil {
t.Errorf("Error sending request: %v", err)
}

// Check that the response was received successfully
if resp == nil || resp.Body == nil {
t.Error("Response was not received")
}

// Check that the response's body was read successfully
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
respBody := string(bodyBytes)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
t.Errorf("Error reading response body: %v", err)
}
if string(body) != "test" {
t.Errorf("Response body was %s, expected test", string(respBody))
}
}

// TestPut tests the Put method.
func TestPut(t *testing.T) {
// Create a new client
client := &api.Client{
HttpClient: http.DefaultClient,
Auth: authentication.NewAuthenticator(),
OLdomain: "https://test.onelogin.com",
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)

// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check that the request's body was read correctly
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if string(body) != "test" {
t.Errorf("Request body was %s, expected test", string(body))
}

// Write a response
w.WriteHeader(http.StatusOK)
w.Write([]byte("test"))
}))
defer ts.Close()

// Send a test request
path := "/test"
body := "test"
resp, err := client.Put(&path, &body)
if err != nil {
t.Errorf("Error sending request: %v", err)
}

// Test unsuccessful POST request
resp, err = client.Post(&test, nil)
if err == nil {
t.Error("Expected error, but got no error")
// Check that the response was received successfully
if resp == nil || resp.Body == nil {
t.Error("Response was not received")
}
if resp != nil && resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("Expected status code %d, but got %d", http.StatusMethodNotAllowed, resp.StatusCode)

// Check that the response's body was read successfully
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
uhoh := "/unexpected"
// Test unsuccessful GET request with unexpected path
resp, err = client.Get(&uhoh, nil)
if err == nil {
t.Error("Expected error, but got no error")
respBody := string(bodyBytes)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
if resp != nil && resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status code %d, but got %d", http.StatusNotFound, resp.StatusCode)
if string(body) != "test" {
t.Errorf("Response body was %s, expected test", string(respBody))
}
}
Loading

0 comments on commit 320ed3d

Please sign in to comment.