enhanced testing

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset68fe5f70a445
branchdefault
phasepublic
hiddenno
parent revision#238291daf370 drop useless files, rename test module to reflect it is not automatic tests (related to #4457903)
child revision#14d7adf96696 [views] allow to specify sender (closes #4457904)
files modified by this revision
test/unittest_massmailing.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1383924346 -3600
# Fri Nov 08 16:25:46 2013 +0100
# Node ID 68fe5f70a445aab765e0ed78acca222ed1b12401
# Parent 238291daf37074f692b92b434a89e06d1b27138c
enhanced testing

diff --git a/test/unittest_massmailing.py b/test/unittest_massmailing.py
@@ -1,5 +1,6 @@
1 +# -*- coding: utf-8 -*-
2  # copyright 2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3  # contact http://www.logilab.fr -- mailto:contact@logilab.fr
4  #
5  # This program is free software: you can redistribute it and/or modify it under
6  # the terms of the GNU Lesser General Public License as published by the Free
@@ -14,40 +15,71 @@
7  # You should have received a copy of the GNU Lesser General Public License along
8  # with this program. If not, see <http://www.gnu.org/licenses/>.
9 
10  from cubicweb.devtools import testlib
11  from cubicweb import NoSelectableObject
12 +from cubicweb.web import Redirect
13 
14  class MassMailingTC(testlib.CubicWebTC):
15 
16 -    def test_massmailing_formview(self):
17 +    def actions(self, req, rset):
18 +        actions = self.vreg['actions'].poss_visible_objects(req, rset=rset)
19 +        return set(action.__regid__ for action in actions)
20 +
21 +
22 +    def setup_database(self):
23          self.execute('INSERT EmailAddress X: X address L + "@cubicweb.org", '
24                       'U use_email X WHERE U is CWUser, U login L')
25 +
26 +
27 +    def test_action_sendmail_admin(self):
28 +        req = self.request()
29 +        self.assertIn('sendemail', self.actions(req, req.execute('CWUser X')))
30 +
31 +    def test_action_sendmail_anon(self):
32 +        self.login('anon')
33 +        req = self.request()
34 +        self.assertNotIn('sendemail', self.actions(req, req.execute('CWUser X')))
35 +
36 +    def test_form(self):
37          rset = self.execute('CWUser X')
38 +        form = self.vreg['forms'].select('massmailing', self.request(), rset)
39 +        self.assertEqual(['creation_date', 'cwuri', 'eid', 'firstname',
40 +                          'last_login_time', 'login', 'modification_date',
41 +                          'surname'],
42 +                         form.get_allowed_substitutions())
43 +
44 +    def test_view(self):
45 +        rset = self.execute('CWUser X')
46 +        # simply test the view is correct (x)HTML
47          self.view('massmailing', rset, template=None)
48 
49 -    def test_not_usable_by_guest(self):
50 -        self.assertRaises(NoSelectableObject,
51 -                          self.vreg['controllers'].select, 'sendmail', self.request())
52 -        self.vreg['controllers'].select('sendmail',
53 -                                        self.request(subject='toto',
54 -                                                     recipient='toto@logilab.fr',
55 -                                                     mailbody='hop'))
56 +    def test_controller_missing_params(self):
57 +        self.assertRaises(NoSelectableObject, self.vreg['controllers'].select,
58 +                          'sendmail', self.request())
59 +
60 +    def test_controller_anon(self):
61          self.login('anon')
62          self.assertRaises(NoSelectableObject,
63                            self.vreg['controllers'].select, 'sendmail', self.request())
64 
65 -    def test_sendmail_action(self):
66 -        req = self.request()
67 -        rset = self.execute('Any X WHERE X login "admin"', req=req)
68 -        actions = self.vreg['actions'].poss_visible_objects(req, rset=rset)
69 -        self.assertTrue([action for action in actions if action.__regid__ == 'sendemail'])
70 -        self.login('anon')
71 -        req = self.request()
72 -        rset = self.execute('Any X WHERE X login "anon"', req=req)
73 -        actions = self.vreg['actions'].poss_visible_objects(req, rset=rset)
74 -        self.assertFalse([action for action in actions if action.__regid__ == 'sendemail'])
75 +    def test_controller_admin(self):
76 +        self.user().cw_set(firstname=u'Sÿt', surname=u'Thénault')
77 +        rset = self.execute('CWUser X')
78 +        req = self.request(subject='tôtô',
79 +                           recipient=[str(eid) for eid, in rset],
80 +                           mailbody='hôp %(firstname)s %(surname)s')
81 +        ctrl = self.vreg['controllers'].select('sendmail', req)
82 +        with self.assertRaises(Redirect) as cm:
83 +            ctrl.publish()
84 +        url = cm.exception.location
85 +        self.assertTrue(url.startswith('http://testing.fr/cubicweb/view?_cwmsgid='))
86 +        self.assertEqual(len(testlib.MAILBOX), len(rset))
87 +        email = [mail for mail in testlib.MAILBOX
88 +                 if mail.recipients == ['admin@cubicweb.org']][0]
89 +        self.assertEqual(email.content, u'hôp Sÿt Thénault')
90 +        self.assertEqual(u'tôtô', email.subject)
91 
92 
93  if __name__ == '__main__':
94      from logilab.common.testlib import unittest_main
95      unittest_main()