package wechat import ( "strings" "testing" ) func TestConfig_Enabled(t *testing.T) { if (Config{AppID: "x"}).Enabled() { t.Fatal("缺 secret 不该 enabled") } if !(Config{AppID: "x", AppSecret: "y"}).Enabled() { t.Fatal("齐全应 enabled") } } func TestAuthorizeURL(t *testing.T) { c := Config{AppID: "wxAPP"} u := c.AuthorizeURL("https://a.b/cb", "tkt123") // 必备参数与微信要求的锚点 for _, want := range []string{ "open.weixin.qq.com/connect/oauth2/authorize", "appid=wxAPP", "scope=snsapi_base", "state=tkt123", "redirect_uri=https%3A%2F%2Fa.b%2Fcb", // 必须 URL 编码 "#wechat_redirect", // 缺了微信不跳转 } { if !strings.Contains(u, want) { t.Fatalf("授权 URL 缺 %q:%s", want, u) } } // 锚点必须在最后 if !strings.HasSuffix(u, "#wechat_redirect") { t.Fatalf("#wechat_redirect 必须在末尾:%s", u) } } // secret 加密往返:落库是密文,取出还原成明文。 func TestConfig_SecretRoundTrip(t *testing.T) { c := Config{AppID: "x", AppSecret: "the-plain-secret"} stored, err := c.EncryptedForStore() if err != nil { t.Fatal(err) } if stored.AppSecret == "the-plain-secret" { t.Fatal("落库的 secret 不该是明文") } back := stored.DecryptFromStore() if back.AppSecret != "the-plain-secret" { t.Fatalf("还原失败:%q", back.AppSecret) } }