@ -21,6 +21,7 @@ func TestMutator(t *testing.T) {
expectedToken string
expectedEncryptedToken string
expectedError string
expectedURL string
} {
{
name : "successful token encryption" ,
@ -30,6 +31,7 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
Token : "secret-token" ,
} ,
@ -44,6 +46,7 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
Token : "secret-token" ,
} ,
@ -64,6 +67,7 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
Token : "secret-token" ,
} ,
@ -78,6 +82,7 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
Token : "secret-token" ,
} ,
@ -97,13 +102,32 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Git : nil ,
Type : provisioning . LocalRepositoryType ,
Git : nil ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
} ,
{
name : "no git spec for git repository type" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : nil ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedError : "git configuration is required for git repository type" ,
} ,
{
name : "empty token" ,
obj : & provisioning . Repository {
@ -112,6 +136,7 @@ func TestMutator(t *testing.T) {
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
Token : "" ,
} ,
@ -128,6 +153,101 @@ func TestMutator(t *testing.T) {
// No expectations
} ,
} ,
{
name : "URL normalization - add .git suffix" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
URL : "https://github.com/grafana/grafana" ,
} ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedURL : "https://github.com/grafana/grafana.git" ,
} ,
{
name : "URL normalization - keep existing .git suffix" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
URL : "https://github.com/grafana/grafana.git" ,
} ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedURL : "https://github.com/grafana/grafana.git" ,
} ,
{
name : "URL normalization - remove trailing slash and add .git" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
URL : "https://github.com/grafana/grafana/" ,
} ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedURL : "https://github.com/grafana/grafana.git" ,
} ,
{
name : "URL normalization - trim whitespace and add .git" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
URL : " https://github.com/grafana/grafana " ,
} ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedURL : "https://github.com/grafana/grafana.git" ,
} ,
{
name : "URL normalization - empty URL after trim" ,
obj : & provisioning . Repository {
ObjectMeta : metav1 . ObjectMeta {
Name : "test-repo" ,
Namespace : "default" ,
} ,
Spec : provisioning . RepositorySpec {
Type : provisioning . GitRepositoryType ,
Git : & provisioning . GitRepositoryConfig {
URL : " " ,
} ,
} ,
} ,
setupMocks : func ( mockSecrets * secrets . MockRepositorySecrets ) {
// No expectations
} ,
expectedURL : "" ,
} ,
}
for _ , tt := range tests {
@ -152,6 +272,11 @@ func TestMutator(t *testing.T) {
// EncryptedToken should be set to the expected value
assert . Equal ( t , tt . expectedEncryptedToken , string ( repo . Spec . Git . EncryptedToken ) , "EncryptedToken should match expected value" )
}
// Check URL normalization
if tt . expectedURL != "" {
assert . Equal ( t , tt . expectedURL , repo . Spec . Git . URL , "URL should be normalized correctly" )
}
}
}
} )