예제 #1
0
파일: iomux.py 프로젝트: nejucomo/iomux
    def test_input_splitter(self):
        m_sbuf = self._make_mock()
        m_lbuf = self._make_mock()

        splitter = InputSplitter(m_sbuf, m_lbuf)

        splitter.write_data('foo')

        self._assertCallsEqual(
            m_sbuf,
            [call.enqueue_data('foo')])

        self._assertCallsEqual(
            m_lbuf,
            [call.write_data('foo')])

        self._reset_mocks()

        splitter.finish()

        self._assertCallsEqual(
            m_sbuf,
            [])

        self._assertCallsEqual(
            m_lbuf,
            [call.finish()])
예제 #2
0
    def _make_request(
            self,
            method, postpath, reqbody,
            resreadsreq, rescode, resbody):

        m_request = MagicMock(name='Request')
        m_request.method = method
        m_request.postpath = postpath
        if reqbody is None:
            readrv = ''
        elif reqbody == 'mangled JSON':
            readrv = reqbody
        else:
            readrv = json.dumps(reqbody, indent=2)

        m_request.content.read.return_value = readrv

        r = self.tar.render(m_request)

        self.assertEqual(r, server.NOT_DONE_YET)

        expected = [
            call.setResponseCode(rescode),
            call.setHeader('Content-Type', 'application/json'),
            call.write(json.dumps(resbody, indent=2)),
            call.finish(),
        ]

        if resreadsreq:
            expected.insert(0, call.content.read())

        check_mock(self, m_request, expected)
예제 #3
0
    def test_render_GET(self):
        r = self.res.render_GET(self.m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            self.m_handle_event,
            [])

        self.assert_calls_equal(
            self.m_request,
            [call.setResponseCode(403, 'FORBIDDEN'),
             call.finish()])
예제 #4
0
    def test_render_GET(self):
        m_request = self._setup_mock_request('fake request type', [])
        r = self.res.render_GET(m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            self.m_handle_push_tag,
            [])

        self.assert_calls_equal(
            m_request,
            [call.setResponseCode(403, 'FORBIDDEN'),
             call.finish()])
예제 #5
0
파일: iomux.py 프로젝트: nejucomo/iomux
    def test_read_close(self):
        m_select = self._patch('select.select')
        m_read = self._patch('os.read')

        rfd = 42
        m_out = self._make_mock()
        m_select.return_value = ([rfd], [], [])
        m_read.return_value = ''

        self.m.add_source(rfd, m_out, permanent=False)
        cont = self.m.process_events()

        self._assertCallsEqual(m_select, [call([rfd], [], [], SELECT_INTERVAL)])
        self._assertCallsEqual(m_read, [call.read(rfd, BUFSIZE)])
        self._assertCallsEqual(m_out, [call.finish()])
        self.assertEqual(False, cont)
예제 #6
0
    def test_render_POST_ping_tampered(self):
        tweakedmessage = self.pingmessage.copy()
        tweakedmessage['hook_id'] += 1

        self.m_request.content.getvalue.return_value = json.dumps(tweakedmessage)

        r = self.res.render_POST(self.m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            self.m_request,
            [call.getHeader('X-Hub-Signature'),
             call.content.getvalue(),
             call.setResponseCode(403, 'FORBIDDEN'),
             call.finish()])

        self.assert_calls_equal(
            self.m_loghandler,
            [call.handle(ArgIsLogRecord(levelname='WARNING'))])
예제 #7
0
    def test_render_POST_ping(self):
        r = self.res.render_POST(self.m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            self.m_request,
            [call.getHeader('X-Hub-Signature'),
             call.content.getvalue(),
             call.getHeader('X-Github-Event'),
             call.getHeader('X-Github-Delivery'),
             call.setResponseCode(200, 'OK'),
             call.finish()])

        self.assert_calls_equal(
            self.m_handle_event,
            [call('a fake unique id', 'ping', self.pingmessage)])

        self.assert_calls_equal(
            self.m_loghandler,
            [])
예제 #8
0
    def test_render_POST_unhandled_event_type(self):
        m_request = self._setup_mock_request('unknown event type', [])

        r = self.res.render_POST(m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            m_request,
            [call.getHeader('X-Hub-Signature'),
             call.content.getvalue(),
             call.getHeader('X-Github-Event'),
             call.getHeader('X-Github-Delivery'),
             call.setResponseCode(400, 'Event Not Supported'),
             call.finish()])

        self.assert_calls_equal(
            self.m_loghandler,
            [call.handle(
                ArgIsLogRecord(
                    levelname='INFO',
                    msg='Unhandled github %r event %r.'))])
예제 #9
0
    def test_render_POST_ping_tampered(self):
        m_request = self._setup_mock_request('ping', self.pingmessage)

        tweakedmessage = self.pingmessage.copy()
        tweakedmessage['hook_id'] += 1

        m_request.content.getvalue.return_value = \
            json.dumps(tweakedmessage)

        r = self.res.render_POST(m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            m_request,
            [call.getHeader('X-Hub-Signature'),
             call.content.getvalue(),
             call.setResponseCode(403, 'FORBIDDEN'),
             call.finish()])

        self.assert_calls_equal(
            self.m_loghandler,
            [call.handle(ArgIsLogRecord(levelname='WARNING'))])
예제 #10
0
    def test_render_POST_ping(self):
        m_request = self._setup_mock_request('ping', self.pingmessage)

        r = self.res.render_POST(m_request)

        self.assertEqual(NOT_DONE_YET, r)
        self.assert_calls_equal(
            m_request,
            [call.getHeader('X-Hub-Signature'),
             call.content.getvalue(),
             call.getHeader('X-Github-Event'),
             call.getHeader('X-Github-Delivery'),
             call.setResponseCode(200, 'OK'),
             call.finish()])

        # Pings do not reach the push handler:
        self.assert_calls_equal(
            self.m_handle_push_tag,
            [])

        self.assert_calls_equal(
            self.m_loghandler,
            [])