diff --git a/accessToken_test.go b/accessToken_test.go index 9e29585..da25fb9 100644 --- a/accessToken_test.go +++ b/accessToken_test.go @@ -9,7 +9,7 @@ import ( //get accesstoken from wechat server or local cache file func TestAccessToken(t *testing.T) { - SetupConfig() + token, err := GetAccessToken() if err != nil { t.Error("Cannot get AccessToken From Tencent Wechat Server") @@ -33,7 +33,7 @@ func TestTokenStorage(t *testing.T) { //read config, make sure its not empty func TestReadConfig(t *testing.T) { - SetupConfig() + if APIConfig.AppSecret == "" { t.Error("AppSecret not available") } diff --git a/common_test.go b/common_test.go index 85a9acf..f05b2b8 100644 --- a/common_test.go +++ b/common_test.go @@ -2,10 +2,21 @@ package main import ( "fmt" + "log" + "os" "runtime" "testing" ) +func TestMain(m *testing.M) { + log.Println("global setup for all test cases ... ") + SetupConfig() + code := m.Run() + //Shutdown + log.Println("global shutdown for all test cases ... ") + os.Exit(code) +} + func SetupConfig() { APIConfig = WechatAPIConfig{ "skdq8vklaurfqemfszuif", diff --git a/crmAttachment_test.go b/crmAttachment_test.go index 6340936..5655db3 100644 --- a/crmAttachment_test.go +++ b/crmAttachment_test.go @@ -5,7 +5,7 @@ import "log" import "encoding/json" func TestCRMAttachFile(t *testing.T) { - SetupConfig() + r, e := crmCreateAttachment("media_for_test/200x200.png") log.Println(r) log.Println(e) @@ -23,7 +23,7 @@ func TestDecodeJsonResponse(t *testing.T) { } func TestCRMDownloadAttachment(t *testing.T) { - SetupConfig() + id := "59054884ef1b0c04f" //crmDownloadAttachment(id) crmDownloadAttachmentAs(id, "/tmp/wechat_hitxy_测试") diff --git a/crmEntity_test.go b/crmEntity_test.go index f8e9f72..be3180d 100644 --- a/crmEntity_test.go +++ b/crmEntity_test.go @@ -8,7 +8,7 @@ import ( ) func TestCrmCreateEntity(t *testing.T) { - SetupConfig() + e := crmdLead{} e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") e.LastName = "ll" @@ -42,7 +42,7 @@ func TestCrmCreateEntity(t *testing.T) { } func TestCreateDuplicate(t *testing.T) { - SetupConfig() + e := crmdLead{} e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") e.LastName = "ll" @@ -72,7 +72,6 @@ func TestCreateDuplicate(t *testing.T) { } func TestCrmCreateEntityServerNotFound(t *testing.T) { - SetupConfig() e := crmdLead{} e.FirstName = "ff" + time.Now().Format("2006-jan-02 03:04:05") @@ -82,11 +81,11 @@ func TestCrmCreateEntityServerNotFound(t *testing.T) { e.WechatHitxyID = "someopenid" b, _ := json.Marshal(e) - //quickly temporarily change base url - oldUrl := CRMConfig.BaseURL + //temporarily change base url + oldURL := CRMConfig.BaseURL CRMConfig.BaseURL = "https://not-exist.hitxy.org.au/" // entity, err := crmCreateEntity("Lead", b) - CRMConfig.BaseURL = oldUrl + CRMConfig.BaseURL = oldURL AssertEqual(t, isErrIndicateDuplicate(err), false, "this should not be a duplicate error") AssertEqual(t, entity, nil, "should not be able to create entity, it should be nil") @@ -94,7 +93,7 @@ func TestCrmCreateEntityServerNotFound(t *testing.T) { } func TestCrmReplaceEntity(t *testing.T) { - SetupConfig() + e := crmdLead{} e.FirstName = "ff1" + time.Now().Format("2006-jan-02 03:04:05") e.LastName = "ll1" @@ -123,7 +122,6 @@ func TestCrmReplaceEntity(t *testing.T) { AssertEqual(t, entity.(crmdLead).WechatHitxyID, "newid", "wechat_hitxy_id should be updated") AssertEqual(t, entity.(crmdLead).Password, "newpass", "password should have been changed to newpass") - return //delete deleted, _ := crmDeleteEntity("Lead", id) AssertEqual(t, deleted, true, "test record should have been deleted.") diff --git a/crmLeadStream_test.go b/crmLeadStream_test.go index c36c955..0900e96 100644 --- a/crmLeadStream_test.go +++ b/crmLeadStream_test.go @@ -78,7 +78,7 @@ func TestDecodeStream(t *testing.T) { } func TestAddNewStream(t *testing.T) { - SetupConfig() + parentid := "595071f8450974b72" parentType := "Lead" diff --git a/crmLead_test.go b/crmLead_test.go index 4f0a9d6..7dc7a21 100644 --- a/crmLead_test.go +++ b/crmLead_test.go @@ -391,7 +391,7 @@ func TestSearchResultZero(t *testing.T) { } func TestFindOpenIDOnline(t *testing.T) { - SetupConfig() + info, found, err := crmFindOpenID("weid1") log.Println(info) log.Println(found) @@ -405,7 +405,7 @@ func TestLeadJsonCompose(t *testing.T) { } func TestGetLead(t *testing.T) { - SetupConfig() + id := "595071f8450974b72" r, err := crmGetLead(id) AssertEqual(t, err, nil, "decode json should be nil") diff --git a/customMenu_test.go b/customMenu_test.go index 59ada8d..c9c5c44 100644 --- a/customMenu_test.go +++ b/customMenu_test.go @@ -4,7 +4,7 @@ import "testing" //This test retrieve current menu func TestGetMenu(t *testing.T) { - SetupConfig() + CreateDefaultMenu() //check menu from Wechat } diff --git a/encrypt_test.go b/encrypt_test.go index 8ea9186..d52dbf2 100644 --- a/encrypt_test.go +++ b/encrypt_test.go @@ -5,7 +5,7 @@ import ( ) func TestEncodingMessage(t *testing.T) { - SetupConfig() + //明文: var msg = ` diff --git a/eventSubscribe_test.go b/eventSubscribe_test.go index bdc1463..1afeaaa 100644 --- a/eventSubscribe_test.go +++ b/eventSubscribe_test.go @@ -49,7 +49,7 @@ func TestDecodeSubscribeJson(t *testing.T) { } func TestRegisterNewUser(t *testing.T) { - SetupConfig() + msg := ` { "subscribe": 1, diff --git a/inMsg_test.go b/inMsg_test.go index 4d78697..cbb265e 100644 --- a/inMsg_test.go +++ b/inMsg_test.go @@ -5,7 +5,7 @@ import ( ) func TestReadCommonHeader(t *testing.T) { - SetupConfig() + var msg = ` 1492972518 diff --git a/kfsend_test.go b/kfsend_test.go index a6bc208..313689c 100644 --- a/kfsend_test.go +++ b/kfsend_test.go @@ -9,7 +9,7 @@ import ( var toUser = "oUN420Wj78vnkNeAJY7RMPXA28oc" // "oUN420bxqFqlx0ZQHciUOesZO3PE" func TestSendTxt(t *testing.T) { - SetupConfig() + msg := fmt.Sprintf("测试消息 & < >, %s ", time.Now().String()) // randinit() @@ -23,18 +23,18 @@ func TestSendTxt(t *testing.T) { } func TestSendPic(t *testing.T) { - SetupConfig() + kfSendPic(toUser, "media_for_test/200x200.png") } func TestSendVoice(t *testing.T) { - SetupConfig() + kfSendVoice(toUser, "media_for_test/example.amr") //kfSendVoiceByMediaID(toUser, "8Tc-pcFxEMtbO6T71AaL0A16taJUpwolXpB06mBBfaPZ68R3__1nN7HuZAUyW8xR") } func TestSendVideo(t *testing.T) { - SetupConfig() + kfSendVideo(toUser, "media_for_test/video.mp4", "测试时品", "普通描述", "media_for_test/music-thumb.jpg") // kfSendVideoByMediaID(toUser, @@ -45,7 +45,7 @@ func TestSendVideo(t *testing.T) { } func TestSendMusic(t *testing.T) { - SetupConfig() + tID := uploadThumb("media_for_test/music-thumb.jpg") description := fmt.Sprintf("音乐描述 %s", time.Now().String()) kfSendMusic(toUser, @@ -57,7 +57,7 @@ func TestSendMusic(t *testing.T) { } func TestSendArticle(t *testing.T) { - SetupConfig() + articles := []sendNewsArticle{} for i := 1; i <= 8; i++ { a := sendNewsArticle{} @@ -71,6 +71,6 @@ func TestSendArticle(t *testing.T) { } func TestSendArticleByMediaID(t *testing.T) { - SetupConfig() + kfSendMPNews(toUser, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw") } diff --git a/kfuser_test.go b/kfuser_test.go index 1425618..6bf60ef 100644 --- a/kfuser_test.go +++ b/kfuser_test.go @@ -10,7 +10,7 @@ import ( //response data is from server, makesure the data is as expected before run this test. // otherwise, the test should fail func TestKfList(t *testing.T) { - SetupConfig() + now := int32(time.Now().Unix()) s := kfCache{} diff --git a/material_test.go b/material_test.go index 119758a..2b59d86 100644 --- a/material_test.go +++ b/material_test.go @@ -6,7 +6,7 @@ import ( ) func TestGetMaterialNewsList(t *testing.T) { - SetupConfig() + l := getNewList() log.Printf("news is : %s", l) nl := unmarshalNewsList(l) @@ -17,25 +17,25 @@ func TestGetMaterialNewsList(t *testing.T) { } func TestGetMaterialVoiceList(t *testing.T) { - SetupConfig() + l := getVoiceList() log.Printf("voice is : %s", l) } func TestGetMaterialVideoList(t *testing.T) { - SetupConfig() + l := getVideoList() log.Printf("voice is : %s", l) } func TestGetMaterialImageList(t *testing.T) { - SetupConfig() + l := getImageList() log.Printf("voice is : %s", l) } func TestGetMaterialCount(t *testing.T) { - SetupConfig() + mc, err := getMaterialCount() if err != nil { log.Fatal(err) diff --git a/outMsg_test.go b/outMsg_test.go index 9cd4f07..725f568 100644 --- a/outMsg_test.go +++ b/outMsg_test.go @@ -3,7 +3,7 @@ package main import "testing" func TestBuildTxtMsg(t *testing.T) { - SetupConfig() + s, _ := BuildTextMsg("你好", "oUN420bxqFqlx0ZQHciUOesZO3PE") sd := decryptToXML(s) h := ReadCommonHeader(sd) diff --git a/outTemplateMsg_test.go b/outTemplateMsg_test.go index 1e8d291..c76a759 100644 --- a/outTemplateMsg_test.go +++ b/outTemplateMsg_test.go @@ -6,7 +6,7 @@ import ( ) func TestSendTemplateJoinVolunteer(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "很高兴有你参加志愿者" remark := "明天给你发1万块钱" @@ -19,7 +19,7 @@ func TestSendTemplateJoinVolunteer(t *testing.T) { } func TestSendTemplateJoinTeam(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "欢迎加入校友会服务团队" remark := "给你发2万张证书" @@ -29,7 +29,7 @@ func TestSendTemplateJoinTeam(t *testing.T) { } func TestSendTemplateCheckinSuccess(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "恭喜签到成功,你是第五位签到者" remark := "团结向上,一起发展" @@ -40,7 +40,7 @@ func TestSendTemplateCheckinSuccess(t *testing.T) { } func TestSendTemplateMsgFormFillupDone(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au" first := "您的录入信息已经收到,正在核实" remark := "若信息有误,会影响您校友会会员的资格" @@ -50,7 +50,7 @@ func TestSendTemplateMsgFormFillupDone(t *testing.T) { } func TestSendTemplateEnrollSuccess(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "即将到来的活动,欢迎你的到来" remark := "怎么才能让校友们积极互动是一个问题" @@ -62,7 +62,7 @@ func TestSendTemplateEnrollSuccess(t *testing.T) { } func TestSendTemplateMeetingReminder(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "恭喜签到成功,你是第五位签到者" remark := "怎么才能让校友们积极互动是一个问题" @@ -73,7 +73,7 @@ func TestSendTemplateMeetingReminder(t *testing.T) { } func TestSendTemplateCheckinReminder(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "即将到来的活动,欢迎你的到来" remark := "来晚了罚钱,你大爷的" @@ -84,7 +84,7 @@ func TestSendTemplateCheckinReminder(t *testing.T) { } func TestSendTemplateMemberFeeReminder(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "校友会会员费\n" remark := "逾期不缴费将会被列出来" @@ -96,7 +96,7 @@ func TestSendTemplateMemberFeeReminder(t *testing.T) { } func TestSendTemplatePaymentAck(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "即将到来的活动,欢迎你的到来" remark := "怎么才能让校友们积极互动是一个问题" @@ -109,7 +109,7 @@ func TestSendTemplatePaymentAck(t *testing.T) { } func TestSendTemplateCheckinFail(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "--" remark := "checkin failed, due to your location is incorrect" @@ -119,7 +119,7 @@ func TestSendTemplateCheckinFail(t *testing.T) { } func TestSendTemplateInformCollection(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au/" first := "恭喜你获得本次Lucky Draw奖金 $200澳币" remark := "请你点击详情,通知你已收到奖励" @@ -129,7 +129,7 @@ func TestSendTemplateInformCollection(t *testing.T) { } func TestSendTemplateMsgJoinCommunity(t *testing.T) { - SetupConfig() + url := "http://www.google.com.au" communityName := "小大同会" first := "欢迎加入Community" + communityName diff --git a/qrcode_test.go b/qrcode_test.go index 3189cc1..2dfbfd4 100644 --- a/qrcode_test.go +++ b/qrcode_test.go @@ -7,7 +7,7 @@ import ( ) func TestCreatePermQR(t *testing.T) { - SetupConfig() + r, e := CreateProfileEditQR() AssertEqual(t, e, nil, "Create QR code should not have error") log.Println("qr ticket = " + r.Ticket) @@ -17,7 +17,7 @@ func TestCreatePermQR(t *testing.T) { } func TestCreateTempQr(t *testing.T) { - SetupConfig() + r, e := CreateTempQr(000, 2592001) AssertEqual(t, e, nil, "Create Temp QR should be success") log.Println("qr ticket = " + r.Ticket) diff --git a/server_test.go b/server_test.go index d102045..f6d3ae9 100644 --- a/server_test.go +++ b/server_test.go @@ -12,7 +12,7 @@ import ( //when we setup wechate parameters,we chat will verify us func TestInitialSetup(t *testing.T) { - SetupConfig() + req := buildReqWechatAPISetup() @@ -27,7 +27,7 @@ func TestInitialSetup(t *testing.T) { } func TestWebRootHandler(t *testing.T) { - SetupConfig() + req := buildReqWechatWebRoot() rr, _ := getHTTPResponse(req, webrootHandler) @@ -46,7 +46,7 @@ echostr => [913461463450840893]` //we only check decrypted ToUserName should be the one we sent out. //as decrypt itself is already a good proof of its working state. func TestPostTxtMsg(t *testing.T) { - SetupConfig() + req := buildReqWechatPostTxtMsg() rr, _ := getHTTPResponse(req, apiV1Main) diff --git a/upload_test.go b/upload_test.go index 1e13aea..66de6ec 100644 --- a/upload_test.go +++ b/upload_test.go @@ -11,7 +11,7 @@ import ( ) func TestUploadImage(t *testing.T) { - SetupConfig() + src := "media_for_test/640x480.jpg" mediaID := uploadImage(src) fmt.Printf("get MediaID: %s \n", mediaID) @@ -31,7 +31,7 @@ func TestUploadImage(t *testing.T) { } func TestUploadVideo(t *testing.T) { - SetupConfig() + src := "media_for_test/video.mp4" mediaID := uploadVideo(src) fmt.Printf("get MediaID: %s \n", mediaID) @@ -56,14 +56,14 @@ func TestUploadVideo(t *testing.T) { } func TestUploadVoice(t *testing.T) { - SetupConfig() + src := "media_for_test/example.amr" mediaID := uploadVoice(src) log.Println(mediaID) } func TestUploadThumb(t *testing.T) { - SetupConfig() + src := "media_for_test/music-thumb.jpg" mediaID := uploadThumb(src) log.Println(mediaID)