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_invite_member.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests the invite_members package.
  4. """
  5. from datetime import date
  6. import unittest
  7. from sqlalchemy import engine_from_config
  8. from pyramid import testing
  9. from pyramid_mailer import get_mailer
  10. import transaction
  11. from c3smembership.data.model.base import (
  12. DBSession,
  13. Base,
  14. )
  15. from c3smembership.models import C3sMember
  16. def init_testing_db():
  17. """
  18. Initializes the memory database with test samples.
  19. """
  20. my_settings = {
  21. 'sqlalchemy.url': 'sqlite:///:memory:', }
  22. engine = engine_from_config(my_settings)
  23. DBSession.configure(bind=engine)
  24. Base.metadata.create_all(engine)
  25. with transaction.manager:
  26. # There is a side effect of test_initialization.py after which there are
  27. # still records in the database although it is setup from scratch.
  28. # Therefore, remove all members to have an empty table.
  29. # pylint: disable=no-member
  30. members = C3sMember.get_all()
  31. for member in members:
  32. DBSession.delete(member)
  33. DBSession.flush()
  34. member1 = C3sMember( # german person
  35. firstname=u'SomeFirstnäme',
  36. lastname=u'SomeLastnäme',
  37. email=u'some@shri.de',
  38. address1=u"addr one",
  39. address2=u"addr two",
  40. postcode=u"12345",
  41. city=u"Footown Mäh",
  42. country=u"Foocountry",
  43. locale=u"de",
  44. date_of_birth=date.today(),
  45. email_is_confirmed=False,
  46. email_confirm_code=u'ABCDEFG1',
  47. password=u'arandompassword',
  48. date_of_submission=date.today(),
  49. membership_type=u'normal',
  50. member_of_colsoc=False,
  51. name_of_colsoc=u"",
  52. num_shares=u'23',
  53. )
  54. member2 = C3sMember( # english person
  55. firstname=u'AAASomeFirstnäme',
  56. lastname=u'XXXSomeLastnäme',
  57. email=u'some2@shri.de',
  58. address1=u"addr one",
  59. address2=u"addr two",
  60. postcode=u"12345",
  61. city=u"Footown Mäh",
  62. country=u"Foocountry",
  63. locale=u"en",
  64. date_of_birth=date.today(),
  65. email_is_confirmed=False,
  66. email_confirm_code=u'ABCDEFG2',
  67. password=u'arandompassword',
  68. date_of_submission=date.today(),
  69. membership_type=u'normal',
  70. member_of_colsoc=False,
  71. name_of_colsoc=u"",
  72. num_shares=u'23',
  73. )
  74. member3 = C3sMember( # german legalentity
  75. firstname=u'Cooles PlattenLabel',
  76. lastname=u'SomeLastnäme',
  77. email=u'some@shri.de',
  78. address1=u"addr one",
  79. address2=u"addr two",
  80. postcode=u"12345",
  81. city=u"Footown Mäh",
  82. country=u"Foocountry",
  83. locale=u"de",
  84. date_of_birth=date.today(),
  85. email_is_confirmed=False,
  86. email_confirm_code=u'ABCDEFG3',
  87. password=u'arandompassword',
  88. date_of_submission=date.today(),
  89. membership_type=u'normal',
  90. member_of_colsoc=False,
  91. name_of_colsoc=u"",
  92. num_shares=u'42',
  93. )
  94. member4 = C3sMember( # english legalentity
  95. firstname=u'Incredible Records',
  96. lastname=u'XXXSomeLastnäme',
  97. email=u'some2@shri.de',
  98. address1=u"addr one",
  99. address2=u"addr two",
  100. postcode=u"12345",
  101. city=u"Footown Mäh",
  102. country=u"Foocountry",
  103. locale=u"en",
  104. date_of_birth=date.today(),
  105. email_is_confirmed=False,
  106. email_confirm_code=u'ABCDEFG4',
  107. password=u'arandompassword',
  108. date_of_submission=date.today(),
  109. membership_type=u'normal',
  110. member_of_colsoc=False,
  111. name_of_colsoc=u"",
  112. num_shares=u'2',
  113. )
  114. member1.membership_accepted = True
  115. DBSession.add(member1)
  116. member2.membership_accepted = True
  117. DBSession.add(member2)
  118. member3.membership_accepted = True
  119. DBSession.add(member3)
  120. member4.membership_accepted = True
  121. DBSession.add(member4)
  122. DBSession.flush()
  123. return DBSession
  124. class TestInvitation(unittest.TestCase):
  125. """
  126. Tests the invitations.
  127. """
  128. def setUp(self):
  129. self.config = testing.setUp()
  130. self.config.include('pyramid_mailer.testing')
  131. self.config.add_route('membership_listing_backend', '/')
  132. self.config.add_route('toolbox', '/toolbox')
  133. self.config.add_route('dashboard', '/dashboard')
  134. self.config.registry.settings['c3smembership.url'] = 'http://foo.com'
  135. self.config.registry.settings['ticketing.url'] = 'http://bar.com'
  136. self.config.registry.settings['testing.mail_to_console'] = 'false'
  137. self.session = init_testing_db()
  138. def tearDown(self):
  139. # pylint: disable=no-member
  140. DBSession.close()
  141. DBSession.remove()
  142. testing.tearDown()
  143. def test_invitation(self):
  144. """
  145. Test the invitation procedure for one single member at a time.
  146. Load this member from the DB,
  147. assure the email_invite_flag_bcgv18 and token are not set,
  148. prepare cookies, invite this member,
  149. assure the email_invite_flag_bcgv18 and token are now set,
  150. """
  151. from c3smembership.invite_members import invite_member_bcgv
  152. member1 = C3sMember.get_by_id(1)
  153. self.assertEqual(member1.email_invite_flag_bcgv18, False)
  154. self.assertTrue(member1.email_invite_token_bcgv18 is None)
  155. req = testing.DummyRequest()
  156. # have some cookies
  157. req.cookies['on_page'] = 0
  158. req.cookies['order'] = 'asc'
  159. req.cookies['orderby'] = 'id'
  160. # try with nonexistant id
  161. req.matchdict = {'m_id': 10000}
  162. res = invite_member_bcgv(req)
  163. self.assertEquals(302, res.status_code)
  164. req.matchdict = {'m_id': member1.id}
  165. res = invite_member_bcgv(req)
  166. self.assertEqual(member1.email_invite_flag_bcgv18, True)
  167. self.assertTrue(member1.email_invite_token_bcgv18 is not None)
  168. # now really send email
  169. self.config.registry.settings['testing.mail_to_console'] = 'false'
  170. mailer = get_mailer(req)
  171. res = invite_member_bcgv(req)
  172. self.assertEqual(len(mailer.outbox), 2)
  173. self.assertTrue(u'[C3S] Einladung zu Barcamp und Generalversammlung'
  174. in mailer.outbox[0].subject)
  175. self.assertTrue(u'[C3S] Einladung zu Barcamp und Generalversammlung'
  176. in mailer.outbox[1].subject)
  177. self.assertTrue(member1.firstname
  178. in mailer.outbox[1].body)
  179. self.assertTrue(member1.email_invite_token_bcgv18
  180. in mailer.outbox[1].body)
  181. # now send invitation to english member
  182. member2 = C3sMember.get_by_id(2)
  183. self.assertEqual(member2.email_invite_flag_bcgv18, False)
  184. self.assertTrue(member2.email_invite_token_bcgv18 is None)
  185. req.matchdict = {'m_id': member2.id}
  186. res = invite_member_bcgv(req)
  187. self.assertEqual(member2.email_invite_flag_bcgv18, True)
  188. self.assertTrue(member2.email_invite_token_bcgv18 is not None)
  189. self.assertEqual(len(mailer.outbox), 3)
  190. self.assertTrue(u'[C3S] Invitation to Barcamp and General Assembly'
  191. in mailer.outbox[2].subject)
  192. self.assertTrue(member2.firstname
  193. in mailer.outbox[2].body)
  194. self.assertTrue(member2.email_invite_token_bcgv18
  195. in mailer.outbox[2].body)
  196. def test_invitation_batch(self):
  197. """
  198. Test the invitation procedure, batch mode.
  199. """
  200. from c3smembership.invite_members import batch_invite
  201. members = C3sMember.get_all()
  202. for member in members:
  203. self.assertEqual(member.email_invite_flag_bcgv18, False)
  204. self.assertTrue(member.email_invite_token_bcgv18 is None)
  205. self.assertTrue(member.membership_accepted is True)
  206. req = testing.DummyRequest()
  207. # have some cookies
  208. req.cookies['on_page'] = 0
  209. req.cookies['order'] = 'asc'
  210. req.cookies['orderby'] = 'id'
  211. # with matchdict
  212. req.matchdict = {'number': 1} # this will trigger 1 invitation
  213. res = batch_invite(req)
  214. _messages = req.session.peek_flash('message_to_staff')
  215. # pylint: disable=superfluous-parens
  216. print(_messages)
  217. self.assertTrue(
  218. 'sent out 1 mails (to members with ids [1])' in _messages)
  219. # without matchdict
  220. req.matchdict = {'number': ''} # this triggers remaining 3
  221. res = batch_invite(req)
  222. self.assertEqual(res.status_code, 302)
  223. _messages = req.session.peek_flash('message_to_staff')
  224. self.assertTrue(
  225. 'sent out 3 mails (to members with ids [2, 3, 4])' in _messages)
  226. # send more request with POST['number']
  227. req = testing.DummyRequest(
  228. POST={
  229. 'number': 'foo',
  230. 'submit': True,
  231. })
  232. res = batch_invite(req)
  233. req = testing.DummyRequest(
  234. POST={
  235. 'number': 1,
  236. 'submit': True,
  237. })
  238. res = batch_invite(req)
  239. _messages = req.session.peek_flash('message_to_staff')
  240. self.assertTrue(
  241. 'no invitees left. all done!' in _messages)
  242. mailer = get_mailer(req)
  243. self.assertEqual(len(mailer.outbox), 4)
  244. # assumptions about those members and emails sent
  245. self.assertTrue('[C3S] Einladung' in mailer.outbox[0].subject) # de
  246. self.assertTrue('[C3S] Invitation' in mailer.outbox[1].subject) # en
  247. self.assertTrue('[C3S] Einladung' in mailer.outbox[2].subject) # de
  248. self.assertTrue('[C3S] Invitation' in mailer.outbox[3].subject) # en
  249. for member in members:
  250. # has been invited
  251. self.assertEqual(member.email_invite_flag_bcgv18, True)
  252. # has a token
  253. self.assertTrue(member.email_invite_token_bcgv18 is not None)
  254. # firstname and token are in email body
  255. self.assertTrue(
  256. members[member.id - 1].firstname in mailer.outbox[member.id - 1].body)
  257. self.assertTrue(
  258. members[member.id - 1].email_invite_token_bcgv18 in mailer.outbox[
  259. member.id - 1].body)