113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWechatTemplateKey(t *testing.T) {
|
|
cases := map[string]string{
|
|
"green": "",
|
|
"yellow": "inspection",
|
|
"orange": "inspection",
|
|
"red": "inspection",
|
|
"": "",
|
|
"unknown": "",
|
|
}
|
|
for level, want := range cases {
|
|
if got := WechatTemplateKey(level); got != want {
|
|
t.Errorf("WechatTemplateKey(%q) = %q, want %q", level, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsAuthorized(t *testing.T) {
|
|
auth := []string{"alarm", "inspection"}
|
|
if !IsAuthorized(auth, "inspection") {
|
|
t.Error("inspection 应在授权列表内")
|
|
}
|
|
if IsAuthorized(auth, "push") {
|
|
t.Error("push 不应在授权列表内")
|
|
}
|
|
if IsAuthorized(nil, "inspection") {
|
|
t.Error("空授权列表应返回 false")
|
|
}
|
|
}
|
|
|
|
func TestBuildSubscribeData(t *testing.T) {
|
|
data := BuildSubscribeData("orange", 75)
|
|
if data["thing1"]["value"] != "橙" {
|
|
t.Errorf("thing1 应为橙色等级,实际 %v", data["thing1"])
|
|
}
|
|
if data["thing2"]["value"] != "75分" {
|
|
t.Errorf("thing2 应为 75分,实际 %v", data["thing2"])
|
|
}
|
|
}
|
|
|
|
func TestWechatCode2Session(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/sns/jscode2session" {
|
|
t.Errorf("path = %s", r.URL.Path)
|
|
}
|
|
q := r.URL.Query()
|
|
if q.Get("appid") != "app1" || q.Get("secret") != "sec1" || q.Get("js_code") != "code123" {
|
|
t.Errorf("参数不正确: %v", q)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"openid": "oAbC123", "session_key": "sk"})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
s := NewWechatService("app1", "sec1")
|
|
s.baseURL = srv.URL
|
|
openid, err := s.Code2Session(context.Background(), "code123")
|
|
if err != nil {
|
|
t.Fatalf("Code2Session 错误: %v", err)
|
|
}
|
|
if openid != "oAbC123" {
|
|
t.Errorf("openid = %s", openid)
|
|
}
|
|
}
|
|
|
|
func TestWechatSendSubscribe(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/cgi-bin/message/subscribe/send" {
|
|
t.Errorf("path = %s", r.URL.Path)
|
|
}
|
|
var body map[string]any
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
if body["touser"] != "oAbC123" || body["template_id"] != "tmpl1" {
|
|
t.Errorf("body 不正确: %v", body)
|
|
}
|
|
_, _ = w.Write([]byte(`{"errcode":0,"errmsg":"ok"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
s := NewWechatService("app1", "sec1")
|
|
s.baseURL = srv.URL
|
|
s.accessToken = "fake-token"
|
|
s.tokenExpire = time.Now().Add(time.Hour)
|
|
err := s.SendSubscribe(context.Background(), "oAbC123", "tmpl1", BuildSubscribeData("red", 88), "")
|
|
if err != nil {
|
|
t.Fatalf("SendSubscribe 错误: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWechatSendSubscribeErrorCode(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"errcode":40003,"errmsg":"invalid openid"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
s := NewWechatService("app1", "sec1")
|
|
s.baseURL = srv.URL
|
|
s.accessToken = "fake-token"
|
|
s.tokenExpire = time.Now().Add(time.Hour)
|
|
if err := s.SendSubscribe(context.Background(), "bad", "tmpl1", nil, ""); err == nil {
|
|
t.Error("errcode!=0 应返回错误")
|
|
}
|
|
}
|