def test_sign_as_anonymous_user(self): """You don't need to be logged in to sign a waiver.""" response = self.client.get('/profile/waiver/') form = response.context['form'] # Form isn't valid as-is: users must add their name & email self.assertFalse(form.is_valid()) with mock.patch.object(waivers, 'initiate_waiver') as initiate_waiver: initiate_waiver.return_value = waivers.InitiatedWaiverResult( email='*****@*****.**', url=None ) response = self.client.post( '/profile/waiver/', {'releasor-name': 'Tim Beaver', 'releasor-email': '*****@*****.**'}, follow=False, ) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/') initiate_waiver.assert_called_once_with( participant=None, releasor=waivers.Person(name='Tim Beaver', email='*****@*****.**'), guardian=None, ) redirected = self.client.get('/') self.assertEqual( [str(m) for m in redirected.context['messages']], ['Waiver sent to [email protected]'], )
def test_initiate_waiver_from_name_email(self): with mock.patch.object(requests, 'post') as requests_post: result = waivers.initiate_waiver( participant=None, releasor=waivers.Person(name='Tim Beaver', email='*****@*****.**'), guardian=None, ) requests_post.assert_called_once() kwargs = requests_post.call_args[1] self.assertEqual( kwargs['json'], { 'status': 'sent', 'templateId': self.mocked_settings.DOCUSIGN_WAIVER_TEMPLATE_ID, 'templateRoles': [ { 'roleName': 'Releasor', 'name': 'Tim Beaver', 'email': '*****@*****.**', }, { 'roleName': 'MITOC Desk', 'name': 'MITOC Desk', 'email': '*****@*****.**', }, ], 'eventNotification': self.EXPECTED_EVENT_NOTIFICATION, }, ) self.assertEqual( result, waivers.InitiatedWaiverResult(email='*****@*****.**', url=None) )
def test_sign_as_anonymous_with_guardian(self): with mock.patch.object(waivers, 'initiate_waiver') as initiate_waiver: initiate_waiver.return_value = waivers.InitiatedWaiverResult( email='*****@*****.**', url=None ) response = self.client.post( '/profile/waiver/', { 'releasor-name': 'Tim Beaver', 'releasor-email': '*****@*****.**', 'guardian-name': 'Timothy Beaver, Sr', 'guardian-email': '*****@*****.**', }, follow=False, ) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, '/') initiate_waiver.assert_called_once_with( participant=None, releasor=waivers.Person(name='Tim Beaver', email='*****@*****.**'), guardian=waivers.Person( name='Timothy Beaver, Sr', email='*****@*****.**' ), )
def form_valid(self, form): """Handle a name & email (plus perhaps guardian) submission from anonymous users. Authenticated users with a participant record can just use the overridden post() """ releasor: Optional[waivers.Person] = None # When there's a participant object, we'll just use that as releasor # (We'll bypass form validation for participants, but handle just in case) if not self.request.participant: # pragma: no cover, type: ignore releasor = waivers.Person(name=form.cleaned_data['name'], email=form.cleaned_data['email']) return self.send_waiver(releasor=releasor, guardian=self.guardian_from_form())
def guardian_from_form(self) -> Optional[waivers.Person]: """Build a Person object from the optional guardian form. Only participants who are minors need to supply their guardian. This method should only be invoked for minors. NB: If the form is invalid, we'll assume no guardian. This is a shortcut we take because: 1. Supporting form validation on multiple forms can be a pain 2. Maybe one or two people a year need the guardian feature 3. Frontend form validation takes care of validating two fields present """ guardian_form = self.get_guardian_form() if guardian_form.is_valid(): return waivers.Person( name=guardian_form.cleaned_data['name'], email=guardian_form.cleaned_data['email'], ) return None
def test_guardian(self): participant = factories.ParticipantFactory.create( name='Tim Beaver', email='*****@*****.**' ) with self._mock_posts() as requests_post: waivers.initiate_waiver( participant, # Specifying releasor is redundant, but allowed releasor=waivers.Person(name='Tim Beaver', email='*****@*****.**'), guardian=waivers.Person( name='Timothy Beaver, Sr', email='*****@*****.**' ), ) self.assertEqual(len(requests_post.call_args_list), 2) # The first call is to create a new waiver ("envelope") env_args, env_kwargs = requests_post.call_args_list[0] self.assertEqual( env_args, ('https://demo.docusign.net/restapi/v2/accounts/123456/envelopes',), ) self.assertEqual( env_kwargs['json'], { 'status': 'sent', 'templateId': self.mocked_settings.DOCUSIGN_WAIVER_TEMPLATE_ID, 'templateRoles': [ { 'roleName': 'Releasor', 'name': 'Tim Beaver', 'email': '*****@*****.**', 'clientUserId': participant.pk, 'tabs': waivers.prefilled_tabs(participant), # Tested earlier }, { 'roleName': 'Parent or Guardian', 'name': 'Timothy Beaver, Sr', 'email': '*****@*****.**', }, { 'roleName': 'MITOC Desk', 'name': 'MITOC Desk', 'email': '*****@*****.**', }, ], 'eventNotification': self.EXPECTED_EVENT_NOTIFICATION, }, ) # The second call is to get a URL for the waiver embedded_args, embedded_kwargs = requests_post.call_args_list[1] self.assertEqual( embedded_args, ( 'https://demo.docusign.net/restapi/v2/accounts/123456/envelopes/some-envelope-id/views/recipient', ), ) # The participant must sign first, then the guardian can self.assertEqual( embedded_kwargs['json'], { 'userName': '******', 'email': '*****@*****.**', 'clientUserId': participant.pk, 'authenticationMethod': 'email', 'returnUrl': 'https://mitoc-trips.mit.edu', }, )