package main

import (
	"encoding/json"
	"testing"
)

func TestDecodeSubscribeJson(t *testing.T) {
	msg := `
{
   "subscribe": 1,
   "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
   "nickname": "Band",
   "sex": 1,
   "language": "zh_CN",
   "city": "广州",
   "province": "广东",
   "country": "中国",

   "headimgurl":  "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
  "subscribe_time": 1382694957,
  "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL",
  "remark": "rr",

  "groupid": 0,

  "tagid_list":[128,2]
}`
	s := WechatUserInfo{}
	err := json.Unmarshal([]byte(msg), &s)

	AssertEqual(t, err, nil, "decode json should have no error")
	AssertEqual(t, s.Avatar, "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", "Avatar not right")
	AssertEqual(t, s.Subscribe, int32(1), "subscribe should =1")
	AssertEqual(t, s.OpenID, "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", "OpenID mismatch")
	AssertEqual(t, s.NickName, "Band", "NickName wrong")
	AssertEqual(t, s.Sex, int32(1), "sex should = 1 ")
	AssertEqual(t, s.Language, "zh_CN", "language should = zh_CN")
	AssertEqual(t, s.City, "广州", "City = 广州")
	AssertEqual(t, s.Province, "广东", "Province = 广东")
	AssertEqual(t, s.Country, "中国", "country = 中国")
	AssertEqual(t, s.SubscribeTime, int32(1382694957), "time = 1382694957")
	AssertEqual(t, s.UnionID, "o6_bmasdasdsad6_2sgVt7hMZOPfL", "Union ID wrong")
	AssertEqual(t, s.Remark, "rr", "Remark wrong")
	AssertEqual(t, s.GroupID, int32(0), "GroupID wrong")
	AssertEqual(t, s.TagIDList[0], int32(128), "TagIDList wrong")
	AssertEqual(t, s.TagIDList[1], int32(2), "TagIDList wrong")

}

func TestRegisterNewUser(t *testing.T) {
	SetupConfig()
	msg := `
{
   "subscribe": 1,
   "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
   "nickname": "Band",
   "sex": 1,
   "language": "zh_CN",
   "city": "广州",
   "province": "广东",
   "country": "中国",

   "headimgurl":  "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
  "subscribe_time": 1382694957,
  "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL",
  "remark": "rr",

  "groupid": 0,

  "tagid_list":[128,2]
}`
	in := InWechatMsg{}
	in.header.FromUserName = "abc"
	s := WechatUserInfo{}
	json.Unmarshal([]byte(msg), &s)
	s.registerNewUserWithInfo(in)
}
