|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +load 'test-helper' |
| 4 | + |
| 5 | +setup_file() { |
| 6 | + if ! helm_supports_credentials; then |
| 7 | + echo "# Skipping credential tests - Helm version < 3.14.0 does not support credential passing" >&2 |
| 8 | + fi |
| 9 | +} |
| 10 | + |
| 11 | +@test "should setup git credentials when HELM_PLUGIN_USERNAME and HELM_PLUGIN_PASSWORD are set" { |
| 12 | + if ! helm_supports_credentials; then |
| 13 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 14 | + fi |
| 15 | + |
| 16 | + export HELM_PLUGIN_USERNAME="testuser" |
| 17 | + export HELM_PLUGIN_PASSWORD="testpass" |
| 18 | + |
| 19 | + # Source the plugin and check that credentials are stored |
| 20 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "git_username=${git_username}" && echo "git_password=${git_password}"' |
| 21 | + [ $status = 0 ] |
| 22 | + [[ "$output" == *"git_username=testuser"* ]] |
| 23 | + [[ "$output" == *"git_password=testpass"* ]] |
| 24 | + |
| 25 | + # Check that the original HELM_PLUGIN_* variables are unset for security |
| 26 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "HELM_PLUGIN_USERNAME=${HELM_PLUGIN_USERNAME:-unset}" && echo "HELM_PLUGIN_PASSWORD=${HELM_PLUGIN_PASSWORD:-unset}"' |
| 27 | + [ $status = 0 ] |
| 28 | + [[ "$output" == *"HELM_PLUGIN_USERNAME=unset"* ]] |
| 29 | + [[ "$output" == *"HELM_PLUGIN_PASSWORD=unset"* ]] |
| 30 | + |
| 31 | + # Check that the internal git_* variables are NOT exported to child processes |
| 32 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && bash -c "echo git_username=\${git_username:-unset}; echo git_password=\${git_password:-unset}"' |
| 33 | + [ $status = 0 ] |
| 34 | + [[ "$output" == *"git_username=unset"* ]] |
| 35 | + [[ "$output" == *"git_password=unset"* ]] |
| 36 | +} |
| 37 | + |
| 38 | +@test "should not setup git credentials when HELM_PLUGIN_USERNAME is missing" { |
| 39 | + if ! helm_supports_credentials; then |
| 40 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 41 | + fi |
| 42 | + |
| 43 | + unset HELM_PLUGIN_USERNAME |
| 44 | + export HELM_PLUGIN_PASSWORD="testpass" |
| 45 | + |
| 46 | + # Source the plugin and verify git_username is empty |
| 47 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "git_username=${git_username:-empty}"' |
| 48 | + [ $status = 0 ] |
| 49 | + [[ "$output" == *"git_username=empty"* ]] |
| 50 | +} |
| 51 | + |
| 52 | +@test "should setup git credentials with username only (empty password allowed)" { |
| 53 | + if ! helm_supports_credentials; then |
| 54 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 55 | + fi |
| 56 | + |
| 57 | + export HELM_PLUGIN_USERNAME="testuser" |
| 58 | + unset HELM_PLUGIN_PASSWORD |
| 59 | + |
| 60 | + # Source the plugin and verify credentials are set (username without password is allowed) |
| 61 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "git_username=${git_username}" && echo "git_password=${git_password:-empty}"' |
| 62 | + [ $status = 0 ] |
| 63 | + [[ "$output" == *"git_username=testuser"* ]] |
| 64 | + [[ "$output" == *"git_password=empty"* ]] |
| 65 | +} |
| 66 | + |
| 67 | +@test "should not setup git credentials when both are missing" { |
| 68 | + if ! helm_supports_credentials; then |
| 69 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 70 | + fi |
| 71 | + |
| 72 | + unset HELM_PLUGIN_USERNAME |
| 73 | + unset HELM_PLUGIN_PASSWORD |
| 74 | + |
| 75 | + # Source the plugin and verify both are empty |
| 76 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "git_username=${git_username:-empty}" && echo "git_password=${git_password:-empty}"' |
| 77 | + [ $status = 0 ] |
| 78 | + [[ "$output" == *"git_username=empty"* ]] |
| 79 | + [[ "$output" == *"git_password=empty"* ]] |
| 80 | +} |
| 81 | + |
| 82 | +@test "helm_git main should use credentials with username and password" { |
| 83 | + if ! helm_supports_credentials; then |
| 84 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 85 | + fi |
| 86 | + |
| 87 | + export HELM_PLUGIN_USERNAME="testuser" |
| 88 | + export HELM_PLUGIN_PASSWORD="testpass" |
| 89 | + export HELM_GIT_TRACE=1 |
| 90 | + |
| 91 | + # Test that git_cmd uses credentials by checking trace output |
| 92 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && git_cmd --version 2>&1' |
| 93 | + |
| 94 | + # Check that the trace message about using credentials appears |
| 95 | + [[ "$output" == *"[git, username:testuser] --version"* ]] |
| 96 | + [[ "$output" == *"git version"* ]] |
| 97 | +} |
| 98 | + |
| 99 | +@test "git credential helper should work with environment variables" { |
| 100 | + if ! helm_supports_credentials; then |
| 101 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 102 | + fi |
| 103 | + |
| 104 | + export HELM_PLUGIN_USERNAME="testuser" |
| 105 | + export HELM_PLUGIN_PASSWORD="testpass" |
| 106 | + |
| 107 | + # Test the credential helper by simulating what git_cmd does |
| 108 | + run bash -c 'GIT_USERNAME="testuser" GIT_PASSWORD="testpass" bash <<EOF |
| 109 | +echo "username=\${GIT_USERNAME}" |
| 110 | +echo "password=\${GIT_PASSWORD}" |
| 111 | +EOF' |
| 112 | + [ $status = 0 ] |
| 113 | + [[ "$output" == *"username=testuser"* ]] |
| 114 | + [[ "$output" == *"password=testpass"* ]] |
| 115 | +} |
| 116 | + |
| 117 | +@test "should handle credentials with special characters" { |
| 118 | + if ! helm_supports_credentials; then |
| 119 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 120 | + fi |
| 121 | + |
| 122 | + export HELM_PLUGIN_USERNAME="user@domain.com" |
| 123 | + export HELM_PLUGIN_PASSWORD="pass/with/special&chars" |
| 124 | + |
| 125 | + # Source the plugin and verify credentials with special characters are stored |
| 126 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && echo "git_username=${git_username}" && echo "git_password=${git_password}"' |
| 127 | + [ $status = 0 ] |
| 128 | + [[ "$output" == *"git_username=user@domain.com"* ]] |
| 129 | + [[ "$output" == *"git_password=pass/with/special&chars"* ]] |
| 130 | + |
| 131 | + # Test that the credential helper can handle special characters |
| 132 | + run bash -c 'GIT_USERNAME="user@domain.com" GIT_PASSWORD="pass/with/special&chars" bash <<EOF |
| 133 | +echo "username=\${GIT_USERNAME}" |
| 134 | +echo "password=\${GIT_PASSWORD}" |
| 135 | +EOF' |
| 136 | + [ $status = 0 ] |
| 137 | + [[ "$output" == *"username=user@domain.com"* ]] |
| 138 | + [[ "$output" == *"password=pass/with/special&chars"* ]] |
| 139 | +} |
| 140 | + |
| 141 | +@test "git_cmd should use credentials when available" { |
| 142 | + if ! helm_supports_credentials; then |
| 143 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 144 | + fi |
| 145 | + |
| 146 | + export HELM_PLUGIN_USERNAME="testuser" |
| 147 | + export HELM_PLUGIN_PASSWORD="testpass" |
| 148 | + |
| 149 | + # Test git_cmd function with credentials |
| 150 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && git_cmd --version' |
| 151 | + [ $status = 0 ] |
| 152 | + [[ "$output" == *"git version"* ]] |
| 153 | +} |
| 154 | + |
| 155 | +@test "git_cmd should work normally when no credentials" { |
| 156 | + if ! helm_supports_credentials; then |
| 157 | + skip "Helm version < 3.14.0 does not support credential passing" |
| 158 | + fi |
| 159 | + |
| 160 | + unset HELM_PLUGIN_USERNAME |
| 161 | + unset HELM_PLUGIN_PASSWORD |
| 162 | + |
| 163 | + # Test git_cmd function without credentials |
| 164 | + run bash -c 'source "${HELM_GIT_DIRNAME}/helm-git-plugin.sh" && git_cmd --version' |
| 165 | + [ $status = 0 ] |
| 166 | + [[ "$output" == *"git version"* ]] |
| 167 | +} |
0 commit comments