9 |
9 |
10 import time |
10 import time |
11 |
11 |
12 from xmlrpclib import Server as xServer, ServerProxy, Fault |
12 from xmlrpclib import Server as xServer, ServerProxy, Fault |
13 |
13 |
14 from iro.model.utils import WithSession |
|
15 from iro.model import POOL_SIZE as DB_POOL_SIZE |
14 from iro.model import POOL_SIZE as DB_POOL_SIZE |
16 |
15 |
17 from iro.model.schema import User, Base, Offer, Userright, Job, Message |
16 from iro.model.schema import User, Base, Offer, Userright, Job, Message |
18 import iro.model.schema as schema |
|
19 |
17 |
20 from iro.main import runReactor |
18 from iro.main import runReactor |
21 |
19 |
22 import iro.error as IroError |
20 import iro.error as IroError |
23 |
21 |
24 from ngdatabase.mysql import Server, createConfig, Database |
22 from ngdatabase.mysql import Server, createConfig, Database |
|
23 |
|
24 from .dbtestcase import DBTestCase |
25 |
25 |
26 class SampleDatabase(Database): |
26 class SampleDatabase(Database): |
27 def createPassword(self): |
27 def createPassword(self): |
28 self.password="test" |
28 self.password="test" |
29 return self.password |
29 return self.password |
35 import logging |
35 import logging |
36 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
36 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
37 observer = log.PythonLoggingObserver() |
37 observer = log.PythonLoggingObserver() |
38 observer.start() |
38 observer.start() |
39 |
39 |
40 class XMLRPCTest(unittest.TestCase): |
40 class XMLRPCTest(DBTestCase): |
41 """tests for the xmlrpc interface""" |
41 """tests for the xmlrpc interface""" |
42 def setUp(self): |
42 def setUp(self): |
|
43 if not self.engine: |
|
44 self.engine = md.engine |
43 self.s = Process(target=startReactor, args=(md.engine,)) |
45 self.s = Process(target=startReactor, args=(md.engine,)) |
44 self.s.start() |
46 self.s.start() |
45 #the new process needs time to get stated, so this process has to sleep |
47 #the new process needs time to get stated, so this process has to sleep |
46 time.sleep(.2) |
48 time.sleep(.2) |
47 |
49 |
48 def tearDown(self): |
50 def tearDown(self): |
49 self.__debug().stop() |
51 self.__debug().stop() |
50 time.sleep(.2) |
52 time.sleep(.2) |
51 self.s.join() |
53 self.s.join() |
52 self.__cleanDB() |
54 DBTestCase.tearDown(self) |
53 |
|
54 def __cleanDB(self): |
|
55 with WithSession(md.engine, autocommit=True) as session: |
|
56 for table in schema.__tables__: |
|
57 session.query(getattr(schema,table)).delete() |
|
58 |
55 |
59 def __debug(self): |
56 def __debug(self): |
60 return xServer('http://localhost:7080/debug') |
57 return xServer('http://localhost:7080/debug') |
61 |
58 |
62 def __rpc2(self): |
59 def __rpc2(self): |
72 ret=self.__rpc2().listMethods() |
69 ret=self.__rpc2().listMethods() |
73 self.failUnlessEqual(ret, ['listMethods', 'status', 'stop', 'sms', 'fax', 'mail', 'routes', 'defaultRoute', 'bill', 'telnumber','email']) |
70 self.failUnlessEqual(ret, ['listMethods', 'status', 'stop', 'sms', 'fax', 'mail', 'routes', 'defaultRoute', 'bill', 'telnumber','email']) |
74 |
71 |
75 def testStatus(self): |
72 def testStatus(self): |
76 ''' test the status function''' |
73 ''' test the status function''' |
77 with WithSession(md.engine, autocommit=True) as session: |
74 with self.session() as session: |
78 u = User(name='test',apikey='abcdef123456789') |
75 u = User(name='test',apikey='abcdef123456789') |
79 session.add(User(name='test',apikey='abcdef123456789')) |
76 session.add(User(name='test',apikey='abcdef123456789')) |
80 self.failUnlessEqual(self.__rpc2().status('abcdef123456789'), {}) |
77 self.failUnlessEqual(self.__rpc2().status('abcdef123456789'), {}) |
81 |
78 |
82 with WithSession(md.engine, autocommit=True) as session: |
79 with self.session() as session: |
83 u = session.merge(u) |
80 u = session.merge(u) |
84 j = Job(hash="a1", info='info', status="started") |
81 j = Job(hash="a1", info='info', status="started") |
85 j.user=u |
82 j.user=u |
86 session.add(j) |
83 session.add(j) |
87 |
84 |
129 self.failUnlessEqual(exc.faultCode, 700) |
126 self.failUnlessEqual(exc.faultCode, 700) |
130 self.failUnlessEqual(exc.faultString, "Validation of 'apikey' failed.") |
127 self.failUnlessEqual(exc.faultString, "Validation of 'apikey' failed.") |
131 |
128 |
132 def testRoutes(self): |
129 def testRoutes(self): |
133 '''test the route function''' |
130 '''test the route function''' |
134 with WithSession(md.engine, autocommit=True) as session: |
131 with self.session() as session: |
135 u=User(name='test',apikey='abcdef123456789') |
132 u=User(name='test',apikey='abcdef123456789') |
136 o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") |
133 o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") |
137 u.rights.append(Userright(o)) |
134 u.rights.append(Userright(o)) |
138 session.add(u) |
135 session.add(u) |
139 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic']) |
136 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic']) |
142 self.__rpc2().routes('abcdef123456789','fax') |
139 self.__rpc2().routes('abcdef123456789','fax') |
143 exc = fault.exception |
140 exc = fault.exception |
144 self.failUnlessEqual(exc.faultCode, 700) |
141 self.failUnlessEqual(exc.faultCode, 700) |
145 self.failUnlessEqual(exc.faultString, "Typ is not valid.") |
142 self.failUnlessEqual(exc.faultString, "Typ is not valid.") |
146 |
143 |
147 with WithSession(md.engine, autocommit=True) as session: |
144 with self.session() as session: |
148 o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") |
145 o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") |
149 u = session.query(User).filter_by(name="test").first() |
146 u = session.query(User).filter_by(name="test").first() |
150 u.rights.append(Userright(o)) |
147 u.rights.append(Userright(o)) |
151 o=Offer(name="faxde", provider="faxde", route="", typ="fax") |
148 o=Offer(name="faxde", provider="faxde", route="", typ="fax") |
152 session.add(o) |
149 session.add(o) |
153 session.commit() |
150 session.commit() |
154 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic','sipgate_plus']) |
151 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic','sipgate_plus']) |
155 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','fax'),[]) |
152 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','fax'),[]) |
156 |
153 |
157 with WithSession(md.engine, autocommit=True) as session: |
154 with self.session() as session: |
158 u = session.query(User).filter_by(name="test").first() |
155 u = session.query(User).filter_by(name="test").first() |
159 u.rights.append(Userright(o)) |
156 u.rights.append(Userright(o)) |
160 |
157 |
161 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic','sipgate_plus']) |
158 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','sms'),['sipgate_basic','sipgate_plus']) |
162 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','fax'),['faxde']) |
159 self.failUnlessEqual(self.__rpc2().routes('abcdef123456789','fax'),['faxde']) |
163 |
160 |
164 def testDefaultRoutes(self): |
161 def testDefaultRoutes(self): |
165 '''test the defaultRoute function''' |
162 '''test the defaultRoute function''' |
166 with WithSession(md.engine, autocommit=True) as session: |
163 with self.session() as session: |
167 u=User(name='test',apikey='abcdef123456789') |
164 u=User(name='test',apikey='abcdef123456789') |
168 o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") |
165 o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") |
169 u.rights.append(Userright(o,True)) |
166 u.rights.append(Userright(o,True)) |
170 o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") |
167 o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") |
171 u.rights.append(Userright(o)) |
168 u.rights.append(Userright(o)) |
200 self.failUnlessEqual(exc.faultString, "No valid email: '%s'" % invalid[0]) |
197 self.failUnlessEqual(exc.faultString, "No valid email: '%s'" % invalid[0]) |
201 |
198 |
202 def testBill(self): |
199 def testBill(self): |
203 '''test bill function''' |
200 '''test bill function''' |
204 apikey='abcdef123456789' |
201 apikey='abcdef123456789' |
205 with WithSession(md.engine, autocommit=True) as session: |
202 with self.session() as session: |
206 u=User(name='test',apikey=apikey) |
203 u=User(name='test',apikey=apikey) |
207 session.add(u) |
204 session.add(u) |
|
205 |
208 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.0,'anz':0}}) |
206 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.0,'anz':0}}) |
209 |
207 |
210 with WithSession(md.engine, autocommit=True) as session: |
208 with self.session() as session: |
211 u = session.merge(u) |
209 u = session.merge(u) |
212 o = Offer(name='sipgate_basic',provider="sipgate",route="basic",typ="sms") |
210 o = Offer(name='sipgate_basic',provider="sipgate",route="basic",typ="sms") |
|
211 u.rights.append(Userright(o)) |
213 j = Job(hash='a1',info='i',status='sended') |
212 j = Job(hash='a1',info='i',status='sended') |
214 m = Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.30, offer=o, job=j) |
213 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.4, offer=o)) |
215 u.rights.append(Userright(o)) |
|
216 u.jobs.append(j) |
214 u.jobs.append(j) |
217 session.add(m) |
|
218 |
|
219 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.3,'anz':1}, |
|
220 'sipgate_basic':{'price':0.3,'anz':1,'info':{'i':{'price':0.3,'anz':1}}}}) |
|
221 |
|
222 with WithSession(md.engine, autocommit=True) as session: |
|
223 j = session.merge(j) |
|
224 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.4, offer=o)) |
|
225 |
215 |
226 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.7,'anz':2}, |
|
227 'sipgate_basic':{'price':0.7,'anz':2,'info':{'i':{'price':0.7,'anz':2}}}}) |
|
228 |
|
229 with WithSession(md.engine, autocommit=True) as session: |
|
230 m = session.merge(m) |
|
231 m.isBilled=True |
|
232 |
|
233 self.failUnlessEqual(self.__rpc2().bill(apikey),{'total':{'price':0.4,'anz':1}, |
|
234 'sipgate_basic':{'price':0.4,'anz':1,'info':{'i':{'price':0.4,'anz':1}}}}) |
|
235 |
|
236 with WithSession(md.engine, autocommit=True) as session: |
|
237 u = session.merge(u) |
|
238 j = Job(hash='a2',info='a',status='sended') |
216 j = Job(hash='a2',info='a',status='sended') |
239 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.4, offer=o)) |
217 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.4, offer=o)) |
240 u.jobs.append(j) |
218 u.jobs.append(j) |
|
219 |
241 ret=self.__rpc2().bill(apikey) |
220 ret=self.__rpc2().bill(apikey) |
242 self.failUnlessEqual(ret['total'],{'price':0.8,'anz':2}) |
221 self.failUnlessEqual(ret['total'],{'price':0.8,'anz':2}) |
243 self.failUnlessEqual(ret['sipgate_basic'], |
222 self.failUnlessEqual(ret['sipgate_basic'], |
244 {'price':0.8,'anz':2, |
223 {'price':0.8,'anz':2, |
245 'info':{'i':{'price':0.4,'anz':1}, |
224 'info':{'i':{'price':0.4,'anz':1}, |
246 'a':{'price':0.4,'anz':1}, |
225 'a':{'price':0.4,'anz':1}, |
247 } |
226 } |
248 }) |
227 }) |
249 |
228 |
250 with WithSession(md.engine, autocommit=True) as session: |
229 |
251 u = session.merge(u) |
|
252 j = Job(hash='a3',info='a',status='sended') |
|
253 o = Offer(name='sipgate_gold',provider="sipgate",route="gold",typ="sms") |
|
254 j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.5, offer=o)) |
|
255 u.rights.append(Userright(offer=o)) |
|
256 u.jobs.append(j) |
|
257 |
|
258 ret=self.__rpc2().bill(apikey) |
|
259 self.failUnlessEqual(ret['total'],{'price':1.3,'anz':3}) |
|
260 self.failUnlessEqual(ret['sipgate_gold'], |
|
261 {'price':0.5,'anz':1, |
|
262 'info':{ |
|
263 'a':{'price':0.5,'anz':1}, |
|
264 } |
|
265 }) |
|
266 |
|
267 def startReactor(engine): |
230 def startReactor(engine): |
268 """starts the Rector with a special debug Clild, so that the reactor can be stopped remotly. """ |
231 """starts the Rector with a special debug Clild, so that the reactor can be stopped remotly. """ |
269 from twisted.internet import reactor |
232 from twisted.internet import reactor |
270 from twisted.web import xmlrpc, resource |
233 from twisted.web import xmlrpc, resource |
271 |
234 |