|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/argoproj-labs/argocd-image-updater/ext/git" |
| 9 | + app_utils "github.com/docplanner/helm-repo-updater/internal/app/utils" |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "gotest.tools/v3/assert" |
| 12 | +) |
| 13 | + |
| 14 | +const validGitCredentialsEmail = "[email protected]" |
| 15 | +const validGitCredentialsUsername = "test-user" |
| 16 | +const validGitCredentialsPassword = "test-password" |
| 17 | +const validSSHPrivKeyRelativeRoute = "/test-git-server/private_keys/helm-repo-updater-test" |
| 18 | +const validGitRepoSSHURL = "[email protected]:kubernetes/kubernetes.git" |
| 19 | +const validGitRepoHTTPSURL = "https://github.com/kubernetes/kubernetes.git" |
| 20 | +const invalidGitRepoURL = "github.com/kubernetes/kubernetes.git" |
| 21 | + |
| 22 | +func TestNewCredsSSHURLSShPrivKey(t *testing.T) { |
| 23 | + |
| 24 | + sshPrivKeyRoute, err := app_utils.GetRouteRelativePath(2, validSSHPrivKeyRelativeRoute) |
| 25 | + if err != nil { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + g := Credentials{ |
| 30 | + Email: validGitCredentialsEmail, |
| 31 | + SSHPrivKey: *sshPrivKeyRoute, |
| 32 | + } |
| 33 | + |
| 34 | + repoURL := validGitRepoSSHURL |
| 35 | + |
| 36 | + creds, err := g.NewGitCreds(repoURL) |
| 37 | + |
| 38 | + if err != nil { |
| 39 | + log.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + expectedCreds := git.NewSSHCreds(*sshPrivKeyRoute, "", true) |
| 43 | + |
| 44 | + assert.DeepEqual(t, creds, expectedCreds, cmp.AllowUnexported(git.SSHCreds{})) |
| 45 | +} |
| 46 | + |
| 47 | +func TestNewCredsHTPPSURLUsernamePassword(t *testing.T) { |
| 48 | + |
| 49 | + g := Credentials{ |
| 50 | + Email: validGitCredentialsEmail, |
| 51 | + Username: validGitCredentialsUsername, |
| 52 | + Password: validGitCredentialsPassword, |
| 53 | + } |
| 54 | + |
| 55 | + creds, err := g.NewGitCreds(validGitRepoHTTPSURL) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + expectedCreds := git.NewHTTPSCreds(g.Username, g.Password, "", "", true, "") |
| 62 | + |
| 63 | + assert.DeepEqual(t, creds, expectedCreds, cmp.AllowUnexported(git.HTTPSCreds{})) |
| 64 | +} |
| 65 | + |
| 66 | +func TestNewCredsSSHURLWithoutSShPrivKey(t *testing.T) { |
| 67 | + |
| 68 | + g := Credentials{ |
| 69 | + Email: validGitCredentialsEmail, |
| 70 | + SSHPrivKey: "", |
| 71 | + } |
| 72 | + |
| 73 | + repoURL := validGitRepoSSHURL |
| 74 | + |
| 75 | + _, err := g.NewGitCreds(repoURL) |
| 76 | + |
| 77 | + expectedErrorMessage := fmt.Sprintf( |
| 78 | + "sshPrivKey not provided for authenticatication to repository %s", |
| 79 | + repoURL, |
| 80 | + ) |
| 81 | + |
| 82 | + assert.Error(t, err, expectedErrorMessage) |
| 83 | +} |
| 84 | + |
| 85 | +func TestNewCredsHTPPSURLWithoutUsernameWithPassword(t *testing.T) { |
| 86 | + |
| 87 | + g := Credentials{ |
| 88 | + Email: validGitCredentialsEmail, |
| 89 | + Username: "", |
| 90 | + Password: validGitCredentialsPassword, |
| 91 | + } |
| 92 | + |
| 93 | + repoURL := validGitRepoHTTPSURL |
| 94 | + |
| 95 | + _, err := g.NewGitCreds(repoURL) |
| 96 | + |
| 97 | + expectedErrorMessage := fmt.Sprintf( |
| 98 | + "no value provided for username and password for authentication to repository %s", |
| 99 | + repoURL, |
| 100 | + ) |
| 101 | + |
| 102 | + assert.Error(t, err, expectedErrorMessage) |
| 103 | +} |
| 104 | + |
| 105 | +func TestNewCredsHTPPSURLWitUsernameWithoutPassword(t *testing.T) { |
| 106 | + |
| 107 | + g := Credentials{ |
| 108 | + Email: validGitCredentialsEmail, |
| 109 | + Username: validGitCredentialsUsername, |
| 110 | + Password: "", |
| 111 | + } |
| 112 | + |
| 113 | + repoURL := validGitRepoHTTPSURL |
| 114 | + |
| 115 | + _, err := g.NewGitCreds(repoURL) |
| 116 | + |
| 117 | + expectedErrorMessage := fmt.Sprintf( |
| 118 | + "no value provided for username and password for authentication to repository %s", |
| 119 | + repoURL, |
| 120 | + ) |
| 121 | + |
| 122 | + assert.Error(t, err, expectedErrorMessage) |
| 123 | +} |
| 124 | + |
| 125 | +func TestNewCredsInvalidURL(t *testing.T) { |
| 126 | + |
| 127 | + g := Credentials{ |
| 128 | + Email: validGitCredentialsEmail, |
| 129 | + Username: validGitCredentialsUsername, |
| 130 | + Password: validGitCredentialsPassword, |
| 131 | + } |
| 132 | + |
| 133 | + repoURL := invalidGitRepoURL |
| 134 | + |
| 135 | + _, err := g.NewGitCreds(repoURL) |
| 136 | + |
| 137 | + expectedErrorMessage := fmt.Sprintf( |
| 138 | + "unknown repository type for git repository URL %s", |
| 139 | + repoURL, |
| 140 | + ) |
| 141 | + |
| 142 | + assert.Error(t, err, expectedErrorMessage) |
| 143 | +} |
0 commit comments