dao_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package geetest
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "testing"
  15. "time"
  16. "go-common/app/interface/main/answer/conf"
  17. "github.com/bouk/monkey"
  18. "github.com/smartystreets/goconvey/convey"
  19. gock "gopkg.in/h2non/gock.v1"
  20. )
  21. var (
  22. d *Dao
  23. )
  24. func TestMain(m *testing.M) {
  25. if os.Getenv("DEPLOY_ENV") != "" {
  26. flag.Set("app_id", "main.account-law.answer")
  27. flag.Set("conf_appid", "main.account-law.answer")
  28. flag.Set("conf_token", "ba3ee255695e8d7b46782268ddc9c8a3")
  29. flag.Set("tree_id", "25260")
  30. flag.Set("conf_version", "docker-1")
  31. flag.Set("deploy_env", "uat")
  32. flag.Set("conf_env", "10")
  33. flag.Set("conf_host", "config.bilibili.co")
  34. flag.Set("conf_path", "/tmp")
  35. flag.Set("region", "sh")
  36. flag.Set("zone", "sh001")
  37. } else {
  38. flag.Set("conf", "../../cmd/answer-test.toml")
  39. }
  40. flag.Parse()
  41. if err := conf.Init(); err != nil {
  42. panic(err)
  43. }
  44. d = New(conf.Conf)
  45. os.Exit(m.Run())
  46. }
  47. func httpMock(method, url string) *gock.Request {
  48. r := gock.New(url)
  49. r.Method = strings.ToUpper(method)
  50. return r
  51. }
  52. func TestDaoPreProcess(t *testing.T) {
  53. var (
  54. c = context.Background()
  55. mid = int64(2205)
  56. ip = "127.0.0,1"
  57. geeType = "1222"
  58. gc = conf.GeetestConfig{CaptchaID: "22"}
  59. newCaptcha = 1
  60. )
  61. convey.Convey("PreProcess", t, func(ctx convey.C) {
  62. ctx.Convey("req, err = http.NewRequest;err!=nil", func(ctx convey.C) {
  63. monkey.Patch(http.NewRequest, func(_ string, _ string, _ io.Reader) (_ *http.Request, _ error) {
  64. return nil, fmt.Errorf("Error")
  65. })
  66. _, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  67. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  68. ctx.So(err, convey.ShouldNotBeNil)
  69. })
  70. })
  71. ctx.Convey("req, err = http.NewRequest;err==nil", func(ctx convey.C) {
  72. ctx.Convey("res, err = d.client.Do; err != nil", func(ctx convey.C) {
  73. monkey.PatchInstanceMethod(reflect.TypeOf(d.client), "Do", func(_ *http.Client, _ *http.Request) (_ *http.Response, _ error) {
  74. return nil, fmt.Errorf("Error")
  75. })
  76. _, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  77. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  78. ctx.So(err, convey.ShouldNotBeNil)
  79. })
  80. })
  81. ctx.Convey("res, err = d.client.Do; err == nil", func(ctx convey.C) {
  82. params := url.Values{}
  83. params.Set("user_id", strconv.FormatInt(mid, 10))
  84. params.Set("new_captcha", strconv.Itoa(newCaptcha))
  85. params.Set("ip_address", ip)
  86. params.Set("client_type", geeType)
  87. params.Set("gt", gc.CaptchaID)
  88. d.client.Transport = gock.DefaultTransport
  89. ctx.Convey("res.StatusCode >= http.StatusInternalServerError", func(ctx convey.C) {
  90. httpMock("GET", d.registerURI+"?"+params.Encode()).Reply(501)
  91. _, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  92. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  93. ctx.So(err, convey.ShouldNotBeNil)
  94. })
  95. })
  96. ctx.Convey("res.StatusCode < http.StatusInternalServerError", func(ctx convey.C) {
  97. httpMock("GET", d.registerURI+"?"+params.Encode()).Reply(200).SetHeaders(map[string]string{
  98. "StatusCode": "200",
  99. })
  100. ctx.Convey("bs, err = ioutil.ReadAll(res.Body); err != nil ", func(ctx convey.C) {
  101. monkey.Patch(ioutil.ReadAll, func(_ io.Reader) (_ []byte, _ error) {
  102. return nil, fmt.Errorf("Error")
  103. })
  104. _, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  105. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  106. ctx.So(err, convey.ShouldNotBeNil)
  107. })
  108. })
  109. ctx.Convey("bs, err = ioutil.ReadAll(res.Body); err == nil ", func(ctx convey.C) {
  110. ctx.Convey("len(bs) != 32 ", func(ctx convey.C) {
  111. challenge, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  112. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  113. ctx.So(challenge, convey.ShouldBeEmpty)
  114. ctx.So(err, convey.ShouldBeNil)
  115. })
  116. })
  117. ctx.Convey("len(bs) == 32 ", func(ctx convey.C) {
  118. var (
  119. str = "testeeeeeeeeeeyyyyyyyyyyyyrrrrrr"
  120. bs = []byte(str)
  121. )
  122. monkey.Patch(ioutil.ReadAll, func(_ io.Reader) (_ []byte, _ error) {
  123. return bs, nil
  124. })
  125. challenge, err := d.PreProcess(c, mid, ip, geeType, gc, newCaptcha)
  126. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  127. ctx.So(challenge, convey.ShouldNotBeNil)
  128. ctx.So(err, convey.ShouldBeNil)
  129. })
  130. })
  131. })
  132. })
  133. })
  134. })
  135. ctx.Reset(func() {
  136. gock.OffAll()
  137. d.client.Transport = http.DefaultClient.Transport
  138. monkey.UnpatchAll()
  139. })
  140. })
  141. }
  142. func TestDaoValidate(t *testing.T) {
  143. var (
  144. c = context.Background()
  145. challenge = "1"
  146. seccode = "127.0.0,1"
  147. clientType = ""
  148. ip = "1222"
  149. captchaID = "22"
  150. mid = int64(14771787)
  151. )
  152. convey.Convey("Validate", t, func(ctx convey.C) {
  153. ctx.Convey("req, err = http.NewRequest;err!=nil", func(ctx convey.C) {
  154. monkey.Patch(http.NewRequest, func(_ string, _ string, _ io.Reader) (_ *http.Request, _ error) {
  155. return nil, fmt.Errorf("Error")
  156. })
  157. _, err := d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
  158. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  159. ctx.So(err, convey.ShouldNotBeNil)
  160. })
  161. })
  162. ctx.Convey("req, err = http.NewRequest;err==nil", func(ctx convey.C) {
  163. ctx.Convey("res, err = d.client.Do; err != nil", func(ctx convey.C) {
  164. monkey.PatchInstanceMethod(reflect.TypeOf(d.client), "Do", func(_ *http.Client, _ *http.Request) (_ *http.Response, _ error) {
  165. return nil, fmt.Errorf("Error")
  166. })
  167. _, err := d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
  168. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  169. ctx.So(err, convey.ShouldNotBeNil)
  170. })
  171. })
  172. ctx.Convey("res, err = d.client.Do; err == nil", func(ctx convey.C) {
  173. params := url.Values{}
  174. params.Set("seccode", seccode)
  175. params.Set("challenge", challenge)
  176. params.Set("captchaid", captchaID)
  177. params.Set("client_type", clientType)
  178. params.Set("ip_address", ip)
  179. params.Set("json_format", "1")
  180. params.Set("sdk", "golang_3.0.0")
  181. params.Set("user_id", strconv.FormatInt(mid, 10))
  182. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  183. // *bm.Client
  184. d.clientx.SetTransport(gock.DefaultTransport)
  185. httpMock("POST", d.validateURI).Reply(200).JSON(`{"code":0}`)
  186. _, err := d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
  187. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  188. ctx.So(err, convey.ShouldBeNil)
  189. })
  190. })
  191. })
  192. ctx.Reset(func() {
  193. gock.OffAll()
  194. d.clientx.SetTransport(http.DefaultClient.Transport)
  195. monkey.UnpatchAll()
  196. })
  197. })
  198. }
  199. func TestDaoGeeConfig(t *testing.T) {
  200. var gtc = &conf.Geetest{PC: conf.GeetestConfig{CaptchaID: "22", PrivateKEY: "123"}, H5: conf.GeetestConfig{CaptchaID: "22", PrivateKEY: "456"}}
  201. convey.Convey("GeeConfi", t, func(ctx convey.C) {
  202. ctx.Convey("t=pc", func(ctx convey.C) {
  203. var t = "pc"
  204. gc, geetype := d.GeeConfig(t, gtc)
  205. ctx.Convey("gc=gtc.PC,geetype =web", func(ctx convey.C) {
  206. ctx.So(gc, convey.ShouldResemble, gtc.PC)
  207. ctx.So(geetype, convey.ShouldResemble, "web")
  208. })
  209. })
  210. ctx.Convey("t=h5", func(ctx convey.C) {
  211. var t = "h5"
  212. gc, geetype := d.GeeConfig(t, gtc)
  213. ctx.Convey("gc=gtc.H5,geetype =web", func(ctx convey.C) {
  214. ctx.So(gc, convey.ShouldResemble, gtc.H5)
  215. ctx.So(geetype, convey.ShouldResemble, "web")
  216. })
  217. })
  218. ctx.Convey("t=", func(ctx convey.C) {
  219. var t = ""
  220. gc, geetype := d.GeeConfig(t, gtc)
  221. ctx.Convey("gc=gtc.PC,geetype =web", func(ctx convey.C) {
  222. ctx.So(gc, convey.ShouldResemble, gtc.PC)
  223. ctx.So(geetype, convey.ShouldResemble, "web")
  224. })
  225. })
  226. })
  227. }