def test_rate_limiting_errors(self): """Finish the task on rate limiting errors.""" try: for err in (InstagramAPIError('503', 'Rate limited', '...'), apiclient.errors.HttpError(httplib2.Response({'status': 429}), ''), urllib2.HTTPError('url', 403, 'msg', {}, None)): self.mox.UnsetStubs() self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag=None, min_id=None, cache=mox.IgnoreArg(), ).AndRaise(err) self.mox.ReplayAll() self.post_task() source = self.sources[0].key.get() self.assertEqual('error', source.status) self.mox.VerifyAll() # should have inserted a new poll task polls = self.taskqueue_stub.GetTasks('poll') self.assertEqual(1, len(polls)) self.assertEqual('/_ah/queue/poll', polls[0]['url']) polls = self.taskqueue_stub.FlushQueue('poll') finally: self.mox.UnsetStubs()
def test_poll_error(self): """If anything goes wrong, the source status should be set to 'error'.""" self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag=None, min_id=None, cache=mox.IgnoreArg(), ).AndRaise(Exception('foo')) self.mox.ReplayAll() self.assertRaises(Exception, self.post_task) source = self.sources[0].key.get() self.assertEqual('error', source.status)
def test_disable_source_on_deauthorized(self): """If the source raises DisableSource, disable it. """ source = self.sources[0] self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag=None, min_id=None, cache=mox.IgnoreArg(), ).AndRaise(models.DisableSource) self.mox.ReplayAll() source.status = 'enabled' source.put() self.post_task() source = source.key.get() self.assertEqual('disabled', source.status)
def test_last_activity_id(self): """We should store the last activity id seen and then send it as min_id.""" self.sources[0].set_activities(list(reversed(self.activities))) self.post_task() source = self.sources[0].key.get() self.assertEqual('c', source.last_activity_id) source.last_polled = util.EPOCH source.put() self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag=None, min_id='c', cache=mox.IgnoreArg(), ).AndReturn({'items': []}) self.mox.ReplayAll() self.post_task()
def test_etag(self): """If we see an ETag, we should send it with the next get_activities().""" self.sources[0]._set('etag', '"my etag"') self.post_task() source = self.sources[0].key.get() self.assertEqual('"my etag"', source.last_activities_etag) source.last_polled = util.EPOCH source.put() self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag='"my etag"', min_id='c', cache=mox.IgnoreArg(), ).AndReturn({'items': [], 'etag': '"new etag"'}) self.mox.ReplayAll() self.post_task() source = self.sources[0].key.get() self.assertEqual('"new etag"', source.last_activities_etag)
def test_site_specific_disable_sources(self): """HTTP 401 and 400 '' for Instagram should disable the source.""" try: for err in (urllib2.HTTPError('url', 401, 'msg', {}, StringIO.StringIO('body')), InstagramAPIError('400', 'OAuthAccessTokenException', 'foo'), AccessTokenRefreshError('invalid_grant'), ): self.mox.UnsetStubs() self.setUp() self.mox.StubOutWithMock(FakeSource, 'get_activities_response') FakeSource.get_activities_response( count=mox.IgnoreArg(), fetch_replies=True, fetch_likes=True, fetch_shares=True, etag=None, min_id=None, cache=mox.IgnoreArg(), ).AndRaise(err) self.mox.ReplayAll() self.post_task() source = self.sources[0].key.get() self.assertEqual('disabled', source.status) finally: self.mox.UnsetStubs()