validator_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package validators
  3. import (
  4. "bytes"
  5. "context"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptest"
  11. "testing"
  12. "time"
  13. "github.com/agiledragon/gomonkey"
  14. "git.nanodreamtech.com/sg/wechatpay-go/core/consts"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. type mockVerifier struct {
  18. }
  19. func (v *mockVerifier) GetSerial(ctx context.Context) (serial string, err error) {
  20. return "SERIAL1234567890", nil
  21. }
  22. func (v *mockVerifier) Verify(ctx context.Context, serialNumber string, message string, signature string) error {
  23. if "["+serialNumber+"-"+message+"]" == signature {
  24. return nil
  25. }
  26. return fmt.Errorf("verification failed")
  27. }
  28. func TestWechatPayResponseValidator_Validate_Success(t *testing.T) {
  29. mockTimestamp := time.Now().Unix()
  30. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  31. validator := NewWechatPayResponseValidator(&mockVerifier{})
  32. type args struct {
  33. ctx context.Context
  34. response *http.Response
  35. }
  36. tests := []struct {
  37. name string
  38. args args
  39. wantErr bool
  40. }{
  41. {
  42. name: "response validate success",
  43. args: args{
  44. ctx: context.Background(),
  45. response: &http.Response{
  46. Header: http.Header{
  47. consts.WechatPaySignature: {
  48. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\nBODY\n]",
  49. },
  50. consts.WechatPaySerial: {"SERIAL1234567890"},
  51. consts.WechatPayTimestamp: {mockTimestampStr},
  52. consts.WechatPayNonce: {"NONCE1234567890"},
  53. consts.RequestID: {"any-request-id"},
  54. },
  55. Body: ioutil.NopCloser(bytes.NewBuffer([]byte("BODY"))),
  56. },
  57. },
  58. wantErr: false,
  59. },
  60. {
  61. name: "response validate success without body",
  62. args: args{
  63. ctx: context.Background(),
  64. response: &http.Response{
  65. Header: http.Header{
  66. consts.WechatPaySignature: {
  67. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n\n]",
  68. },
  69. consts.WechatPaySerial: {"SERIAL1234567890"},
  70. consts.WechatPayTimestamp: {mockTimestampStr},
  71. consts.WechatPayNonce: {"NONCE1234567890"},
  72. consts.RequestID: {"any-request-id"},
  73. },
  74. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  75. },
  76. },
  77. wantErr: false,
  78. },
  79. {
  80. name: "response validate success without RequestID",
  81. args: args{
  82. ctx: context.Background(),
  83. response: &http.Response{
  84. Header: http.Header{
  85. consts.WechatPaySignature: {
  86. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n\n]",
  87. },
  88. consts.WechatPaySerial: {"SERIAL1234567890"},
  89. consts.WechatPayTimestamp: {mockTimestampStr},
  90. consts.WechatPayNonce: {"NONCE1234567890"},
  91. },
  92. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  93. },
  94. },
  95. wantErr: false,
  96. },
  97. }
  98. for _, tt := range tests {
  99. t.Run(
  100. tt.name, func(t *testing.T) {
  101. if err := validator.Validate(tt.args.ctx, tt.args.response); (err != nil) != tt.wantErr {
  102. t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
  103. }
  104. },
  105. )
  106. }
  107. }
  108. func TestWechatPayResponseValidator_Validate_Failure(t *testing.T) {
  109. mockTimestamp := time.Now().Unix()
  110. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  111. validator := NewWechatPayResponseValidator(&mockVerifier{})
  112. type args struct {
  113. ctx context.Context
  114. response *http.Response
  115. }
  116. tests := []struct {
  117. name string
  118. args args
  119. wantErr bool
  120. }{
  121. {
  122. name: "response validate error with error signature",
  123. args: args{
  124. ctx: context.Background(),
  125. response: &http.Response{
  126. Header: http.Header{
  127. consts.WechatPaySignature: {"[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567\n\n]"},
  128. consts.WechatPaySerial: {"SERIAL1234567890"},
  129. consts.WechatPayTimestamp: {mockTimestampStr},
  130. consts.WechatPayNonce: {"NONCE1234567890"},
  131. consts.RequestID: {"any-request-id"},
  132. },
  133. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  134. },
  135. },
  136. wantErr: true,
  137. },
  138. {
  139. name: "response validate error missing signature",
  140. args: args{
  141. ctx: context.Background(),
  142. response: &http.Response{
  143. Header: http.Header{
  144. consts.WechatPaySerial: {"SERIAL1234567890"},
  145. consts.WechatPayTimestamp: {mockTimestampStr},
  146. consts.WechatPayNonce: {"NONCE1234567890"},
  147. consts.RequestID: {"any-request-id"},
  148. },
  149. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  150. },
  151. },
  152. wantErr: true,
  153. },
  154. {
  155. name: "response validate error missing serial",
  156. args: args{
  157. ctx: context.Background(),
  158. response: &http.Response{
  159. Header: http.Header{
  160. consts.WechatPaySignature: {"[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n]"},
  161. consts.WechatPayTimestamp: {mockTimestampStr},
  162. consts.WechatPayNonce: {"NONCE1234567890"},
  163. consts.RequestID: {"any-request-id"},
  164. },
  165. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  166. },
  167. },
  168. wantErr: true,
  169. },
  170. {
  171. name: "response validate error missing timestamp",
  172. args: args{
  173. ctx: context.Background(),
  174. response: &http.Response{
  175. Header: http.Header{
  176. consts.WechatPaySignature: {"[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n]"},
  177. consts.WechatPaySerial: {"SERIAL1234567890"},
  178. consts.WechatPayNonce: {"NONCE1234567890"},
  179. consts.RequestID: {"any-request-id"},
  180. },
  181. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  182. },
  183. },
  184. wantErr: true,
  185. },
  186. {
  187. name: "response validate error invalid timestamp",
  188. args: args{
  189. ctx: context.Background(),
  190. response: &http.Response{
  191. Header: http.Header{
  192. consts.WechatPaySignature: {"[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n]"},
  193. consts.WechatPaySerial: {"SERIAL1234567890"},
  194. consts.WechatPayTimestamp: {"invalid timestamp"},
  195. consts.WechatPayNonce: {"NONCE1234567890"},
  196. consts.RequestID: {"any-request-id"},
  197. },
  198. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  199. },
  200. },
  201. wantErr: true,
  202. },
  203. {
  204. name: "response validate error missing nonce",
  205. args: args{
  206. ctx: context.Background(),
  207. response: &http.Response{
  208. Header: http.Header{
  209. consts.WechatPaySignature: {"[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\n]"},
  210. consts.WechatPaySerial: {"SERIAL1234567890"},
  211. consts.WechatPayTimestamp: {mockTimestampStr},
  212. consts.RequestID: {"any-request-id"},
  213. },
  214. Body: ioutil.NopCloser(bytes.NewBuffer([]byte(""))),
  215. },
  216. },
  217. wantErr: true,
  218. },
  219. }
  220. for _, tt := range tests {
  221. t.Run(
  222. tt.name, func(t *testing.T) {
  223. if err := validator.Validate(tt.args.ctx, tt.args.response); (err != nil) != tt.wantErr {
  224. t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
  225. }
  226. },
  227. )
  228. }
  229. }
  230. func TestWechatPayResponseValidator_WithoutVerifierShouldFail(t *testing.T) {
  231. mockTimestamp := time.Now().Unix()
  232. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  233. invalidValidator := NewWechatPayResponseValidator(nil)
  234. response := &http.Response{
  235. Header: http.Header{
  236. consts.WechatPaySignature: {
  237. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\nBODY\n]",
  238. },
  239. consts.WechatPaySerial: {"SERIAL1234567890"},
  240. consts.WechatPayTimestamp: {mockTimestampStr},
  241. consts.WechatPayNonce: {"NONCE1234567890"},
  242. consts.RequestID: {"any-request-id"},
  243. },
  244. Body: ioutil.NopCloser(bytes.NewBuffer([]byte("BODY"))),
  245. }
  246. err := invalidValidator.Validate(context.Background(), response)
  247. assert.Error(t, err)
  248. }
  249. func TestWechatPayResponseValidator_ValidateReadBodyErrorShouldFail(t *testing.T) {
  250. patches := gomonkey.NewPatches()
  251. defer patches.Reset()
  252. patches.ApplyFunc(ioutil.ReadAll, func(r io.Reader) ([]byte, error) {
  253. return nil, fmt.Errorf("read error")
  254. })
  255. mockTimestamp := time.Now().Unix()
  256. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  257. validator := NewWechatPayResponseValidator(&mockVerifier{})
  258. response := &http.Response{
  259. Header: http.Header{
  260. consts.WechatPaySignature: {
  261. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\nBODY\n]",
  262. },
  263. consts.WechatPaySerial: {"SERIAL1234567890"},
  264. consts.WechatPayTimestamp: {mockTimestampStr},
  265. consts.WechatPayNonce: {"NONCE1234567890"},
  266. consts.RequestID: {"any-request-id"},
  267. },
  268. Body: ioutil.NopCloser(bytes.NewBuffer([]byte("BODY"))),
  269. }
  270. err := validator.Validate(context.Background(), response)
  271. assert.Error(t, err)
  272. }
  273. func TestNullValidator_Validate(t *testing.T) {
  274. nullValidator := NullValidator{}
  275. assert.NoError(t, nullValidator.Validate(context.Background(), &http.Response{}))
  276. assert.NoError(t, nullValidator.Validate(context.Background(), nil))
  277. }
  278. func TestWechatPayNotifyValidator_Validate(t *testing.T) {
  279. mockTimestamp := time.Now().Unix()
  280. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  281. validator := NewWechatPayNotifyValidator(&mockVerifier{})
  282. request := httptest.NewRequest("Post", "http://127.0.0.1", ioutil.NopCloser(bytes.NewBuffer([]byte("BODY"))))
  283. request.Header = http.Header{
  284. consts.WechatPaySignature: {
  285. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\nBODY\n]",
  286. },
  287. consts.WechatPaySerial: {"SERIAL1234567890"},
  288. consts.WechatPayTimestamp: {mockTimestampStr},
  289. consts.WechatPayNonce: {"NONCE1234567890"},
  290. consts.RequestID: {"any-request-id"},
  291. }
  292. err := validator.Validate(context.Background(), request)
  293. assert.NoError(t, err)
  294. }
  295. func TestWechatPayNotifyValidator_ValidateReadBodyError(t *testing.T) {
  296. patches := gomonkey.NewPatches()
  297. defer patches.Reset()
  298. patches.ApplyFunc(ioutil.ReadAll, func(r io.Reader) ([]byte, error) {
  299. return nil, fmt.Errorf("read error")
  300. })
  301. mockTimestamp := time.Now().Unix()
  302. mockTimestampStr := fmt.Sprintf("%d", mockTimestamp)
  303. validator := NewWechatPayNotifyValidator(&mockVerifier{})
  304. request := httptest.NewRequest("Post", "http://127.0.0.1", ioutil.NopCloser(bytes.NewBuffer([]byte("BODY"))))
  305. request.Header = http.Header{
  306. consts.WechatPaySignature: {
  307. "[SERIAL1234567890-" + mockTimestampStr + "\nNONCE1234567890\nBODY\n]",
  308. },
  309. consts.WechatPaySerial: {"SERIAL1234567890"},
  310. consts.WechatPayTimestamp: {mockTimestampStr},
  311. consts.WechatPayNonce: {"NONCE1234567890"},
  312. consts.RequestID: {"any-request-id"},
  313. }
  314. err := validator.Validate(context.Background(), request)
  315. assert.Error(t, err)
  316. }