wechat_pay_credential_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package credentials
  3. import (
  4. "context"
  5. "fmt"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. "github.com/agiledragon/gomonkey"
  10. "github.com/stretchr/testify/require"
  11. "github.com/wechatpay-apiv3/wechatpay-go/core/auth"
  12. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  13. )
  14. type mockSigner struct {
  15. MchID string
  16. CertificateSerialNo string
  17. }
  18. func (s *mockSigner) Sign(_ context.Context, message string) (*auth.SignatureResult, error) {
  19. result := &auth.SignatureResult{
  20. MchID: s.MchID,
  21. CertificateSerialNo: s.CertificateSerialNo,
  22. Signature: "Sign:" + message,
  23. }
  24. return result, nil
  25. }
  26. func (s *mockSigner) Algorithm() string {
  27. return "Mock"
  28. }
  29. type mockErrorSigner struct {
  30. }
  31. func (s *mockErrorSigner) Sign(_ context.Context, message string) (*auth.SignatureResult, error) {
  32. return nil, fmt.Errorf("mock sign error")
  33. }
  34. func (s *mockErrorSigner) Algorithm() string {
  35. return "ErrorMock"
  36. }
  37. const (
  38. testMchID = "1234567890"
  39. testCertificateSerial = "0123456789ABC"
  40. mockNonce = "A1B2C3D4E5F6G7"
  41. mockTimestamp = 1624523846
  42. )
  43. func TestWechatPayCredentials_GenerateAuthorizationHeader(t *testing.T) {
  44. patches := gomonkey.NewPatches()
  45. defer patches.Reset()
  46. patches.ApplyFunc(
  47. utils.GenerateNonce, func() (string, error) {
  48. return mockNonce, nil
  49. },
  50. )
  51. patches.ApplyFunc(
  52. time.Now, func() time.Time {
  53. return time.Unix(mockTimestamp, 0)
  54. },
  55. )
  56. signer := mockSigner{
  57. MchID: testMchID,
  58. CertificateSerialNo: testCertificateSerial,
  59. }
  60. type args struct {
  61. signer auth.Signer
  62. ctx context.Context
  63. method string
  64. canonicalURL string
  65. signBody string
  66. }
  67. tests := []struct {
  68. name string
  69. args args
  70. wantErr bool
  71. want string
  72. }{
  73. {
  74. name: "gen success without body",
  75. args: args{
  76. signer: &signer,
  77. ctx: context.Background(),
  78. method: "GET",
  79. canonicalURL: "/v3/certificates",
  80. signBody: "",
  81. },
  82. wantErr: false,
  83. want: `WECHATPAY2-Mock mchid="1234567890",nonce_str="A1B2C3D4E5F6G7",timestamp="1624523846",` +
  84. `serial_no="0123456789ABC",signature=` +
  85. "\"Sign:GET\n/v3/certificates\n1624523846\nA1B2C3D4E5F6G7\n\n\"",
  86. },
  87. {
  88. name: "gen success with body",
  89. args: args{
  90. signer: &signer,
  91. ctx: context.Background(),
  92. method: "POST",
  93. canonicalURL: "/v3/certificates",
  94. signBody: "Hello World!\n",
  95. },
  96. wantErr: false,
  97. want: `WECHATPAY2-Mock mchid="1234567890",nonce_str="A1B2C3D4E5F6G7",timestamp="1624523846",` +
  98. `serial_no="0123456789ABC",signature=` +
  99. "\"Sign:POST\n/v3/certificates\n1624523846\nA1B2C3D4E5F6G7\nHello World!\n\n\"",
  100. },
  101. {
  102. name: "gen error wihout signer",
  103. args: args{
  104. signer: nil,
  105. ctx: context.Background(),
  106. method: "post",
  107. canonicalURL: "/v3/certificates",
  108. signBody: "Hello World!\n",
  109. },
  110. wantErr: true,
  111. },
  112. }
  113. for _, tt := range tests {
  114. t.Run(
  115. tt.name, func(t *testing.T) {
  116. credential := WechatPayCredentials{Signer: tt.args.signer}
  117. authorization, err := credential.GenerateAuthorizationHeader(
  118. tt.args.ctx, tt.args.method, tt.args.canonicalURL, tt.args.signBody,
  119. )
  120. require.Equal(t, tt.wantErr, err != nil)
  121. require.Equal(t, tt.want, authorization)
  122. },
  123. )
  124. }
  125. }
  126. func TestWechatPayCredentials_GenerateAuthorizationHeaderErrorGenerateNonce(t *testing.T) {
  127. patches := gomonkey.NewPatches()
  128. defer patches.Reset()
  129. mockGenerateNonceErr := fmt.Errorf("generate nonce error")
  130. patches.ApplyFunc(
  131. utils.GenerateNonce, func() (string, error) {
  132. return "", mockGenerateNonceErr
  133. },
  134. )
  135. signer := mockSigner{
  136. MchID: testMchID,
  137. CertificateSerialNo: testCertificateSerial,
  138. }
  139. credential := WechatPayCredentials{Signer: &signer}
  140. authorization, err := credential.GenerateAuthorizationHeader(context.Background(), "GET", "/v3/certificates", "")
  141. require.Error(t, err)
  142. assert.Empty(t, authorization)
  143. }
  144. func TestWechatPayCredentials_GenerateAuthorizationHeaderErrorSigner(t *testing.T) {
  145. patches := gomonkey.NewPatches()
  146. defer patches.Reset()
  147. patches.ApplyFunc(
  148. utils.GenerateNonce, func() (string, error) {
  149. return mockNonce, nil
  150. },
  151. )
  152. patches.ApplyFunc(
  153. time.Now, func() time.Time {
  154. return time.Unix(mockTimestamp, 0)
  155. },
  156. )
  157. signer := mockErrorSigner{}
  158. credential := WechatPayCredentials{Signer: &signer}
  159. authorization, err := credential.GenerateAuthorizationHeader(context.Background(), "GET", "/v3/certificates", "")
  160. require.Error(t, err)
  161. assert.Empty(t, authorization)
  162. }