A webapp/form for people to join pEp coop. Fork of Cultural Commons Collecting Society (C3S) SCE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_afm.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # -*- coding: utf-8 -*-
  2. from datetime import date
  3. import unittest
  4. # from pyramid.config import Configurator
  5. from pyramid import testing
  6. from pyramid_mailer import get_mailer
  7. from sqlalchemy import engine_from_config
  8. from c3smembership.data.model.base import (
  9. DBSession,
  10. Base,
  11. )
  12. import transaction
  13. from webtest import TestApp
  14. from c3smembership.models import (
  15. C3sMember,
  16. C3sStaff,
  17. Group,
  18. )
  19. from c3smembership.data.model.base import DBSession
  20. from c3smembership import main
  21. import c3smembership.views.afm as afm
  22. def _initTestingDB():
  23. # from sqlalchemy import create_engine
  24. # from c3smembership.models import initialize_sql
  25. # session = initialize_sql(create_engine('sqlite://'))
  26. session = DBSession
  27. return session
  28. class DummyDate(object):
  29. def __init__(self, today):
  30. self._today = today
  31. def __call__(self, *args, **kwargs):
  32. return date(*args, **kwargs)
  33. def today(self):
  34. return self._today
  35. class TestViews(unittest.TestCase):
  36. """
  37. very basic tests for the main views
  38. """
  39. def setUp(self):
  40. self.config = testing.setUp()
  41. self.config.include('pyramid_mailer.testing')
  42. self.config.registry.settings[
  43. 'c3smembership.url'] = 'https://yes.c3s.cc'
  44. self.config.registry.settings['c3smembership.mailaddr'] = 'c@c3s.cc'
  45. self.config.registry.settings['testing.mail_to_console'] = 'false'
  46. self.config.registry.get_mailer = get_mailer
  47. DBSession.remove()
  48. self.session = _initTestingDB()
  49. def tearDown(self):
  50. DBSession.remove()
  51. testing.tearDown()
  52. def test_show_success(self):
  53. """
  54. test the success page
  55. """
  56. from c3smembership.views.afm import show_success
  57. self.config.add_route('join', '/')
  58. request = testing.DummyRequest(
  59. params={
  60. 'appstruct': {
  61. 'firstname': 'foo',
  62. 'lastname': 'bar',
  63. }
  64. }
  65. )
  66. request.session['appstruct'] = {
  67. 'person': {
  68. 'firstname': 'foo',
  69. 'lastname': 'bar',
  70. }
  71. }
  72. result = show_success(request)
  73. # print result
  74. self.assertTrue(result['lastname'] is 'bar')
  75. self.assertTrue(result['firstname'] is 'foo')
  76. def test_success_check_email(self):
  77. """
  78. test the success_check_email view
  79. """
  80. from c3smembership.views.afm import success_check_email
  81. self.config.add_route('join', '/')
  82. request = testing.DummyRequest(
  83. params={
  84. 'appstruct': {
  85. 'firstname': 'foo',
  86. 'lastname': 'bar',
  87. }
  88. }
  89. )
  90. request.session['appstruct'] = {
  91. 'person': {
  92. 'firstname': 'foo',
  93. 'lastname': 'bar',
  94. 'email': 'bar@shri.de',
  95. 'password': 'bad password',
  96. 'address1': 'Some Street',
  97. 'address2': '',
  98. 'postcode': 'ABC123',
  99. 'city': 'Stockholm',
  100. 'country': 'SE',
  101. 'locale': 'de',
  102. 'date_of_birth': '1980-01-01',
  103. },
  104. 'membership_info': {
  105. 'membership_type': 'person',
  106. 'member_of_colsoc': 'no',
  107. 'name_of_colsoc': '',
  108. },
  109. 'shares': {
  110. 'num_shares': '3',
  111. },
  112. }
  113. mailer = get_mailer(request)
  114. result = success_check_email(request)
  115. # print result
  116. self.assertTrue(result['lastname'] is 'bar')
  117. self.assertTrue(result['firstname'] is 'foo')
  118. self.assertEqual(len(mailer.outbox), 1)
  119. self.assertEqual(
  120. mailer.outbox[0].subject,
  121. 'C3S: confirm your email address and load your PDF')
  122. # self.assertEqual(mailer.outbox[0]., "hello world")
  123. verif_link = "https://yes.c3s.cc/verify/bar@shri.de/"
  124. self.assertTrue("Hallo foo bar!" in mailer.outbox[0].body)
  125. self.assertTrue(verif_link in mailer.outbox[0].body)
  126. def test_success_check_email_redirect(self):
  127. """
  128. test the success_check_email view redirection when appstruct is missing
  129. """
  130. from c3smembership.views.afm import success_check_email
  131. self.config.add_route('join', '/')
  132. request = testing.DummyRequest()
  133. result = success_check_email(request)
  134. self.assertEqual('302 Found', result._status)
  135. self.assertEqual('http://example.com/', result.location)
  136. def _fill_form_valid_natural(self, form):
  137. # print form.fields
  138. form['firstname'] = u'SomeFirstname'
  139. form['lastname'] = u'SomeLastname'
  140. form['email'] = u'some@shri.de'
  141. form['password'] = u'jG2NVfOn0BroGrAXR7wy'
  142. form['password-confirm'] = u'jG2NVfOn0BroGrAXR7wy'
  143. form['address1'] = u"addr one"
  144. form['address2'] = u"addr two"
  145. form['postcode'] = u"12345"
  146. form['city'] = u"Footown Meeh"
  147. form['country'].value__set(u"DE")
  148. form['year'] = unicode(date.today().year-40)
  149. form['month'] = '1'
  150. form['day'] = '1'
  151. form['locale'] = u"DE"
  152. form['membership_type'].value__set(u'normal')
  153. form['other_colsoc'].value__set(u'no')
  154. form['name_of_colsoc'] = u"GEMA"
  155. form['num_shares'] = u'23'
  156. form['got_statute'].value__set(True)
  157. form['got_dues_regulations'].value__set(True)
  158. return form
  159. def test_join_c3s(self):
  160. # setup
  161. self.config = testing.setUp()
  162. self.config.include('pyramid_mailer.testing')
  163. DBSession.close()
  164. DBSession.remove()
  165. my_settings = {
  166. 'sqlalchemy.url': 'sqlite:///:memory:',
  167. 'available_languages': 'da de en es fr',
  168. 'c3smembership.dashboard_number': '30'}
  169. engine = engine_from_config(my_settings)
  170. DBSession.configure(bind=engine)
  171. Base.metadata.create_all(engine)
  172. with transaction.manager:
  173. # a group for accountants/staff
  174. accountants_group = Group(name=u"staff")
  175. DBSession.add(accountants_group)
  176. DBSession.flush()
  177. # staff personnel
  178. staffer1 = C3sStaff(
  179. login=u"rut",
  180. password=u"berries",
  181. email=u"noreply@c3s.cc",
  182. )
  183. staffer1.groups = [accountants_group]
  184. DBSession.add(accountants_group)
  185. DBSession.add(staffer1)
  186. DBSession.flush()
  187. app = main({}, **my_settings)
  188. self.testapp = TestApp(app)
  189. # sucess for valid entry
  190. res = self.testapp.get('/', status=200)
  191. form = self._fill_form_valid_natural(res.form)
  192. res = form.submit(u'submit', status=302)
  193. res = res.follow()
  194. self.assertTrue('information below to be correct' in res.body)
  195. # success for 18th birthday
  196. res = self.testapp.get('/', status=200)
  197. form = self._fill_form_valid_natural(res.form)
  198. afm.date = DummyDate(date(2018, 4, 29))
  199. form['year'] = u'2000'
  200. form['month'] = u'04'
  201. form['day'] = u'29'
  202. res = form.submit(u'submit', status=302)
  203. res = res.follow()
  204. self.assertTrue('information below to be correct' in res.body)
  205. # failure on test one day before 18th birthday
  206. res = self.testapp.get('/', status=200)
  207. form = self._fill_form_valid_natural(res.form)
  208. afm.date = DummyDate(date(2018, 4, 29))
  209. form['year'] = u'2000'
  210. form['month'] = u'04'
  211. form['day'] = u'30'
  212. res = form.submit(u'submit', status=200)
  213. self.assertTrue('underaged person is currently not' in res.body)
  214. # failure for statute not checked
  215. res = self.testapp.get('/', status=200)
  216. form = self._fill_form_valid_natural(res.form)
  217. form['got_dues_regulations'].value__set(False)
  218. res = form.submit(u'submit', status=200)
  219. # failure for dues regulations not checked
  220. res = self.testapp.get('/', status=200)
  221. form = self._fill_form_valid_natural(res.form)
  222. form['got_dues_regulations'].value__set(False)
  223. res = form.submit(u'submit', status=200)
  224. # teardown
  225. DBSession.close()
  226. DBSession.remove()
  227. testing.tearDown()
  228. def test_success_verify_email(self):
  229. """
  230. test the success_verify_email view
  231. """
  232. from c3smembership.views.afm import success_verify_email
  233. self.config.add_route('join', '/')
  234. # without submitting
  235. request = testing.DummyRequest()
  236. request.matchdict['email'] = 'foo@shri.de'
  237. request.matchdict['code'] = '12345678'
  238. result = success_verify_email(request)
  239. self.assertEqual(
  240. request.session.peek_flash('message_above_login'),
  241. [u'Please enter your password.'])
  242. self.assertEqual(result['result_msg'], 'something went wrong.')
  243. self.assertEqual(result['firstname'], '')
  244. self.assertEqual(result['lastname'], '')
  245. self.assertEqual(result['post_url'], '/verify/foo@shri.de/12345678')
  246. self.assertEqual(result['namepart'], '')
  247. self.assertEqual(result['correct'], False)
  248. # TODO: try to submit, maybe in webtest?
  249. # def test_success_verify_email_(self):
  250. # """
  251. # test the success_verify_email view
  252. # """
  253. # from c3smembership.views.afm import success_verify_email
  254. # self.config.add_route('join', '/')
  255. # #from pyramid_mailer import get_mailer
  256. # # without submitting
  257. # request = testing.DummyRequest()
  258. # request.matchdict['email'] = 'MMMMMMMMMMMMMMMMM@shri.de'
  259. # request.matchdict['code'] = '12345678'
  260. # request.POST['submit'] = True
  261. # result = success_verify_email(request)
  262. # self.assertEqual(
  263. # request.session.peek_flash('message_above_form'),
  264. # [u'Please enter your password.'])
  265. # self.assertEqual(result['result_msg'], 'something went wrong.')
  266. # self.assertEqual(result['firstname'], '')
  267. # self.assertEqual(result['lastname'], '')
  268. # self.assertEqual(result['post_url'],
  269. # '/verify/MMMMMMMMMMMMMMMMM@shri.de/ABCDEFGHIJ')
  270. # self.assertEqual(result['namepart'], '')
  271. # self.assertEqual(result['correct'], False)
  272. # TODO: try to submit, maybe in webtest?
  273. # def _makeLocalizer(self, *arg, **kw):
  274. # from pyramid.i18n import Localizer
  275. # return Localizer(*arg, **kw)
  276. # def test_join_c3s(self):
  277. # """
  278. # test the join form
  279. # """
  280. # from c3smembership.views.afm import join_c3s
  281. # request = testing.DummyRequest()
  282. # from pyramid.i18n import get_localizer
  283. # from pyramid.threadlocal import get_current_request
  284. # from pkg_resources import resource_filename
  285. # import deform
  286. # def translator(term):
  287. # return get_localizer(get_current_request()).translate(term)
  288. # my_template_dir = resource_filename('c3smembership', 'templates/')
  289. # deform_template_dir = resource_filename('deform', 'templates/')
  290. # zpt_renderer = deform.ZPTRendererFactory(
  291. # [
  292. # my_template_dir,
  293. # deform_template_dir,
  294. # ],
  295. # translator=translator,
  296. # )
  297. # request.localizer = self._makeLocalizer('en_US', None)
  298. # # import pdb
  299. # # pdb.set_trace()
  300. # #result = join_c3s(request)
  301. # def test_dashboard_view(self):
  302. # from c3smembership.accountants_views import accountants_desk
  303. # request = testing.DummyRequest(
  304. # params={
  305. # 'numdisplay': '20', # this stopped working with the newly
  306. # }
  307. # )
  308. # print(str(dir(request)))
  309. # print("request.params: " + str(request.params.get('locale')))
  310. # result = accountants_desk(request)
  311. # self.assertTrue('form' in result)
  312. # import pdb
  313. # pdb.set_trace()
  314. # def test_join_membership_view_nosubmit(self):
  315. # from c3sintent.views import join_membership
  316. # request = testing.DummyRequest(
  317. # params={
  318. # 'locale': 'en', # this stopped working with the newly
  319. # } # # # introduced # zpt_renderer :-/
  320. # )
  321. # print(str(dir(request)))
  322. # print("request.params: " + str(request.params.get('locale')))
  323. # result = join_membership(request)
  324. # self.assertTrue('form' in result)
  325. # def test_join_membership_non_validating(self):
  326. # from c3sintent.views import join_membership
  327. # request = testing.DummyRequest(
  328. # post={
  329. # 'submit': True,
  330. # 'locale': 'de'
  331. # # lots of values missing
  332. # },
  333. # )
  334. # result = join_membership(request)
  335. # self.assertTrue('form' in result)
  336. # self.assertTrue('There was a problem with your submission'
  337. # in str(result))
  338. # def test_intent_validating(self):
  339. # """
  340. # check that valid input to the form produces a pdf
  341. # - with right content type of response
  342. # - with a certain size
  343. # - with appropriate content (form details)
  344. # and a mail would be sent
  345. # """
  346. # from c3sintent.views import declare_intent
  347. # from pyramid_mailer import get_mailer
  348. # request = testing.DummyRequest(
  349. # post={
  350. # 'submit': True,
  351. # 'firstname': 'TheFirstName',
  352. # 'lastname': 'TheLastName',
  353. # 'date_of_birth': '1987-06-05',
  354. # 'city': 'Devilstown',
  355. # 'email': 'email@example.com',
  356. # 'locale': 'en',
  357. # 'activity': set([u'composer', u'dj']),
  358. # 'country': 'AF',
  359. # 'invest_member': 'yes',
  360. # 'member_of_colsoc': 'yes',
  361. # 'name_of_colsoc': 'schmoo',
  362. # 'opt_band': 'yes band',
  363. # 'opt_URL': 'http://yes.url',
  364. # 'noticed_dataProtection': 'yes'
  365. # }
  366. # )
  367. # # print(dir(request.params))
  368. # mailer = get_mailer(request)
  369. # # skip test iff pdftk is not installed
  370. # import subprocess
  371. # from subprocess import CalledProcessError
  372. # try:
  373. # res = subprocess.check_call(["which", "pdftk"])
  374. # if res == 0:
  375. # # go ahead with the tests:
  376. # # feed the test data to the form/view function
  377. # result = declare_intent(request)
  378. # # at this point -if the test fails- we cannot be sure whether
  379. # # we actually got the PDF or the form we tried to submit
  380. # # failed validation, e.g. because the requirements weren't
  381. # # fulfilled. let's see...
  382. # self.assertEquals(result.content_type,
  383. # 'application/pdf')
  384. # #print("size of pdf: " + str(len(result.body)))
  385. # # check pdf size
  386. # self.assertTrue(100000 > len(result.body) > 78000)
  387. # # check pdf contents
  388. # content = ""
  389. # from StringIO import StringIO
  390. # resultstring = StringIO(result.body)
  391. # import slate
  392. # content = slate.PDF(resultstring)
  393. # # uncomment to see the text in the PDF produced
  394. # print(content)
  395. # # test if text shows up as expected
  396. # # self.assertTrue('TheFirstName' in str(content))
  397. # # self.assertTrue('TheLastName' in str(content))
  398. # # self.assertTrue('Address1' in str(content))
  399. # # self.assertTrue('Address2' in str(content))
  400. # # self.assertTrue('email@example.com' in str(content))
  401. # # self.assertTrue('Afgahnistan' in str(content))
  402. # # check outgoing mails
  403. # self.assertTrue(len(mailer.outbox) == 1)
  404. # self.assertTrue(
  405. # mailer.outbox[
  406. # 0].subject == "[c3s] Yes! a new letter of intent")
  407. # except CalledProcessError, cpe: # pragma: no cover
  408. # print("pdftk not installed. skipping test!")
  409. # print(cpe)