def test_open_request_resource_constraint(self):
        open_ = ibb_xso.Open()
        open_.sid = "sentinel"
        open_.block_size = 1 << 32
        open_.stanza = ibb_xso.IBBStanzaType.IQ

        iq = aioxmpp.IQ(aioxmpp.IQType.SET, from_=TEST_JID1, payload=open_)
        try:
            run_coroutine(self.s._handle_open_request(iq))
        except aioxmpp.errors.XMPPModifyError as e:
            self.assertEqual(e.condition,
                             aioxmpp.errors.ErrorCondition.RESOURCE_CONSTRAINT)
        else:
            self.fail("missing expected exception")
    def test_open_request_enforce_limit(self):
        open_ = ibb_xso.Open()
        open_.sid = "sentinel"
        open_.block_size = 8192
        open_.stanza = ibb_xso.IBBStanzaType.IQ

        iq = aioxmpp.IQ(aioxmpp.IQType.SET, from_=TEST_JID1, payload=open_)
        try:
            run_coroutine(self.s._handle_open_request(iq))
        except aioxmpp.errors.XMPPCancelError as e:
            self.assertEqual(e.condition,
                             aioxmpp.errors.ErrorCondition.NOT_ACCEPTABLE)
        else:
            self.fail("missing expected exception")
    def test_open_request_allowed_limit(self):
        self.s.session_limit = 1
        self.s.default_protocol_factory = unittest.mock.Mock()

        def on_connection_accepted(transport, protocol):
            pass

        self.s.on_session_accepted.connect(on_connection_accepted)

        open_ = ibb_xso.Open()
        open_.sid = "sentinel"
        open_.block_size = 8192
        open_.stanza = ibb_xso.IBBStanzaType.IQ

        iq = aioxmpp.IQ(aioxmpp.IQType.SET, from_=TEST_JID1, payload=open_)

        run_coroutine(self.s._handle_open_request(iq))
    def test_open_request_established_session(self):
        self.s.session_limit = 2
        self.s.default_protocol_factory = unittest.mock.Mock()
        on_session_accepted = unittest.mock.Mock()
        self.s.on_session_accepted.connect(on_session_accepted)

        open_ = ibb_xso.Open()
        open_.sid = self.handle.get_extra_info("sid")
        open_.block_size = 4096
        open_.stanza = ibb_xso.IBBStanzaType.IQ

        iq = aioxmpp.IQ(aioxmpp.IQType.SET, from_=TEST_JID1, payload=open_)

        try:
            run_coroutine(self.s._handle_open_request(iq))
        except aioxmpp.errors.XMPPCancelError as e:
            self.assertEqual(e.condition,
                             aioxmpp.errors.ErrorCondition.NOT_ACCEPTABLE)

        self.assertEqual(len(on_session_accepted.mock_calls), 0)
    def test_open_request_expected_session(self):
        protocol_factory = unittest.mock.Mock()
        s_fut = self.s.expect_session(protocol_factory, TEST_JID1, "sentinel")

        open_ = ibb_xso.Open()
        open_.sid = "sentinel"
        open_.block_size = 8192
        open_.stanza = ibb_xso.IBBStanzaType.IQ

        iq = aioxmpp.IQ(aioxmpp.IQType.SET, from_=TEST_JID1, payload=open_)
        run_coroutine(self.s._handle_open_request(iq))

        async def await_s_fut():
            return await s_fut

        handle, proto = run_coroutine(await_s_fut())
        self.assertEqual(handle.get_extra_info("peer_jid"), TEST_JID1)
        self.assertEqual(handle.get_extra_info("sid"), "sentinel")
        self.assertEqual(handle.get_extra_info("block_size"), 8192)

        protocol_factory().connection_made.assert_called_with(handle)