Package: drf-tester
DRF TesterThis module aims to help developers with testingDjangoRestFrameworkAPI endpoints:Minimize the time (and lines of code) requiredMantain consistent testingIncrease productivity!The philosophy behind the design of this module, is that a developer should only need to:Prepare thesetUpmethod of a Test classChoose the correct classes to inheritb for each type of accessSmile!!This saves us developers lots of time writing repetitive, boiler-plate code, while reassuring that our tests are, at the very least, consistent.For more details,read the docsTable of ContentsRequirementsInstallationBaseDrfTestViewset TestsExampleContributionsRequirementsPython 3.7Django 2.2DjagnoRESTFramework 3.12.2factory-boy 3.1.0InstallationTo installdrf-testerin your systems, use pip:pipinstalldrf-testerBaseDrfTestLocated indrf_tester.utilsthis class is the glue that makes it all work.It contains thesetUpmethod to be run before every test, as well as some helper functions and Object variables.Check out the code for more details.Built-in FunctionsSome generic methods are created for DRYer single-action test classes.get_admin_user(self, data: dict) -> Userget_active_user(self, data: dict) -> Userget_active_staff(self, data: dict) -> Userget_model_instances(self) -> listObject VariablesSome Object-level variables are declared outside ofsetUpfor convenience.EXACT_AMOUNT: if is set to an integer value,get_model_instanceswill return a list of instances of exactly that size.MINandMAX: used as limits when usingrandom.randintto create a list of instances of random size. Default:5and10.setUp()The variables required for correct operation:self.endpoint=None# string with the url of the endpointself.factory=None# factory-boy class to create model instancesself.model=None# the model accessed through the endpointself.instance_data={}# dict of valid SERIALIZED data for instance creationself.view=viewsets.YourViewSet.as_view({"get":"list","post":"create","put":"update","delete":"destroy"})self.user_data={}# Required for authenticated user testingself.admin_data={}# Required for super user testingself.staff_data={}# Required for staff user testingself.USER_FIELD_NAME='creator'# Required for testing user object accessAccess LevelOnce you know what level of access each kind of user should have, just add those classes to your tests, afterAPITestCase.Example:fromdrf_tester.viewsetsimportanon,admin,authclassYourViewSetTest(APITestCase,anon.AnonNoAccess,auth.AuthFullAccess,admin.AdminFullAccess):"""To test a ModelViewSet with IsAuthenticated as the only Permission"""defsetUp(self):...Viewset TestsAll the classes to assist with the development of tests forViewSetsare located underdrf_tester.viewsets, and separated by user type:anon.py: Anonymous usersauth.py: Authenticated usersadmin.py: Admin user (superusers)staff.py: Staff usersWithin each file, there are classes that test the effects of different actions on the endpoint. For example:# drf_tester/viewsets/anon.pyclassNoCreate(BaseDrfTest):deftest_anon_user_cannot_create_instance(self):"""Anonymous user cannot create new instance"""...These single-action classes are grouped in bigger classes meant to be inherited by the final test cases. For example:classAnonReadOnly(CanList,CanRetrieve,NoCreate,NoUpdate,NoDestroy):"""Anonymous user has only read access to endopint"""passA few different combinations are provided for your convenience:For anonymous users:AnonNoAccessAnonReadOnlyAnonFullAccessFor authenticated users:AuthFullAccessAuthNoAccessAuthReadOnlyAuthOwner: only controls instances linked to userFor admin users:AdminNoAccessAdminReadOnlyAdminFullAccessFor staff users:StaffNoAccessStaffReadOnlyStaffFullAccessCustom groups can be made mixing and matching classes according with the level of access expected by each user-type from each endpoint.ExampleIncluded in the repository, there's an example illustrating how to implement in your project.Fromexample_one:classThingViewSetTest(APITestCase,AnonNoAccess,AuthFullAccess,AdminFullAccess):"""Thing viewset testsPermission level: IsAuthenticated"""defsetUp(self):"""Tests setup"""self.endpoint="/api/v1/things/"self.factory=factories.ThingFactoryself.model=models.Thingself.view=views.ThingViewSet.as_view({"get":"list","post":"create","put":"update","delete":"destroy"})self.instance_data={...}self.user_data={...}self.admin_data={...}ContributionsI would love for the project to cover as many use-cases as possible, and for that I need your help.There will always be some customized endpoint functionality that will never be covered by this module, but I'd like to make it as extensive as reasonably possible.If you know of any situations for which the current solution won't work, please let me know and I'll try to make allowances for it.Check outCONTRIBUTING.mdfor more details.