In my previous post, Creating an API with python: Part 9: Authentication Scopes, I added authentication scopes, which will allow a caller of the API to authenticate either with an admin or an account scope. In this post, I will add integration tests, which will replace the test.sh
script with python unit tests that test the API functionality.
Prerequisites
These prerequisites are assumed for this post:
- Creating an API with python: Part 1: GET Endpoints
- Creating an API with python: Part 2: MariaDB Database
- Creating an API with python: Part 3: POST Endpoints
- Creating an API with python: Part 4: DELETE Endpoints
- Creating an API with python: Part 5: Authentication
- Creating an API with python: Part 6: HTTPS and Proxying
- Creating an API with python: Part 7: CORS
- Creating an API with python: Part 8: Multiple Account Support
- Creating an API with python: Part 9: Authentication Scopes
Step 1: Update code
The code for the FastAPI API is now on GitHub, with the project name taglink-api
. You can checkout the latest copy of the code here: https://github.com/liz-allthecoding/taglink-api.
Step 2: Update SSL Certs
I had to make a tweak to the self-signed SSL certs in order that they would work with python’s requests
module. If you created your certs before Friday, 4th November 2022, please recreate them using the instructions in Creating an API with python: Part 6: HTTPS and Proxying.
Step 3: Create the integration-tests venv
Create the integration-tests venv for running the integration tests. The tests use pytest
under the hood, so you will need that package as well as requests
for making requests to the API, and pyyaml
for reading the test.yaml
config file.
- On your server, change to the code directory (
~/vboxshare/fastapi
should be replaced with the path to your FastAPI python code):$ cd ~/vboxshare/fastapi
-
Now create the venv and install the required packages:
$ python venv -m .venv-integration-tests $ . .venv-integration-tests/bin/activate (.venv-integration-tests) $ pip install requests (.venv-integration-tests) $ pip install pyyaml (.venv-integration-tests) $ pip install pytest
Step 4: Update config.yaml
Make sure that the config.yaml
file has your database password, secret key and CORS origin hosts as created in previous parts.
Step 5: Integration Test Configuration
Configure integration_tests/test.yaml
.
- Create a suitable integration test user password, and run (replacing <INTEGRATION_TEST_USER_PASSWORD> with your password):
(.venv-fastapi) $ export PASSWORD='<INTEGRATION_TEST_USER_PASSWORD>' (.venv-fastapi) $ python hash_password.py
Take a note of the hash password output.
-
Add the original INTEGRATION_TEST_USER_PASSWORD to
test.yaml
, replacing INTEGRATION_TEST_USER_PASSWORD with your password. -
Create a suitable password for the integration test account. Add the password to
test.yaml
, replacing INTEGRATION_TEST_ACCOUNT_PASSWORD with your password. -
Replace <YOUR_IP> in
test.yaml
with the IP of your virtual machine.
Step 6: Add integration_test_user to the Database
- Connect to your database, using the password set up in Creating an API with python: Part 2: MariaDB Database:
$ mysql -u root -p
-
Switch to the
apiservice
database:MariaDB [(none)]> use apiservice;
-
Add the integration_test_user, replacing ‘INTEGRATION_TEST_USER_HASHED_PASSWORD’ with the hashed password you generated in step 4:
MariaDB [(apiservice)]> INSERT INTO user SET user_id=UUID(), username='integration_test_user', hashed_password='INTEGRATION_TEST_USER_HASHED_PASSWORD';
Step 7: Start FastAPI
- On your server, change to the code directory (
~/vboxshare/fastapi
should be replaced with the path to your FastAPI python code):$ cd ~/vboxshare/fastapi
-
Run the FastAPI server:
$ . ~/.venv-fastapi/bin/activate (.venv-fastapi) $ uvicorn --host 0.0.0.0 main:app --root-path /api --reload
Step 8: Run the Integration Tests
Run the integration tests to verify that the API is working correctly.
- On your server, change to the code directory (
~/vboxshare/fastapi
should be replaced with the path to your FastAPI python code):$ cd ~/vboxshare/fastapi
-
Run the integration tests:
$ ./integration_test.sh
Conclusion
You should now have a FastAPI API running, with integration tests that can be used to verify any changes made to the code. The integration tests can also be updated if new features are added, to verify them.
If you want to find out about running the API as a service, see my follow-on post, Creating an API with python: Part 11: Running as a Service.
Thanks for reading!
Recent Comments