services_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package apiv1
  2. import (
  3. "testing"
  4. )
  5. type simpleCAS struct{}
  6. func (*simpleCAS) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
  7. return nil, NotImplementedError{}
  8. }
  9. func (*simpleCAS) RenewCertificate(req *RenewCertificateRequest) (*RenewCertificateResponse, error) {
  10. return nil, NotImplementedError{}
  11. }
  12. func (*simpleCAS) RevokeCertificate(req *RevokeCertificateRequest) (*RevokeCertificateResponse, error) {
  13. return nil, NotImplementedError{}
  14. }
  15. type fakeCAS struct {
  16. simpleCAS
  17. }
  18. func (*fakeCAS) Type() Type { return SoftCAS }
  19. func TestType_String(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. t Type
  23. want string
  24. }{
  25. {"default", "", "softcas"},
  26. {"SoftCAS", SoftCAS, "softcas"},
  27. {"CloudCAS", CloudCAS, "cloudcas"},
  28. {"ExternalCAS", ExternalCAS, "externalcas"},
  29. {"UnknownCAS", "UnknownCAS", "unknowncas"},
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. if got := tt.t.String(); got != tt.want {
  34. t.Errorf("Type.String() = %v, want %v", got, tt.want)
  35. }
  36. })
  37. }
  38. }
  39. func TestTypeOf(t *testing.T) {
  40. type args struct {
  41. c CertificateAuthorityService
  42. }
  43. tests := []struct {
  44. name string
  45. args args
  46. want Type
  47. }{
  48. {"ok", args{&simpleCAS{}}, ExternalCAS},
  49. {"ok with type", args{&fakeCAS{}}, SoftCAS},
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. if got := TypeOf(tt.args.c); got != tt.want {
  54. t.Errorf("TypeOf() = %v, want %v", got, tt.want)
  55. }
  56. })
  57. }
  58. }
  59. func TestNotImplementedError_Error(t *testing.T) {
  60. type fields struct {
  61. Message string
  62. }
  63. tests := []struct {
  64. name string
  65. fields fields
  66. want string
  67. }{
  68. {"default", fields{""}, "not implemented"},
  69. {"with message", fields{"method not supported"}, "method not supported"},
  70. }
  71. for _, tt := range tests {
  72. t.Run(tt.name, func(t *testing.T) {
  73. e := NotImplementedError{
  74. Message: tt.fields.Message,
  75. }
  76. if got := e.Error(); got != tt.want {
  77. t.Errorf("NotImplementedError.Error() = %v, want %v", got, tt.want)
  78. }
  79. })
  80. }
  81. }
  82. func TestNotImplementedError_StatusCode(t *testing.T) {
  83. type fields struct {
  84. Message string
  85. }
  86. tests := []struct {
  87. name string
  88. fields fields
  89. want int
  90. }{
  91. {"default", fields{""}, 501},
  92. {"with message", fields{"method not supported"}, 501},
  93. }
  94. for _, tt := range tests {
  95. t.Run(tt.name, func(t *testing.T) {
  96. s := NotImplementedError{
  97. Message: tt.fields.Message,
  98. }
  99. if got := s.StatusCode(); got != tt.want {
  100. t.Errorf("NotImplementedError.StatusCode() = %v, want %v", got, tt.want)
  101. }
  102. })
  103. }
  104. }
  105. func TestValidationError_Error(t *testing.T) {
  106. type fields struct {
  107. Message string
  108. }
  109. tests := []struct {
  110. name string
  111. fields fields
  112. want string
  113. }{
  114. {"default", fields{""}, "bad request"},
  115. {"with message", fields{"token is empty"}, "token is empty"},
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. e := ValidationError{
  120. Message: tt.fields.Message,
  121. }
  122. if got := e.Error(); got != tt.want {
  123. t.Errorf("ValidationError.Error() = %v, want %v", got, tt.want)
  124. }
  125. })
  126. }
  127. }
  128. func TestValidationError_StatusCode(t *testing.T) {
  129. type fields struct {
  130. Message string
  131. }
  132. tests := []struct {
  133. name string
  134. fields fields
  135. want int
  136. }{
  137. {"default", fields{""}, 400},
  138. {"with message", fields{"token is empty"}, 400},
  139. }
  140. for _, tt := range tests {
  141. t.Run(tt.name, func(t *testing.T) {
  142. e := ValidationError{
  143. Message: tt.fields.Message,
  144. }
  145. if got := e.StatusCode(); got != tt.want {
  146. t.Errorf("ValidationError.StatusCode() = %v, want %v", got, tt.want)
  147. }
  148. })
  149. }
  150. }