Skip to content

Commit

Permalink
test: add unit test for proxy (#1092)
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <[email protected]>
Co-authored-by: Sertaç Özercan <[email protected]>
  • Loading branch information
aramase and sozercan committed Aug 24, 2023
1 parent 4a889b7 commit 41f2e5e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -340,3 +341,56 @@ func TestGetScope(t *testing.T) {
})
}
}

func TestNewProxy(t *testing.T) {
testLogger := mlog.New()
tests := []struct {
name string
tenantID string
authorityHost string
expected *proxy
expectedErr string
}{
{
name: "tenant id not set",
tenantID: "",
authorityHost: "https://login.microsoftonline.com/",
expected: nil,
expectedErr: "AZURE_TENANT_ID not set",
},
{
name: "authority host not set",
tenantID: "tenant_id",
authorityHost: "",
expected: nil,
expectedErr: "AZURE_AUTHORITY_HOST not set",
},
{
name: "valid tenant id and authority host",
tenantID: "tenant_id",
authorityHost: "https://login.microsoftonline.com/",
expected: &proxy{logger: testLogger, tenantID: "tenant_id", authorityHost: "https://login.microsoftonline.com/", port: 8000},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
os.Setenv(webhook.AzureTenantIDEnvVar, test.tenantID)
defer os.Unsetenv(webhook.AzureTenantIDEnvVar)

os.Setenv(webhook.AzureAuthorityHostEnvVar, test.authorityHost)
defer os.Unsetenv(webhook.AzureAuthorityHostEnvVar)

got, err := NewProxy(8000, testLogger)
if err != nil && err.Error() != test.expectedErr {
t.Errorf("expected error %s, got %s", test.expectedErr, err.Error())
}
if err == nil && test.expectedErr != "" {
t.Errorf("expected error %s, got none", test.expectedErr)
}
if test.expected != nil && !reflect.DeepEqual(got, test.expected) {
t.Errorf("expected proxy %v, got %v", test.expected, got)
}
})
}
}

0 comments on commit 41f2e5e

Please sign in to comment.