password_test.go 3.3 KB

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