Commit 16e4eacb by bmedx

Make sure httpretty usage cleans up after itself

parent b67e3e9e
...@@ -484,6 +484,5 @@ class TrackSelectionEmbargoTest(UrlResetMixin, ModuleStoreTestCase): ...@@ -484,6 +484,5 @@ class TrackSelectionEmbargoTest(UrlResetMixin, ModuleStoreTestCase):
@httpretty.activate @httpretty.activate
def test_embargo_allow(self): def test_embargo_allow(self):
response = self.client.get(self.url) response = self.client.get(self.url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
...@@ -49,6 +49,9 @@ class SamlIntegrationTestUtilities(object): ...@@ -49,6 +49,9 @@ class SamlIntegrationTestUtilities(object):
) )
# Mock out HTTP requests that may be made to TestShib: # Mock out HTTP requests that may be made to TestShib:
httpretty.enable() httpretty.enable()
httpretty.reset()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable)
def metadata_callback(_request, _uri, headers): def metadata_callback(_request, _uri, headers):
""" Return a cached copy of TestShib's metadata by reading it from disk """ """ Return a cached copy of TestShib's metadata by reading it from disk """
...@@ -66,8 +69,6 @@ class SamlIntegrationTestUtilities(object): ...@@ -66,8 +69,6 @@ class SamlIntegrationTestUtilities(object):
content_type='text/xml', content_type='text/xml',
body=cache_duration_metadata_callback body=cache_duration_metadata_callback
) )
self.addCleanup(httpretty.disable)
self.addCleanup(httpretty.reset)
# Configure the SAML library to use the same request ID for every request. # Configure the SAML library to use the same request ID for every request.
# Doing this and freezing the time allows us to play back recorded request/response pairs # Doing this and freezing the time allows us to play back recorded request/response pairs
......
...@@ -581,6 +581,7 @@ class GetThreadListTest(ForumsEnableMixin, CommentsServiceMockMixin, UrlResetMix ...@@ -581,6 +581,7 @@ class GetThreadListTest(ForumsEnableMixin, CommentsServiceMockMixin, UrlResetMix
super(GetThreadListTest, self).setUp() super(GetThreadListTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.maxDiff = None # pylint: disable=invalid-name self.maxDiff = None # pylint: disable=invalid-name
self.user = UserFactory.create() self.user = UserFactory.create()
...@@ -998,6 +999,7 @@ class GetCommentListTest(ForumsEnableMixin, CommentsServiceMockMixin, SharedModu ...@@ -998,6 +999,7 @@ class GetCommentListTest(ForumsEnableMixin, CommentsServiceMockMixin, SharedModu
super(GetCommentListTest, self).setUp() super(GetCommentListTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.maxDiff = None # pylint: disable=invalid-name self.maxDiff = None # pylint: disable=invalid-name
self.user = UserFactory.create() self.user = UserFactory.create()
...@@ -1477,6 +1479,7 @@ class CreateThreadTest( ...@@ -1477,6 +1479,7 @@ class CreateThreadTest(
super(CreateThreadTest, self).setUp() super(CreateThreadTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
...@@ -1741,6 +1744,7 @@ class CreateCommentTest( ...@@ -1741,6 +1744,7 @@ class CreateCommentTest(
super(CreateCommentTest, self).setUp() super(CreateCommentTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
...@@ -2006,6 +2010,7 @@ class UpdateThreadTest( ...@@ -2006,6 +2010,7 @@ class UpdateThreadTest(
super(UpdateThreadTest, self).setUp() super(UpdateThreadTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
...@@ -2388,6 +2393,7 @@ class UpdateCommentTest( ...@@ -2388,6 +2393,7 @@ class UpdateCommentTest(
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
...@@ -2787,6 +2793,7 @@ class DeleteThreadTest( ...@@ -2787,6 +2793,7 @@ class DeleteThreadTest(
super(DeleteThreadTest, self).setUp() super(DeleteThreadTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
...@@ -2924,6 +2931,7 @@ class DeleteCommentTest( ...@@ -2924,6 +2931,7 @@ class DeleteCommentTest(
super(DeleteCommentTest, self).setUp() super(DeleteCommentTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
...@@ -3078,6 +3086,7 @@ class RetrieveThreadTest( ...@@ -3078,6 +3086,7 @@ class RetrieveThreadTest(
super(RetrieveThreadTest, self).setUp() super(RetrieveThreadTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
......
...@@ -44,6 +44,7 @@ class SerializerTestMixin(ForumsEnableMixin, CommentsServiceMockMixin, UrlResetM ...@@ -44,6 +44,7 @@ class SerializerTestMixin(ForumsEnableMixin, CommentsServiceMockMixin, UrlResetM
super(SerializerTestMixin, self).setUp() super(SerializerTestMixin, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.maxDiff = None # pylint: disable=invalid-name self.maxDiff = None # pylint: disable=invalid-name
self.user = UserFactory.create() self.user = UserFactory.create()
...@@ -411,6 +412,7 @@ class ThreadSerializerDeserializationTest( ...@@ -411,6 +412,7 @@ class ThreadSerializerDeserializationTest(
super(ThreadSerializerDeserializationTest, self).setUp() super(ThreadSerializerDeserializationTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
...@@ -612,6 +614,7 @@ class CommentSerializerDeserializationTest(ForumsEnableMixin, CommentsServiceMoc ...@@ -612,6 +614,7 @@ class CommentSerializerDeserializationTest(ForumsEnableMixin, CommentsServiceMoc
super(CommentSerializerDeserializationTest, self).setUp() super(CommentSerializerDeserializationTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.user = UserFactory.create() self.user = UserFactory.create()
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
......
...@@ -1421,6 +1421,7 @@ class CommentViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTes ...@@ -1421,6 +1421,7 @@ class CommentViewSetPartialUpdateTest(DiscussionAPIViewTestMixin, ModuleStoreTes
super(CommentViewSetPartialUpdateTest, self).setUp() super(CommentViewSetPartialUpdateTest, self).setUp()
httpretty.reset() httpretty.reset()
httpretty.enable() httpretty.enable()
self.addCleanup(httpretty.reset)
self.addCleanup(httpretty.disable) self.addCleanup(httpretty.disable)
self.register_get_user_response(self.user) self.register_get_user_response(self.user)
self.url = reverse("comment-detail", kwargs={"comment_id": "test_comment"}) self.url = reverse("comment-detail", kwargs={"comment_id": "test_comment"})
......
...@@ -71,6 +71,8 @@ class CommentsServiceMockMixin(object): ...@@ -71,6 +71,8 @@ class CommentsServiceMockMixin(object):
"""Mixin with utility methods for mocking the comments service""" """Mixin with utility methods for mocking the comments service"""
def register_get_threads_response(self, threads, page, num_pages): def register_get_threads_response(self, threads, page, num_pages):
"""Register a mock response for GET on the CS thread list endpoint""" """Register a mock response for GET on the CS thread list endpoint"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/threads", "http://localhost:4567/api/v1/threads",
...@@ -85,6 +87,7 @@ class CommentsServiceMockMixin(object): ...@@ -85,6 +87,7 @@ class CommentsServiceMockMixin(object):
def register_get_threads_search_response(self, threads, rewrite, num_pages=1): def register_get_threads_search_response(self, threads, rewrite, num_pages=1):
"""Register a mock response for GET on the CS thread search endpoint""" """Register a mock response for GET on the CS thread search endpoint"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/search/threads", "http://localhost:4567/api/v1/search/threads",
...@@ -100,6 +103,7 @@ class CommentsServiceMockMixin(object): ...@@ -100,6 +103,7 @@ class CommentsServiceMockMixin(object):
def register_post_thread_response(self, thread_data): def register_post_thread_response(self, thread_data):
"""Register a mock response for POST on the CS commentable endpoint""" """Register a mock response for POST on the CS commentable endpoint"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.POST, httpretty.POST,
re.compile(r"http://localhost:4567/api/v1/(\w+)/threads"), re.compile(r"http://localhost:4567/api/v1/(\w+)/threads"),
...@@ -111,6 +115,7 @@ class CommentsServiceMockMixin(object): ...@@ -111,6 +115,7 @@ class CommentsServiceMockMixin(object):
Register a mock response for PUT on the CS endpoint for the given Register a mock response for PUT on the CS endpoint for the given
thread_id. thread_id.
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.PUT, httpretty.PUT,
"http://localhost:4567/api/v1/threads/{}".format(thread_data["id"]), "http://localhost:4567/api/v1/threads/{}".format(thread_data["id"]),
...@@ -119,6 +124,7 @@ class CommentsServiceMockMixin(object): ...@@ -119,6 +124,7 @@ class CommentsServiceMockMixin(object):
def register_get_thread_error_response(self, thread_id, status_code): def register_get_thread_error_response(self, thread_id, status_code):
"""Register a mock error response for GET on the CS thread endpoint.""" """Register a mock error response for GET on the CS thread endpoint."""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/threads/{id}".format(id=thread_id), "http://localhost:4567/api/v1/threads/{id}".format(id=thread_id),
...@@ -130,6 +136,7 @@ class CommentsServiceMockMixin(object): ...@@ -130,6 +136,7 @@ class CommentsServiceMockMixin(object):
""" """
Register a mock response for GET on the CS thread instance endpoint. Register a mock response for GET on the CS thread instance endpoint.
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/threads/{id}".format(id=thread["id"]), "http://localhost:4567/api/v1/threads/{id}".format(id=thread["id"]),
...@@ -148,6 +155,7 @@ class CommentsServiceMockMixin(object): ...@@ -148,6 +155,7 @@ class CommentsServiceMockMixin(object):
else: else:
url = "http://localhost:4567/api/v1/threads/{}/comments".format(thread_id) url = "http://localhost:4567/api/v1/threads/{}/comments".format(thread_id)
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.POST, httpretty.POST,
url, url,
...@@ -161,6 +169,7 @@ class CommentsServiceMockMixin(object): ...@@ -161,6 +169,7 @@ class CommentsServiceMockMixin(object):
""" """
thread_id = comment_data["thread_id"] thread_id = comment_data["thread_id"]
parent_id = comment_data.get("parent_id") parent_id = comment_data.get("parent_id")
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.PUT, httpretty.PUT,
"http://localhost:4567/api/v1/comments/{}".format(comment_data["id"]), "http://localhost:4567/api/v1/comments/{}".format(comment_data["id"]),
...@@ -172,6 +181,7 @@ class CommentsServiceMockMixin(object): ...@@ -172,6 +181,7 @@ class CommentsServiceMockMixin(object):
Register a mock error response for GET on the CS comment instance Register a mock error response for GET on the CS comment instance
endpoint. endpoint.
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/comments/{id}".format(id=comment_id), "http://localhost:4567/api/v1/comments/{id}".format(id=comment_id),
...@@ -184,6 +194,7 @@ class CommentsServiceMockMixin(object): ...@@ -184,6 +194,7 @@ class CommentsServiceMockMixin(object):
Register a mock response for GET on the CS comment instance endpoint. Register a mock response for GET on the CS comment instance endpoint.
""" """
comment = make_minimal_cs_comment(response_overrides) comment = make_minimal_cs_comment(response_overrides)
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/comments/{id}".format(id=comment["id"]), "http://localhost:4567/api/v1/comments/{id}".format(id=comment["id"]),
...@@ -193,6 +204,7 @@ class CommentsServiceMockMixin(object): ...@@ -193,6 +204,7 @@ class CommentsServiceMockMixin(object):
def register_get_user_response(self, user, subscribed_thread_ids=None, upvoted_ids=None): def register_get_user_response(self, user, subscribed_thread_ids=None, upvoted_ids=None):
"""Register a mock response for GET on the CS user instance endpoint""" """Register a mock response for GET on the CS user instance endpoint"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/users/{id}".format(id=user.id), "http://localhost:4567/api/v1/users/{id}".format(id=user.id),
...@@ -206,6 +218,7 @@ class CommentsServiceMockMixin(object): ...@@ -206,6 +218,7 @@ class CommentsServiceMockMixin(object):
def register_subscribed_threads_response(self, user, threads, page, num_pages): def register_subscribed_threads_response(self, user, threads, page, num_pages):
"""Register a mock response for GET on the CS user instance endpoint""" """Register a mock response for GET on the CS user instance endpoint"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
"http://localhost:4567/api/v1/users/{}/subscribed_threads".format(user.id), "http://localhost:4567/api/v1/users/{}/subscribed_threads".format(user.id),
...@@ -223,6 +236,7 @@ class CommentsServiceMockMixin(object): ...@@ -223,6 +236,7 @@ class CommentsServiceMockMixin(object):
Register a mock response for POST and DELETE on the CS user subscription Register a mock response for POST and DELETE on the CS user subscription
endpoint endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
for method in [httpretty.POST, httpretty.DELETE]: for method in [httpretty.POST, httpretty.DELETE]:
httpretty.register_uri( httpretty.register_uri(
method, method,
...@@ -236,6 +250,7 @@ class CommentsServiceMockMixin(object): ...@@ -236,6 +250,7 @@ class CommentsServiceMockMixin(object):
Register a mock response for PUT and DELETE on the CS thread votes Register a mock response for PUT and DELETE on the CS thread votes
endpoint endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
for method in [httpretty.PUT, httpretty.DELETE]: for method in [httpretty.PUT, httpretty.DELETE]:
httpretty.register_uri( httpretty.register_uri(
method, method,
...@@ -249,6 +264,7 @@ class CommentsServiceMockMixin(object): ...@@ -249,6 +264,7 @@ class CommentsServiceMockMixin(object):
Register a mock response for PUT and DELETE on the CS comment votes Register a mock response for PUT and DELETE on the CS comment votes
endpoint endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
for method in [httpretty.PUT, httpretty.DELETE]: for method in [httpretty.PUT, httpretty.DELETE]:
httpretty.register_uri( httpretty.register_uri(
method, method,
...@@ -259,6 +275,7 @@ class CommentsServiceMockMixin(object): ...@@ -259,6 +275,7 @@ class CommentsServiceMockMixin(object):
def register_flag_response(self, content_type, content_id): def register_flag_response(self, content_type, content_id):
"""Register a mock response for PUT on the CS flag endpoints""" """Register a mock response for PUT on the CS flag endpoints"""
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
for path in ["abuse_flag", "abuse_unflag"]: for path in ["abuse_flag", "abuse_unflag"]:
httpretty.register_uri( httpretty.register_uri(
"PUT", "PUT",
...@@ -275,6 +292,7 @@ class CommentsServiceMockMixin(object): ...@@ -275,6 +292,7 @@ class CommentsServiceMockMixin(object):
""" """
Register a mock response for POST on the CS 'read' endpoint Register a mock response for POST on the CS 'read' endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.POST, httpretty.POST,
"http://localhost:4567/api/v1/users/{id}/read".format(id=user.id), "http://localhost:4567/api/v1/users/{id}/read".format(id=user.id),
...@@ -295,6 +313,7 @@ class CommentsServiceMockMixin(object): ...@@ -295,6 +313,7 @@ class CommentsServiceMockMixin(object):
""" """
Register a mock response for DELETE on the CS thread instance endpoint Register a mock response for DELETE on the CS thread instance endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.DELETE, httpretty.DELETE,
"http://localhost:4567/api/v1/threads/{id}".format(id=thread_id), "http://localhost:4567/api/v1/threads/{id}".format(id=thread_id),
...@@ -306,6 +325,7 @@ class CommentsServiceMockMixin(object): ...@@ -306,6 +325,7 @@ class CommentsServiceMockMixin(object):
""" """
Register a mock response for DELETE on the CS comment instance endpoint Register a mock response for DELETE on the CS comment instance endpoint
""" """
assert httpretty.is_enabled(), 'httpretty must be enabled to mock calls.'
httpretty.register_uri( httpretty.register_uri(
httpretty.DELETE, httpretty.DELETE,
"http://localhost:4567/api/v1/comments/{id}".format(id=comment_id), "http://localhost:4567/api/v1/comments/{id}".format(id=comment_id),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment