Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split APIv3 tests on different files #5911

Merged
merged 6 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion readthedocs/api/v3/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from readthedocs.builds.models import Build, Version
from readthedocs.projects.constants import LANGUAGES, PROGRAMMING_LANGUAGES, REPO_CHOICES
from readthedocs.projects.models import Project
from readthedocs.projects.models import Project, EnvironmentVariable
from readthedocs.redirects.models import Redirect, TYPE_CHOICES as REDIRECT_TYPE_CHOICES


Expand Down Expand Up @@ -338,6 +338,7 @@ class ProjectLinksSerializer(BaseLinksSerializer):

versions = serializers.SerializerMethodField()
builds = serializers.SerializerMethodField()
environmentvariables = serializers.SerializerMethodField()
redirects = serializers.SerializerMethodField()
subprojects = serializers.SerializerMethodField()
superproject = serializers.SerializerMethodField()
Expand All @@ -356,6 +357,15 @@ def get_versions(self, obj):
)
return self._absolute_url(path)

def get_environmentvariables(self, obj):
path = reverse(
'projects-environmentvariables-list',
kwargs={
'parent_lookup_project__slug': obj.slug,
},
)
return self._absolute_url(path)

def get_redirects(self, obj):
path = reverse(
'projects-redirects-list',
Expand Down Expand Up @@ -562,3 +572,46 @@ def get_from_url(self, obj):
def get_to_url(self, obj):
# Overridden only to return ``None`` when the description is ``''``
return obj.to_url or None


class EnvironmentVariableLinksSerializer(BaseLinksSerializer):
_self = serializers.SerializerMethodField()
project = serializers.SerializerMethodField()

def get__self(self, obj):
path = reverse(
'projects-environmentvariables-detail',
kwargs={
'parent_lookup_project__slug': obj.project.slug,
'environmentvariable_pk': obj.pk,
},
)
return self._absolute_url(path)

def get_project(self, obj):
path = reverse(
'projects-detail',
kwargs={
'project_slug': obj.project.slug,
},
)
return self._absolute_url(path)


class EnvironmentVariableSerializer(serializers.ModelSerializer):

value = serializers.CharField(write_only=True)
project = serializers.SlugRelatedField(slug_field='slug', read_only=True)
_links = EnvironmentVariableLinksSerializer(source='*', read_only=True)

class Meta:
model = EnvironmentVariable
fields = [
'pk',
'created',
'modified',
'name',
'value',
'project',
'_links',
]
133 changes: 133 additions & 0 deletions readthedocs/api/v3/tests/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import datetime
import json
from pathlib import Path

import django_dynamic_fixture as fixture
from django.contrib.auth.models import User
from django.core.cache import cache
from django.test import TestCase
from django.utils.timezone import make_aware
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

from readthedocs.builds.constants import TAG
from readthedocs.builds.models import Build, Version
from readthedocs.projects.models import Project
from readthedocs.redirects.models import Redirect


class APIEndpointMixin(TestCase):

fixtures = []

def setUp(self):
self.created = make_aware(datetime.datetime(2019, 4, 29, 10, 0, 0))
self.modified = make_aware(datetime.datetime(2019, 4, 29, 12, 0, 0))

self.me = fixture.get(
User,
date_joined=self.created,
username='testuser',
projects=[],
)
self.token = fixture.get(Token, key='me', user=self.me)
# Defining all the defaults helps to avoid creating ghost / unwanted
# objects (like a Project for translations/subprojects)
self.project = fixture.get(
Project,
pub_date=self.created,
modified_date=self.modified,
description='Project description',
repo='https://github.com/rtfd/project',
project_url='http://project.com',
name='project',
slug='project',
related_projects=[],
main_language_project=None,
users=[self.me],
versions=[],
)
for tag in ('tag', 'project', 'test'):
self.project.tags.add(tag)

self.redirect = fixture.get(
Redirect,
create_dt=self.created,
update_dt=self.modified,
from_url='/docs/',
to_url='/documentation/',
redirect_type='page',
project=self.project,
)

self.subproject = fixture.get(
Project,
pub_date=self.created,
modified_date=self.modified,
description='SubProject description',
repo='https://github.com/rtfd/subproject',
project_url='http://subproject.com',
name='subproject',
slug='subproject',
related_projects=[],
main_language_project=None,
users=[],
versions=[],
)
self.project.add_subproject(self.subproject)

self.version = fixture.get(
Version,
slug='v1.0',
verbose_name='v1.0',
identifier='a1b2c3',
project=self.project,
active=True,
built=True,
type=TAG,
)

self.build = fixture.get(
Build,
date=self.created,
type='html',
state='finished',
error='',
success=True,
_config = {'property': 'test value'},
version=self.version,
project=self.project,
builder='builder01',
commit='a1b2c3',
length=60,
)

self.other = fixture.get(User, projects=[])
self.others_token = fixture.get(Token, key='other', user=self.other)
self.others_project = fixture.get(
Project,
slug='others_project',
related_projects=[],
main_language_project=None,
users=[self.other],
versions=[],
)

self.client = APIClient()

def tearDown(self):
# Cleanup cache to avoid throttling on tests
cache.clear()

def _get_response_dict(self, view_name):
filename = Path(__file__).absolute().parent / 'responses' / f'{view_name}.json'
return json.load(open(filename))

def assertDictEqual(self, d1, d2):
"""
Show the differences between the dicts in a human readable way.

It's just a helper for debugging API responses.
"""
import datadiff
return super().assertDictEqual(d1, d2, datadiff.diff(d1, d2))
1 change: 1 addition & 0 deletions readthedocs/api/v3/tests/responses/projects-detail.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/",
"builds": "https://readthedocs.org/api/v3/projects/project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/project/superproject/",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/environmentvariables/1/",
"project": "https://readthedocs.org/api/v3/projects/project/"
},
"created": "2019-04-29T10:00:00Z",
"modified": "2019-04-29T12:00:00Z",
"pk": 1,
"project": "project",
"name": "ENVVAR"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/environmentvariables/1/",
"project": "https://readthedocs.org/api/v3/projects/project/"
},
"created": "2019-04-29T10:00:00Z",
"modified": "2019-04-29T12:00:00Z",
"pk": 1,
"project": "project",
"name": "ENVVAR"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/environmentvariables/2/",
"project": "https://readthedocs.org/api/v3/projects/project/"
},
"created": "2019-04-29T10:00:00Z",
"modified": "2019-04-29T12:00:00Z",
"pk": 2,
"project": "project",
"name": "NEWENVVAR"
}
1 change: 1 addition & 0 deletions readthedocs/api/v3/tests/responses/projects-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"_self": "https://readthedocs.org/api/v3/projects/project/",
"versions": "https://readthedocs.org/api/v3/projects/project/versions/",
"builds": "https://readthedocs.org/api/v3/projects/project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/project/superproject/",
Expand Down
7 changes: 5 additions & 2 deletions readthedocs/api/v3/tests/responses/projects-list_POST.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/test-project/",
"builds": "https://readthedocs.org/api/v3/projects/test-project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/test-project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/test-project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/test-project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/test-project/superproject/",
Expand All @@ -11,8 +12,8 @@
"created": "2019-04-29T10:00:00Z",
"default_branch": "master",
"default_version": "latest",
"description": null,
"id": 4,
"homepage": "http://template.readthedocs.io/",
"language": {
"code": "en",
"name": "English"
Expand All @@ -36,8 +37,10 @@
"tags": [],
"translation_of": null,
"urls": {
"builds": "https://readthedocs.org/projects/test-project/builds/",
"documentation": "http://readthedocs.org/docs/test-project/en/latest/",
"project_homepage": "http://template.readthedocs.io/"
"home": "https://readthedocs.org/projects/test-project/",
"versions": "https://readthedocs.org/projects/test-project/versions/"
},
"users": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"_self": "https://readthedocs.org/api/v3/projects/project/",
"versions": "https://readthedocs.org/api/v3/projects/project/versions/",
"builds": "https://readthedocs.org/api/v3/projects/project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/project/superproject/",
Expand All @@ -95,6 +96,7 @@
"_self": "https://readthedocs.org/api/v3/projects/subproject/",
"versions": "https://readthedocs.org/api/v3/projects/subproject/versions/",
"builds": "https://readthedocs.org/api/v3/projects/subproject/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/subproject/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/subproject/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/subproject/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/subproject/superproject/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/",
"builds": "https://readthedocs.org/api/v3/projects/project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/project/superproject/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"_links": {
"_self": "https://readthedocs.org/api/v3/projects/project/",
"builds": "https://readthedocs.org/api/v3/projects/project/builds/",
"environmentvariables": "https://readthedocs.org/api/v3/projects/project/environmentvariables/",
"redirects": "https://readthedocs.org/api/v3/projects/project/redirects/",
"subprojects": "https://readthedocs.org/api/v3/projects/project/subprojects/",
"superproject": "https://readthedocs.org/api/v3/projects/project/superproject/",
Expand Down
54 changes: 54 additions & 0 deletions readthedocs/api/v3/tests/test_builds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from .mixins import APIEndpointMixin
from django.urls import reverse


class BuildsEndpointTests(APIEndpointMixin):

def test_projects_builds_list(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
response = self.client.get(
reverse(
'projects-builds-list',
kwargs={
'parent_lookup_project__slug': self.project.slug,
}),
)
self.assertEqual(response.status_code, 200)

def test_projects_builds_detail(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
response = self.client.get(
reverse(
'projects-builds-detail',
kwargs={
'parent_lookup_project__slug': self.project.slug,
'build_pk': self.build.pk,
}),
)
self.assertEqual(response.status_code, 200)

self.assertDictEqual(
response.json(),
self._get_response_dict('projects-builds-detail'),
)

def test_projects_versions_builds_list_post(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
self.assertEqual(self.project.builds.count(), 1)
response = self.client.post(
reverse(
'projects-versions-builds-list',
kwargs={
'parent_lookup_project__slug': self.project.slug,
'parent_lookup_version__slug': self.version.slug,
}),
)
self.assertEqual(response.status_code, 202)
self.assertEqual(self.project.builds.count(), 2)

response_json = response.json()
response_json['build']['created'] = '2019-04-29T14:00:00Z'
self.assertDictEqual(
response_json,
self._get_response_dict('projects-versions-builds-list_POST'),
)
Loading