github_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package github
  13. import (
  14. "context"
  15. "fmt"
  16. "net/http"
  17. "net/http/httptest"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. v1 "k8s.io/api/core/v1"
  23. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
  27. )
  28. const (
  29. tstCrtName = "github_test.pem"
  30. )
  31. func testHTTPSrv(t *testing.T, r []byte) *httptest.Server {
  32. return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
  33. assert.Equal(t, "POST", req.Method, "Expected POST request")
  34. assert.Empty(t, req.Body)
  35. assert.NotEmpty(t, req.Header.Get("Authorization"))
  36. assert.Equal(t, "application/vnd.github.v3+json", req.Header.Get("Accept"))
  37. // Send response to be tested
  38. rw.Write(r)
  39. }))
  40. }
  41. func TestGenerate(t *testing.T) {
  42. type args struct {
  43. ctx context.Context
  44. jsonSpec *apiextensions.JSON
  45. kube client.Client
  46. namespace string
  47. }
  48. pem, err := os.ReadFile(tstCrtName)
  49. assert.NoError(t, err, "Should not error when reading privateKey")
  50. validResponce := []byte(`{
  51. "token": "ghs_16C7e42F292c6912E7710c838347Ae178B4a",
  52. "expires_at": "2016-07-11T22:14:10Z",
  53. "permissions": {
  54. "issues": "write",
  55. "contents": "read"
  56. },
  57. "repository_selection": "selected"
  58. }`)
  59. server := testHTTPSrv(t, validResponce)
  60. tests := []struct {
  61. name string
  62. g *Generator
  63. args args
  64. want map[string][]byte
  65. wantErr bool
  66. }{
  67. {
  68. name: "nil spec",
  69. args: args{
  70. jsonSpec: nil,
  71. },
  72. wantErr: true,
  73. },
  74. {
  75. name: "full spec",
  76. args: args{
  77. ctx: context.TODO(),
  78. namespace: "foo",
  79. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  80. ObjectMeta: metav1.ObjectMeta{
  81. Name: "testName",
  82. Namespace: "foo",
  83. },
  84. Data: map[string][]byte{
  85. "privateKey": pem,
  86. },
  87. }).Build(),
  88. jsonSpec: &apiextensions.JSON{
  89. Raw: []byte(fmt.Sprintf(`apiVersion: generators.external-secrets.io/v1alpha1
  90. kind: GithubToken
  91. spec:
  92. appID: "0000000"
  93. installID: "00000000"
  94. URL: %q
  95. auth:
  96. privateKey:
  97. secretRef:
  98. name: "testName"
  99. namespace: "foo"
  100. key: "privateKey"`, server.URL)),
  101. },
  102. },
  103. want: map[string][]byte{
  104. "token": []byte("ghs_16C7e42F292c6912E7710c838347Ae178B4a"),
  105. },
  106. },
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. g := &Generator{httpClient: server.Client()}
  111. got, err := g.generate(
  112. tt.args.ctx,
  113. tt.args.jsonSpec,
  114. tt.args.kube,
  115. tt.args.namespace,
  116. )
  117. if (err != nil) != tt.wantErr {
  118. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  119. return
  120. }
  121. if !reflect.DeepEqual(got, tt.want) {
  122. t.Errorf("Generator.Generate() = %s, want %s", got, tt.want)
  123. }
  124. })
  125. }
  126. }