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

Project updated when subproject modified #3649

Merged
merged 7 commits into from
Mar 9, 2018
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
22 changes: 20 additions & 2 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,26 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ
log.exception('failed to sync supported versions')
try:
if not first_save:
broadcast(type='app', task=tasks.symlink_project,
args=[self.pk],)
log.info(
'Re-symlinking project and subprojects: project=%s',
self.slug,
)
broadcast(
type='app',
task=tasks.symlink_project,
args=[self.pk],
)
log.info(
'Re-symlinking superprojects: project=%s',
self.slug,
)
for superproject in self.superprojects.all():
broadcast(
type='app',
task=tasks.symlink_project,
args=[superproject.pk],
)

except Exception:
log.exception('failed to symlink project')
try:
Expand Down
203 changes: 201 additions & 2 deletions readthedocs/rtd_tests/tests/test_project_symlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import os
import shutil
import tempfile
import collections
from functools import wraps

import mock
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from django_dynamic_fixture import get

from readthedocs.builds.models import Version
from readthedocs.projects.models import Project, Domain
from readthedocs.projects.tasks import symlink_project
from readthedocs.core.symlink import PublicSymlink, PrivateSymlink


Expand Down Expand Up @@ -908,3 +908,202 @@ def test_symlink_no_error(self):
self.symlink.run()
except:
self.fail('Symlink run raised an exception on unicode slug')

def test_symlink_broadcast_calls_on_project_save(self):
"""
Test calls to ``readthedocs.core.utils.broadcast`` on Project.save().

When a Project is saved, we need to check that we are calling
``broadcast`` utility with the proper task and arguments to re-symlink
them.
"""
with mock.patch('readthedocs.projects.models.broadcast') as broadcast:
project = get(Project)
# skipped on first save
broadcast.assert_not_called()

broadcast.reset_mock()
project.description = 'New description'
project.save()
# called once for this project itself
broadcast.assert_any_calls(
type='app',
task=symlink_project,
args=[project.pk],
)

broadcast.reset_mock()
subproject = get(Project)
# skipped on first save
broadcast.assert_not_called()

project.add_subproject(subproject)
# subproject.save() is not called
broadcast.assert_not_called()

subproject.description = 'New subproject description'
subproject.save()
# subproject symlinks
broadcast.assert_any_calls(
type='app',
task=symlink_project,
args=[subproject.pk],
)
# superproject symlinks
broadcast.assert_any_calls(
type='app',
task=symlink_project,
args=[project.pk],
)


@override_settings()
class TestPublicPrivateSymlink(TempSiterootCase, TestCase):

def setUp(self):
super(TestPublicPrivateSymlink, self).setUp()
from django.contrib.auth.models import User

self.user = get(User)
self.project = get(
Project, name='project', slug='project', privacy_level='public',
users=[self.user], main_language_project=None)
self.project.versions.update(privacy_level='public')
self.project.save()

self.subproject = get(
Project, name='subproject', slug='subproject', privacy_level='public',
users=[self.user], main_language_project=None)
self.subproject.versions.update(privacy_level='public')
self.subproject.save()

def test_change_subproject_privacy(self):
"""
Change subproject's ``privacy_level`` creates proper symlinks.

When the ``privacy_level`` changes in the subprojects, we need to
re-symlink the superproject also to keep in sync its symlink under the
private/public roots.
"""
filesystem_before = {
'private_cname_project': {},
'private_cname_root': {},
'private_web_root': {
'project': {
'en': {},
},
'subproject': {
'en': {},
},
},
'public_cname_project': {},
'public_cname_root': {},
'public_web_root': {
'project': {
'en': {
'latest': {
'type': 'link',
'target': 'user_builds/project/rtd-builds/latest',
},
},
'projects': {
'subproject': {
'type': 'link',
'target': 'public_web_root/subproject',
},
},
},
'subproject': {
'en': {
'latest': {
'type': 'link',
'target': 'user_builds/subproject/rtd-builds/latest',
},
},
},
},
}

filesystem_after = {
'private_cname_project': {},
'private_cname_root': {},
'private_web_root': {
'project': {
'en': {},
'projects': {
'subproject': {
'type': 'link',
'target': 'private_web_root/subproject',
},
},
},
'subproject': {
'en': {
'latest': {
'type': 'link',
'target': 'user_builds/subproject/rtd-builds/latest',
},
},
},
},
'public_cname_project': {},
'public_cname_root': {},
'public_web_root': {
'project': {
'en': {
'latest': {
'type': 'link',
'target': 'user_builds/project/rtd-builds/latest',
},
},
'projects': {},
},
'subproject': {
'en': {},
},
},
}

self.assertEqual(self.project.subprojects.all().count(), 0)
self.assertEqual(self.subproject.superprojects.all().count(), 0)
self.project.add_subproject(self.subproject)
self.assertEqual(self.project.subprojects.all().count(), 1)
self.assertEqual(self.subproject.superprojects.all().count(), 1)

self.assertTrue(self.project.versions.first().active)
self.assertTrue(self.subproject.versions.first().active)
symlink_project(self.project.pk)

self.assertFilesystem(filesystem_before)

self.client.force_login(self.user)
self.client.post(
reverse('project_version_detail',
kwargs={
'project_slug': self.subproject.slug,
'version_slug': self.subproject.versions.first().slug,
}),
data={'privacy_level': 'private', 'active': True},
)

self.assertEqual(self.subproject.versions.first().privacy_level, 'private')
self.assertTrue(self.subproject.versions.first().active)

self.client.post(
reverse('projects_advanced',
kwargs={
'project_slug': self.subproject.slug,
}),
data={
# Required defaults
'python_interpreter': 'python',
'default_version': 'latest',

'privacy_level': 'private',
},
)

self.assertTrue(self.subproject.versions.first().active)
self.subproject.refresh_from_db()
self.assertEqual(self.subproject.privacy_level, 'private')
self.assertFilesystem(filesystem_after)
1 change: 1 addition & 0 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Mercurial==4.4.2

# local debugging tools
pdbpp
datadiff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks unused, but is it helpful to keep around?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a lot while debugging to diff two dicts (current filesystem and expected one). It's a debugging tool that I usually use, but we can definetly remove from here if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, let's keep it for debug purposes