unittest_hooks.py 8.07 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: iso-8859-1 -*-
from logilab.common.testlib import unittest_main
from cubicweb.devtools.apptest import EnvBasedTC

from socket import gethostname
from StringIO import StringIO
from cubicweb.sobjects.notification import construct_message_id

MSG = u'''From sthenault@free.fr  Tue Jan 23 15:21:10 2007
Return-Path: <sthenault@free.fr>
X-Original-To: Sylvain.Thenault@logilab.fr
Delivered-To: syt@logilab.fr
Received: from tucana.logilab.fr (tucana.logilab.fr [172.17.0.4])
        by orion.logilab.fr (Postfix) with ESMTP id E4D79EAA37
        for <Sylvain.Thenault@logilab.fr>; Tue, 23 Jan 2007 15:21:09 +0100 (CET)
Received: from smtp2-g19.free.fr (smtp2-g19.free.fr [212.27.42.28])
        by tucana.logilab.fr (Postfix) with ESMTP id 23B3D7140CE
        for <Sylvain.Thenault@logilab.fr>; Tue, 23 Jan 2007 15:21:08 +0100 (CET)
Received: from nor75-16-82-239-114-67.fbx.proxad.net
        (cap31-2-82-224-153-77.fbx.proxad.net [82.224.153.77])
        by smtp2-g19.free.fr (Postfix) with ESMTP id B057D7D54
        for <Sylvain.Thenault@logilab.fr>; Tue, 23 Jan 2007 15:21:07 +0100 (CET)
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Subject: =?utf-8?q?new_comment_for_bug_avoir_un_champ_=22python_compat=22_sur_les_?=
        =?utf-8?q?projets_=3F?=
From: =?utf-8?q?Sylvain_Th=C3=A9nault?= <Sylvain.Thenault@logilab.fr>
Reply-To: =?utf-8?q?Sylvain_Th=C3=A9nault?= <Sylvain.Thenault@logilab.fr>
X-ERUDI: jpl
To: Sylvain.Thenault@logilab.fr
Message-id: <iblaoliejr>
References: <rooteid=15058&eid=17244&timestamp=1169562059@%s.%s>
Date: Tue, 23 Jan 2007 15:21:07 +0100 (CET)
X-Spambayes-Classification: ham; 0.00
Status: RO
Content-Length: 122
Lines: 2
'''

class ChangeStateHooksTC(EnvBasedTC):

    def setup_database(self):
        self.add_entity('BlogEntry', title=u"une news !", content=u"cubicweb c'est beau")
        self.add_entity('EmailAddress', address=u'devel@logilab.fr', alias=u'devel')
        self.msg = MSG % (self.vreg.config.appid, gethostname())


    def test_email_change_state(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
50
        u = self.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G, X in_state S '
Nicolas Chauvat's avatar
Nicolas Chauvat committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                         'WHERE G name "users", S name "activated"').get_entity(0, 0)
        subject = u':deactivate: %s' % u.eid
        eeid = self.execute('INSERT Email X: '
                             'X headers %(headers)s, X subject %(subject)s,'
                             'X messageid "hop", X sender EA',
                             {'subject': subject, 'headers': self.msg})[0][0]
        self.commit()
        userstate = self.execute('Any SN WHERE X in_state S, S name SN, X eid %(x)s',
                                  {'x': u.eid}, 'x')[0][0]
        self.assertEquals(userstate, 'deactivated')
        self.assertEquals(u.latest_trinfo().creator.login, 'admin')

    def test_email_change_state_trinfo_owner(self):
        self.execute('SET U use_email E WHERE U login "anon"')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
65
        u = self.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G, X in_state S '
Nicolas Chauvat's avatar
Nicolas Chauvat committed
66
67
68
69
70
71
72
73
74
75
76
77
78
                         'WHERE G name "users", S name "activated"').get_entity(0, 0)
        subject = u':deactivate: %s' % u.eid
        eeid = self.execute('INSERT Email X: '
                             'X headers %(headers)s, X subject %(subject)s,'
                             'X messageid "hop", X sender EA',
                             {'subject': subject, 'headers': self.msg})[0][0]
        self.commit()
        userstate = self.execute('Any SN WHERE X in_state S, S name SN, X eid %(x)s',
                                  {'x': u.eid}, 'x')[0][0]
        self.assertEquals(userstate, 'deactivated')
        self.assertEquals(u.latest_trinfo().creator.login, 'anon')

    def test_email_change_state_wrong_eid(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
79
        ueid = self.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G, X in_state S '
Nicolas Chauvat's avatar
Nicolas Chauvat committed
80
81
82
83
84
85
86
87
88
89
90
91
                             'WHERE G name "users", S name "activated"')[0][0]
        subject = u':deactivate: %s' % 10
        eeid = self.execute('INSERT Email X: '
                             'X headers %(headers)s, X subject %(subject)s,'
                             'X messageid "hop", X sender EA',
                             {'subject': subject, 'headers': self.msg})[0][0]
        self.commit()
        userstate = self.execute('Any SN WHERE X in_state S, S name SN, X eid %(x)s',
                                  {'x': ueid}, 'x')[0][0]
        self.assertEquals(userstate, 'activated')
        
    def test_email_change_state_unexistant_eid(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
92
        ueid = self.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G, X in_state S '
Nicolas Chauvat's avatar
Nicolas Chauvat committed
93
94
95
96
97
98
99
100
101
102
103
104
                             'WHERE G name "users", S name "activated"')[0][0]
        subject = u':deactivate: %s' % 999999
        eeid = self.execute('INSERT Email X: '
                             'X headers %(headers)s, X subject %(subject)s,'
                             'X messageid "hop", X sender EA',
                             {'subject': subject, 'headers': self.msg})[0][0]
        self.commit()
        userstate = self.execute('Any SN WHERE X in_state S, S name SN, X eid %(x)s',
                                  {'x': ueid}, 'x')[0][0]
        self.assertEquals(userstate, 'activated')

    def test_email_change_state_unexistant_transition(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
105
        ueid = self.execute('INSERT CWUser X: X login "toto", X upassword "sosafe", X in_group G, X in_state S '
Nicolas Chauvat's avatar
Nicolas Chauvat committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                             'WHERE G name "users", S name "activated"')[0][0]
        subject = u':blabla: %s' % ueid
        eeid = self.execute('INSERT Email X: '
                             'X headers %(headers)s, X subject %(subject)s,'
                             'X messageid "hop", X sender EA',
                             {'subject': subject, 'headers': self.msg})[0][0]
        self.commit()
        userstate = self.execute('Any SN WHERE X in_state S, S name SN, X eid %(x)s',
                                  {'x': ueid}, 'x')[0][0]
        self.assertEquals(userstate, 'activated')
    


MSG2 = u'''MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Subject: =?utf-8?q?new_comment_for_bug_avoir_un_champ_=22python_compat=22_sur_les_?=
        =?utf-8?q?projets_=3F?=
From: =?utf-8?q?Sylvain_Th=C3=A9nault?= <Sylvain.Thenault@logilab.fr>
Reply-To: =?utf-8?q?Sylvain_Th=C3=A9nault?= <Sylvain.Thenault@logilab.fr>
X-ERUDI: jpl
To: Sylvain.Thenault@logilab.fr
Message-id: <iblaoliejr>
References: %s
Date: Tue, 23 Jan 2007 15:21:07 +0100 (CET)
X-Spambayes-Classification: ham; 0.00
Status: RO
Content-Length: 122
Lines: 2

hop
hop
'''

from cubes.email.mboximport import MBOXImporter

class ReplyCommentHooksTC(EnvBasedTC):

    def setup_database(self):
        self.b = self.add_entity('BlogEntry', title=u"une news !", content=u"cubicweb c'est beau")
        e = self.add_entity('EmailAddress', address=u'sylvain.thenault@logilab.fr', alias=u'syt')
        self.execute('SET X use_email E WHERE X login "anon", E eid %(e)s', {'e': e.eid})

    def test_comment_created(self):
        mi = MBOXImporter(self.cursor(), self.schema)
        msg = open('data/reply.mbox', 'rb').read() % \
              construct_message_id(self.vreg.config.appid, self.b.eid, False)
        mi.import_mbox_stream(StringIO(msg))
        self.commit()
        self.failUnless(self.execute('Email X'))
        self.failUnless(self.b.reverse_comments)
        c = self.b.reverse_comments[0]
        self.failUnlessEqual(c.content_format, 'text/plain')
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
159
        self.failUnlessEqual(c.content, u'''New ticket for project erudi-email :
Nicolas Chauvat's avatar
Nicolas Chauvat committed
160

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
161
#22742 - erudi-email #22742: commentaires de mailbot
Nicolas Chauvat's avatar
Nicolas Chauvat committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
====================

description
-----------
Sur http://intranet.logilab.fr/jpl/ticket/22309 on voit un commentaire sign\xe9
mailbot. Ce serait mieux d'utiliser l'auteur du mail.

submitter
---------
alf

URL
---
http://intranet.logilab.fr/jpl/ticket/22742
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
176
(project URL: http://intranet.logilab.fr/jpl/project/erudi-email)''')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
177
178
179
180
181
182
        self.failUnlessEqual(c.creator.login, 'anon')
        self.failUnlessEqual(sorted(u.login for u in c.owned_by),
                             ['admin', 'anon'])

if __name__ == '__main__':
    unittest_main()