provider_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package github
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. )
  21. func TestValidateGithubProvider(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. provider *esv1.GithubProvider
  25. wantType esv1.GithubSecretType
  26. wantErr string
  27. }{
  28. {
  29. name: "omitted secret type defaults to Actions",
  30. provider: &esv1.GithubProvider{},
  31. wantType: esv1.GithubSecretTypeActions,
  32. },
  33. {
  34. name: "explicit Actions",
  35. provider: &esv1.GithubProvider{SecretType: esv1.GithubSecretTypeActions},
  36. wantType: esv1.GithubSecretTypeActions,
  37. },
  38. {
  39. name: "Dependabot with empty environment",
  40. provider: &esv1.GithubProvider{
  41. SecretType: esv1.GithubSecretTypeDependabot,
  42. Environment: "",
  43. },
  44. wantType: esv1.GithubSecretTypeDependabot,
  45. },
  46. {
  47. name: "Dependabot environment is unsupported",
  48. provider: &esv1.GithubProvider{
  49. SecretType: esv1.GithubSecretTypeDependabot,
  50. Repository: "repository",
  51. Environment: "production",
  52. },
  53. wantErr: "Dependabot secrets do not support environments",
  54. },
  55. {
  56. name: "unsupported secret type",
  57. provider: &esv1.GithubProvider{SecretType: esv1.GithubSecretType("Unsupported")},
  58. wantErr: "unsupported GitHub secret type",
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. gotType, err := validateGithubProvider(tt.provider)
  64. if tt.wantErr != "" {
  65. assert.ErrorContains(t, err, tt.wantErr)
  66. return
  67. }
  68. require.NoError(t, err)
  69. assert.Equal(t, tt.wantType, gotType)
  70. })
  71. }
  72. }
  73. func TestProviderValidateStoreRejectsInvalidSecretTypeConfiguration(t *testing.T) {
  74. tests := []struct {
  75. name string
  76. provider *esv1.GithubProvider
  77. wantErr string
  78. }{
  79. {
  80. name: "unsupported secret type",
  81. provider: &esv1.GithubProvider{SecretType: esv1.GithubSecretType("Unsupported")},
  82. wantErr: "unsupported GitHub secret type",
  83. },
  84. {
  85. name: "Dependabot environment",
  86. provider: &esv1.GithubProvider{
  87. SecretType: esv1.GithubSecretTypeDependabot,
  88. Environment: "production",
  89. },
  90. wantErr: "Dependabot secrets do not support environments",
  91. },
  92. }
  93. for _, tt := range tests {
  94. t.Run(tt.name, func(t *testing.T) {
  95. store := &esv1.SecretStore{
  96. Spec: esv1.SecretStoreSpec{
  97. Provider: &esv1.SecretStoreProvider{Github: tt.provider},
  98. },
  99. }
  100. _, err := (&Provider{}).ValidateStore(store)
  101. require.ErrorContains(t, err, tt.wantErr)
  102. })
  103. }
  104. }
  105. func TestNewClientRejectsInvalidSecretTypeBeforeAuthentication(t *testing.T) {
  106. tests := []struct {
  107. name string
  108. provider *esv1.GithubProvider
  109. wantErr string
  110. }{
  111. {
  112. name: "unsupported secret type",
  113. provider: &esv1.GithubProvider{SecretType: esv1.GithubSecretType("Unsupported")},
  114. wantErr: "unsupported GitHub secret type",
  115. },
  116. {
  117. name: "Dependabot environment",
  118. provider: &esv1.GithubProvider{
  119. SecretType: esv1.GithubSecretTypeDependabot,
  120. Environment: "production",
  121. },
  122. wantErr: "Dependabot secrets do not support environments",
  123. },
  124. }
  125. for _, tt := range tests {
  126. t.Run(tt.name, func(t *testing.T) {
  127. store := &esv1.SecretStore{
  128. Spec: esv1.SecretStoreSpec{
  129. Provider: &esv1.SecretStoreProvider{Github: tt.provider},
  130. },
  131. }
  132. _, err := newClient(context.Background(), store, nil, "default")
  133. require.ErrorContains(t, err, tt.wantErr)
  134. assert.NotContains(t, err.Error(), "private key")
  135. })
  136. }
  137. }