def setUp(self): self.stateMngr = Mock() self.pageMngr = Mock() self.chdlr = CommandHandler('testNode1', self.stateMngr, self.pageMngr) self.next_hdlr = CommandHandler('testNode2', self.stateMngr, self.pageMngr) self.chdlr.successor = self.next_hdlr self.iq = iq() self.iq.addChild(cmd())
class CommandHandlerTests(unittest.TestCase): timeout = 2 def setUp(self): self.stateMngr = Mock() self.pageMngr = Mock() self.chdlr = CommandHandler('testNode1', self.stateMngr, self.pageMngr) self.next_hdlr = CommandHandler('testNode2', self.stateMngr, self.pageMngr) self.chdlr.successor = self.next_hdlr self.iq = iq() self.iq.addChild(cmd()) def tearDown(self): pass def test_process_iq(self): self.next_hdlr.process_iq = Mock() self.chdlr.can_process = Mock(return_value = True) self.chdlr._process = Mock(side_effect=utils.good_side_effect('hello')) iq = self.iq d = self.chdlr.process_iq(iq) self.chdlr.can_process.assertCalledOnceWith(iq) self.chdlr._process.assertCalledOnceWith(iq) self.assertFalse(self.next_hdlr.process_iq.called) def check(val): self.assertEquals(val, 'hello') d.addCallback(check) return d def test_process_iq_cantprocess(self): self.next_hdlr.process_iq = Mock(side_effect=utils.good_side_effect('hello')) self.chdlr.can_process = Mock(return_value = False) self.chdlr._process = Mock() iq = self.iq d = self.chdlr.process_iq(iq) self.assertTrue(self.chdlr.can_process.called) self.assertFalse(self.chdlr._process.called) self.assertTrue(self.next_hdlr.process_iq.called) def check(val): self.assertEquals(val, 'hello') d.addCallback(check) return d def test_process_iq_nosuccessor(self): self.successor = None self.chdlr.can_process = Mock(return_value = False) self.chdlr._process = Mock() iq = self.iq d = self.chdlr.process_iq(iq) self.assertTrue(self.chdlr.can_process.called) self.assertFalse(self.chdlr._process.called) self.assertTrue(d is None) def test__process(self): s = dict() self.chdlr._parse_iq = Mock(return_value=s) self.chdlr._manage_state = Mock(return_value=s) iq = self.iq response = xmlstream.toResponse(iq) self.chdlr._make_next_iq = Mock(side_effect=utils.good_side_effect(response)) self.pageMngr.ready = Mock(return_value=True) self.stateMngr.commit = Mock(side_effect=utils.good_side_effect('Committed stuff')) self.pageMngr.reset = Mock() self.stateMngr.reset = Mock() d = self.chdlr._process(iq) def cb(msg): self.chdlr._parse_iq.assert_called_once_with(iq) self.chdlr._manage_state.assert_called_once_with(s) self.chdlr._make_next_iq.assert_called_once_with(iq, s) self.assertEquals(msg, response) d.addCallback(cb) return d def test__process_parseThrows(self): self.chdlr._parse_iq = Mock(side_effect=ValueError('%s: No Collab forms' % self.__class__)) self.chdlr._manage_state = Mock() self.chdlr._make_next_iq = Mock() iq = self.iq d = self.chdlr._process(iq) self.assertTrue(self.chdlr._parse_iq.called) self.assertFalse(self.chdlr._manage_state.called) self.assertFalse(self.chdlr._make_next_iq.called) def cb(msg): err = makeErr('%s: No Collab forms' % self.__class__, iq) self.assertEquals(msg.toXml(), err.toXml()) d.addCallback(cb) return d def test__process_manageStateThrows(self): self.chdlr._parse_iq = Mock() self.chdlr._manage_state = Mock(side_effect=EmptyStateManagerError('%s: Cannot revert to previous state' % self.__class__)) self.chdlr._make_next_iq = Mock() iq = self.iq d = self.chdlr._process(iq) self.assertTrue(self.chdlr._parse_iq.called) self.assertTrue(self.chdlr._manage_state.called) self.assertFalse(self.chdlr._make_next_iq.called) def cb(msg): err = makeErr('%s: Cannot revert to previous state' % self.__class__, iq) self.assertEquals(msg.toXml(), err.toXml()) d.addCallback(cb) return d def test__process_makeNextIqThrows(self): self.chdlr._parse_iq = Mock() self.chdlr._manage_state = Mock() self.chdlr._make_next_iq = Mock(side_effect=utils.bad_side_effect(PageManagerError('%s: Massive corn clog in port seven' % self.__class__))) iq = self.iq d = self.chdlr._process(iq) self.assertTrue(self.chdlr._parse_iq.called) self.assertTrue(self.chdlr._manage_state.called) self.assertTrue(self.chdlr._make_next_iq.called) def cb(msg): f = failure.Failure('%s: Massive corn clog in port seven' % self.__class__, PageManagerError) err = makeErr(str(f), iq) self.assertEquals(msg.toXml(), err.toXml()) d.addCallback(cb) return d def test__process_readyToCommitOK(self): self.chdlr._parse_iq = Mock() self.chdlr._manage_state = Mock() response = xmlstream.toResponse(self.iq) self.chdlr._make_next_iq = Mock(side_effect=utils.good_side_effect(response)) self.pageMngr.ready = Mock(return_value=True) self.stateMngr.commit = Mock(side_effect=utils.good_side_effect('Committed stuff')) self.pageMngr.reset = Mock() self.stateMngr.reset = Mock() def checkIt(msg): self.assertTrue(self.pageMngr.ready.called) self.assertTrue(self.stateMngr.commit.called) self.assertTrue(self.pageMngr.reset.called) self.assertTrue(self.stateMngr.reset.called) self.assertEquals(msg, response) d = self.chdlr._process(self.iq) d.addCallback(checkIt) return d def test__process_readyToCommitNotOK_stanzaError(self): self.chdlr._parse_iq = Mock() self.chdlr._manage_state = Mock() response = xmlstream.toResponse(iq()) response.addChild(cmd()) self.chdlr._make_next_iq = Mock(side_effect=utils.good_side_effect(response)) self.pageMngr.ready = Mock(return_value=True) self.stateMngr.commit = Mock(side_effect=utils.bad_side_effect(error.StanzaError('%s: Committed fail' % self.__class__))) self.pageMngr.reset = Mock() self.stateMngr.reset = Mock() def checkIt(msg): self.assertTrue(self.pageMngr.ready.called) self.assertTrue(self.stateMngr.commit.called) self.assertTrue(self.pageMngr.reset.called) self.assertTrue(self.stateMngr.reset.called) self.assertEquals(msg, response) self.assertTrue(msg.firstChildElement().firstChildElement().name, 'note') d = self.chdlr._process(self.iq) d.addCallback(checkIt) return d def test__process_readyToCommitNotOK_otherError(self): self.chdlr._parse_iq = Mock() self.chdlr._manage_state = Mock() response = xmlstream.toResponse(iq()) response.addChild(cmd()) self.chdlr._make_next_iq = Mock(side_effect=utils.good_side_effect(response)) self.pageMngr.ready = Mock(return_value=True) self.stateMngr.commit = Mock(side_effect=utils.bad_side_effect(ValueError('%s: Committed fail' % self.__class__))) self.pageMngr.reset = Mock() self.stateMngr.reset = Mock() def checkIt(msg): self.assertTrue(self.pageMngr.ready.called) self.assertTrue(self.stateMngr.commit.called) self.assertTrue(self.pageMngr.reset.called) self.assertTrue(self.stateMngr.reset.called) self.assertNotEquals(msg, response) d = self.chdlr._process(self.iq) d.addCallbacks(lambda _: self.assertTrue(False), checkIt) return d