执行结果:
我们来看下我们的测试用例,目前少了二个场景,密码错误超过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
用例设计是门艺术,我们在不断的测试中调整去修改优化我们的代码。