Skip to content

Commit

Permalink
allow for internal and external waterbutler urls, docker-compose fix
Browse files Browse the repository at this point in the history
  • Loading branch information
icereval committed Jan 14, 2017
1 parent db0d300 commit 885e1cb
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 30 deletions.
7 changes: 4 additions & 3 deletions .docker-compose.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ API_DOMAIN=http://localhost:8000/
ELASTIC_URI=192.168.168.167:9200
OSF_DB_HOST=192.168.168.167
DB_HOST=192.168.168.167
WATERBUTLER_URL=http://192.168.168.167:7777
WATERBUTLER_URL=http://localhost:7777
WATERBUTLER_INTERNAL_URL=http://192.168.168.167:7777
CAS_SERVER_URL=http://192.168.168.167:8080
MFR_SERVER_URL=http://192.168.168.167:7778
SHARE_URL=http://192.168.168.167:8000/
MFR_SERVER_URL=http://localhost:7778
#SHARE_URL=http://192.168.168.167:8000/
BROKER_URL=amqp://guest:guest@192.168.168.167:5672/
10 changes: 10 additions & 0 deletions README-docker-compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ _NOTE: After making changes to `Environment Variables` or `Volume Mounts` (e.g.
- `Single Instance only`
- Configure `.docker-compose.env` `<APP>_REMOTE_DEBUG` environment variables to match these settings.

## Application Tests
- Run All Tests
- `$ docker-compose run --rm web invoke test`

- Run OSF Specific Tests
- `$ docker-compose run --rm web invoke test_osf`

- Test a Specific Module
- `$ docker-compose run --rm web invoke test_module -m tests/test_conferences.py`

## Managing Container State

Restart a container:
Expand Down
2 changes: 1 addition & 1 deletion addons/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None)
kwargs['cookie'] = cookie
if version:
kwargs['version'] = version
metadata_url = waterbutler_url_for('metadata', **kwargs)
metadata_url = waterbutler_url_for('metadata', _internal=True, **kwargs)

res = requests.get(metadata_url)
if res.status_code != 200:
Expand Down
1 change: 1 addition & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def test_module(ctx, module=None):
'tests/test_subjects.py',
'tests/test_tokens.py',
'tests/test_webtests.py',
'tests/test_utils.py',
]
@task
def test_osf(ctx):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_conferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def test_upload(self, mock_put, mock_get_url):
'osfstorage',
'/' + self.attachment.filename,
self.node,
_internal=True,
user=self.user,
)
mock_put.assert_called_with(
Expand All @@ -230,6 +231,7 @@ def test_upload_no_file_name(self, mock_put, mock_get_url):
'osfstorage',
'/' + settings.MISSING_FILE_NAME,
self.node,
_internal=True,
user=self.user,
)
mock_put.assert_called_with(
Expand Down
18 changes: 6 additions & 12 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4837,21 +4837,17 @@ def test_enqueue_called(self, enqueue_task):
assert_equals(task.args[2], False)
assert_equals(task.args[3], {'title'})

@mock.patch('website.project.tasks.settings.SHARE_URL', None)
@mock.patch('website.project.tasks.settings.SHARE_API_TOKEN', None)
@mock.patch('website.project.tasks.requests')
def test_skips_no_settings(self, requests):
from website.project.tasks import settings
settings.SHARE_URL = None
settings.SHARE_API_TOKEN = None

on_node_updated(self.node._id, self.user._id, False, {'is_public'})
assert_false(requests.post.called)

@mock.patch('website.project.tasks.settings.SHARE_URL', 'https://share.osf.io')
@mock.patch('website.project.tasks.settings.SHARE_API_TOKEN', 'Token')
@mock.patch('website.project.tasks.requests')
def test_updates_share(self, requests):
from website.project.tasks import settings
settings.SHARE_URL = 'https://share.osf.io'
settings.SHARE_API_TOKEN = 'Token'

on_node_updated(self.node._id, self.user._id, False, {'is_public'})

kwargs = requests.post.call_args[1]
Expand All @@ -4861,12 +4857,10 @@ def test_updates_share(self, requests):
assert_equals(kwargs['headers']['Authorization'], 'Bearer Token')
assert_equals(graph[0]['uri'], '{}{}/'.format(settings.DOMAIN, self.node._id))

@mock.patch('website.project.tasks.settings.SHARE_URL', 'https://share.osf.io')
@mock.patch('website.project.tasks.settings.SHARE_API_TOKEN', 'Token')
@mock.patch('website.project.tasks.requests')
def test_update_share_correctly(self, requests):
from website.project.tasks import settings
settings.SHARE_URL = 'https://share.osf.io'
settings.SHARE_API_TOKEN = 'Token'

cases = [{
'is_deleted': False,
'attrs': {'is_public': True, 'is_deleted': False, 'spam_status': SpamStatus.HAM}
Expand Down
1 change: 1 addition & 0 deletions tests/test_registrations/test_archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def callback(request, uri, headers):
node=self.src,
user=self.user,
view_only=True,
_internal=True,
)
httpretty.register_uri(httpretty.GET,
url,
Expand Down
20 changes: 14 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import blinker

from tests.base import OsfTestCase
from tests.factories import RegistrationFactory
from osf_tests.factories import RegistrationFactory

from framework.routing import Rule, json_renderer
from framework.utils import secure_filename
Expand Down Expand Up @@ -211,6 +211,18 @@ def test_waterbutler_url_for(self):
assert_in('path=path', url)
assert_in('provider=provider', url)

def test_waterbutler_url_for_internal(self):
settings.WATERBUTLER_INTERNAL_URL = 'http://1.2.3.4:7777'
with self.app.test_request_context():
url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'), _internal=True)

assert_not_in(settings.WATERBUTLER_URL, url)
assert_in(settings.WATERBUTLER_INTERNAL_URL, url)
assert_in('nid=_id', url)
assert_in('/file?', url)
assert_in('path=path', url)
assert_in('provider=provider', url)

def test_waterbutler_url_for_implicit_cookie(self):
with self.app.test_request_context() as context:
context.request.cookies = {settings.COOKIE_NAME: 'cookie'}
Expand Down Expand Up @@ -388,11 +400,7 @@ def zero_if_not_check(item, check, checkFn=lambda n: n):
class TestProjectUtils(OsfTestCase):

def set_registered_date(self, reg, date):
reg._fields['registered_date'].__set__(
reg,
date,
safe=True
)
reg.registered_date = date
reg.save()

def test_get_recent_public_registrations(self):
Expand Down
1 change: 1 addition & 0 deletions website/addons/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None)
kwargs['version'] = version
metadata_url = waterbutler_url_for(
'metadata',
_internal=True,
**kwargs
)
res = requests.get(metadata_url)
Expand Down
4 changes: 2 additions & 2 deletions website/addons/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ def addon_view_or_download_file(auth, path, provider, **kwargs):
# TODO clean up these urls and unify what is used as a version identifier
if request.method == 'HEAD':
return make_response(('', 200, {
'Location': file_node.generate_waterbutler_url(**dict(extras, direct=None, version=version.identifier))
'Location': file_node.generate_waterbutler_url(**dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render'))
}))

if action == 'download':
return redirect(file_node.generate_waterbutler_url(**dict(extras, direct=None, version=version.identifier)))
return redirect(file_node.generate_waterbutler_url(**dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render')))

if action == 'get_guid':
draft_id = extras.get('draft')
Expand Down
2 changes: 1 addition & 1 deletion website/archiver/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def archive_addon(addon_short_name, job_pk, stat_result):
src_provider = src.get_addon(addon_name)
folder_name = src_provider.archive_folder_name
cookie = user.get_or_create_cookie()
copy_url = settings.WATERBUTLER_URL + '/ops/copy'
copy_url = settings.WATERBUTLER_INTERNAL_URL + '/ops/copy'
if addon_name == 'dataverse':
# The dataverse API will not differentiate between published and draft files
# unless expcicitly asked. We need to create seperate folders for published and
Expand Down
2 changes: 1 addition & 1 deletion website/conferences/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def upload_attachment(user, node, attachment):
attachment.seek(0)
name = '/' + (attachment.filename or settings.MISSING_FILE_NAME)
content = attachment.read()
upload_url = util.waterbutler_url_for('upload', 'osfstorage', name, node, user=user)
upload_url = util.waterbutler_url_for('upload', 'osfstorage', name, node, user=user, _internal=True)

requests.put(
upload_url,
Expand Down
1 change: 1 addition & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def parent_dir(path):
DEFAULT_HMAC_SECRET = 'changeme'
DEFAULT_HMAC_ALGORITHM = hashlib.sha256
WATERBUTLER_URL = 'http://localhost:7777'
WATERBUTLER_INTERNAL_URL = WATERBUTLER_URL
WATERBUTLER_ADDRS = ['127.0.0.1']

# Test identifier namespaces
Expand Down
8 changes: 4 additions & 4 deletions website/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def is_json_request():
return content_type and ('application/json' in content_type)


def waterbutler_url_for(route, provider, path, node, user=None, **kwargs):
def waterbutler_url_for(route, provider, path, node, user=None, _internal=False, **kwargs):
"""DEPRECATED Use waterbutler_api_url_for
Reverse URL lookup for WaterButler routes
:param str route: The action to preform, upload, download, delete...
Expand All @@ -156,7 +156,7 @@ def waterbutler_url_for(route, provider, path, node, user=None, **kwargs):
:param User user: The user whos cookie will be used or None
:param dict kwargs: Addition query parameters to be appended
"""
url = furl.furl(website_settings.WATERBUTLER_URL)
url = furl.furl(website_settings.WATERBUTLER_INTERNAL_URL if _internal else website_settings.WATERBUTLER_URL)
url.path.segments.append(waterbutler_action_map[route])

url.args.update({
Expand All @@ -182,9 +182,9 @@ def waterbutler_url_for(route, provider, path, node, user=None, **kwargs):
return url.url


def waterbutler_api_url_for(node_id, provider, path='/', **kwargs):
def waterbutler_api_url_for(node_id, provider, path='/', _internal=False, **kwargs):
assert path.startswith('/'), 'Path must always start with /'
url = furl.furl(website_settings.WATERBUTLER_URL)
url = furl.furl(website_settings.WATERBUTLER_INTERNAL_URL if _internal else website_settings.WATERBUTLER_URL)
segments = ['v1', 'resources', node_id, 'providers', provider] + path.split('/')[1:]
url.path.segments.extend([urllib.quote(x.encode('utf-8')) for x in segments])
url.args.update(kwargs)
Expand Down

0 comments on commit 885e1cb

Please sign in to comment.