subscribe_push_config_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package sender
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "encoding/json"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. )
  12. func TestConfigureSubscribePushUsesGMTextAndOpenSwitch(t *testing.T) {
  13. rows := []SubscribePushRow{
  14. {ID: 101, Title1: "挂机标题", Desc: "挂机内容", IsOpen: 1},
  15. {ID: 102, Title1: "闯关标题", Desc: "闯关内容", IsOpen: 0},
  16. {ID: 103, Title1: "门派标题", Desc: "门派内容", IsOpen: 1},
  17. {ID: 104, Title1: "三界标题", Desc: "三界内容", IsOpen: 1},
  18. {ID: 105, Title1: "诸仙标题", Desc: "诸仙内容", IsOpen: 1},
  19. }
  20. if err := ConfigureSubscribePush(rows); err != nil {
  21. t.Fatal(err)
  22. }
  23. got, ok := subscribePushMessage(101)
  24. if !ok || got.Title != "挂机标题" || got.Description != "挂机内容" {
  25. t.Fatalf("hangup message = %+v, enabled = %v", got, ok)
  26. }
  27. if _, ok := subscribePushMessage(102); ok {
  28. t.Fatal("disabled config must suppress push")
  29. }
  30. if _, ok := subscribePushMessage(999); ok {
  31. t.Fatal("unknown module must not be sent")
  32. }
  33. if err := ConfigureSubscribePush([]SubscribePushRow{{ID: 112, Title1: "新通知", Desc: "新内容", IsOpen: 1}}); err != nil {
  34. t.Fatal("config IDs must not require a hard-coded module list:", err)
  35. }
  36. if got, ok := subscribePushMessage(112); !ok || got.Title != "新通知" {
  37. t.Fatalf("config ID 112 message = %+v, enabled = %v", got, ok)
  38. }
  39. }
  40. func TestLoadSubscribePushFromGM(t *testing.T) {
  41. const secret = "gm-test-secret"
  42. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  43. if r.Method != http.MethodPost || r.URL.Path != "/internal/subscribe_push/config" {
  44. t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
  45. w.WriteHeader(http.StatusNotFound)
  46. return
  47. }
  48. var body map[string]string
  49. if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
  50. t.Error(err)
  51. w.WriteHeader(http.StatusBadRequest)
  52. return
  53. }
  54. mac := hmac.New(sha256.New, []byte(secret))
  55. _, _ = mac.Write([]byte("{}"))
  56. if body["token"] != base64.StdEncoding.EncodeToString(mac.Sum(nil)) {
  57. t.Error("invalid GM signature")
  58. w.WriteHeader(http.StatusForbidden)
  59. return
  60. }
  61. _ = json.NewEncoder(w).Encode(map[string]any{
  62. "101": map[string]any{"id": 101, "title1": "配置标题", "desc": "配置文案", "is_open": 1, "is_set": 0, "set_initialize": 1},
  63. "102": map[string]any{"id": 102, "is_open": 0},
  64. "103": map[string]any{"id": 103, "is_open": 0},
  65. "104": map[string]any{"id": 104, "is_open": 0},
  66. "105": map[string]any{"id": 105, "is_open": 0},
  67. "112": map[string]any{"id": 112, "is_open": 0},
  68. })
  69. }))
  70. defer server.Close()
  71. if err := LoadSubscribePushFromGM(context.Background(), server.URL+"/internal/subscribe_push/config", secret); err != nil {
  72. t.Fatal(err)
  73. }
  74. got, ok := subscribePushMessage(101)
  75. if !ok || got.Title != "配置标题" || got.Description != "配置文案" {
  76. t.Fatalf("loaded message = %+v, enabled = %v", got, ok)
  77. }
  78. row, ok := SubscribePushConfig(101)
  79. if !ok || row.IsSet != 0 || row.SetInitialize != 1 || SubscribePushRequiresSetting(row) {
  80. t.Fatalf("GM authorization fields not loaded: %+v", row)
  81. }
  82. }
  83. func TestSubscribePushRecipientFilter(t *testing.T) {
  84. cases := []struct {
  85. name string
  86. row SubscribePushRow
  87. wantSetting bool
  88. }{
  89. {"fixed-on", SubscribePushRow{ID: 109, IsOpen: 1, IsSet: 0, SetInitialize: 1}, false},
  90. {"not-initialized", SubscribePushRow{ID: 108, IsOpen: 1, IsSet: 0, SetInitialize: 0}, true},
  91. {"player-setting", SubscribePushRow{ID: 101, IsOpen: 1, IsSet: 1, SetInitialize: 1}, true},
  92. {"disabled", SubscribePushRow{ID: 110, IsOpen: 0, IsSet: 0, SetInitialize: 1}, true},
  93. {"other-nonzero-open", SubscribePushRow{ID: 111, IsOpen: 2, IsSet: 0, SetInitialize: 1}, true},
  94. }
  95. for _, tc := range cases {
  96. t.Run(tc.name, func(t *testing.T) {
  97. filter := SubscribePushRecipientFilter(tc.row)
  98. setting, hasSetting := filter["settings"]
  99. if hasSetting != tc.wantSetting || (hasSetting && setting != tc.row.ID) {
  100. t.Fatalf("unexpected settings filter: %v", filter)
  101. }
  102. if filter["open_id"] == nil {
  103. t.Fatalf("missing open_id condition: %v", filter)
  104. }
  105. })
  106. }
  107. }