|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | """actions tests for this package.""" |
| 3 | +from AccessControl import Unauthorized |
3 | 4 | from collective.iconifiedcategory.utils import calculate_category_id |
| 5 | +from HTMLParser import HTMLParser |
| 6 | +from imio.esign.browser.actions import SessionAnnotationInfoView |
4 | 7 | from imio.esign.testing import IMIO_ESIGN_INTEGRATION_TESTING |
5 | 8 | from imio.esign.utils import add_files_to_session |
6 | 9 | from imio.esign.utils import get_session_annotation |
7 | 10 | from plone import api |
| 11 | +from plone.app.testing import login |
8 | 12 | from plone.app.testing import setRoles |
9 | 13 | from plone.app.testing import TEST_USER_ID |
10 | 14 | from plone.namedfile.file import NamedBlobFile |
@@ -124,3 +128,175 @@ def test_finished_shows_message_and_redirects(self): |
124 | 128 | self.assertEqual(len(messages), 1) |
125 | 129 | self.assertIn("removed from session", messages[0].message) |
126 | 130 | self.assertEqual(self.request.RESPONSE.getHeader("location"), self.annexes[0].absolute_url()) |
| 131 | + |
| 132 | + |
| 133 | +class TestSessionAnnotationInfoView(unittest.TestCase): |
| 134 | + """Test SessionAnnotationInfoView""" |
| 135 | + |
| 136 | + layer = IMIO_ESIGN_INTEGRATION_TESTING |
| 137 | + |
| 138 | + def setUp(self): |
| 139 | + self.portal = self.layer["portal"] |
| 140 | + setRoles(self.portal, TEST_USER_ID, ["Manager"]) |
| 141 | + at_folder = api.content.create( |
| 142 | + container=self.portal, |
| 143 | + id="annexes_types", |
| 144 | + title="Annexes Types", |
| 145 | + type="ContentCategoryConfiguration", |
| 146 | + exclude_from_nav=True, |
| 147 | + ) |
| 148 | + category_group = api.content.create( |
| 149 | + type="ContentCategoryGroup", |
| 150 | + title="Annexes", |
| 151 | + container=at_folder, |
| 152 | + id="annexes", |
| 153 | + ) |
| 154 | + icon_path = os.path.join(os.path.dirname(collective.iconifiedcategory.__file__), "tests", "icône1.png") |
| 155 | + with open(icon_path, "rb") as fl: |
| 156 | + api.content.create( |
| 157 | + type="ContentCategory", |
| 158 | + title="To sign", |
| 159 | + container=category_group, |
| 160 | + icon=NamedBlobImage(fl.read(), filename=u"icône1.png"), |
| 161 | + id="to_sign", |
| 162 | + predefined_title="To be signed", |
| 163 | + to_sign=True, |
| 164 | + show_preview=False, |
| 165 | + ) |
| 166 | + self.folder = api.content.create( |
| 167 | + container=self.portal, type="Folder", id="test_session_folder", title="Test Session Folder" |
| 168 | + ) |
| 169 | + tests_dir = os.path.dirname(__file__) |
| 170 | + self.annexes = [] |
| 171 | + for i in range(2): |
| 172 | + with open(os.path.join(tests_dir, "annex1.pdf"), "rb") as f: |
| 173 | + annex = api.content.create( |
| 174 | + container=self.folder, |
| 175 | + type="annex", |
| 176 | + id="annex{}".format(i), |
| 177 | + title=u"Annex {}".format(i), |
| 178 | + content_category=calculate_category_id(self.portal["annexes_types"]["annexes"]["to_sign"]), |
| 179 | + scan_id="012345600000{:02d}".format(i), |
| 180 | + file=NamedBlobFile(data=f.read(), filename=u"annex{}.pdf".format(i), contentType="application/pdf"), |
| 181 | + ) |
| 182 | + self.annexes.append(annex) |
| 183 | + self.signers = [ |
| 184 | + ("user1", "user1@sign.com", u"User 1", u"Position 1"), |
| 185 | + ("user2", "user2@sign.com", u"User 2", u"Position 2"), |
| 186 | + ] |
| 187 | + self.view = SessionAnnotationInfoView(self.folder, self.portal.REQUEST) |
| 188 | + |
| 189 | + def test_call(self): |
| 190 | + setRoles(self.portal, TEST_USER_ID, ["Member"]) |
| 191 | + view = getMultiAdapter((self.folder, self.portal.REQUEST), name="session-annotation-info") |
| 192 | + with self.assertRaises(Unauthorized): |
| 193 | + view() |
| 194 | + login(self.layer["app"], "admin") |
| 195 | + view = getMultiAdapter((self.folder, self.portal.REQUEST), name="session-annotation-info") |
| 196 | + self.assertIsInstance(view(), basestring) |
| 197 | + |
| 198 | + def test_render_value(self): |
| 199 | + # Dict |
| 200 | + self.assertEqual(self.view._render_value({}), u"{}") |
| 201 | + self.assertEqual( |
| 202 | + self.view._render_value({"key": "val"}), |
| 203 | + u"{\n 'key': 'val',\n}", |
| 204 | + ) |
| 205 | + |
| 206 | + # Indentation: nested value increases indent level |
| 207 | + self.assertEqual( |
| 208 | + self.view._render_value({"key": ["a"]}), |
| 209 | + u"{\n 'key': [\n 'a',\n ],\n}", |
| 210 | + ) |
| 211 | + |
| 212 | + # List |
| 213 | + self.assertEqual(self.view._render_value([]), u"[]") |
| 214 | + self.assertEqual( |
| 215 | + self.view._render_value(["a", "b"]), |
| 216 | + u"[\n 'a',\n 'b',\n]", |
| 217 | + ) |
| 218 | + |
| 219 | + # Tuple |
| 220 | + self.assertEqual(self.view._render_value(()), u"[]") |
| 221 | + |
| 222 | + # String |
| 223 | + self.assertEqual(self.view._render_value(u"hello"), u"u'hello'") |
| 224 | + |
| 225 | + def test_uid_to_link(self): |
| 226 | + uid = self.folder.UID() |
| 227 | + result = self.view._uid_to_link(uid) |
| 228 | + self.assertEqual( |
| 229 | + result, |
| 230 | + u"<a href='http://nohost/plone/test_session_folder' title='/plone/test_session_folder'>Test Session Folder</a>", |
| 231 | + ) |
| 232 | + |
| 233 | + result = self.view._uid_to_link(u"a" * 32) |
| 234 | + self.assertEqual( |
| 235 | + result, |
| 236 | + u"<span title='not found'>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</span>", |
| 237 | + ) |
| 238 | + |
| 239 | + def test_esign_sessions(self): |
| 240 | + uids = [a.UID() for a in self.annexes] |
| 241 | + add_files_to_session(self.signers, uids, title=u"[ia.parapheo] Session {sign_id}") |
| 242 | + view = SessionAnnotationInfoView(self.folder, self.portal.REQUEST) |
| 243 | + |
| 244 | + esign_sessions = view.esign_sessions |
| 245 | + self.assertEqual(len(esign_sessions), 1) |
| 246 | + esign_session = esign_sessions[0] |
| 247 | + self.assertIsInstance(esign_session, tuple) |
| 248 | + self.assertEqual(esign_session[0], 0) |
| 249 | + |
| 250 | + self.assertEqual( |
| 251 | + HTMLParser().unescape(view.esign_session_html(esign_session[1])), |
| 252 | + u"""{{ |
| 253 | + 'acroform': True, |
| 254 | + 'client_id': '0123456', |
| 255 | + 'discriminators': [], |
| 256 | + 'files': [ |
| 257 | + {{ |
| 258 | + 'context_uid': <a href='http://nohost/plone/test_session_folder' title='/plone/test_session_folder'>Test Session Folder</a>, |
| 259 | + 'filename': u'annex0.pdf', |
| 260 | + 'scan_id': '01234560000000', |
| 261 | + 'status': '', |
| 262 | + 'title': u'Annex 0', |
| 263 | + 'uid': <a href='http://nohost/plone/test_session_folder/annex0' title='/plone/test_session_folder/annex0'>Annex 0</a>, |
| 264 | + }}, |
| 265 | + {{ |
| 266 | + 'context_uid': <a href='http://nohost/plone/test_session_folder' title='/plone/test_session_folder'>Test Session Folder</a>, |
| 267 | + 'filename': u'annex1.pdf', |
| 268 | + 'scan_id': '01234560000001', |
| 269 | + 'status': '', |
| 270 | + 'title': u'Annex 1', |
| 271 | + 'uid': <a href='http://nohost/plone/test_session_folder/annex1' title='/plone/test_session_folder/annex1'>Annex 1</a>, |
| 272 | + }}, |
| 273 | + ], |
| 274 | + 'last_update': {}, |
| 275 | + 'returns': [], |
| 276 | + 'seal': None, |
| 277 | + 'sign_id': '012345600000', |
| 278 | + 'sign_url': None, |
| 279 | + 'signers': [ |
| 280 | + {{ |
| 281 | + 'email': 'user1@sign.com', |
| 282 | + 'fullname': u'User 1', |
| 283 | + 'position': u'Position 1', |
| 284 | + 'status': '', |
| 285 | + 'userid': 'user1', |
| 286 | + }}, |
| 287 | + {{ |
| 288 | + 'email': 'user2@sign.com', |
| 289 | + 'fullname': u'User 2', |
| 290 | + 'position': u'Position 2', |
| 291 | + 'status': '', |
| 292 | + 'userid': 'user2', |
| 293 | + }}, |
| 294 | + ], |
| 295 | + 'size': 13936, |
| 296 | + 'state': 'draft', |
| 297 | + 'title': u'[ia.parapheo] Session 012345600000', |
| 298 | + 'watchers': [], |
| 299 | +}}""".format( |
| 300 | + repr(esign_session[1]['last_update']), |
| 301 | + ), |
| 302 | + ) |
0 commit comments