github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/secret/shared/shared_test.go (about) 1 package shared 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestGetSecretEntity(t *testing.T) { 10 tests := []struct { 11 name string 12 orgName string 13 envName string 14 userSecrets bool 15 want SecretEntity 16 wantErr bool 17 }{ 18 { 19 name: "org", 20 orgName: "myOrg", 21 want: Organization, 22 }, 23 { 24 name: "env", 25 envName: "myEnv", 26 want: Environment, 27 }, 28 { 29 name: "user", 30 userSecrets: true, 31 want: User, 32 }, 33 { 34 name: "defaults to repo", 35 want: Repository, 36 }, 37 { 38 name: "Errors if both org and env are set", 39 orgName: "myOrg", 40 envName: "myEnv", 41 wantErr: true, 42 }, 43 { 44 name: "Errors if both org and user secrets are set", 45 orgName: "myOrg", 46 userSecrets: true, 47 wantErr: true, 48 }, 49 { 50 name: "Errors if both env and user secrets are set", 51 envName: "myEnv", 52 userSecrets: true, 53 wantErr: true, 54 }, 55 } 56 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 entity, err := GetSecretEntity(tt.orgName, tt.envName, tt.userSecrets) 60 61 if tt.wantErr { 62 assert.Error(t, err) 63 } else { 64 assert.NoError(t, err) 65 assert.Equal(t, tt.want, entity) 66 } 67 }) 68 } 69 } 70 71 func TestGetSecretApp(t *testing.T) { 72 tests := []struct { 73 name string 74 app string 75 entity SecretEntity 76 want App 77 wantErr bool 78 }{ 79 { 80 name: "Actions", 81 app: "actions", 82 want: Actions, 83 }, 84 { 85 name: "Codespaces", 86 app: "codespaces", 87 want: Codespaces, 88 }, 89 { 90 name: "Dependabot", 91 app: "dependabot", 92 want: Dependabot, 93 }, 94 { 95 name: "Defaults to Actions for repository", 96 app: "", 97 entity: Repository, 98 want: Actions, 99 }, 100 { 101 name: "Defaults to Actions for organization", 102 app: "", 103 entity: Organization, 104 want: Actions, 105 }, 106 { 107 name: "Defaults to Actions for environment", 108 app: "", 109 entity: Environment, 110 want: Actions, 111 }, 112 { 113 name: "Defaults to Codespaces for user", 114 app: "", 115 entity: User, 116 want: Codespaces, 117 }, 118 { 119 name: "Unknown for invalid apps", 120 app: "invalid", 121 want: Unknown, 122 wantErr: true, 123 }, 124 { 125 name: "case insensitive", 126 app: "ACTIONS", 127 want: Actions, 128 }, 129 } 130 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 app, err := GetSecretApp(tt.app, tt.entity) 134 if tt.wantErr { 135 assert.Error(t, err) 136 } else { 137 assert.NoError(t, err) 138 } 139 assert.Equal(t, tt.want, app) 140 }) 141 } 142 } 143 144 func TestIsSupportedSecretEntity(t *testing.T) { 145 tests := []struct { 146 name string 147 app App 148 supportedEntities []SecretEntity 149 unsupportedEntities []SecretEntity 150 }{ 151 { 152 name: "Actions", 153 app: Actions, 154 supportedEntities: []SecretEntity{ 155 Repository, 156 Organization, 157 Environment, 158 }, 159 unsupportedEntities: []SecretEntity{ 160 User, 161 Unknown, 162 }, 163 }, 164 { 165 name: "Codespaces", 166 app: Codespaces, 167 supportedEntities: []SecretEntity{ 168 User, 169 Repository, 170 Organization, 171 }, 172 unsupportedEntities: []SecretEntity{ 173 Environment, 174 Unknown, 175 }, 176 }, 177 { 178 name: "Dependabot", 179 app: Dependabot, 180 supportedEntities: []SecretEntity{ 181 Repository, 182 Organization, 183 }, 184 unsupportedEntities: []SecretEntity{ 185 Environment, 186 User, 187 Unknown, 188 }, 189 }, 190 } 191 192 for _, tt := range tests { 193 t.Run(tt.name, func(t *testing.T) { 194 for _, entity := range tt.supportedEntities { 195 assert.True(t, IsSupportedSecretEntity(tt.app, entity)) 196 } 197 198 for _, entity := range tt.unsupportedEntities { 199 assert.False(t, IsSupportedSecretEntity(tt.app, entity)) 200 } 201 }) 202 } 203 }