-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |