From 933ec2fb8db5cfb6b1089f1a5b4f04623f8d6cd7 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Tue, 25 Apr 2017 22:41:24 +1000 Subject: [PATCH] Test case for accessToken --- accessToken_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 accessToken_test.go diff --git a/accessToken_test.go b/accessToken_test.go new file mode 100644 index 0000000..7469f43 --- /dev/null +++ b/accessToken_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "os" + "testing" +) + +func TestAccessToken(t *testing.T) { + readConfig() + token, err := GetAccessToken() + if err != nil { + t.Error("Cannot get AccessToken From Tencent Wechat Server") + } + + AssertEqual(t, token, WechatAccessToken.AccessToken, "Global accesstoken is not uptodate") +} + +func TestTokenStorage(t *testing.T) { + var s AuthToken + s.AccessToken = "s0wf65p-KMzvYtH8qPu2qSX_EXLE2NaBgFHl7MZwedc7Kv_hdO0FG1QeUmBYJAGmQqJinPwFr67MRZwJee4rDnGVwhbuIfKs29N4ZJSXFP8fbAheuas08UuRe13UsdCtWSMcAFAGCW" + s.ExpireIn = 7200 + msg, _ := json.Marshal(s) + file, _ := ioutil.TempFile(os.TempDir(), "wechat_token_") + defer os.Remove(file.Name()) + writeTokenToFile([]byte(msg), file.Name()) + token, _ := readTokenFromFile(file.Name()) + AssertEqual(t, s, token, "read token should be correct") +} + +func TestReadConfig(t *testing.T) { + readConfig() + if APIConfig.AppSecret == "" { + t.Error("AppSecret not available") + } + if APIConfig.Appid == "" { + t.Error("AppId not available") + } + if APIConfig.AuthTokenSaveTo == "" { + t.Error("where to save accesstoken is unknown") + } + if APIConfig.EncodingAESKey == "" { + t.Error("unknown AesEncryptionKey") + } + if APIConfig.Token == "" { + t.Error("hidden shared secret token is empty") + } +}