|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import django_dynamic_fixture as fixture |
| 6 | +from django.contrib.auth.models import User |
| 7 | +from django.core.cache import cache |
| 8 | +from django.test import TestCase |
| 9 | +from django.utils.timezone import make_aware |
| 10 | +from rest_framework.authtoken.models import Token |
| 11 | +from rest_framework.test import APIClient |
| 12 | + |
| 13 | +from readthedocs.builds.models import Build, Version |
| 14 | +from readthedocs.projects.models import Project |
| 15 | +from readthedocs.redirects.models import Redirect |
| 16 | + |
| 17 | + |
| 18 | +class APIEndpointMixin(TestCase): |
| 19 | + |
| 20 | + fixtures = [] |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + created = make_aware(datetime.datetime(2019, 4, 29, 10, 0, 0)) |
| 24 | + modified = make_aware(datetime.datetime(2019, 4, 29, 12, 0, 0)) |
| 25 | + |
| 26 | + self.me = fixture.get( |
| 27 | + User, |
| 28 | + date_joined=created, |
| 29 | + username='testuser', |
| 30 | + projects=[], |
| 31 | + ) |
| 32 | + self.token = fixture.get(Token, key='me', user=self.me) |
| 33 | + # Defining all the defaults helps to avoid creating ghost / unwanted |
| 34 | + # objects (like a Project for translations/subprojects) |
| 35 | + self.project = fixture.get( |
| 36 | + Project, |
| 37 | + pub_date=created, |
| 38 | + modified_date=modified, |
| 39 | + description='Project description', |
| 40 | + repo='https://github.com/rtfd/project', |
| 41 | + project_url='http://project.com', |
| 42 | + name='project', |
| 43 | + slug='project', |
| 44 | + related_projects=[], |
| 45 | + main_language_project=None, |
| 46 | + users=[self.me], |
| 47 | + versions=[], |
| 48 | + ) |
| 49 | + for tag in ('tag', 'project', 'test'): |
| 50 | + self.project.tags.add(tag) |
| 51 | + |
| 52 | + self.redirect = fixture.get( |
| 53 | + Redirect, |
| 54 | + create_dt=created, |
| 55 | + update_dt=modified, |
| 56 | + from_url='/docs/', |
| 57 | + to_url='/documentation/', |
| 58 | + redirect_type='page', |
| 59 | + project=self.project, |
| 60 | + ) |
| 61 | + |
| 62 | + self.subproject = fixture.get( |
| 63 | + Project, |
| 64 | + pub_date=created, |
| 65 | + modified_date=modified, |
| 66 | + description='SubProject description', |
| 67 | + repo='https://github.com/rtfd/subproject', |
| 68 | + project_url='http://subproject.com', |
| 69 | + name='subproject', |
| 70 | + slug='subproject', |
| 71 | + related_projects=[], |
| 72 | + main_language_project=None, |
| 73 | + users=[], |
| 74 | + versions=[], |
| 75 | + ) |
| 76 | + # self.translation = fixture.get(Project, slug='translation') |
| 77 | + |
| 78 | + self.project.add_subproject(self.subproject) |
| 79 | + # self.project.add_translation(self.translation) |
| 80 | + |
| 81 | + self.version = fixture.get( |
| 82 | + Version, |
| 83 | + slug='v1.0', |
| 84 | + verbose_name='v1.0', |
| 85 | + identifier='a1b2c3', |
| 86 | + project=self.project, |
| 87 | + active=True, |
| 88 | + built=True, |
| 89 | + type='tag', |
| 90 | + ) |
| 91 | + |
| 92 | + self.build = fixture.get( |
| 93 | + Build, |
| 94 | + date=created, |
| 95 | + type='html', |
| 96 | + state='finished', |
| 97 | + error='', |
| 98 | + success=True, |
| 99 | + _config = {'property': 'test value'}, |
| 100 | + version=self.version, |
| 101 | + project=self.project, |
| 102 | + builder='builder01', |
| 103 | + commit='a1b2c3', |
| 104 | + length=60, |
| 105 | + ) |
| 106 | + |
| 107 | + self.other = fixture.get(User, projects=[]) |
| 108 | + self.others_token = fixture.get(Token, key='other', user=self.other) |
| 109 | + self.others_project = fixture.get( |
| 110 | + Project, |
| 111 | + slug='others_project', |
| 112 | + related_projects=[], |
| 113 | + main_language_project=None, |
| 114 | + users=[self.other], |
| 115 | + versions=[], |
| 116 | + ) |
| 117 | + |
| 118 | + self.client = APIClient() |
| 119 | + |
| 120 | + def tearDown(self): |
| 121 | + # Cleanup cache to avoid throttling on tests |
| 122 | + cache.clear() |
| 123 | + |
| 124 | + def _get_response_dict(self, view_name): |
| 125 | + filename = Path(__file__).absolute().parent / 'responses' / f'{view_name}.json' |
| 126 | + return json.load(open(filename)) |
| 127 | + |
| 128 | + def assertDictEqual(self, d1, d2): |
| 129 | + """ |
| 130 | + Show the differences between the dicts in a human readable way. |
| 131 | +
|
| 132 | + It's just a helper for debugging API responses. |
| 133 | + """ |
| 134 | + import datadiff |
| 135 | + return super().assertDictEqual(d1, d2, datadiff.diff(d1, d2)) |
0 commit comments