Beispiel #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)
}
Beispiel #2
0
// @Title CreateUserWithPhone
// @Description 通过手机号注册, 返回所有字段
// @Param	phone		query 	string	true		"用户手机号"
// @Param	code		query 	string	true		"手机验证码"
// @Param	secret		query 	string	true		"加密处理后的密码"
// @Param	Authorization		header 	string	true		"Basic auth的授权码, 计算方式见wiki"
// @Success 201 {object} models.User
// @Failure 401 basic auth失败
// @Failure 403 参数错误:缺失或格式错误
// @Failure 422 手机号已注册
// @Failure 500 系统错误
// @router / [post]
func (this *UsersController) Post() {
	phone := this.GetString("phone")
	code := this.GetString("code")
	secret := this.GetString("secret")
	authorization := this.Ctx.Request.Header.Get("Authorization")
	if authorization != BASIC_AUTH_AUTHORIZATION {
		this.Ctx.ResponseWriter.WriteHeader(401)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_BASIC_AUTH_FAIL, this.Ctx.Request.URL.String())
	} else if !utils.IsValidPhone(phone) || code == "" || len(secret) != 40 {
		this.Ctx.ResponseWriter.WriteHeader(403)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
	} else if exists := models.UserPhoneExists(&phone); exists {
		this.Ctx.ResponseWriter.WriteHeader(422)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_USERS_PHONE_REGISTERED, this.Ctx.Request.URL.String())
	} else if err := models.CheckVerifyCode(phone, code); err > 0 {
		this.Ctx.ResponseWriter.WriteHeader(422)
		this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
	} else if user, err := models.CreateUserByPhone(&phone, secret); err > 0 {
		this.Ctx.ResponseWriter.WriteHeader(500)
		this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
	} else {
		this.Ctx.ResponseWriter.WriteHeader(201)
		this.Data["json"] = user
	}

	this.ServeJSON()
}
Beispiel #3
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)
}