def test_invalidate_token(self): """Performig causes a call to authenticator.invalidate.""" mock_auth = iMock(ICachingAuthenticator) mock_auth.invalidate.return_value = None eff = Effect(InvalidateToken(mock_auth, 'tenant_id1')) self.assertEqual(sync_perform(get_simple_dispatcher(None), eff), None) mock_auth.invalidate.assert_called_once_with('tenant_id1')
def test_log_none_effectful_fields(self): """ When log is not passed, but there are log fields from BoundFields, the log passed to treq has those fields. """ log = mock_log() # we have to include system='otter' in the expected log here because # the code falls back to otter.log.log, which has the system key bound. expected_log = matches(IsBoundWith(bound='stuff', system='otter')) req = ('GET', 'http://google.com/', None, None, None, { 'log': expected_log }) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/") req.treq = treq req_eff = Effect(req) bound_log_eff = with_log(req_eff, bound='stuff') dispatcher = ComposedDispatcher( [get_simple_dispatcher(None), get_log_dispatcher(log, {})]) self.assertEqual( self.successResultOf(perform(dispatcher, bound_log_eff)), (response, "content"))
def test_authenticate(self): """Performing causes a call to authenticator.authenticate_tenant.""" result = ('token', {'catalog': 'foo'}) mock_auth = iMock(IAuthenticator) mock_auth.authenticate_tenant.return_value = succeed(result) log = object() eff = Effect(Authenticate(mock_auth, 'tenant_id1', log)) self.assertEqual(sync_perform(get_simple_dispatcher(None), eff), result) mock_auth.authenticate_tenant.assert_called_once_with('tenant_id1', log=log)
def test_log(self): """ The log specified in the Request is passed on to the treq implementation. """ log = object() req = ('GET', 'http://google.com/', None, None, None, {'log': log}) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/", log=log) req.treq = treq dispatcher = get_simple_dispatcher(None) self.assertEqual( self.successResultOf(perform(dispatcher, Effect(req))), (response, "content"))
def test_perform(self): """ The Request effect dispatches a request to treq, and returns a two-tuple of the Twisted Response object and the content as bytes. """ req = ('GET', 'http://google.com/', None, None, None, {'log': default_log}) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/") req.treq = treq dispatcher = get_simple_dispatcher(None) self.assertEqual( self.successResultOf(perform(dispatcher, Effect(req))), (response, "content"))
def test_perform(self): """ The Request effect dispatches a request to treq, and returns a two-tuple of the Twisted Response object and the content as bytes. """ req = ('GET', 'http://google.com/', None, None, None, { 'log': default_log }) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/") req.treq = treq dispatcher = get_simple_dispatcher(None) self.assertEqual( self.successResultOf(perform(dispatcher, Effect(req))), (response, "content"))
def test_log_effectful_fields(self): """ The log passed to treq is bound with the fields from BoundFields. """ log = mock_log().bind(duplicate='should be overridden') expected_log = matches(IsBoundWith(duplicate='effectful', bound='stuff')) req = ('GET', 'http://google.com/', None, None, None, {'log': expected_log}) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/", log=log) req.treq = treq req_eff = Effect(req) bound_log_eff = with_log(req_eff, bound='stuff', duplicate='effectful') dispatcher = ComposedDispatcher([ get_simple_dispatcher(None), get_log_dispatcher(log, {})]) self.assertEqual( self.successResultOf(perform(dispatcher, bound_log_eff)), (response, "content"))
def test_log_effectful_fields(self): """ The log passed to treq is bound with the fields from BoundFields. """ log = mock_log().bind(duplicate='should be overridden') expected_log = matches( IsBoundWith(duplicate='effectful', bound='stuff')) req = ('GET', 'http://google.com/', None, None, None, { 'log': expected_log }) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/", log=log) req.treq = treq req_eff = Effect(req) bound_log_eff = with_log(req_eff, bound='stuff', duplicate='effectful') dispatcher = ComposedDispatcher( [get_simple_dispatcher(None), get_log_dispatcher(log, {})]) self.assertEqual( self.successResultOf(perform(dispatcher, bound_log_eff)), (response, "content"))
def test_log_none_effectful_fields(self): """ When log is not passed, but there are log fields from BoundFields, the log passed to treq has those fields. """ log = mock_log() # we have to include system='otter' in the expected log here because # the code falls back to otter.log.log, which has the system key bound. expected_log = matches(IsBoundWith(bound='stuff', system='otter')) req = ('GET', 'http://google.com/', None, None, None, {'log': expected_log}) response = StubResponse(200, {}) treq = StubTreq(reqs=[(req, response)], contents=[(response, "content")]) req = Request(method="get", url="http://google.com/") req.treq = treq req_eff = Effect(req) bound_log_eff = with_log(req_eff, bound='stuff') dispatcher = ComposedDispatcher([ get_simple_dispatcher(None), get_log_dispatcher(log, {})]) self.assertEqual( self.successResultOf(perform(dispatcher, bound_log_eff)), (response, "content"))
def get_dispatcher(self): return get_simple_dispatcher(None)