downloader_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2021 Tencent Inc. All rights reserved.
  2. package downloader_test
  3. import (
  4. "context"
  5. "testing"
  6. "git.nanodreamtech.com/sg/wechatpay-go/core"
  7. "git.nanodreamtech.com/sg/wechatpay-go/core/downloader"
  8. "git.nanodreamtech.com/sg/wechatpay-go/core/option"
  9. "git.nanodreamtech.com/sg/wechatpay-go/utils"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestNewCertificateDownloaderWithClient(t *testing.T) {
  14. patches := mockDownloadServer(t)
  15. defer patches.Reset()
  16. ctx := context.Background()
  17. privateKey, err := utils.LoadPrivateKey(testingKey(mockMchPrivateKey))
  18. require.NoError(t, err)
  19. opts := []core.ClientOption{
  20. option.WithMerchantCredential(mockMchID, mockMchCertificateSerial, privateKey),
  21. option.WithoutValidator(),
  22. }
  23. client, err := core.NewClient(ctx, opts...)
  24. require.NoError(t, err)
  25. d, err := downloader.NewCertificateDownloaderWithClient(ctx, client, mockAPIv3Key)
  26. require.NoError(t, err)
  27. assert.NotEmpty(t, d.GetAll(ctx))
  28. for serialNo, cert := range d.GetAll(ctx) {
  29. assert.Equal(t, serialNo, utils.GetCertificateSerialNumber(*cert))
  30. }
  31. // call Download
  32. err = d.DownloadCertificates(ctx)
  33. require.NoError(t, err)
  34. // call Download Again
  35. err = d.DownloadCertificates(ctx)
  36. require.NoError(t, err)
  37. }
  38. func TestNewCertificateDownloader(t *testing.T) {
  39. patches := mockDownloadServer(t)
  40. defer patches.Reset()
  41. privateKey, err := utils.LoadPrivateKey(testingKey(mockMchPrivateKey))
  42. require.NoError(t, err)
  43. ctx := context.Background()
  44. d, err := downloader.NewCertificateDownloader(
  45. context.Background(), mockMchID, privateKey, mockMchCertificateSerial, mockAPIv3Key,
  46. )
  47. require.NoError(t, err)
  48. assert.NotEmpty(t, d.GetAll(ctx))
  49. for serialNo, cert := range d.GetAll(ctx) {
  50. assert.Equal(t, serialNo, utils.GetCertificateSerialNumber(*cert))
  51. }
  52. // call Download
  53. err = d.DownloadCertificates(ctx)
  54. require.NoError(t, err)
  55. // call Download Again
  56. err = d.DownloadCertificates(ctx)
  57. require.NoError(t, err)
  58. }