provisioner_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package provisioner
  2. import (
  3. "testing"
  4. "github.com/pkg/errors"
  5. "github.com/smallstep/assert"
  6. )
  7. func TestType_String(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. t Type
  11. want string
  12. }{
  13. {"JWK", TypeJWK, "JWK"},
  14. {"OIDC", TypeOIDC, "OIDC"},
  15. {"AWS", TypeAWS, "AWS"},
  16. {"Azure", TypeAzure, "Azure"},
  17. {"GCP", TypeGCP, "GCP"},
  18. {"noop", noopType, ""},
  19. {"notFound", 1000, ""},
  20. }
  21. for _, tt := range tests {
  22. t.Run(tt.name, func(t *testing.T) {
  23. if got := tt.t.String(); got != tt.want {
  24. t.Errorf("Type.String() = %v, want %v", got, tt.want)
  25. }
  26. })
  27. }
  28. }
  29. func TestSanitizeSSHUserPrincipal(t *testing.T) {
  30. type args struct {
  31. email string
  32. }
  33. tests := []struct {
  34. name string
  35. args args
  36. want string
  37. }{
  38. {"simple", args{"foobar"}, "foobar"},
  39. {"camelcase", args{"FooBar"}, "foobar"},
  40. {"email", args{"foo@example.com"}, "foo"},
  41. {"email with dots", args{"foo.bar.zar@example.com"}, "foobarzar"},
  42. {"email with dashes", args{"foo-bar-zar@example.com"}, "foo-bar-zar"},
  43. {"email with underscores", args{"foo_bar_zar@example.com"}, "foo_bar_zar"},
  44. {"email with symbols", args{"Foo.Bar0123456789!#$%&'*+-/=?^_`{|}~;@example.com"}, "foobar0123456789________-___________"},
  45. }
  46. for _, tt := range tests {
  47. t.Run(tt.name, func(t *testing.T) {
  48. if got := SanitizeSSHUserPrincipal(tt.args.email); got != tt.want {
  49. t.Errorf("SanitizeSSHUserPrincipal() = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }
  54. func TestDefaultIdentityFunc(t *testing.T) {
  55. type test struct {
  56. p Interface
  57. email string
  58. err error
  59. identity *Identity
  60. }
  61. tests := map[string]func(*testing.T) test{
  62. "fail/unsupported-provisioner": func(t *testing.T) test {
  63. return test{
  64. p: &X5C{},
  65. err: errors.New("provisioner type '*provisioner.X5C' not supported by identity function"),
  66. }
  67. },
  68. "fail/bad-ssh-regex": func(t *testing.T) test {
  69. return test{
  70. p: &OIDC{},
  71. email: "$%^#_>@smallstep.com",
  72. err: errors.New("invalid principal '______' from email '$%^#_>@smallstep.com'"),
  73. }
  74. },
  75. "ok": func(t *testing.T) test {
  76. return test{
  77. p: &OIDC{},
  78. email: "max.furman@smallstep.com",
  79. identity: &Identity{Usernames: []string{"maxfurman", "max.furman@smallstep.com"}},
  80. }
  81. },
  82. }
  83. for name, get := range tests {
  84. t.Run(name, func(t *testing.T) {
  85. tc := get(t)
  86. identity, err := DefaultIdentityFunc(tc.p, tc.email)
  87. if err != nil {
  88. if assert.NotNil(t, tc.err) {
  89. assert.Equals(t, tc.err.Error(), err.Error())
  90. }
  91. } else {
  92. if assert.Nil(t, tc.err) {
  93. assert.Equals(t, identity.Usernames, tc.identity.Usernames)
  94. }
  95. }
  96. })
  97. }
  98. }