tree_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "github.com/smartystreets/goconvey/convey"
  7. gock "gopkg.in/h2non/gock.v1"
  8. )
  9. var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig"
  10. func TestToken(t *testing.T) {
  11. d.client.SetTransport(gock.DefaultTransport)
  12. convey.Convey("get token", t, func(ctx convey.C) {
  13. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  14. httpMock("POST", "http://easyst.bilibili.co/v1/token").Reply(200).JSON(`{
  15. "code": 90000,
  16. "data": {
  17. "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJtYWluIiwicGxhdGZvcm1faWQiOiIyRjNiOGZEVkdsTW5qOGFDRGxNYVciLCJleHAiOjE1NDA2NDE3MjQsImlzcyI6Im1haW4ifQ.SxKceDl69N1su3kDO70c4P1NuPhXOxp5rXpM3n8Jyig",
  18. "user_name": "main",
  19. "secret": "",
  20. "expired": 1540641724
  21. },
  22. "message": "success",
  23. "status": 200
  24. }`)
  25. data, err := d.Token(context.Background(), "")
  26. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  27. ctx.So(err, convey.ShouldBeNil)
  28. ctx.So(data, convey.ShouldNotBeNil)
  29. })
  30. })
  31. ctx.Reset(func() {
  32. gock.Off()
  33. d.client.SetTransport(http.DefaultClient.Transport)
  34. })
  35. })
  36. }
  37. func TestAuth(t *testing.T) {
  38. d.client.SetTransport(gock.DefaultTransport)
  39. convey.Convey("get auth", t, func(ctx convey.C) {
  40. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  41. httpMock("GET", "http://easyst.bilibili.co/v1/auth").Reply(200).JSON(`{"code":90000,"message":"success"}`)
  42. _, err := d.Auth(context.Background(), "sven-apm=afab110001a20fa3a1c9b5bd67564ccd71c6253406725184bfa5d88c7ece9d17; Path=/; Domain=bilibili.co; Expires=Tue, 23 Oct 2018 07:25:02 GMT; Max-Age=1800; HttpOnly")
  43. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  44. ctx.So(err, convey.ShouldBeNil)
  45. // ctx.So(data, convey.ShouldNotBeNil)
  46. })
  47. })
  48. ctx.Reset(func() {
  49. gock.Off()
  50. d.client.SetTransport(http.DefaultClient.Transport)
  51. })
  52. })
  53. }
  54. func TestRole(t *testing.T) {
  55. d.client.SetTransport(gock.DefaultTransport)
  56. convey.Convey("TestRole", t, func(ctx convey.C) {
  57. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  58. httpMock("GET", "http://easyst.bilibili.co/v1/node/role/app").Reply(200).JSON(`{"code":90000,"message":"success"}`)
  59. data, err := d.Role(context.Background(), "linli", token)
  60. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  61. ctx.So(err, convey.ShouldBeNil)
  62. ctx.So(data, convey.ShouldNotBeNil)
  63. })
  64. })
  65. ctx.Reset(func() {
  66. gock.Off()
  67. d.client.SetTransport(http.DefaultClient.Transport)
  68. })
  69. })
  70. }
  71. func TestNodeTree(t *testing.T) {
  72. d.client.SetTransport(gock.DefaultTransport)
  73. convey.Convey("NodeTree", t, func(ctx convey.C) {
  74. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  75. httpMock("GET", "http://easyst.bilibili.co/v1/node/bilibili.main.common-arch").Reply(200).JSON(`{
  76. "status": 200,
  77. "message": "success",
  78. "code": 90000,
  79. "data": {
  80. "count": 0,
  81. "results": 20,
  82. "page": 1,
  83. "data": null
  84. }
  85. }`)
  86. _, err := d.NodeTree(context.Background(), token, "main.common-arch", "")
  87. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  88. ctx.So(err, convey.ShouldBeNil)
  89. // ctx.So(data, convey.ShouldNotBeNil)
  90. })
  91. })
  92. ctx.Reset(func() {
  93. gock.Off()
  94. d.client.SetTransport(http.DefaultClient.Transport)
  95. })
  96. })
  97. }
  98. func TestTree(t *testing.T) {
  99. d.client.SetTransport(gock.DefaultTransport)
  100. convey.Convey("TestTree", t, func(ctx convey.C) {
  101. ctx.Convey("When http response code != 0", func(ctx convey.C) {
  102. httpMock("GET", "http://easyst.bilibili.co/v1/node/apptree").Reply(200).JSON(`{
  103. "code": 90000,
  104. "data": {
  105. "bilibili": {
  106. "id": 0,
  107. "name": "bilibili",
  108. "alias": "哔哩哔哩",
  109. "type": 1,
  110. "path": "bilibili",
  111. "tags": {},
  112. "children": null
  113. }
  114. },
  115. "message": "success",
  116. "status": 200
  117. }`)
  118. _, err := d.Tree(context.Background(), token)
  119. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  120. ctx.So(err, convey.ShouldBeNil)
  121. // ctx.So(data, convey.ShouldNotBeNil)
  122. })
  123. })
  124. ctx.Reset(func() {
  125. gock.Off()
  126. d.client.SetTransport(http.DefaultClient.Transport)
  127. })
  128. })
  129. }