password_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 password
  13. import (
  14. "errors"
  15. "reflect"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. )
  20. func TestGenerate(t *testing.T) {
  21. type args struct {
  22. jsonSpec *apiextensions.JSON
  23. passGen generateFunc
  24. }
  25. tests := []struct {
  26. name string
  27. g *Generator
  28. args args
  29. want map[string][]byte
  30. wantErr bool
  31. }{
  32. {
  33. name: "no json spec should result in error",
  34. args: args{
  35. jsonSpec: nil,
  36. },
  37. wantErr: true,
  38. },
  39. {
  40. name: "invalid json spec should result in error",
  41. args: args{
  42. jsonSpec: &apiextensions.JSON{
  43. Raw: []byte(`no json`),
  44. },
  45. },
  46. wantErr: true,
  47. },
  48. {
  49. name: "empty spec should return defaults",
  50. args: args{
  51. jsonSpec: &apiextensions.JSON{
  52. Raw: []byte(`{}`),
  53. },
  54. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  55. ) (string, error) {
  56. assert.Equal(t, defaultLength, len)
  57. assert.Equal(t, defaultSymbolChars, symbolCharacters)
  58. assert.Equal(t, 6, symbols)
  59. assert.Equal(t, 6, digits)
  60. assert.Equal(t, false, noUpper)
  61. assert.Equal(t, false, allowRepeat)
  62. return "foobar", nil
  63. },
  64. },
  65. want: map[string][]byte{
  66. "password": []byte(`foobar`),
  67. },
  68. wantErr: false,
  69. },
  70. {
  71. name: "spec should override defaults",
  72. args: args{
  73. jsonSpec: &apiextensions.JSON{
  74. Raw: []byte(`{"spec":{"length":48,"digits":2, "symbols":2, "symbolCharacters":"-_.", "noUpper": true, "allowRepeat": true}}`),
  75. },
  76. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  77. ) (string, error) {
  78. assert.Equal(t, 48, len)
  79. assert.Equal(t, "-_.", symbolCharacters)
  80. assert.Equal(t, 2, symbols)
  81. assert.Equal(t, 2, digits)
  82. assert.Equal(t, true, noUpper)
  83. assert.Equal(t, true, allowRepeat)
  84. return "foobar", nil
  85. },
  86. },
  87. want: map[string][]byte{
  88. "password": []byte(`foobar`),
  89. },
  90. wantErr: false,
  91. },
  92. {
  93. name: "generator error should be returned",
  94. args: args{
  95. jsonSpec: &apiextensions.JSON{
  96. Raw: []byte(`{}`),
  97. },
  98. passGen: func(len int, symbols int, symbolCharacters string, digits int, noUpper bool, allowRepeat bool,
  99. ) (string, error) {
  100. return "", errors.New("boom")
  101. },
  102. },
  103. wantErr: true,
  104. },
  105. }
  106. for _, tt := range tests {
  107. t.Run(tt.name, func(t *testing.T) {
  108. g := &Generator{}
  109. got, err := g.generate(tt.args.jsonSpec, tt.args.passGen)
  110. if (err != nil) != tt.wantErr {
  111. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  112. return
  113. }
  114. if !reflect.DeepEqual(got, tt.want) {
  115. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  116. }
  117. })
  118. }
  119. }