Skip to content

Commit

Permalink
basic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmfwolf committed Jun 6, 2023
1 parent 27f6ce8 commit cebd8da
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/api_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
package tests

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

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

type MockDoType func(req *http.Request) (*http.Response, error)

type MockClient struct {
MockDo MockDoType
}

func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return m.MockDo(req)
}

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
},
}

authenticator := authentication.NewAuthenticator()

client := api.Client{
HttpClient: &httpmock,
Auth: authenticator,
OLdomain: "http://localhost",
}

_, err := client.Get("/test", nil)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
}

// Other tests go here.
63 changes: 63 additions & 0 deletions tests/authentication_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
package tests

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

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

func TestGenerateToken(t *testing.T) {
// Setup environment variables
os.Setenv("ONELOGIN_CLIENT_ID", "test-id")
os.Setenv("ONELOGIN_CLIENT_SECRET", "test-secret")

// Create an HTTP test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := map[string]interface{}{
"access_token": "test-token",
}

json.NewEncoder(w).Encode(response)
}))
defer server.Close()

// Update the domain in the Authenticator
authenticator := authentication.NewAuthenticator()
*authenticator.OLDomain = server.URL

// Generate a token
token, err := authenticator.GenerateToken()
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}

if token != "test-token" {
t.Errorf("Expected token 'test-token', but got '%v'", token)
}
}

func TestRevokeToken(t *testing.T) {
// Setup environment variables
os.Setenv("ONELOGIN_CLIENT_ID", "test-id")
os.Setenv("ONELOGIN_CLIENT_SECRET", "test-secret")

// Create an HTTP test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()

// Update the domain in the Authenticator
authenticator := authentication.NewAuthenticator()

// Revoke a token
domain := server.URL
token := "test-token"
err := authenticator.RevokeToken(&token, &domain)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
}

0 comments on commit cebd8da

Please sign in to comment.