Conversation
9914ebf to
7d20cc1
Compare
simonpasquier
left a comment
There was a problem hiding this comment.
Thanks, just a few remarks! Could you add tests too please?
notify/wechat/wechat.go
Outdated
| // Refresh AccessToken over 2 hours | ||
| if n.accessToken == "" || time.Since(n.accessTokenAt) > 2*time.Hour { | ||
| // Refresh AccessToken over `expireIn` | ||
| if n.accessToken == "" || time.Since(n.accessTokenAt) > n.expireIn { |
There was a problem hiding this comment.
It may be safer to renew the token before it expires (for instance when it reached 90% of its validity).
notify/wechat/wechat.go
Outdated
| n.accessToken = wechatToken.AccessToken | ||
| n.accessTokenAt = time.Now() | ||
| n.accessTokenAt = startTime | ||
| n.expireIn = time.Duration(wechatToken.ExpiresIn) * time.Second |
There was a problem hiding this comment.
instead of storing the creation time of the token and its period, you could record its expiration time (minus a safety interval as described above).
Signed-off-by: BinacsLee <bin646891055@gmail.com>
Signed-off-by: BinacsLee <bin646891055@gmail.com>
Signed-off-by: BinacsLee <bin646891055@gmail.com>
|
Thank you very much for your advice! I have submitted the latest code. Could you please review it again? @simonpasquier And the unit test works like this: |
96bce15 to
43c7699
Compare
| type token struct { | ||
| AccessToken string `json:"access_token"` | ||
| accessToken string | ||
| expireAt time.Time |
There was a problem hiding this comment.
| expireAt time.Time | |
| tokenRenewAt time.Time |
| // the correct configuration: secret and WechatConfig. | ||
| // You can get a full description of the WechatConfig's fields on | ||
| // https://work.weixin.qq.com/api/doc/90001/90143/91199 | ||
| func TestNotify(t *testing.T) { |
There was a problem hiding this comment.
The unit test can't access the real WeChat endpoint. We need to mock the WeChat API to simulate the various responses. Since it isn't trivial, I can have a look at it. Also this code doesn't test anything.

Accoding to WeChat Work API, we will get response such as:
{ "errcode": 0, "errmsg": "ok", "access_token": "accesstoken000001", "expires_in": 7200 }instead of:
{ "code": 0, "error": "ok", "access_token": "accesstoken000001", }And sometimes the request fails because it uses an expired token
So I did these things to update the code:
weChatResponseandtokenResponse;expireIninstead of2 * time.Hour;For test, I wrote the following code:
And it works:


Thanks for your review.