def test_postPage_can_repost_on_redirect(self): # Some pages (that means you, BugZilla bug-search page!) can # redirect on POST, but without honouring the POST. Standard # requests behaviour is to redirect to a GET, but if the caller # says it's safe, _postPage can re-do the POST at the new URL. base_url = "http://example.com/" form = self.factory.getUniqueString() target = self.factory.getUniqueString() fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString() bugtracker = ExternalBugTracker(base_url) transaction.commit() responses.add("POST", base_url + form, status=302, headers={"Location": base_url + target}) responses.add("POST", base_url + target, body=fake_form) bugtracker._postPage(form, form={}, repost_on_redirect=True) requests = [call.request for call in responses.calls] self.assertThat( requests, MatchesListwise([ MatchesStructure.byEquality(method="POST", path_url="/" + form), MatchesStructure.byEquality(method="POST", path_url="/" + target), ]))
def test_postPage_does_not_repost_on_redirect(self): # By default, if the POST redirects, _postPage leaves requests to # handle it in the normal, RFC-compliant way. base_url = "http://example.com/" form = self.factory.getUniqueString() target = self.factory.getUniqueString() fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString() bugtracker = ExternalBugTracker(base_url) transaction.commit() responses.add("POST", base_url + form, status=302, headers={"Location": base_url + target}) responses.add("GET", base_url + target, body=fake_form) bugtracker._postPage(form, {}) requests = [call.request for call in responses.calls] self.assertThat( requests, MatchesListwise([ MatchesStructure.byEquality(method="POST", path_url="/" + form), MatchesStructure.byEquality(method="GET", path_url="/" + target), ]))
def test_postPage_returns_response_page(self): # _postPage posts, then returns the page text it gets back from # the server. base_url = "http://example.com/" form = self.factory.getUniqueString() fake_form = "<bugzilla>%s</bugzilla>" % self.factory.getUniqueString() bugtracker = ExternalBugTracker(base_url) transaction.commit() responses.add("POST", base_url + form, body=fake_form) self.assertEqual(fake_form, bugtracker._postPage(form, {}).text)
def test_post_sends_host(self): # When posting, a Host header is sent. base_host = 'example.com' base_url = 'http://%s/' % base_host bugtracker = ExternalBugTracker(base_url) def assert_headers(request, data, timeout=None): self.assertContentEqual( [('User-agent', LP_USER_AGENT), ('Host', base_host)], request.header_items()) with monkey_patch(urllib2, urlopen=assert_headers): bugtracker._post('some-url', {'post-data': 'here'})
def test_post_sends_host(self): # When posting, a Host header is sent. base_host = 'example.com' base_url = 'http://%s/' % base_host bugtracker = ExternalBugTracker(base_url) def assert_headers(request, data, timeout=None): self.assertContentEqual([('User-agent', LP_USER_AGENT), ('Host', base_host)], request.header_items()) with monkey_patch(urllib2, urlopen=assert_headers): bugtracker._post('some-url', {'post-data': 'here'})
def test_postPage_sends_host(self): # When posting, a Host header is sent. base_host = 'example.com' base_url = 'http://%s/' % base_host bugtracker = ExternalBugTracker(base_url) transaction.commit() responses.add("POST", base_url + "some-url") bugtracker._postPage('some-url', {'post-data': 'here'}) self.assertThat( responses.calls[-1].request, MatchesStructure( headers=ContainsDict({ "User-Agent": Equals(LP_USER_AGENT), "Host": Equals(base_host), })))
def test_postPage_raises_on_404(self): # When posting, a 404 is converted to a BugTrackerConnectError. base_url = "http://example.com/" bugtracker = ExternalBugTracker(base_url) transaction.commit() responses.add("POST", base_url + "some-url", status=404) self.assertRaises(BugTrackerConnectError, bugtracker._postPage, 'some-url', {'post-data': 'here'})
def test_post_raises_on_404(self): # When posting, a 404 is converted to a BugTrackerConnectError. base_url = "http://example.com/" bugtracker = ExternalBugTracker(base_url) def raise404(request, data, timeout=None): raise urllib2.HTTPError('url', 404, 'Not Found', None, None) with monkey_patch(urllib2, urlopen=raise404): self.assertRaises(BugTrackerConnectError, bugtracker._post, 'some-url', {'post-data': 'here'})
def test_sync_comments_disabled(self): # If the global config checkwatches.sync_comments is False, # external bug trackers will always set their sync_comments # attribute to False. self.pushConfig('checkwatches', sync_comments=False) tracker = ExternalBugTracker(self.base_url) self.assertFalse(tracker.sync_comments) tracker = BackLinkingExternalBugTracker(self.base_url) self.assertFalse(tracker.sync_comments) tracker = CommentImportingExternalBugTracker(self.base_url) self.assertFalse(tracker.sync_comments) tracker = CommentPushingExternalBugTracker(self.base_url) self.assertFalse(tracker.sync_comments)
def test_sync_comments_enabled(self): # If the global config checkwatches.sync_comments is True, # external bug trackers will set their sync_comments attribute # according to their support of comment syncing. self.pushConfig('checkwatches', sync_comments=True) # A plain tracker will never support syncing comments. tracker = ExternalBugTracker(self.base_url) self.assertFalse(tracker.sync_comments) # Trackers that support comment pushing, comment pulling or # back-linking will have sync_comments set to True. tracker = BackLinkingExternalBugTracker(self.base_url) self.assertTrue(tracker.sync_comments) tracker = CommentImportingExternalBugTracker(self.base_url) self.assertTrue(tracker.sync_comments) tracker = CommentPushingExternalBugTracker(self.base_url) self.assertTrue(tracker.sync_comments)
def _fakeExternalBugTracker(self, base_url, fake_form): """Create an `ExternalBugTracker` with a fake `_post` method.""" bugtracker = ExternalBugTracker(base_url) bugtracker._post = FakeMethod(result=fake_form) return bugtracker