Skip to content

Commit

Permalink
test: Add a test to SDK GetGroup using a mock http server
Browse files Browse the repository at this point in the history
  • Loading branch information
mariatsji committed Jan 8, 2025
1 parent b0f9874 commit a65fffd
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/sdk/cloudian/sdk_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,71 @@
package cloudian

import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"testing/quick"
)

func TestGetGroup(t *testing.T) {
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"active": "true",
"groupId": "QA",
"groupName": "Quality Assurance Group",
"ldapEnabled": false,
"ldapGroup": "",
"ldapMatchAttribute": "",
"ldapSearch": "",
"ldapSearchUserBase": "",
"ldapServerURL": "",
"ldapUserDNTemplate": "",
"s3endpointshttp": ["ALL"],
"s3endpointshttps": ["ALL"],
"s3websiteendpoints": ["ALL"]
}`))
}))
defer mockServer.Close()

mockClient := &http.Client{}

req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, mockServer.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}

resp, err := mockClient.Do(req)
if err != nil {
t.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()

cloudianClient := Client{
baseURL: mockServer.URL,
httpClient: mockClient,
authHeader: "",
}

group, err := cloudianClient.GetGroup(context.TODO(), "QA")

if err != nil {
t.Errorf("Error getting group: %v", err)
}

if group.GroupID != "QA" {
t.Errorf("Expected QA, got %v", group.GroupID)
}
}

func TestRealisticGroupSerialization(t *testing.T) {
jsonString := `{
"active": "true",
Expand Down

0 comments on commit a65fffd

Please sign in to comment.