140 def sendmail(self, helo_addr, recipients, msg): |
140 def sendmail(self, helo_addr, recipients, msg): |
141 MAILBOX.append(Email(recipients, msg)) |
141 MAILBOX.append(Email(recipients, msg)) |
142 |
142 |
143 cwconfig.SMTP = MockSMTP |
143 cwconfig.SMTP = MockSMTP |
144 |
144 |
|
145 class TestCaseConnectionProxy(object): |
|
146 """thin wrapper around `cubicweb.dbapi.Connection` context-manager |
|
147 used in CubicWebTC (cf. `cubicweb.devtools.testlib.CubicWebTC.login` method) |
|
148 |
|
149 It just proxies to the default connection context manager but |
|
150 restores the original connection on exit. |
|
151 """ |
|
152 def __init__(self, testcase, cnx): |
|
153 self.testcase = testcase |
|
154 self.cnx = cnx |
|
155 |
|
156 def __getattr__(self, attrname): |
|
157 return getattr(self.cnx, attrname) |
|
158 |
|
159 def __enter__(self): |
|
160 return self.cnx.__enter__() |
|
161 |
|
162 def __exit__(self, exctype, exc, tb): |
|
163 try: |
|
164 return self.cnx.__exit__(exctype, exc, tb) |
|
165 finally: |
|
166 self.cnx.close() |
|
167 self.testcase.restore_connection() |
145 |
168 |
146 # base class for cubicweb tests requiring a full cw environments ############### |
169 # base class for cubicweb tests requiring a full cw environments ############### |
147 |
170 |
148 class CubicWebTC(TestCase): |
171 class CubicWebTC(TestCase): |
149 """abstract class for test using an apptest environment |
172 """abstract class for test using an apptest environment |
329 self.cnx = repo_connect(self.repo, unicode(login), **kwargs) |
352 self.cnx = repo_connect(self.repo, unicode(login), **kwargs) |
330 self.websession = DBAPISession(self.cnx) |
353 self.websession = DBAPISession(self.cnx) |
331 self._cnxs.append(self.cnx) |
354 self._cnxs.append(self.cnx) |
332 if login == self.vreg.config.anonymous_user()[0]: |
355 if login == self.vreg.config.anonymous_user()[0]: |
333 self.cnx.anonymous_connection = True |
356 self.cnx.anonymous_connection = True |
334 return self.cnx |
357 return TestCaseConnectionProxy(self, self.cnx) |
335 |
358 |
336 def restore_connection(self): |
359 def restore_connection(self): |
337 if not self.cnx is self._orig_cnx[0]: |
360 if not self.cnx is self._orig_cnx[0]: |
338 if not self.cnx._closed: |
361 if not self.cnx._closed: |
339 self.cnx.close() |
362 self.cnx.close() |