Skip to content

Instantly share code, notes, and snippets.

@phlax
Created February 14, 2018 17:05
Show Gist options
  • Save phlax/83c60786e78c26bed3b13889e1f2db84 to your computer and use it in GitHub Desktop.
Save phlax/83c60786e78c26bed3b13889e1f2db84 to your computer and use it in GitHub Desktop.
import pytest
from mock import MagicMock, PropertyMock, patch
from django.http import HttpResponse
from django.urls import reverse
@patch('pontoon.base.views.get_object_or_404')
@patch('pontoon.base.views.utils.get_project_locale_from_request')
@patch('pontoon.base.views.Project.objects.available')
@patch('pontoon.base.views.redirect')
def test_view_lang_agnostic_anon(redirect_mock, projects_mock,
util_mock, get_mock, client):
view = reverse(
'pontoon.translate.locale.agnostic',
kwargs=dict(slug='FOO', part='BAR'))
# mock return value for Project.objects.available
projects_mock.return_value = 'AVAILABLEPROJECTS'
# create a mock Project with .locales
project_mock = MagicMock()
type(project_mock).locales = PropertyMock(return_value='LOCALES')
# mock return value for get_object_or_404 for Project
get_mock.return_value = project_mock
# mock return value for redirect
mock_response = HttpResponse()
redirect_mock.return_value = mock_response
# mock return_value for get_project_locale_from_request
util_mock.return_value = 23
response = client.get(view)
# Project.objects.available was called with no args
assert list(projects_mock.call_args) == [(), {}]
# get_object_or_404 was called with Project.objects.available and
# the requested slug
assert (
list(get_mock.call_args)
== [('AVAILABLEPROJECTS',), {'slug': u'FOO'}])
# get_project_locale_from_request
assert (
list(util_mock.call_args)
== [(response.wsgi_request, 'LOCALES'), {}])
# redirect was called with args...
assert (
list(redirect_mock.call_args)
[('pontoon.translate',),
{'locale': 23,
'part': u'BAR',
'slug': u'FOO'}]
assert response is mock_response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment