dao_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package manager
  2. import (
  3. "flag"
  4. "os"
  5. "testing"
  6. "context"
  7. "github.com/smartystreets/goconvey/convey"
  8. "go-common/app/admin/main/up/conf"
  9. "go-common/app/admin/main/up/model/signmodel"
  10. "gopkg.in/h2non/gock.v1"
  11. "strings"
  12. )
  13. var (
  14. d *Dao
  15. )
  16. func TestMain(m *testing.M) {
  17. if os.Getenv("DEPLOY_ENV") != "" {
  18. flag.Set("app_id", "main.archive.up-admin")
  19. flag.Set("conf_token", "930697bb7def4df0713ef8080596b863")
  20. flag.Set("tree_id", "36438")
  21. flag.Set("conf_version", "1")
  22. flag.Set("deploy_env", "uat")
  23. flag.Set("conf_host", "config.bilibili.co")
  24. flag.Set("conf_path", "/tmp")
  25. flag.Set("region", "sh")
  26. flag.Set("zone", "sh001")
  27. } else {
  28. flag.Set("conf", "../../cmd/up-admin.toml")
  29. }
  30. if os.Getenv("UT_LOCAL_TEST") != "" {
  31. flag.Set("conf", "../../cmd/up-admin.toml")
  32. }
  33. flag.Parse()
  34. if err := conf.Init(); err != nil {
  35. panic(err)
  36. }
  37. d = New(conf.Conf)
  38. d.HTTPClient.SetTransport(gock.DefaultTransport)
  39. os.Exit(m.Run())
  40. }
  41. func httpMock(method, url string) *gock.Request {
  42. r := gock.New(url)
  43. r.Method = strings.ToUpper(method)
  44. return r
  45. }
  46. func TestGetUIDByNames(t *testing.T) {
  47. convey.Convey("TestGetUIDByNames", t, func(ctx convey.C) {
  48. var (
  49. names []string
  50. c = context.Background()
  51. )
  52. defer gock.OffAll()
  53. httpMock("GET", d.c.Host.Manager+URLUids).Reply(200).BodyString(`
  54. {
  55. "code" : 0,
  56. "data" : { "1" : 1 }
  57. }
  58. `)
  59. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  60. res, err := d.GetUIDByNames(c, names)
  61. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  62. ctx.So(err, convey.ShouldBeNil)
  63. ctx.So(res, convey.ShouldNotBeNil)
  64. })
  65. })
  66. })
  67. }
  68. func TestGetUNamesByUids(t *testing.T) {
  69. convey.Convey("TestGetUNamesByUids", t, func(ctx convey.C) {
  70. var (
  71. uids = []int64{0}
  72. c = context.Background()
  73. )
  74. defer gock.OffAll()
  75. httpMock("GET", d.c.Host.Manager+URLUNames).Reply(200).BodyString(`
  76. {
  77. "code" : 0,
  78. "data" : { "1" : "adm1" }
  79. }
  80. `)
  81. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  82. res, err := d.GetUNamesByUids(c, uids)
  83. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  84. ctx.So(err, convey.ShouldBeNil)
  85. ctx.So(res, convey.ShouldNotBeNil)
  86. })
  87. })
  88. })
  89. }
  90. func TestSignUpAuditLogs(t *testing.T) {
  91. convey.Convey("SendMail", t, func(ctx convey.C) {
  92. var (
  93. arg = &signmodel.SignOpSearchArg{}
  94. c = context.Background()
  95. )
  96. defer gock.OffAll()
  97. httpMock("GET", d.c.Host.Manager+URLAuditLog).Reply(200).BodyString(`
  98. {
  99. "code" : 0,
  100. "data" : {
  101. "order" : "asc",
  102. "sort" : "fe"
  103. }
  104. }
  105. `)
  106. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  107. res, err := d.SignUpAuditLogs(c, arg)
  108. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  109. ctx.So(err, convey.ShouldBeNil)
  110. ctx.So(res, convey.ShouldNotBeNil)
  111. })
  112. })
  113. })
  114. }