wechat_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package alarm
  2. import (
  3. "context"
  4. "testing"
  5. "go-common/app/service/main/resource/model"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestAlarmstateDescribe(t *testing.T) {
  9. convey.Convey("stateDescribe", t, func(ctx convey.C) {
  10. ctx.Convey("When state = 0 is in the const map", func(ctx convey.C) {
  11. res := stateDescribe(0)
  12. ctx.Convey("Then res should equal 开放浏览", func(ctx convey.C) {
  13. ctx.So(res, convey.ShouldEqual, "开放浏览")
  14. })
  15. })
  16. ctx.Convey("When state = -99 is not in the const map", func(ctx convey.C) {
  17. res := stateDescribe(-99)
  18. ctx.Convey("Then res should equal -99", func(ctx convey.C) {
  19. ctx.So(res, convey.ShouldEqual, "-99")
  20. })
  21. })
  22. })
  23. }
  24. func TestAlarmSendWeChart(t *testing.T) {
  25. convey.Convey("SendWeChart", t, func() {
  26. convey.Convey("When everything is correct", func(ctx convey.C) {
  27. httpMock("POST", "http://bap.bilibili.co/api/v1/message/add").Reply(200).JSON("{}")
  28. err := d.SendWeChart(context.Background(), 1, "", []*model.ResWarnInfo{{}}, "unit test msg")
  29. ctx.Convey("Then err should be nil", func(ctx convey.C) {
  30. ctx.So(err, convey.ShouldBeNil)
  31. })
  32. })
  33. convey.Convey("When set http request gets 404", func(ctx convey.C) {
  34. httpMock("POST", "http://bap.bilibili.co/api/v1/message/add").Reply(404)
  35. err := d.SendWeChart(context.Background(), 1, "", []*model.ResWarnInfo{{}}, "unit test msg")
  36. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  37. ctx.So(err, convey.ShouldNotBeNil)
  38. })
  39. })
  40. convey.Convey("When set titleType == \"warn\"", func(ctx convey.C) {
  41. httpMock("POST", "http://bap.bilibili.co/api/v1/message/add").Reply(200).JSON("{}")
  42. err := d.SendWeChart(context.Background(), 1, "", []*model.ResWarnInfo{{}}, "warn")
  43. ctx.Convey("Then err should be nil", func(ctx convey.C) {
  44. ctx.So(err, convey.ShouldBeNil)
  45. })
  46. })
  47. })
  48. }
  49. func TestAlarmsendWeChartURL(t *testing.T) {
  50. convey.Convey("sendWeChartURL", t, func() {
  51. convey.Convey("When everything is correct", func(ctx convey.C) {
  52. httpMock("POST", "http://bap.bilibili.co/api/v1/message/add").Reply(200).JSON("{}")
  53. err := d.sendWeChartURL(context.Background(), 1, "", []*model.ResWarnInfo{{}})
  54. ctx.Convey("Then err should be nil", func(ctx convey.C) {
  55. ctx.So(err, convey.ShouldBeNil)
  56. })
  57. })
  58. convey.Convey("When set http request gets 404", func(ctx convey.C) {
  59. httpMock("POST", "http://bap.bilibili.co/api/v1/message/add").Reply(404)
  60. err := d.sendWeChartURL(context.Background(), 1, "", []*model.ResWarnInfo{{}})
  61. ctx.Convey("Then err should not be nil", func(ctx convey.C) {
  62. ctx.So(err, convey.ShouldNotBeNil)
  63. })
  64. })
  65. })
  66. }