Skip to content

Commit

Permalink
[test] add unittest for asterisk 1.4 version
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Bour committed Jul 29, 2012
1 parent abe10bb commit bcdc36c
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
115 changes: 115 additions & 0 deletions tests/test_decoding.py
@@ -0,0 +1,115 @@
# -*- coding: utf8 -*-
# vim:syntax=python:sw=4:ts=4:expandtab
"""
pyajam, Python Asterisk AJAM binding
Copyright (C) 2009-2012 Guillaume Bour <guillaume@bour.cc>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import unittest
import os
import os.path

from pyajam import Pyajam


def fake_query(_path, _action):
"""We fake the code querying Asterisk AJAM interface
_qq() read the file traces/${ersion}/${action}
this file must contains what AJAM would return (including status code and HTTP headers)
"""
def _qq(mode, action, args={}):
headers = dict()
content = None

if action == 'command':
action = args['command']

with open(os.path.join(_path, action)) as f:
# first line is HTTP
f.readline()

while True:
l = f.readline()
if l == '\r\n':
break

(key, value) = l[:-1].split(':', 1)
if key.lower() == 'set-cookie':
key = key.lower()
headers[key] = value.strip()

content = f.read()
return (headers, content)
return _qq

class TestDecoding(unittest.TestCase):
def __init__(self, name, version=None):
super(TestDecoding, self).__init__(name)
self.version = version
self.path = os.path.join(os.getcwd(), "traces", version)

def setUp(self):
self.ajam = Pyajam()
self.ajam._query = fake_query(self.path, self._testMethodName.split('_')[1])

def tearDown(self):
self.ajam = None

def test_login(self):
self.assertTrue(self.ajam.login())
self.assertEqual(self.ajam.version(), self.version)
self.assertEqual(self.ajam._sessionid_, '3ac1e27b')

def test_sippeers(self):
peers = self.ajam.sippeers()
self.assertEqual(len(peers), 2)
self.assertTrue(peers[0].get('objectname', 'none') in ['101','102'])

def test_iaxpeers(self):
peers = self.ajam.iaxpeers()
self.assertEqual(len(peers), 1)
self.assertEqual(peers[0].get('objectname','none'), 'demo')

def test_peers(self):
peers = self.ajam.peers()
self.assertEqual(len(peers), 3)
self.assertEqual(len([x for x in peers if x['channeltype'] == 'SIP']), 2)
self.assertEqual(len([x for x in peers if x['channeltype'] == 'IAX2']), 1)

def test_sipregistry(self):
regs = self.ajam.sipregistry()
self.assertEqual(len(regs), 1)
self.assertEqual(regs[0]['username'], 'myISP')

def test_peer(self):
peer = self.ajam.sippeer('101')
self.assertTrue(isinstance(peer, dict))
self.assertEqual(peer.get('name',None), '101')
self.assertEqual(peer.get('status',None), 'Unmonitored')


if __name__ == '__main__':
testnames = unittest.TestLoader().getTestCaseNames(TestDecoding)

for vers in ['1.4']:
print "** with asterisk 1.4 **"

suite = unittest.TestSuite()
for name in testnames:
suite.addTest(TestDecoding(name, vers))

unittest.TextTestRunner(verbosity=2).run(suite)

13 changes: 13 additions & 0 deletions tests/traces/1.4/iaxpeers
@@ -0,0 +1,13 @@
HTTP/1.1 200 OK
Server: Asterisk/1.4.44
Date: Mon, 23 Jul 2012 13:46:30 GMT
Connection: close
Cache-Control: no-cache, no-store
Content-type: text/plain
Set-Cookie: mansession_id="6b8802a1"; Version=1; Max-Age=999

Name/Username Host Mask Port Status
demo/asterisk 216.207.245.47 (S) 255.255.255.255 4569 Unmonitored
1 iax2 peers [0 online, 0 offline, 1 unmonitored]


17 changes: 17 additions & 0 deletions tests/traces/1.4/login
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Connection: close
Date: Mon, 23 Jul 2012 09:54:14 GMT
Server: Asterisk/1.4.44
Content-Type: text/html
Client-Date: Mon, 23 Jul 2012 09:54:15 GMT
Client-Peer: 127.0.0.1:8088
Client-Response-Num: 1
Set-Cookie: mansession_id="3ac1e27b"; Version=1; Max-Age=60
Title: Asterisk™ Manager Interface

<title>Asterisk&trade; Manager Interface</title><body bgcolor="#ffffff"><table align=center bgcolor="#f1f1f1" width="500">
<tr><td colspan="2" bgcolor="#f1f1ff"><h1>&nbsp;&nbsp;Manager Tester</h1></td></tr>
<tr><td>Response</td><td>Success</td></tr>
<tr><td>Message</td><td>Authentication accepted</td></tr>
</table></body>
60 changes: 60 additions & 0 deletions tests/traces/1.4/sip show peer 101
@@ -0,0 +1,60 @@
HTTP/1.1 200 OK
Server: Asterisk/1.4.44
Date: Mon, 23 Jul 2012 13:54:34 GMT
Connection: close
Cache-Control: no-cache, no-store
Content-type: text/plain
Set-Cookie: mansession_id="04803839"; Version=1; Max-Age=999

Response: Follows
Privilege: Command


* Name : 101
Secret : <Not set>
MD5Secret : <Not set>
Context : default
Subscr.Cont. : <Not set>
Language :
AMA flags : Unknown
Transfer mode: open
CallingPres : Presentation Allowed, Not Screened
Callgroup :
Pickupgroup :
Mailbox :
VM Extension : asterisk
LastMsgsSent : 32767/65535
Call limit : 0
Dynamic : Yes
Callerid : "" <>
MaxCallBR : 384 kbps
Expire : -1
Insecure : no
Nat : Always
ACL : No
T38 pt UDPTL : No
CanReinvite : Yes
PromiscRedir : No
User=Phone : No
Video Support: No
Trust RPID : No
Send RPID : No
Subscriptions: Yes
Overlap dial : No
Forward Loop : Yes
DTMFmode : rfc2833
LastMsg : 0
ToHost :
Addr->IP : (Unspecified) Port 0
Defaddr->IP : 0.0.0.0 Port 5060
Def. Username:
SIP Options : (none)
Codecs : 0x8000e (gsm|ulaw|alaw|h263)
Codec Order : (none)
Auto-Framing: No
Status : Unmonitored
Useragent :
Reg. Contact :

--END COMMAND--

14 changes: 14 additions & 0 deletions tests/traces/1.4/sip show registry
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK
Server: Asterisk/1.4.44
Date: Mon, 23 Jul 2012 13:53:00 GMT
Connection: close
Cache-Control: no-cache, no-store
Content-type: text/plain
Set-Cookie: mansession_id="04803839"; Version=1; Max-Age=999

Response: Follows
Privilege: Command
Host Username Refresh State Reg.Time
localhost:5060 myISP 120 Request Sent
--END COMMAND--

14 changes: 14 additions & 0 deletions tests/traces/1.4/sippeers
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK
Server: Asterisk/1.4.44
Date: Mon, 23 Jul 2012 12:05:55 GMT
Connection: close
Cache-Control: no-cache, no-store
Content-type: text/xml
Set-Cookie: mansession_id="0bf10631"; Version=1; Max-Age=999

<ajax-response>
<response type='object' id='unknown'><generic response='Success' message='Peer status list will follow' /></response>
<response type='object' id='unknown'><generic event='PeerEntry' channeltype='SIP' objectname='102' chanobjecttype='peer' ipaddress='-none-' ipport='0' dynamic='yes' natsupport='yes' videosupport='no' acl='no' status='Unmonitored' realtimedevice='no' /></response>
<response type='object' id='unknown'><generic event='PeerEntry' channeltype='SIP' objectname='101' chanobjecttype='peer' ipaddress='-none-' ipport='0' dynamic='yes' natsupport='yes' videosupport='no' acl='no' status='Unmonitored' realtimedevice='no' /></response>
<response type='object' id='unknown'><generic event='PeerlistComplete' listitems='2' /></response>
</ajax-response>

0 comments on commit bcdc36c

Please sign in to comment.