| func TestCreatePermenentWechatQr(t *testing.T) { | func TestCreatePermenentWechatQr(t *testing.T) { | ||||
| scene := "edit_profile" //do not create rubbish, create something that we can use later on | scene := "edit_profile" //do not create rubbish, create something that we can use later on | ||||
| //permenant ticked for "0" | |||||
| expected := "gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA" | expected := "gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA" | ||||
| expected = "gQFR8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZnNRMVJmZ3VjU2gxMDAwME0wN28AAgTQaSVZAwQAAAAA" | |||||
| req := buildReqPermQr(scene) | req := buildReqPermQr(scene) | ||||
| rr, _ := getHTTPResponse(req, iapiCreateWechatQrCode) | rr, _ := getHTTPResponse(req, iapiCreateWechatQrCode) | ||||
| m := rr.Body.String() | m := rr.Body.String() | ||||
| AssertEqual(t, info.Ticket, expected, "expected ticket not match") | AssertEqual(t, info.Ticket, expected, "expected ticket not match") | ||||
| } | } | ||||
| func TestCreateTmpWechatQr(t *testing.T) { | |||||
| qrValue := int32(time.Now().Unix()) | |||||
| log.Println(qrValue) | |||||
| qrExpire := int32(1200) | |||||
| req := buildReqTmpQr(qrValue, qrExpire) | |||||
| rr, _ := getHTTPResponse(req, iapiCreateWechatQrCode) | |||||
| m := rr.Body.String() | |||||
| info := QRSrcInfo{} | |||||
| err := json.Unmarshal([]byte(m), &info) | |||||
| AssertEqual(t, err, nil, "decode json should be correct") | |||||
| log.Println(info) | |||||
| log.Printf("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s", info.Ticket) | |||||
| AssertEqual(t, info.Ticket != "", true, "expected ticket not match") | |||||
| //use wechat to check scan code is correct | |||||
| } | |||||
| func getHTTPResponse(req *http.Request, handler http.HandlerFunc) (rr *httptest.ResponseRecorder, err error) { | func getHTTPResponse(req *http.Request, handler http.HandlerFunc) (rr *httptest.ResponseRecorder, err error) { | ||||
| // Our handlers satisfy http.Handler, so we can call their ServeHTTP method | // Our handlers satisfy http.Handler, so we can call their ServeHTTP method | ||||
| } | } | ||||
| func buildReqTmpQr(val, expire int32) *http.Request { | |||||
| req, err := http.NewRequest("GET", "/iapi/createWechatQr", nil) | |||||
| if err != nil { | |||||
| log.Fatal(err) | |||||
| } | |||||
| q := req.URL.Query() | |||||
| q.Add("qrValue", fmt.Sprintf("%d", val)) | |||||
| q.Add("expire", fmt.Sprintf("%d", expire)) | |||||
| req.URL.RawQuery = q.Encode() | |||||
| buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete) | |||||
| buildReqCommonHeader(req) | |||||
| return req | |||||
| } | |||||
| func buildSignature(token string) (signature, timestamp, nonce string) { | func buildSignature(token string) (signature, timestamp, nonce string) { | ||||
| timestamp = fmt.Sprintf("%d", int32(time.Now().Unix())) | timestamp = fmt.Sprintf("%d", int32(time.Now().Unix())) | ||||
| nonce = "1461107899" //a randome string cut from previous wechat request | nonce = "1461107899" //a randome string cut from previous wechat request |