def _startAndLoginMD5(self): """Helper to start the protocol and do most of an MD5 login""" self.protocol.started = True d = self.protocol.loginMD5('username', 'secret') fields = disassembleMessage(self.transport.value()) self.transport.clear() self.assertEqual(fields['action'], 'Challenge') self.assertIn('actionid', fields) self.assertEqual(fields['authtype'], 'MD5') self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + fields['actionid'] + '\r\n' 'Challenge: foo\r\n' '\r\n' ) fields = disassembleMessage(self.transport.value()) self.assertEqual(fields['action'], 'Login') self.assertIn('actionid', fields) self.assertEqual(fields['authtype'], 'MD5') self.assertEqual(fields['username'], 'username') self.assertEqual(fields['key'], md5('foo' + 'secret').hexdigest()) return d, fields
def test_AGIHangupFailed(self): """Fail to hang up the channel""" channel = self._spawnChannel() d = channel.sendAGIHangup() message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'HANGUP') self.assertIn('commandid', message) hangupFailed = Mock() d.addErrback(hangupFailed) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(hangupFailed.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' '' + quote('200 result=0\n') + '' '\r\n' '\r\n' ) self.assertEqual(len(hangupFailed.mock_calls), 1)
def test_AGIHangup(self): """Successfully send an AGI hangup""" channel = self._spawnChannel() d = channel.sendAGIHangup() message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'HANGUP') self.assertIn('commandid', message) cbSuccess = Mock() d.addCallback(cbSuccess) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' + quote('200 result=1\n') + '\r\n' '\r\n' ) cbSuccess.assert_called_once_with(1)
def test_AGIExecPlaybackBackground(self): """Successfully execute Playback in background mode""" channel = self._spawnChannel() d = channel.sendAGIExecPlayback('hello-world', background=True) message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'EXEC Background hello-world') self.assertIn('commandid', message) cbSuccess = Mock() d.addCallback(cbSuccess) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' + quote('200 result=48\n') + '\r\n' '\r\n' ) cbSuccess.assert_called_once_with(48)
def test_AGIExecPlaybackFailed(self): """Fail to execute the Playback command""" channel = self._spawnChannel() d = channel.sendAGIExecPlayback('hello-world') message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'EXEC Playback hello-world') self.assertIn('commandid', message) playbackFailed = Mock() d.addErrback(playbackFailed) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(playbackFailed.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' '' + quote('200 result=-1\n') + '' '\r\n' '\r\n' ) self.assertEqual(len(playbackFailed.mock_calls), 1)
def test_AGIInvalid(self): """Fail to run an invalid AGI command""" channel = self._spawnChannel() d = channel.sendAGI('FOO') message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'FOO') self.assertIn('commandid', message) cbSuccess = Mock() d.addCallback(cbSuccess) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' + quote('510 Invalid or unknown command\n') + '\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.assertFailure(d, AGIException) return d
def test_AGI(self): """Successfully run an AGI command""" channel = self._spawnChannel() d = channel.sendAGI('EXEC Playback hello-world') message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'AGI') self.assertIn('actionid', message) self.assertEqual(message['command'], 'EXEC Playback hello-world') self.assertIn('commandid', message) cbSuccess = Mock() d.addCallback(cbSuccess) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.protocol.dataReceived( 'Event: AsyncAGI\r\n' 'SubEvent: Exec\r\n' 'Channel: Foo/202-0\r\n' 'CommandID: ' + message['commandid'] + '\r\n' 'Result: ' + quote('200 result=0 foo=bar\n') + '\r\n' '\r\n' ) cbSuccess.assert_called_once_with((0, {'foo': 'bar'}))
def _setUpOriginateAsyncAGI(self): channel = self._spawnChannel() d = self.protocol.originateAsyncAGI('Foo/202') message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'Originate') self.assertIn('actionid', message) self.assertEqual(message['application'], 'AGI') self.assertEqual(message['data'], 'agi:async') self.assertEqual(message['async'], 'true') varName, octoId = message['variable'].split('=') self.assertEqual(varName, 'AsyncOrigId') self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + message['actionid'] + '\r\n' 'Message: Originate successfully queued\r\n' '\r\n' 'Event: VarSet\r\n' 'Channel: Foo/202-0\r\n' 'Variable: AsyncOrigId\r\n' 'Value: ' + octoId + '\r\n' 'Uniqueid: 1234567890.0\r\n' '\r\n' ) return d, channel, message
def test_sendActionOnChannel(self): """Send an action on a channel""" channel = self._startAndSpawnChannel() d = channel.sendAction( 'Bar', {'key': 'Value', 'key2': 'Value2'} ) fields = disassembleMessage(self.transport.value()) self.assertEqual(fields['action'], 'Bar') self.assertIn('actionid', fields) self.assertEqual(fields['key'], 'Value') self.assertEqual(fields['key2'], 'Value2') self.assertEqual(fields['channel'], 'Foo/202-0')
def test_originateWithCallerId(self): """Originate an AsyncAGI call with caller ID Only tests the Caller ID kwarg. The remainder of the originate functionality is tested by test_originateAsyncAGI. """ channel = self._spawnChannel() d = self.protocol.originateAsyncAGI('Foo/202', callerId='Bar <303>') message = disassembleMessage(self.transport.value()) self.assertEqual(message['action'], 'Originate') self.assertIn('actionid', message) self.assertEqual(message['application'], 'AGI') self.assertEqual(message['data'], 'agi:async') self.assertEqual(message['async'], 'true') self.assertEqual(message['callerid'], 'Bar <303>')
def test_originateCEP(self): """Originate a call into a context/exten/priority""" self._startAndSpawnChannel() d = self.protocol.originateCEP('Foo/202', 'context', 'exten', 1) fields = disassembleMessage(self.transport.value()) self.assertEqual(fields['action'], 'Originate') self.assertIn('actionid', fields) self.assertEqual(fields['channel'], 'Foo/202') self.assertEqual(fields['context'], 'context') self.assertEqual(fields['exten'], 'exten') self.assertEqual(fields['priority'], '1') self.assertEqual(fields['async'], 'true') cbSuccess = Mock() d.addCallback(cbSuccess) self.protocol.dataReceived( 'Response: Success\r\n' 'ActionID: ' + fields['actionid'] + '\r\n' 'Message: Originate successfully queued\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 0) self.protocol.dataReceived( 'Event: OriginateResponse\r\n' 'ActionID: ' + fields['actionid'] + '\r\n' 'Channel: Foo/202-0\r\n' 'Context: context\r\n' 'Exten: exten\r\n' 'Priority: 1\r\n' 'Response: Success\r\n' 'Uniqueid: 1234567890.0\r\n' '\r\n' ) self.assertEqual(len(cbSuccess.mock_calls), 1)