Esempio n. 1
0
func TestUpdateUser(t *testing.T) {
	phone := "18801234567"
	secret := "8428d916f8cca9ba5971bf58b34d38da20bc3dff"
	phone2 := "18801234568"

	// insert two
	user, err := models.CreateUserByPhone(&phone, secret)
	assert.Zero(t, err)
	user2, err := models.CreateUserByPhone(&phone2, secret)
	assert.Zero(t, err)

	weixin := "wx:piasy_umumu"
	user.WeiXin = &weixin
	user.Nickname = "Piasy"
	user.Gender = 1
	err = models.UpdateUser(user)
	assert.Zero(t, err)

	got, err := models.GetUserByToken(user.Token)
	assert.Zero(t, err)
	assertUserEquals(t, user, got)

	user.Phone = &phone2
	err = models.UpdateUser(user)
	assert.Equal(t, utils.ERROR_CODE_SYSTEM_ERROR, err)

	deleteUser(t, user.Id)
	deleteUser(t, user2.Id)
}
Esempio n. 2
0
func TestCreateUserByPhone(t *testing.T) {
	initORM()

	phone := "18801234567"
	secret := "8428d916f8cca9ba5971bf58b34d38da20bc3dff"
	password := "******"

	// insert one
	user, err := models.CreateUserByPhone(&phone, secret)
	assert.NotNil(t, user)
	assert.Zero(t, err)
	assert.Empty(t, user.Nickname)
	assert.True(t, user.Uid >= utils.USER_MIN_UID)
	assert.Equal(t, password, user.Password)
	assert.True(t, len(user.Token) == 40)
	assert.True(t, utils.AreStringEquals(user.Phone, &phone))
	now := utils.GetTimeMillis()
	assert.True(t, now-1000 < user.CreateAt)
	assert.True(t, user.CreateAt < now+1000)

	// get it by phone
	getByPhone, err := models.GetUserByUid(user.Uid)
	assert.Zero(t, err)
	assertUserEquals(t, user, getByPhone)

	// get it by uid
	getByUid, err := models.GetUserByUid(user.Uid)
	assert.Zero(t, err)
	assertUserEquals(t, user, getByUid)

	// get it by token
	getByToken, err := models.GetUserByToken(user.Token)
	assert.Zero(t, err)
	assertUserEquals(t, user, getByToken)

	// clean up
	deleteUser(t, user.Id)

	// no such user after delete
	user, err = models.GetUserByUid(user.Uid)
	assert.Nil(t, user)
	assert.Equal(t, utils.ERROR_CODE_USERS_USER_NOT_EXISTS, err)
}
Esempio n. 3
0
// @Title GetUserByUid
// @Description [允许游客]通过uid获取用户信息; <br>请求自己的信息返回所有字段; <br>请求他人信息只有uid, nickname, avatar, gender字段; <br>其中gender 0表示未知, 1表示男, 2表示女
// @Param	uid		path 	int64	true		"目标用户uid"
// @Param	token		query 	string	true		"自己的token"
// @Success 200 {object} models.User
// @Failure 401 token无效
// @Failure 403 参数错误:非法uid
// @Failure 404 目标用户不存在
// @router /:uid [get]
func (this *UsersController) Get() {
	uid, err := this.GetInt64(":uid")
	token := this.GetString("token")
	if token == "" {
		this.Ctx.ResponseWriter.WriteHeader(401)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_TOKENS_INVALID_TOKEN, this.Ctx.Request.URL.String())
	} else if err != nil || uid < utils.USER_MIN_UID {
		this.Ctx.ResponseWriter.WriteHeader(403)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
	} else if token == VISITOR_TOKEN {
		if user, errNum := models.GetUserByUid(uid); errNum > 0 {
			this.Ctx.ResponseWriter.WriteHeader(404)
			this.Data["json"] = utils.Issue(errNum, this.Ctx.Request.URL.String())
		} else {
			this.Ctx.ResponseWriter.WriteHeader(200)
			ret := models.User{Uid: user.Uid, Nickname: user.Nickname, Avatar: user.Avatar,
				Gender: user.Gender}
			this.Data["json"] = &ret
		}
	} else if self, errNum := models.GetUserByToken(token); errNum > 0 {
		this.Ctx.ResponseWriter.WriteHeader(401)
		this.Data["json"] = utils.Issue(errNum, this.Ctx.Request.URL.String())
	} else if self.Uid == uid {
		this.Ctx.ResponseWriter.WriteHeader(200)
		this.Data["json"] = self
	} else if user, errNum := models.GetUserByUid(uid); errNum > 0 {
		this.Ctx.ResponseWriter.WriteHeader(404)
		this.Data["json"] = utils.Issue(errNum, this.Ctx.Request.URL.String())
	} else {
		this.Ctx.ResponseWriter.WriteHeader(200)
		ret := models.User{Uid: user.Uid, Nickname: user.Nickname, Avatar: user.Avatar,
			Gender: user.Gender}
		this.Data["json"] = &ret
	}
	this.ServeJSON()
}
Esempio n. 4
0
// @Title UpdateUserInfo
// @Description 修改用户自己的信息, 修改哪些字段就传哪些字段, 成功后返回所有字段, <br/>注意: N位中英数限制, 指只能有汉字/英文字母/阿拉伯数字, 不能有标点符号, 特殊符号, 每个汉字/字母/数字长度都算1
// @Param	token		query 	string	true		"Token"
// @Param	phone		query 	string	false		"更换绑定手机号"
// @Param	code		query 	string	false		"手机验证码, 换绑手机号时需要"
// @Param	wx_openid		query 	string	false		"微信授权的openid"
// @Param	wx_token		query 	string	false		"微信授权的token"
// @Param	qq_openid		query 	string	false		"QQ授权的openid"
// @Param	qq_token		query 	string	false		"QQ授权的token"
// @Param	wb_token		query 	string	false		"微博授权的token"
// @Param	nickname		query 	string	false		"昵称, 小于12位中英数"
// @Param	gender		query 	int	false		"性别, 1为男, 2为女"
// @Param	avatar		query 	string	false		"头像url, 通过上传头像接口上传成功后获得"
// @Success 201 {object} models.User
// @Failure 401 token无效
// @Failure 403 参数错误:缺失或格式错误
// @Failure 500 系统错误
// @router / [patch]
func (this *UsersController) Patch() {
	token := this.GetString("token")
	phone := this.GetString("phone")
	code := this.GetString("code")
	wx_openid := this.GetString("wx_openid")
	wx_token := this.GetString("wx_token")
	qq_openid := this.GetString("qq_openid")
	qq_token := this.GetString("qq_token")
	wb_token := this.GetString("wb_token")
	nickname := this.GetString("nickname")
	gender, errGender := this.GetInt("gender", 0)
	avatar := this.GetString("avatar")
	if (phone != "" && !utils.IsValidPhone(phone)) || errGender != nil {
		// has phone, but invalid; parse gender/birthday error
		this.Ctx.ResponseWriter.WriteHeader(403)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
	} else if user, err := models.GetUserByToken(token); err > 0 {
		// invalid token
		this.Ctx.ResponseWriter.WriteHeader(401)
		this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
	} else {
		for {
			// has valid phone
			if phone != "" {
				if code == "" {
					// has valid phone but not valid code
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_VERIFY_CODE_MISMATCH, this.Ctx.Request.URL.String())
					break

				}
				if err = models.CheckVerifyCode(phone, code); err > 0 {
					// code mismatch
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// valid
				user.Phone = &phone
			}
			// has wx_openid
			if wx_openid != "" {
				if wx_token == "" {
					// empty wx_token
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
					break
				}
				authUser, err := utils.AuthWithWeiXin(wx_openid, wx_token)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.WeiXin = &authUser.Openid
				user.WeiXinNickName = authUser.Nickname
			}
			// has wb_token
			if wb_token != "" {
				authUser, err := utils.AuthWithWeiBo(wb_token)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.WeiBo = &authUser.Openid
				user.WeiBoNickName = authUser.Nickname
			}
			// has qq_openid
			if qq_openid != "" {
				if qq_token == "" {
					// empty qq_token
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
					break
				}
				authUser, err := utils.AuthWithQQ(qq_openid, qq_token, QQ_OAUTH_CONSUMER_KEY)
				if err > 0 {
					// auth fail
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
					break
				}
				// verified
				user.QQ = &authUser.Openid
				user.QQNickName = authUser.Nickname
			}
			if nickname != "" {
				if !utils.IsLegalRestrictedStringWithLength(nickname, utils.USER_NICKNAME_MEX_LEN) {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_NICKNAME, this.Ctx.Request.URL.String())
					break
				}
				user.Nickname = nickname
			}
			if gender > 0 {
				if gender != 1 && gender != 2 {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_GENDER_VALUE, this.Ctx.Request.URL.String())
					break
				}
				user.Gender = gender
			}
			if avatar != "" {
				if len(avatar) > utils.USER_AVATAR_MEX_LEN {
					this.Ctx.ResponseWriter.WriteHeader(403)
					this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_INVALID_AVATAR, this.Ctx.Request.URL.String())
					break
				}
				user.Avatar = avatar
			}

			err = models.UpdateUser(user)
			if err > 0 {
				this.Ctx.ResponseWriter.WriteHeader(403)
				this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
				break
			}

			// success
			this.Ctx.ResponseWriter.WriteHeader(201)
			this.Data["json"] = user
			break
		}
	}
	this.ServeJSON()
}
Esempio n. 5
0
func TestGetUserByToken(t *testing.T) {
	initORM()
	user, err := models.GetUserByToken("lgJYnQXrKVPoInPTPnokdPOZISzosxQzNUceRJyA")
	assert.Nil(t, user)
	assert.Equal(t, utils.ERROR_CODE_TOKENS_INVALID_TOKEN, err)
}