Example #1
0
 def test_init_full(self):
     cmd = adhoc_xso.Command(
         node="foo",
         action=adhoc_xso.ActionType.COMPLETE,
         status=adhoc_xso.CommandStatus.EXECUTING,
         sessionid="foobar",
         payload=[
             unittest.mock.sentinel.payload1,
             unittest.mock.sentinel.payload2,
         ],
         notes=[
             unittest.mock.sentinel.note1,
             unittest.mock.sentinel.note2,
         ],
         actions=unittest.mock.sentinel.actions,
     )
     self.assertEqual(cmd.node, "foo")
     self.assertEqual(cmd.action, adhoc_xso.ActionType.COMPLETE)
     self.assertEqual(cmd.status, adhoc_xso.CommandStatus.EXECUTING)
     self.assertEqual(cmd.sessionid, "foobar")
     self.assertSequenceEqual(cmd.payload, [
         unittest.mock.sentinel.payload1,
         unittest.mock.sentinel.payload2,
     ])
     self.assertSequenceEqual(cmd.notes, [
         unittest.mock.sentinel.note1,
         unittest.mock.sentinel.note2,
     ])
     self.assertEqual(cmd.actions, unittest.mock.sentinel.actions)
     self.assertEqual(cmd.first_payload, unittest.mock.sentinel.payload1)
Example #2
0
    def test__handle_command_raises_forbidden_for_disallowed_node(self):
        handler = CoroutineMock()
        handler.return_value = unittest.mock.sentinel.result
        is_allowed = unittest.mock.Mock()
        is_allowed.return_value = False

        self.s.register_stateless_command(
            "node",
            "Command name",
            handler,
            is_allowed=is_allowed,
        )

        req = aioxmpp.IQ(type_=aioxmpp.IQType.SET,
                         from_=TEST_PEER_JID,
                         to=TEST_LOCAL_JID,
                         payload=adhoc_xso.Command("node", ))

        with self.assertRaises(aioxmpp.errors.XMPPCancelError) as ctx:
            run_coroutine(self.s._handle_command(req))

        is_allowed.assert_called_once_with(req.from_)

        self.assertEqual(
            ctx.exception.condition,
            aioxmpp.ErrorCondition.FORBIDDEN,
        )

        handler.assert_not_called()
Example #3
0
    def test__handle_command_dispatches_to_command(self):
        handler = CoroutineMock()
        handler.return_value = unittest.mock.sentinel.result

        self.s.register_stateless_command(
            "node",
            "Command name",
            handler,
        )

        req = aioxmpp.IQ(
            type_=aioxmpp.IQType.SET,
            from_=TEST_PEER_JID,
            to=TEST_LOCAL_JID,
            payload=adhoc_xso.Command(
                "node",
            )
        )

        result = run_coroutine(self.s._handle_command(req))

        handler.assert_called_once_with(req)

        self.assertEqual(
            result,
            unittest.mock.sentinel.result,
        )
Example #4
0
 def test_init(self):
     cmd = adhoc_xso.Command(node="foo")
     self.assertEqual(cmd.node, "foo")
     self.assertEqual(cmd.action, adhoc_xso.ActionType.EXECUTE)
     self.assertIsNone(cmd.status)
     self.assertIsNone(cmd.sessionid)
     self.assertSequenceEqual(cmd.payload, [])
     self.assertSequenceEqual(cmd.notes, [])
     self.assertIsNone(cmd.actions)
     self.assertIsNone(cmd.first_payload)
Example #5
0
    def test_init_single_payload(self):
        cmd = adhoc_xso.Command(
            "foo",
            payload=unittest.mock.sentinel.payload1,
        )

        self.assertSequenceEqual(cmd.payload, [
            unittest.mock.sentinel.payload1,
        ])

        self.assertEqual(cmd.first_payload, unittest.mock.sentinel.payload1)
Example #6
0
    def test__handle_command_raises_item_not_found_for_unknown_node(self):
        req = aioxmpp.IQ(type_=aioxmpp.IQType.SET,
                         from_=TEST_PEER_JID,
                         to=TEST_LOCAL_JID,
                         payload=adhoc_xso.Command("node", ))

        with self.assertRaises(aioxmpp.errors.XMPPCancelError) as ctx:
            run_coroutine(self.s._handle_command(req))

        self.assertEqual(
            ctx.exception.condition,
            aioxmpp.ErrorCondition.ITEM_NOT_FOUND,
        )

        self.assertRegex(ctx.exception.text, "no such command: 'node'")
Example #7
0
 def test_init_default(self):
     with self.assertRaisesRegex(TypeError,
                                 r"required positional argument: 'node'"):
         adhoc_xso.Command()