| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package sender
- import (
- "context"
- "crypto/hmac"
- "crypto/sha256"
- "encoding/base64"
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- func TestConfigureSubscribePushUsesGMTextAndOpenSwitch(t *testing.T) {
- rows := []SubscribePushRow{
- {ID: 101, Title1: "挂机标题", Desc: "挂机内容", IsOpen: 1},
- {ID: 102, Title1: "闯关标题", Desc: "闯关内容", IsOpen: 0},
- {ID: 103, Title1: "门派标题", Desc: "门派内容", IsOpen: 1},
- {ID: 104, Title1: "三界标题", Desc: "三界内容", IsOpen: 1},
- {ID: 105, Title1: "诸仙标题", Desc: "诸仙内容", IsOpen: 1},
- }
- if err := ConfigureSubscribePush(rows); err != nil {
- t.Fatal(err)
- }
- got, ok := subscribePushMessage(101)
- if !ok || got.Title != "挂机标题" || got.Description != "挂机内容" {
- t.Fatalf("hangup message = %+v, enabled = %v", got, ok)
- }
- if _, ok := subscribePushMessage(102); ok {
- t.Fatal("disabled config must suppress push")
- }
- if _, ok := subscribePushMessage(999); ok {
- t.Fatal("unknown module must not be sent")
- }
- if err := ConfigureSubscribePush([]SubscribePushRow{{ID: 112, Title1: "新通知", Desc: "新内容", IsOpen: 1}}); err != nil {
- t.Fatal("config IDs must not require a hard-coded module list:", err)
- }
- if got, ok := subscribePushMessage(112); !ok || got.Title != "新通知" {
- t.Fatalf("config ID 112 message = %+v, enabled = %v", got, ok)
- }
- }
- func TestLoadSubscribePushFromGM(t *testing.T) {
- const secret = "gm-test-secret"
- server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost || r.URL.Path != "/internal/subscribe_push/config" {
- t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
- w.WriteHeader(http.StatusNotFound)
- return
- }
- var body map[string]string
- if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
- t.Error(err)
- w.WriteHeader(http.StatusBadRequest)
- return
- }
- mac := hmac.New(sha256.New, []byte(secret))
- _, _ = mac.Write([]byte("{}"))
- if body["token"] != base64.StdEncoding.EncodeToString(mac.Sum(nil)) {
- t.Error("invalid GM signature")
- w.WriteHeader(http.StatusForbidden)
- return
- }
- _ = json.NewEncoder(w).Encode(map[string]any{
- "101": map[string]any{"id": 101, "title1": "配置标题", "desc": "配置文案", "is_open": 1, "is_set": 0, "set_initialize": 1},
- "102": map[string]any{"id": 102, "is_open": 0},
- "103": map[string]any{"id": 103, "is_open": 0},
- "104": map[string]any{"id": 104, "is_open": 0},
- "105": map[string]any{"id": 105, "is_open": 0},
- "112": map[string]any{"id": 112, "is_open": 0},
- })
- }))
- defer server.Close()
- if err := LoadSubscribePushFromGM(context.Background(), server.URL+"/internal/subscribe_push/config", secret); err != nil {
- t.Fatal(err)
- }
- got, ok := subscribePushMessage(101)
- if !ok || got.Title != "配置标题" || got.Description != "配置文案" {
- t.Fatalf("loaded message = %+v, enabled = %v", got, ok)
- }
- row, ok := SubscribePushConfig(101)
- if !ok || row.IsSet != 0 || row.SetInitialize != 1 || SubscribePushRequiresSetting(row) {
- t.Fatalf("GM authorization fields not loaded: %+v", row)
- }
- }
- func TestSubscribePushRecipientFilter(t *testing.T) {
- cases := []struct {
- name string
- row SubscribePushRow
- wantSetting bool
- }{
- {"fixed-on", SubscribePushRow{ID: 109, IsOpen: 1, IsSet: 0, SetInitialize: 1}, false},
- {"not-initialized", SubscribePushRow{ID: 108, IsOpen: 1, IsSet: 0, SetInitialize: 0}, true},
- {"player-setting", SubscribePushRow{ID: 101, IsOpen: 1, IsSet: 1, SetInitialize: 1}, true},
- {"disabled", SubscribePushRow{ID: 110, IsOpen: 0, IsSet: 0, SetInitialize: 1}, true},
- {"other-nonzero-open", SubscribePushRow{ID: 111, IsOpen: 2, IsSet: 0, SetInitialize: 1}, true},
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- filter := SubscribePushRecipientFilter(tc.row)
- setting, hasSetting := filter["settings"]
- if hasSetting != tc.wantSetting || (hasSetting && setting != tc.row.ID) {
- t.Fatalf("unexpected settings filter: %v", filter)
- }
- if filter["open_id"] == nil {
- t.Fatalf("missing open_id condition: %v", filter)
- }
- })
- }
- }
|