func TestCreateVerification(t *testing.T) { initORM() o := orm.NewOrm() phone := "18801234567" // not exist verification := models.Verification{Phone: phone} err := o.Read(&verification, "Phone") assert.NotNil(t, err) assert.Equal(t, phone, verification.Phone) assert.Empty(t, verification.Id) assert.Empty(t, verification.Code) assert.Empty(t, verification.Expire) // create one errNum := models.CreateVerification(phone) assert.Equal(t, 0, errNum) verification = models.Verification{Phone: phone} err = o.Read(&verification, "Phone") assert.Nil(t, err) assert.Equal(t, phone, verification.Phone) assert.True(t, verification.Id > 0) assert.True(t, len(verification.Code) == 6) now := time.Now().Unix() assert.True(t, now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS-5 < verification.Expire) assert.True(t, verification.Expire < now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS+5) // create it again errNum = models.CreateVerification(phone) assert.Equal(t, 0, errNum) another := models.Verification{Phone: phone} err = o.Read(&another, "Phone") assert.Nil(t, err) assert.Equal(t, verification.Id, another.Id) assert.Equal(t, phone, another.Phone) assert.True(t, another.Id > 0) assert.True(t, len(another.Code) == 6) now = time.Now().Unix() assert.True(t, now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS-5 < another.Expire) assert.True(t, another.Expire < now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS+5) // clean up deleteVerification(t, another.Id) // not exist after delete verification = models.Verification{Phone: phone} err = o.Read(&verification, "Phone") assert.NotNil(t, err) }
func TestCheckVerifyCode(t *testing.T) { initORM() o := orm.NewOrm() phone := "18801234567" // not exist verification := models.Verification{Phone: phone} err := o.Read(&verification, "Phone") assert.NotNil(t, err) assert.Equal(t, phone, verification.Phone) assert.Empty(t, verification.Id) assert.Empty(t, verification.Code) assert.Empty(t, verification.Expire) // create one errNum := models.CreateVerification(phone) assert.Equal(t, 0, errNum) verification = models.Verification{Phone: phone} err = o.Read(&verification, "Phone") assert.Nil(t, err) assert.Equal(t, phone, verification.Phone) assert.True(t, verification.Id > 0) assert.True(t, len(verification.Code) == 6) now := time.Now().Unix() assert.True(t, now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS-5 < verification.Expire) assert.True(t, verification.Expire < now+utils.VERIFY_CODE_EXPIRE_IN_SECONDS+5) // check wrong code errNum = models.CheckVerifyCode(phone, verification.Code+"1") assert.Equal(t, utils.ERROR_CODE_VERIFY_CODE_MISMATCH, errNum) // check it errNum = models.CheckVerifyCode(phone, verification.Code) assert.Equal(t, 0, errNum) // check again will fail errNum = models.CheckVerifyCode(phone, verification.Code) assert.Equal(t, utils.ERROR_CODE_VERIFY_CODE_MISMATCH, errNum) // check not exist errNum = models.CheckVerifyCode("18812345678", verification.Code) assert.Equal(t, utils.ERROR_CODE_VERIFY_CODE_MISMATCH, errNum) // simulate expire verification.Expire = time.Now().Unix() - 100 _, err = o.Update(&verification) assert.Nil(t, err) // check should fail errNum = models.CheckVerifyCode(phone, verification.Code) assert.Equal(t, utils.ERROR_CODE_VERIFY_CODE_MISMATCH, errNum) // clean up deleteVerification(t, verification.Id) // not exist after delete verification = models.Verification{Phone: phone} err = o.Read(&verification, "Phone") assert.NotNil(t, err) }
// @Title CreateVerification // @Description 请求短信验证码 // @Param phone query string true "手机号" // @Param Authorization header string true "Basic auth的授权码, 计算方式见wiki" // @Success 201 "" "" // @Failure 401 basic auth失败 // @Failure 403 参数错误:缺失或格式错误 // @Failure 422 手机号已注册 // @Failure 500 系统错误 // @router / [post] func (this *VerificationsController) Post() { phone := this.GetString("phone") 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) { this.Ctx.ResponseWriter.WriteHeader(403) this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String()) } else if models.UserPhoneExists(&phone) { 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.CreateVerification(phone); 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.ServeJSON() }