dao_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package geetest
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "go-common/app/interface/main/creative/conf"
  7. "go-common/app/interface/main/creative/model/geetest"
  8. "os"
  9. "strings"
  10. "testing"
  11. "github.com/smartystreets/goconvey/convey"
  12. gock "gopkg.in/h2non/gock.v1"
  13. )
  14. var (
  15. d *Dao
  16. )
  17. func TestMain(m *testing.M) {
  18. if os.Getenv("DEPLOY_ENV") != "" {
  19. flag.Set("app_id", "main.archive.creative")
  20. flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
  21. flag.Set("tree_id", "2305")
  22. flag.Set("conf_version", "docker-1")
  23. flag.Set("deploy_env", "uat")
  24. flag.Set("conf_host", "config.bilibili.co")
  25. flag.Set("conf_path", "/tmp")
  26. flag.Set("region", "sh")
  27. flag.Set("zone", "sh001")
  28. } else {
  29. flag.Set("conf", "../../cmd/creative.toml")
  30. }
  31. flag.Parse()
  32. if err := conf.Init(); err != nil {
  33. panic(err)
  34. }
  35. d = New(conf.Conf)
  36. m.Run()
  37. os.Exit(0)
  38. }
  39. func httpMock(method, url string) *gock.Request {
  40. r := gock.New(url)
  41. r.Method = strings.ToUpper(method)
  42. d.clientx.SetTransport(gock.DefaultTransport)
  43. d.client.Transport = gock.DefaultTransport
  44. return r
  45. }
  46. func TestPreProcess(t *testing.T) {
  47. var (
  48. mid int64
  49. ip, clientType, challenge string
  50. newCaptcha int
  51. c = context.TODO()
  52. err error
  53. )
  54. convey.Convey("1", t, func(ctx convey.C) {
  55. defer gock.OffAll()
  56. httpMock("Do", d.registerURI).Reply(-502)
  57. challenge, err = d.PreProcess(c, mid, ip, clientType, newCaptcha)
  58. ctx.Convey("1", func(ctx convey.C) {
  59. ctx.So(err, convey.ShouldNotBeNil)
  60. ctx.So(len(challenge), convey.ShouldEqual, 0)
  61. })
  62. })
  63. convey.Convey("2", t, func(ctx convey.C) {
  64. challenge, err = d.PreProcess(c, mid, ip, clientType, newCaptcha)
  65. ctx.Convey("2", func(ctx convey.C) {
  66. ctx.So(err, convey.ShouldBeNil)
  67. ctx.So(len(challenge), convey.ShouldEqual, 32)
  68. })
  69. })
  70. }
  71. func TestValidate(t *testing.T) {
  72. var (
  73. c = context.TODO()
  74. err error
  75. challenge, seccode, clientType, ip, captchaID string
  76. mid int64
  77. res = &geetest.ValidateRes{}
  78. )
  79. convey.Convey("1", t, func(ctx convey.C) {
  80. defer gock.OffAll()
  81. httpMock("POST", d.validateURI).Reply(-502)
  82. res, err = d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
  83. ctx.Convey("1", func(ctx convey.C) {
  84. ctx.So(err, convey.ShouldNotBeNil)
  85. ctx.So(res, convey.ShouldBeNil)
  86. })
  87. })
  88. convey.Convey("2", t, func(ctx convey.C) {
  89. defer gock.OffAll()
  90. res = &geetest.ValidateRes{
  91. Seccode: "ok",
  92. }
  93. js, _ := json.Marshal(res)
  94. defer gock.OffAll()
  95. httpMock("Post", d.validateURI).Reply(200).JSON(string(js))
  96. res, err = d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
  97. ctx.Convey("2", func(ctx convey.C) {
  98. ctx.So(err, convey.ShouldBeNil)
  99. ctx.So(res, convey.ShouldNotBeNil)
  100. })
  101. })
  102. }