Skip to content

Commit

Permalink
Improve config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 23, 2024
1 parent a9e3c81 commit e39edd9
Showing 1 changed file with 87 additions and 34 deletions.
121 changes: 87 additions & 34 deletions pkg/handlers/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,54 @@ limitations under the License.
package handlers

import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"

"bou.ke/monkey"
"github.com/gin-gonic/gin"
"github.com/grycap/oscar/v3/pkg/types"
"github.com/grycap/oscar/v3/pkg/utils/auth"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
testclient "k8s.io/client-go/kubernetes/fake"
)

func createExpectedBody(access_key string, secret_key string, cfg *types.Config) map[string]interface{} {
return map[string]interface{}{
"config": map[string]interface{}{
"name": "",
"namespace": "",
"services_namespace": "",
"gpu_available": false,
"interLink_available": false,
"yunikorn_enable": false,
},
"minio_provider": map[string]interface{}{
"endpoint": cfg.MinIOProvider.Endpoint,
"verify": cfg.MinIOProvider.Verify,
"access_key": access_key,
"secret_key": secret_key,
"region": cfg.MinIOProvider.Region,
},
}
}

func TestMakeConfigHandler(t *testing.T) {
gin.SetMode(gin.TestMode)

cfg := &types.Config{
// Initialize with necessary fields
MinIOProvider: &types.MinIOProvider{
Endpoint: "http://minio.example.com",
Verify: true,
Region: "us-east-1",
Endpoint: "http://minio.example.com",
Verify: true,
Region: "us-east-1",
AccessKey: "accessKey1",
SecretKey: "secretKey1",
},
}

Expand All @@ -55,54 +85,77 @@ func TestMakeConfigHandler(t *testing.T) {

})

/*
K8sObjects := []runtime.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret",
Namespace: "namespace",
},
K8sObjects := []runtime.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "somelonguserid",
Namespace: auth.ServicesNamespace,
},
}
Data: map[string][]byte{
"accessKey": []byte("accessKey"),
"secretKey": []byte("secretKey"),
},
},
}

kubeClientset := testclient.NewSimpleClientset(K8sObjects...)
t.Run("With Authorization Header", func(t *testing.T) {
router := gin.New()
router.GET("/config", MakeConfigHandler(cfg))
kubeClientset := testclient.NewSimpleClientset(K8sObjects...)
t.Run("With Bearer Authorization Header", func(t *testing.T) {
router := gin.New()
router.GET("/config", MakeConfigHandler(cfg))

req, _ := http.NewRequest("GET", "/config", nil)
req.Header.Set("Authorization", "Bearer some-token")
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/config", nil)
req.Header.Set("Authorization", "Bearer some-token")
w := httptest.NewRecorder()

// Mocking auth functions
monkey.Patch(auth.GetUIDFromContext, func(c *gin.Context) (string, error) {
return "[email protected]", nil
})
// Mocking auth functions
monkey.Patch(auth.GetUIDFromContext, func(c *gin.Context) (string, error) {
return "[email protected]", nil
})

monkey.Patch(auth.GetMultitenancyConfigFromContext, func(c *gin.Context) (*auth.MultitenancyConfig, error) {
return auth.NewMultitenancyConfig(kubeClientset, "[email protected]"), nil
})
monkey.Patch(auth.GetMultitenancyConfigFromContext, func(c *gin.Context) (*auth.MultitenancyConfig, error) {
return auth.NewMultitenancyConfig(kubeClientset, "[email protected]"), nil
})

router.ServeHTTP(w, req)
router.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Fatalf("expected status code 200, got %d", w.Code)
}
})
*/
if w.Code != http.StatusOK {
t.Fatalf("expected status code 200, got %d", w.Code)
}

t.Run("With Invalid Authorization Header", func(t *testing.T) {
expected_body := createExpectedBody("accessKey", "secretKey", cfg)

var responseBody map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &responseBody); err != nil {
t.Fatalf("Failed to parse response body: %v", err)
}

if !reflect.DeepEqual(responseBody, expected_body) {
t.Fatalf("Unexpected response body: %s", w.Body.String())
}
})

t.Run("With Token Authorization Header", func(t *testing.T) {
router := gin.New()
router.GET("/config", MakeConfigHandler(cfg))

req, _ := http.NewRequest("GET", "/config", nil)
req.Header.Set("Authorization", "InvalidToken")
req.Header.Set("Authorization", "SomeToken")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Fatalf("expected status code 200, got %d", w.Code)
}
//assert.Contains(t, w.Body.String(), "http://minio.example.com")

expected_body := createExpectedBody("accessKey1", "secretKey1", cfg)

var responseBody map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &responseBody); err != nil {
t.Fatalf("Failed to parse response body: %v", err)
}

if !reflect.DeepEqual(responseBody, expected_body) {
t.Fatalf("Unexpected response body: %s", w.Body.String())
}
})
}

0 comments on commit e39edd9

Please sign in to comment.