Issue: https://github.com/opencv/cvat/issues/1217 Currently there are a few proposals for SSO authentication to bypass the current user/password login on the UI. By using Django social accounts it is also possible to use SSO on the API, retrieving the security token by passing the code from the OAuth2 workflow. This is an example using Amazon Cognito, but any other social account could also be added. ### Motivation and context Currently CVAT has no functionality to log in with SSO. Other current proposals bypass the current Django framework to add SSO in the UI only, but still use username and password for the API. Using Django social accounts integrates SSO with the API as well, allowing it to be used as an alternative to the username and password, but can also be used together with other SSO frameworks that are UI only. ### How has this been tested? Unit tests for SSO manager in cvat-core and integration test with cvat-sdk for /auth/cognito endpoint. ### Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. If an item isn't applicable by a reason then ~~explicitly strikethrough~~ the whole line. If you don't do that github will show an incorrect process for the pull request. If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I submit my changes into the `develop` branch - [ ] I have added a description of my changes into [CHANGELOG](https://github.com/cvat-ai/cvat/blob/develop/CHANGELOG.md) file - [x] I have updated the [documentation]( https://github.com/cvat-ai/cvat/blob/develop/README.md#documentation) accordingly - [x] I have added tests to cover my changes - [x] I have linked related issues ([read github docs]( https://help.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword)) - [ ] I have increased versions of npm packages if it is necessary ([cvat-canvas](https://github.com/cvat-ai/cvat/tree/develop/cvat-canvas#versioning), [cvat-core](https://github.com/cvat-ai/cvat/tree/develop/cvat-core#versioning), [cvat-data](https://github.com/cvat-ai/cvat/tree/develop/cvat-data#versioning) and [cvat-ui](https://github.com/cvat-ai/cvat/tree/develop/cvat-ui#versioning)) ### License - [x] I submit _my code changes_ under the same [MIT License]( https://github.com/cvat-ai/cvat/blob/develop/LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern. Co-authored-by: Melanie Day <mday@EYEDIAGNOSIS.local> Co-authored-by: Maria Khrustaleva <maria@cvat.ai> Co-authored-by: Nikita Manovich <nikita@cvat.ai> |
3 years ago | |
|---|---|---|
| .. | ||
| cli | 3 years ago | |
| mock_oauth2 | 3 years ago | |
| rest_api | 3 years ago | |
| sdk | 3 years ago | |
| shared | 3 years ago | |
| social_auth | 3 years ago | |
| webhook_receiver | 3 years ago | |
| README.md | 3 years ago | |
| conftest.py | 4 years ago | |
| pyproject.toml | 4 years ago | |
| pytest.ini | 3 years ago | |
| requirements.txt | 3 years ago | |
README.md
Testing infrastructure for REST API v2.0
Motivation
It was very annoying to support the testing infrastructure with FakeRedis, unittest framework, hardcoded data in the code. DRF testing approach works well only for a single server. But if you have a number of microservices, it becomes really hard to implement reliable tests. For example, CVAT consists of server itself, OPA, Redis, DB, Nuclio services. Also it is worth to have a real instance with real data inside and tests the server calling REST API directly (as it done by users).
How to run?
-
After that please look at documentation for pytest. Generally, you have to install requirements and run the following command from the root directory of the cloned CVAT repository:
pip install -e cvat-sdk/ pip install -e cvat-cli/ pip install -r tests/python/requirements.txt pytest tests/python/See the contributing guide to get more information about tests running.
How to upgrade testing assets?
When you have a new use case which cannot be expressed using objects already available in the system like comments, users, issues, please use the following procedure to add them:
- Run a clean CVAT instance
- Restore DB and data volume using commands below or running tests
- Add new objects (e.g. issues, comments, tasks, projects)
- Backup DB and data volume using commands below
- Don't forget to dump new objects into corresponding json files inside assets directory
- Commit cvat_data.tar.bz2 and data.json into git. Be sure that they are small enough: ~300K together.
It is recommended to use dummy and tiny images. You can generate them using Pillow library. See a sample code below:
from PIL import Image
from PIL.ImageColor import colormap, getrgb
from random import randint
for i, color in enumerate(colormap):
size = (randint(100, 1000), randint(100, 1000))
img = Image.new('RGB', size, getrgb(color))
img.save(f'{i}.png')
How to backup DB and data volume?
To backup DB and data volume, please use commands below.
docker exec test_cvat_server_1 python manage.py dumpdata --indent 2 --natural-foreign --exclude=auth.permission --exclude=contenttypes > shared/assets/cvat_db/data.json
docker exec test_cvat_server_1 tar -cjv /home/django/data > shared/assets/cvat_db/cvat_data.tar.bz2
Note: if you won't be use --indent options or will be use with other value it potentially will lead to problems with merging of this file with other branch.
How to update *.json files in the assets directory?
If you have updated the test database and want to update the assets/*.json files as well, run the appropriate script:
python shared/utils/dump_objects.py
How to restore DB and data volume?
To restore DB and data volume, please use commands below.
cat shared/assets/cvat_db/data.json | docker exec -i test_cvat_server_1 python manage.py loaddata --format=json -
cat shared/assets/cvat_db/cvat_data.tar.bz2 | docker exec -i test_cvat_server_1 tar --strip 3 -C /home/django/data/ -xj
Assets directory structure
Assets directory has two parts:
cvat_dbdirectory --- this directory contains all necessary files for successful restoring of test dbcvat_data.tar.bz2--- archieve with data volumes;data.json--- file required for DB restoring. Contains all information about test db;restore.sql--- SQL script for creating copy of database and killing connection forcvatdatabase. Script should be run with varialbe declaration:
# create database <new> with template <existing> psql -U root -d postgres -v from=<existing> -v to=<new> restore.sql*.jsonfiles --- these file contains all necessary data for getting expected results from HTTP responses
FAQ
-
How to merge two DB dumps?
In common case it should be easy just to merge two JSON files. But in the case when a simple merge fails, you have to first merge the branches, then re-create the changes that you made.
-
How to upgrade cvat_data.tar.bz2 and data.json?
After every commit which changes the layout of DB and data directory it is possible to break these files. But failed tests should be a clear indicator of that.
-
Should we use only json files to re-create all objects in the testing system?
Construction of some objects can be complex and takes time (backup and restore should be much faster). Construction of objects in UI is more intuitive.
-
How we solve the problem of dependent tests?
Since some tests change the database, these tests may be dependent on each other, so in current implementation we avoid such problem by restoring the database after each test function (see
conftest.py) -
Which user should be selected to create new resources in test DB?
If for your test it's no matter what user should send a request, then better to choose
admin1user for creating new resource.
Troubleshooting
-
If your test session was exit with message:
_pytest.outcomes.Exit: Command failed: ... Add `-s` option to see more details.Rerun tests to see error messages:
pytest ./tests/python/rest_api -s -
If your tests was failed due to date field incompatibility and you have error message like this:
assert {'values_chan...34.908528Z'}}} == {} E Left contains 1 more item: E {'values_changed': {"root['results'][0]['updated_date']": {'new_value': '2022-03-05T08:52:34.908000Z', E 'old_value': '2022-03-05T08:52:34.908528Z'}}} E Use -v to get the full diffJust dump JSON assets with:
python3 tests/python/shared/utils/dump_objects.py -
If your test infrastructure has been corrupted and you have errors during db restoring. You should to create (or recreate)
cvatdatabase:docker exec test_cvat_db_1 dropdb --if-exists cvat docker exec test_cvat_db_1 createdb cvat docker exec test_cvat_server_1 python manage.py migrate -
Perform migrate when some relation does not exists. Example of error message:
django.db.utils.ProgrammingError: Problem installing fixture '/data.json': Could not load admin.LogEntry(pk=1): relation "django_admin_log" does not exist`Solution:
docker exec test_cvat_server_1 python manage.py migrate -
If for some reason you need to recreate cvat database, but using
dropdbyou have error message:ERROR: database "cvat" is being accessed by other users DETAIL: There are 1 other session(s) using the database.In this case you should terminate all existent connections for cvat database, you can perform it with command:
docker exec test_cvat_db_1 psql -U root -d postgres -v from=cvat_server -v to=test_db -f restore.sql