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) }
func TestGetUserByUid(t *testing.T) { initORM() user := models.User{Uid: 2971788563, Nickname: "Piasy", Gender: 1} o := orm.NewOrm() id, err := o.Insert(&user) assert.Equal(t, user.Id, id) assert.Nil(t, err) got, errNum := models.GetUserByUid(user.Uid) assert.Zero(t, errNum) assertUserEquals(t, &user, got) deleteUser(t, user.Id) }
// @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() }
func TestGetUserByUidNotExist(t *testing.T) { initORM() user, err := models.GetUserByUid(2971788563) assert.Nil(t, user) assert.Equal(t, utils.ERROR_CODE_USERS_USER_NOT_EXISTS, err) }