FastAPI actual development of online course learning system -- password modification interface test

preface

We shared it last time FastAPI (84) actual development of online course learning system -- personal information interface test , this time let's look at changing the password.

text

According to the previous requirements analysis, interface design and interface development, we need to test the password modification interface.

1. No login user

2. The user's original password is incorrect

3. The length of user password does not meet the requirements

4. The password has been modified successfully

5. Original password verification failed

6. Old and new passwords cannot be the same

Then we first write the test cases of the interface according to the above use case points.

We still rely on the previously encapsulated login.

import unittest
from test.userlogin import *
class TestpasswordCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.url = 'http://127.0.0.1:8000/user/changepassword'
    @classmethod
    def tearDownClass(cls) -> None:
        cls.url=""
    def test_userNOtlogin(self):
        reponse = requests.post(self.url)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 421)
    def test_student_error(self):
        student_parame['newpassword']='1234567'
        toekn=get_students_token()
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url,json=student_parame,headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100303)
    def test_student_user(self):
        toekn = get_students_token()
        headers = {
            "token": toekn
        }
        student_parame['newpassword']='123456'

        reponse = requests.post(self.url,json=student_parame,headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100304)
        self.assertEqual(reslut['message'], "Old and new passwords cannot be the same")
    def test_student_new(self):
        student_parame['newpassword'] = '1234567'
        toekn = get_students_token()
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url, json=student_parame, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        print(reslut)
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100303)
        self.assertEqual(reslut['message'], "New password length mismatch")
    def test_student_newS(self):
        student_parame['newpassword'] = '0123456789'
        toekn = get_students_token()
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url, json=student_parame, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 200)
        self.assertEqual(reslut['message'], "success")
    def test_student_not(self):
        toekn = get_students_token()
        headers = {
            "token": toekn
        }
        student_parame['newpassword'] = '01234567819'
        student_parame['password'] = '01123456789'

        reponse = requests.post(self.url, json=student_parame, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100301)
        self.assertEqual(reslut['message'], "Original password verification failed")

if __name__ == '__main__':
    unittest.main()

After the use case, we found two problems. One is the request method. The written get should be post. There was also a problem updating the password. The changed code is as follows:

@usersRouter.post(path='/changepassword')
async def changepassword(request: Request, userchangepasword: UserChangepassword,
                         user: UsernameRole = Depends(get_cure_user),
                         db: Session = Depends(get_db)):
    if userchangepasword.password == userchangepasword.newpassword:
        return reponse(code=100304, message='Old and new passwords cannot be the same', data='')
    if len(userchangepasword.newpassword) < 8 or len(userchangepasword.newpassword) > 16:
        return reponse(code=100303, message='New password length mismatch', data='')
    username = user.username
    user_name = get_user_username(db, username)
    verify = verify_password(userchangepasword.password, user_name.password)
    if verify:
        hashpassword = get_password_hash(userchangepasword.newpassword)

        user_name.password=hashpassword
        try:
            db.commit()
            db.refresh(user_name)
        except Exception as e:
            logger.exception(e)
            return reponse(code=100302, message='Failed to save password', data='')
        request.app.state.redis.delete(user.username)
        request.app.state.redis.delete(user.username + "_password")
        return reponse(code=200, message="success", data=user.username)
    return reponse(code=100301, message='Original password verification failed', data='')

However, when it is executed again, it is found that the previous method cannot be used. Because the user name is fixed when designing the test cases before, we need to transform the test cases again.

First, transform the landing area

import  requests
login_url = 'http://127.0.0.1:8000/user/login/'
student_parame={
        "username": "liwanle1i",
        "password": "0123456789"
    }
teacher_parame= {
        "username": "liwanlei",
        "password": "123456"
    }
def get_students_token(parame) ->str:
    reponse = requests.post(login_url, json=parame)
    reslut = reponse.json()
    print(parame)
    print(reslut)
    token = reslut['data']['token']
    return  token
def get_teacher_tone(parame)->str:

    reponse = requests.post(login_url, json=parame)

    reslut = reponse.json()
    token = reslut['data']['token']
    return token

Then, we transform the test cases. After the transformation, the test cases that meet the requirements are tested again with the actual test changes. After the test, the test password is restored.

import unittest
from copy import deepcopy
from test.userlogin import *
class TestpasswordCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.url = 'http://127.0.0.1:8000/user/changepassword'
        cls.student_parame = {
            "username": "liwanle1i",
            "password": "012345678910",
            "newpassword": "012345678910"
        }
    @classmethod
    def tearDownClass(cls) -> None:
        cls.url=""
    def setUp(self) -> None:
        pass
    def tearDown(self) -> None:
        toekn = get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        self.student_parame["newpassword"] = '012345678910'
        requests.post(self.url, json=self.student_parame, headers=headers)
        self.student_parame["password"] = '012345678910'


    def test_userNOtlogin(self):
        reponse = requests.post(self.url)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 421)
    def test_student_error(self):
        self.student_parame['newpassword']='1234567'
        toekn=get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url,json=self.student_parame,headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100303)
    def test_student_user(self):
        toekn = get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url,json=self.student_parame,headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100304)
        self.assertEqual(reslut['message'], "Old and new passwords cannot be the same")
    def test_student_new(self):
        self.student_parame['newpassword'] = '1234567'
        toekn = get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url, json=self.student_parame, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()

        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100303)
        self.assertEqual(reslut['message'], "New password length mismatch")
    def test_student_newS(self):
        self.student_parame['newpassword'] = '0123456789101'
        toekn = get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        reponse = requests.post(self.url, json=self.student_parame, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.student_parame['password'] = '0123456789101'
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 200)
        self.assertEqual(reslut['message'], "success")

    def test_student_not(self):
        toekn = get_students_token(self.student_parame)
        headers = {
            "token": toekn
        }
        self.name=deepcopy(self.student_parame)
        self.name['password']='1212132213'
        self.name['newpassword']='111111111111'
        reponse = requests.post(self.url, json=self.name, headers=headers)
        status = reponse.status_code
        reslut = reponse.json()
        self.assertEqual(status, 200)
        self.assertEqual(reslut['code'], 100301)
        self.assertEqual(reslut['message'], "Original password verification failed")

if __name__ == '__main__':
    unittest.main()

After the transformation, we can modify the data every time we test. Here, changing the password has nothing to do with the user identity, and there is no need to verify the user identity differently.

Postscript

Find and solve problems. If you encounter problems, you can solve them slowly.

Welcome to Leizi's test development. We will continue to share more technical knowledge for you in the future

If you have any questions, you can leave a message or add me to wechat: 952943386.

2021, a cow turns to Qian Kun and a cow forces.

If you think this article is good, let's have a [share, like and watch] three links to let more people see it~

Posted by kfresh on Mon, 22 Nov 2021 23:05:20 -0800