1 from multiprocessing import Process |
1 from multiprocessing import Process |
2 from sqlalchemy import create_engine, pool |
2 from sqlalchemy import create_engine, pool |
3 import unittest |
3 import unittest |
4 |
4 |
5 |
|
6 from tempfile import mkdtemp |
5 from tempfile import mkdtemp |
7 import shutil |
6 import shutil |
8 |
7 |
|
8 from datetime import datetime |
|
9 |
|
10 import time |
|
11 |
|
12 from xmlrpclib import Server as xServer, ServerProxy, Fault |
|
13 |
9 from iro.model.utils import WithSession |
14 from iro.model.utils import WithSession |
10 from iro.model import POOL_SIZE as DB_POOL_SIZE |
15 from iro.model import POOL_SIZE as DB_POOL_SIZE |
11 |
16 |
12 from iro.model.schema import User, Base, Offer, Userright, Job |
17 from iro.model.schema import User, Base, Offer, Userright, Job, Message |
13 import iro.model.schema as schema |
18 import iro.model.schema as schema |
14 |
19 |
|
20 from iro.main import runReactor |
|
21 |
|
22 import iro.error as IroError |
|
23 |
15 from ngdatabase.mysql import Server, createConfig, Database |
24 from ngdatabase.mysql import Server, createConfig, Database |
16 |
|
17 from iro.main import runReactor |
|
18 |
|
19 import iro.error as IroError |
|
20 |
|
21 import time |
|
22 |
|
23 from xmlrpclib import Server as xServer, ServerProxy, Fault |
|
24 |
25 |
25 class SampleDatabase(Database): |
26 class SampleDatabase(Database): |
26 def createPassword(self): |
27 def createPassword(self): |
27 self.password="test" |
28 self.password="test" |
28 return self.password |
29 return self.password |
67 self.failUnlessEqual(ret,'hello') |
68 self.failUnlessEqual(ret,'hello') |
68 |
69 |
69 def testListMethods(self): |
70 def testListMethods(self): |
70 '''list of all offical Methods, that can be executed''' |
71 '''list of all offical Methods, that can be executed''' |
71 ret=self.__rpc2().listMethods() |
72 ret=self.__rpc2().listMethods() |
72 self.failUnlessEqual(ret, ['listMethods', 'status', 'stop', 'sms', 'fax', 'mail', 'routes', 'defaultRoute', 'statistic', 'telnumber','email']) |
73 self.failUnlessEqual(ret, ['listMethods', 'status', 'stop', 'sms', 'fax', 'mail', 'routes', 'defaultRoute', 'bill', 'telnumber','email']) |
73 |
74 |
74 def testStatus(self): |
75 def testStatus(self): |
75 ''' test the status function''' |
76 ''' test the status function''' |
76 with WithSession(md.engine, autocommit=True) as session: |
77 with WithSession(md.engine, autocommit=True) as session: |
77 u = User(name='test',apikey='abcdef123456789') |
78 u = User(name='test',apikey='abcdef123456789') |
225 with self.assertRaises(Fault) as fault: |
226 with self.assertRaises(Fault) as fault: |
226 self.__rpc2().email([number]) |
227 self.__rpc2().email([number]) |
227 exc = fault.exception |
228 exc = fault.exception |
228 self.failUnlessEqual(exc.faultCode, 702) |
229 self.failUnlessEqual(exc.faultCode, 702) |
229 self.failUnlessEqual(exc.faultString, "No valid email: '%s'"%number) |
230 self.failUnlessEqual(exc.faultString, "No valid email: '%s'"%number) |
230 |
231 |
|
232 def testBill(self): |
|
233 '''test bill function''' |
|
234 apikey='abcdef123456789' |
|
235 with WithSession(md.engine, autocommit=True) as session: |
|
236 u=User(name='test',apikey=apikey) |
|
237 session.add(u) |
|
238 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.0,'anz':0}}) |
|
239 |
|
240 with WithSession(md.engine, autocommit=True) as session: |
|
241 u = session.merge(u) |
|
242 o = Offer(name='sipgate_basic',provider="sipgate",route="basic",typ="sms") |
|
243 j = Job(hash='a1',info='i',status='sended') |
|
244 m = Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.30, offer=o, job=j) |
|
245 u.rights.append(Userright(o)) |
|
246 u.jobs.append(j) |
|
247 session.add(m) |
|
248 |
|
249 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.3,'anz':1}, |
|
250 'sipgate_basic':{'price':0.3,'anz':1,'info':{'i':{'price':0.3,'anz':1}}}}) |
|
251 |
|
252 with WithSession(md.engine, autocommit=True) as session: |
|
253 j = session.merge(j) |
|
254 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.4, offer=o)) |
231 |
255 |
|
256 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.7,'anz':2}, |
|
257 'sipgate_basic':{'price':0.7,'anz':2,'info':{'i':{'price':0.7,'anz':2}}}}) |
|
258 |
|
259 with WithSession(md.engine, autocommit=True) as session: |
|
260 m = session.merge(m) |
|
261 m.isBilled=True |
|
262 |
|
263 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.4,'anz':1}, |
|
264 'sipgate_basic':{'price':0.4,'anz':1,'info':{'i':{'price':0.4,'anz':1}}}}) |
|
265 |
|
266 with WithSession(md.engine, autocommit=True) as session: |
|
267 u = session.merge(u) |
|
268 j = Job(hash='a2',info='a',status='sended') |
|
269 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.4, offer=o)) |
|
270 u.jobs.append(j) |
|
271 ret=self.__rpc2().bill(apikey) |
|
272 self.failUnlessEqual(ret['total'],{'price':0.8,'anz':2}) |
|
273 self.failUnlessEqual(ret['sipgate_basic'], |
|
274 {'price':0.8,'anz':2, |
|
275 'info':{'i':{'price':0.4,'anz':1}, |
|
276 'a':{'price':0.4,'anz':1}, |
|
277 } |
|
278 }) |
|
279 |
|
280 with WithSession(md.engine, autocommit=True) as session: |
|
281 u = session.merge(u) |
|
282 j = Job(hash='a3',info='a',status='sended') |
|
283 o = Offer(name='sipgate_gold',provider="sipgate",route="gold",typ="sms") |
|
284 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.5, offer=o)) |
|
285 u.rights.append(Userright(offer=o)) |
|
286 u.jobs.append(j) |
|
287 |
|
288 ret=self.__rpc2().bill(apikey) |
|
289 self.failUnlessEqual(ret['total'],{'price':1.3,'anz':3}) |
|
290 self.failUnlessEqual(ret['sipgate_gold'], |
|
291 {'price':0.5,'anz':1, |
|
292 'info':{ |
|
293 'a':{'price':0.5,'anz':1}, |
|
294 } |
|
295 }) |
|
296 |
232 def startReactor(engine): |
297 def startReactor(engine): |
233 """starts the Rector with a special debug Clild, so that the reactor can be stopped remotly. """ |
298 """starts the Rector with a special debug Clild, so that the reactor can be stopped remotly. """ |
234 from twisted.internet import reactor |
299 from twisted.internet import reactor |
235 from twisted.web import xmlrpc, resource |
300 from twisted.web import xmlrpc, resource |
236 |
301 |
254 def __init__(self): |
319 def __init__(self): |
255 self.tdir = mkdtemp(prefix='iro-mysql-') |
320 self.tdir = mkdtemp(prefix='iro-mysql-') |
256 self.server = Server('%s/my.cnf'%self.tdir) |
321 self.server = Server('%s/my.cnf'%self.tdir) |
257 self.db = SampleDatabase("test","test",'%s/my.cnf'%self.tdir) |
322 self.db = SampleDatabase("test","test",'%s/my.cnf'%self.tdir) |
258 self.engine = create_engine('mysql://test:test@localhost/test?unix_socket=%s/socket'%self.tdir, |
323 self.engine = create_engine('mysql://test:test@localhost/test?unix_socket=%s/socket'%self.tdir, |
259 poolclass = pool.SingletonThreadPool, pool_size=DB_POOL_SIZE, ) |
324 poolclass = pool.SingletonThreadPool, pool_size=DB_POOL_SIZE, )#echo=True) |
260 |
325 |
261 def setUp(self): |
326 def setUp(self): |
262 with open('%s/my.cnf'%self.tdir,'w') as cnf: |
327 with open('%s/my.cnf'%self.tdir,'w') as cnf: |
263 cnf.write(createConfig(self.tdir)) |
328 cnf.write(createConfig(self.tdir)) |
264 self.server.create() |
329 self.server.create() |