pay_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "go-common/app/service/main/vip/model"
  10. "go-common/library/ecode"
  11. "github.com/smartystreets/goconvey/convey"
  12. "gopkg.in/h2non/gock.v1"
  13. )
  14. func TestDaoproductID(t *testing.T) {
  15. var (
  16. months = int16(1)
  17. )
  18. convey.Convey("productID", t, func(ctx convey.C) {
  19. id := d.productID(months)
  20. ctx.Convey("id should not be nil", func(ctx convey.C) {
  21. ctx.So(id, convey.ShouldEqual, _productMonthID)
  22. })
  23. })
  24. convey.Convey("productID == _productQuarterID", t, func(ctx convey.C) {
  25. months = 3
  26. id := d.productID(months)
  27. ctx.Convey("productID == 61", func(ctx convey.C) {
  28. ctx.So(id, convey.ShouldEqual, 61)
  29. })
  30. })
  31. convey.Convey("productID == _productYearID", t, func(ctx convey.C) {
  32. months = _yearMonths
  33. id := d.productID(months)
  34. ctx.Convey("productID == 61", func(ctx convey.C) {
  35. ctx.So(id, convey.ShouldEqual, _productYearID)
  36. })
  37. })
  38. }
  39. func TestDaoPayWallet(t *testing.T) {
  40. var (
  41. c = context.TODO()
  42. mid = int64(20606508)
  43. ip = ""
  44. data = &model.PayAccountResp{}
  45. )
  46. convey.Convey("PayWallet", t, func(ctx convey.C) {
  47. defer gock.OffAll()
  48. httpMock("POST", _payWallet).Reply(200).JSON(`{"code":0}`)
  49. err := d.PayWallet(c, mid, ip, data)
  50. ctx.Convey("Error should be nil", func(ctx convey.C) {
  51. ctx.So(err, convey.ShouldBeNil)
  52. })
  53. })
  54. }
  55. func TestDaoPayBanks(t *testing.T) {
  56. var (
  57. c = context.TODO()
  58. ip = ""
  59. data []*model.PayBankResp
  60. )
  61. convey.Convey("PayBanks", t, func(ctx convey.C) {
  62. defer gock.OffAll()
  63. httpMock("GET", _payBanks).Reply(200).JSON(`{"code":0}`)
  64. err := d.PayBanks(c, ip, data)
  65. ctx.Convey("Error should be nil", func(ctx convey.C) {
  66. ctx.So(err, convey.ShouldBeNil)
  67. })
  68. })
  69. }
  70. func TestDaoAddPayOrder(t *testing.T) {
  71. var (
  72. c = context.TODO()
  73. ip = ""
  74. o = &model.PayOrder{
  75. OrderNo: "201808141642435698853709",
  76. Money: 148,
  77. }
  78. data = &model.AddPayOrderResp{}
  79. )
  80. convey.Convey("AddPayOrder", t, func(ctx convey.C) {
  81. defer gock.OffAll()
  82. httpMock("POST", _addPayOrder).Reply(200).JSON(`{"code":0}`)
  83. err := d.AddPayOrder(c, ip, o, data)
  84. ctx.Convey("Error should be nil", func(ctx convey.C) {
  85. ctx.So(err, convey.ShouldBeNil)
  86. })
  87. })
  88. }
  89. func TestDaoPaySDK(t *testing.T) {
  90. var (
  91. c = context.TODO()
  92. ip = ""
  93. o = &model.PayOrder{}
  94. data = &model.APIPayOrderResp{}
  95. payCode = ""
  96. )
  97. convey.Convey("PaySDK", t, func(ctx convey.C) {
  98. defer gock.OffAll()
  99. httpMock("POST", _paySDK).Reply(200).JSON(`{"code":0}`)
  100. err := d.PaySDK(c, ip, o, data, payCode)
  101. ctx.Convey("Error should be nil", func(ctx convey.C) {
  102. ctx.So(err, convey.ShouldBeNil)
  103. })
  104. })
  105. }
  106. func TestDaoPayQrcode(t *testing.T) {
  107. var (
  108. c = context.TODO()
  109. ip = "120.11.188.230"
  110. o = &model.PayOrder{
  111. OrderNo: "2014100120411262876516",
  112. Mid: 4908640,
  113. Money: 148.00,
  114. }
  115. data = &model.APIPayOrderResp{}
  116. payCode = "alipay"
  117. )
  118. convey.Convey("PayQrcode", t, func(ctx convey.C) {
  119. defer gock.OffAll()
  120. httpMock("POST", _payQrcode).Reply(200).JSON(`{"code":0}`)
  121. err := d.PayQrcode(c, ip, o, data, payCode)
  122. ctx.Convey("Error should be nil", func(ctx convey.C) {
  123. ctx.So(err, convey.ShouldBeNil)
  124. })
  125. })
  126. }
  127. func TestDaoQuickPayToken(t *testing.T) {
  128. var (
  129. c = context.TODO()
  130. ip = ""
  131. accessKey = ""
  132. cookie []*http.Cookie
  133. data = &model.QucikPayResp{}
  134. )
  135. convey.Convey("QuickPayToken", t, func(ctx convey.C) {
  136. defer gock.OffAll()
  137. httpMock("POST", d.c.Property.PayURL+_quickPayToken).Reply(200).JSON(`{"code":0}`)
  138. err := d.QuickPayToken(c, ip, accessKey, cookie, data)
  139. ctx.Convey("Error should be nil", func(ctx convey.C) {
  140. ctx.So(err, convey.ShouldBeNil)
  141. })
  142. })
  143. }
  144. func TestDaoDoQuickPay(t *testing.T) {
  145. var (
  146. c = context.TODO()
  147. ip = ""
  148. token = ""
  149. thirdTradeNo = ""
  150. data = &model.PayRetResp{}
  151. )
  152. convey.Convey("DoQuickPay", t, func(ctx convey.C) {
  153. defer gock.OffAll()
  154. httpMock("POST", _quickPayDo).Reply(200).JSON(`{"code":0}`)
  155. err := d.DoQuickPay(c, ip, token, thirdTradeNo, data)
  156. ctx.Convey("Error should be nil", func(ctx convey.C) {
  157. ctx.So(err, convey.ShouldBeNil)
  158. })
  159. })
  160. }
  161. func TestDaoPayCashier(t *testing.T) {
  162. var (
  163. c = context.TODO()
  164. ip = ""
  165. o = &model.PayOrder{}
  166. data = &model.APIPayOrderResp{}
  167. payCode = ""
  168. bankCode = ""
  169. )
  170. convey.Convey("PayCashier", t, func(ctx convey.C) {
  171. defer gock.OffAll()
  172. httpMock("POST", _payCashier).Reply(200).JSON(`{"code":0}`)
  173. err := d.PayCashier(c, ip, o, data, payCode, bankCode)
  174. ctx.Convey("Error should be nil", func(ctx convey.C) {
  175. ctx.So(err, convey.ShouldBeNil)
  176. })
  177. })
  178. }
  179. func TestDaoPayIapAccess(t *testing.T) {
  180. var (
  181. c = context.TODO()
  182. ip = ""
  183. o = &model.PayOrder{
  184. OrderNo: "123",
  185. AppID: 1,
  186. Platform: 1,
  187. OrderType: 1,
  188. AppSubID: "456",
  189. Mid: 20606508,
  190. ToMid: 20606509,
  191. BuyMonths: 1,
  192. Money: 3.0,
  193. Status: 1,
  194. PayType: 1,
  195. RechargeBp: 1.0,
  196. ThirdTradeNo: "123",
  197. Ver: 1,
  198. }
  199. data = &model.APIPayOrderResp{}
  200. productID = "123"
  201. )
  202. convey.Convey("PayIapAccess", t, func(ctx convey.C) {
  203. defer gock.OffAll()
  204. httpMock("POST", _payIapAccess).Reply(200).JSON(`{"code":0}`)
  205. err := d.PayIapAccess(c, ip, o, data, productID)
  206. ctx.Convey("Error should be nil", func(ctx convey.C) {
  207. ctx.So(err, convey.ShouldBeNil)
  208. })
  209. })
  210. }
  211. func TestDaoPayClose(t *testing.T) {
  212. var (
  213. c = context.TODO()
  214. orderNO = "123456"
  215. ip = "127.0.0.1"
  216. )
  217. convey.Convey("PayClose", t, func(ctx convey.C) {
  218. defer gock.OffAll()
  219. httpMock("POST", d.payCloseURL).Reply(200).JSON(`{"code":0}`)
  220. _, err := d.PayClose(c, orderNO, ip)
  221. ctx.Convey("Error should be nil", func(ctx convey.C) {
  222. ctx.So(err, convey.ShouldBeNil)
  223. })
  224. })
  225. }
  226. func TestDaodopay(t *testing.T) {
  227. var (
  228. c = context.TODO()
  229. urlPath = _payWallet
  230. ip = "172.18.35.12"
  231. params = url.Values{}
  232. data = &model.PayAccountResp{}
  233. mid = int64(20606508)
  234. )
  235. convey.Convey("dopay", t, func(ctx convey.C) {
  236. params.Add("mid", fmt.Sprintf("%d", mid))
  237. defer gock.OffAll()
  238. httpMock("POST", _payWallet).Reply(200).JSON(`{"code":0}`)
  239. err := d.dopay(c, urlPath, ip, params, data, d.client.Post)
  240. ctx.Convey("Error should be nil", func(ctx convey.C) {
  241. ctx.So(err, convey.ShouldBeNil)
  242. })
  243. })
  244. }
  245. func TestDaoPayRecission(t *testing.T) {
  246. var (
  247. c = context.TODO()
  248. params map[string]interface{}
  249. clietIP = ""
  250. )
  251. convey.Convey("PayRecission", t, func(ctx convey.C) {
  252. defer gock.OffAll()
  253. httpMock("POST", _payWallet).Reply(200).JSON(`{"code":0}`)
  254. err := d.PayRecission(c, params, clietIP)
  255. ctx.Convey("Error should be nil", func(ctx convey.C) {
  256. ctx.So(err, convey.ShouldEqual, ecode.VipRescissionErr)
  257. })
  258. })
  259. }
  260. func TestDaoPaySign(t *testing.T) {
  261. var (
  262. params map[string]string
  263. token = ""
  264. )
  265. convey.Convey("PaySign", t, func(ctx convey.C) {
  266. sign := d.PaySign(params, token)
  267. ctx.Convey("sign should not be nil", func(ctx convey.C) {
  268. ctx.So(sign, convey.ShouldNotBeNil)
  269. })
  270. })
  271. }
  272. func TestDaoPaySignNotDel(t *testing.T) {
  273. var (
  274. params map[string]string
  275. token = ""
  276. )
  277. convey.Convey("PaySignNotDel", t, func(ctx convey.C) {
  278. sign := d.PaySignNotDel(params, token)
  279. ctx.Convey("sign should not be nil", func(ctx convey.C) {
  280. ctx.So(sign, convey.ShouldNotBeNil)
  281. })
  282. })
  283. }
  284. func TestDaosortParamsKey(t *testing.T) {
  285. var (
  286. v map[string]string
  287. )
  288. convey.Convey("sortParamsKey", t, func(ctx convey.C) {
  289. p1 := d.sortParamsKey(v)
  290. ctx.Convey("p1 should not be nil", func(ctx convey.C) {
  291. ctx.So(p1, convey.ShouldNotBeNil)
  292. })
  293. })
  294. }
  295. func TestDaodoPaySend(t *testing.T) {
  296. var (
  297. c = context.TODO()
  298. basePath = d.c.Property.PayURL
  299. path = _quickPayToken
  300. IP = ""
  301. cookie []*http.Cookie
  302. header map[string]string
  303. method = http.MethodPost
  304. params = map[string]string{"name": "vip"}
  305. data = new(struct {
  306. Code int64 `json:"errno"`
  307. Data int64 `json:"msg"`
  308. })
  309. )
  310. convey.Convey("doPaySend", t, func(ctx convey.C) {
  311. defer gock.OffAll()
  312. httpMock("POST", basePath+path).Reply(200).JSON(`{"code":0}`)
  313. err := d.doPaySend(c, basePath, path, IP, cookie, header, method, params, data, d.PaySign)
  314. ctx.Convey("Error should be nil", func(ctx convey.C) {
  315. ctx.So(err, convey.ShouldBeNil)
  316. })
  317. })
  318. }
  319. func TestDaoreadAll(t *testing.T) {
  320. var (
  321. r = bytes.NewReader([]byte{})
  322. capacity = int64(0)
  323. )
  324. convey.Convey("readAll", t, func(ctx convey.C) {
  325. b, err := readAll(r, capacity)
  326. ctx.Convey("Error should be nil", func(ctx convey.C) {
  327. ctx.So(err, convey.ShouldBeNil)
  328. })
  329. ctx.Convey("b should not be nil", func(ctx convey.C) {
  330. ctx.So(b, convey.ShouldNotBeNil)
  331. })
  332. })
  333. }
  334. // go test -test.v -test.run TestDaoPayClose
  335. func TestDaoPayQrCode(t *testing.T) {
  336. convey.Convey("TestDaoPayQrCode", t, func() {
  337. res, err := d.PayQrCode(context.TODO(), 1, "1", make(map[string]interface{}))
  338. convey.So(err, convey.ShouldBeNil)
  339. convey.So(res, convey.ShouldNotBeNil)
  340. })
  341. }