github_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.NotEmpty(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. "contents": "read"
  55. },
  56. "repositories": [
  57. {
  58. "id": 10000
  59. }
  60. ],
  61. "repository_selection": "selected"
  62. }`)
  63. server := testHTTPSrv(t, validResponce)
  64. tests := []struct {
  65. name string
  66. g *Generator
  67. args args
  68. want map[string][]byte
  69. wantErr bool
  70. }{
  71. {
  72. name: "nil spec",
  73. args: args{
  74. jsonSpec: nil,
  75. },
  76. wantErr: true,
  77. },
  78. {
  79. name: "full spec",
  80. args: args{
  81. ctx: context.TODO(),
  82. namespace: "foo",
  83. kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
  84. ObjectMeta: metav1.ObjectMeta{
  85. Name: "testName",
  86. Namespace: "foo",
  87. },
  88. Data: map[string][]byte{
  89. "privateKey": pem,
  90. },
  91. }).Build(),
  92. jsonSpec: &apiextensions.JSON{
  93. Raw: []byte(fmt.Sprintf(`apiVersion: generators.external-secrets.io/v1alpha1
  94. kind: GithubToken
  95. spec:
  96. appID: "0000000"
  97. installID: "00000000"
  98. URL: %q
  99. repositories:
  100. - "Hello-World"
  101. permissions:
  102. contents: "read"
  103. auth:
  104. privateKey:
  105. secretRef:
  106. name: "testName"
  107. namespace: "foo"
  108. key: "privateKey"`, server.URL)),
  109. },
  110. },
  111. want: map[string][]byte{
  112. "token": []byte("ghs_16C7e42F292c6912E7710c838347Ae178B4a"),
  113. },
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. g := &Generator{httpClient: server.Client()}
  119. got, err := g.generate(
  120. tt.args.ctx,
  121. tt.args.jsonSpec,
  122. tt.args.kube,
  123. tt.args.namespace,
  124. )
  125. if (err != nil) != tt.wantErr {
  126. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  127. return
  128. }
  129. if !reflect.DeepEqual(got, tt.want) {
  130. t.Errorf("Generator.Generate() = %s, want %s", got, tt.want)
  131. }
  132. })
  133. }
  134. }