FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)

执行结果:


image.png


     我们来看下我们的测试用例,目前少了二个场景,密码错误超过10次但是时间小于30min,一个是时间大于30min,那么我们如何实现呢,最简单的就是for循环10次,其实这样做不可以,因为没有了,那么应该怎么做呢,我们可以直接这么来做。直接修改redis即可。


def test_log_error_big(self):
        red = redis.Redis(host='localhost', port=6379, db=0)
        red.hset("liwanle1i_password",'num',11)
        red.hset("liwanle1i_password", 'time', "2021-11-17 22:16:57")
        self.parame['password'] = '2222222222'
        reponse = requests.post(self.url, json=self.parame)
        status = reponse.status_code
        print(reponse.text)
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'],100204)
        self.assertEqual(reslut['message'], "输入密码错误次数过多,账号暂时锁定,请30min再来登录")
        red.hdel("liwanle1i_password",'num')
    def test_log_error_bigtime(self):
        red = redis.Redis(host='localhost', port=6379, db=0)
        red.hset("liwanle1i_password",'num','1')
        red.hset("liwanle1i_password", 'time', "2021-10-17 22:16:57")
        self.parame['password'] = '2222222222'
        reponse = requests.post(self.url, json=self.parame)
        status = reponse.status_code
        print(reponse.text)
        reslut = reponse.json()
        print(reslut)
        self.assertEqual(status, 200)
        self.assertEqual(reslut['message'], "密码错误")
         red.hdel("liwanle1i_password", 'time')
 def test_log_error_bigtime_success(self):
        red = redis.Redis(host='localhost', port=6379, db=0)
        red.hset("liwanle1i_password", 'num', '1')
        red.hset("liwanle1i_password", 'time', "2021-10-17 22:16:57")
        reponse = requests.post(self.url, json=self.parame)
        status = reponse.status_code
        print(reponse.text)
        reslut = reponse.json()
        print(reslut)
        self.assertEqual(status, 200)
        self.assertEqual(reslut['message'], "成功")
        red.hdel("liwanle1i_password", 'time')
        red.hdel("liwanle1i_password", 'num')


我们还在增加了一个当有错误密码,但是次数不大于10,时间大于30min的可以正常登陆成功,不过我门也发现了接口存在问题。


@usersRouter.post("/login")
async def login(request: Request, user: UserLogin, db: Session = Depends(get_db)):
    db_crest = get_user_username(db, user.username)
    if not db_crest:
        logger.info("login:" + user.username + "不存在")
        return reponse(code=100205, message='用户不存在', data="")
    verifypassowrd = verify_password(user.password, db_crest.password)
    if verifypassowrd:
        useris = await request.app.state.redis.get(user.username)
        if not useris:
            try:
                token = create_access_token(data={"sub": user.username})
            except Exception as e:
                logger.exception(e)
                return reponse(code=100203, message='产生token失败', data='')
            request.app.state.redis.set(user.username, token, expire=ACCESS_TOKEN_EXPIRE_MINUTES * 60)
            return reponse(code=200, message='成功', data={"token": token})
        return reponse(code=200, message='成功', data={"token": useris})
    else:
        result = await  request.app.state.redis.hgetall(user.username + "_password", encoding='utf8')
        if not result:
            times = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
            request.app.state.redis.hmset_dict(user.username + "_password", num=0, time=times)
            return reponse(code=100206, data='', message='密码错误')
        else:
          errornum = int(result['num'])
            numtime = (datetime.now() - datetime.strptime(result['time'], '%Y-%m-%d %H:%M:%S')).seconds / 60
            if errornum < 10 and numtime < 30:
                # 更新错误次数
                errornum += 1
                request.app.state.redis.hmset_dict(user.username + "_password", num=errornum)
                return reponse(code=100206, data='', message='密码错误')
            elif errornum < 10 and numtime > 30:
                # 次数置于1,时间设置现在时间
                errornum = 1
                times = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
                request.app.state.redis.hmset_dict(user.username + "_password", num=errornum, time=times)
                return reponse(code=100206, data='', message='密码错误')
            elif errornum > 10 and numtime < 30:
                # 次数设置成最大,返回
                errornum += 1
                request.app.state.redis.hmset_dict(user.username + "_password", num=errornum)
                return reponse(code=100204, message='输入密码错误次数过多,账号暂时锁定,请30min再来登录', data='')
            else:
                errornum = 1
                times = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
                request.app.state.redis.hmset_dict(user.username + "_password", num=errornum, time=times)
                return reponse(code=100206, data='', message='密码错误')


我们的接口测试就是在不断的去发现问题改造问题。


这里提供了一个demo,所有的代码都放在了


https://gitee.com/liwanlei/fastapistuday


  用例设计是门艺术,我们在不断的测试中调整去修改优化我们的代码。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
测试技术 数据安全/隐私保护
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
|
存储 测试技术 数据安全/隐私保护
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
|
NoSQL Redis 数据库
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
FastAPI(七十八)实战开发《在线课程学习系统》接口开发-- 评论
FastAPI(七十八)实战开发《在线课程学习系统》接口开发-- 评论
FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论
FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论
FastAPI(七十六)实战开发《在线课程学习系统》接口开发-- 课程详情
FastAPI(七十六)实战开发《在线课程学习系统》接口开发-- 课程详情
FastAPI(七十五)实战开发《在线课程学习系统》接口开发-- 创建课程
FastAPI(七十五)实战开发《在线课程学习系统》接口开发-- 创建课程