def test_other_registration_from_invite(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        user_data = {'abc': 'def'}

        # Initiate Registration Workflow
        SelfRegistrationInvitation.initiate_workflow(
            self.domain,
            [SelfRegistrationUserInfo('999123', user_data)],
            app_id=self.app_id,
        )

        self.assertRegistrationInvitation(
            phone_number='999123',
            app_id=self.app_id,
            phone_type=None,
            android_only=False,
            require_email=False,
            custom_user_data=user_data,
            status=SelfRegistrationInvitation.STATUS_PENDING,
        )

        self.assertLastOutgoingSMS(
            '+999123', [_MESSAGES[MSG_MOBILE_WORKER_INVITATION_START]])

        # Choose phone type 'other'
        incoming('+999123', '2', self.backend.hq_api_id)

        self.assertRegistrationInvitation(
            phone_number='999123',
            app_id=self.app_id,
            phone_type=SelfRegistrationInvitation.PHONE_TYPE_OTHER,
            android_only=False,
            require_email=False,
            custom_user_data=user_data,
            status=SelfRegistrationInvitation.STATUS_PENDING,
        )

        self.assertLastOutgoingSMS(
            '+999123',
            [_MESSAGES[MSG_MOBILE_WORKER_JAVA_INVITATION].format(self.domain)])

        # Register over SMS
        incoming('+999123', 'JOIN {} WORKER test'.format(self.domain),
                 self.backend.hq_api_id)
        user = CommCareUser.get_by_username(
            format_username('test', self.domain))
        self.assertIsNotNone(user)
        self.assertEqual(user.user_data,
                         dict(self.default_user_data, **user_data))
        self.assertEqual(
            PhoneNumber.get_two_way_number('999123').owner_id, user.get_id)

        self.assertLastOutgoingSMS(
            '+999123', [_MESSAGES[MSG_REGISTRATION_WELCOME_MOBILE_WORKER]])

        self.assertRegistrationInvitation(
            status=SelfRegistrationInvitation.STATUS_REGISTERED, )
    def test_android_only_registration_from_invite(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        # Initiate Registration Workflow
        with patch('corehq.apps.sms.models.SelfRegistrationInvitation.odk_url') as mock_odk_url, \
                patch.object(SelfRegistrationInvitation, 'get_user_registration_url', return_value=DUMMY_REGISTRATION_URL), \
                patch.object(SelfRegistrationInvitation, 'get_app_info_url', return_value=DUMMY_APP_INFO_URL):
            mock_odk_url.__get__ = Mock(return_value=DUMMY_APP_ODK_URL)
            SelfRegistrationInvitation.initiate_workflow(
                self.domain,
                [SelfRegistrationUserInfo('999123')],
                app_id=self.app_id,
                android_only=True,
            )

        self.assertRegistrationInvitation(
            phone_number='999123',
            app_id=self.app_id,
            phone_type=SelfRegistrationInvitation.PHONE_TYPE_ANDROID,
            android_only=True,
            require_email=False,
            custom_user_data={},
            status=SelfRegistrationInvitation.STATUS_PENDING,
        )

        self.assertLastOutgoingSMS('+999123', [
            _MESSAGES[MSG_MOBILE_WORKER_ANDROID_INVITATION].format(
                DUMMY_REGISTRATION_URL),
            '[commcare app - do not delete] {}'.format(DUMMY_APP_INFO_URL_B64),
        ])

        invite = self._get_sms_registration_invitation()
        c = Client()
        response = c.post(
            '/a/{}/settings/users/commcare/register/{}/'.format(
                self.domain, invite.token), {
                    'username': '******',
                    'password': '******',
                    'password2': 'abc',
                    'email': '*****@*****.**',
                })
        self.assertEqual(response.status_code, 200)

        user = CommCareUser.get_by_username(
            format_username('new_user', self.domain))
        self.assertIsNotNone(user)
        self.assertEqual(user.user_data, self.default_user_data)
        self.assertEqual(user.email, '*****@*****.**')
        self.assertEqual(
            PhoneNumber.get_two_way_number('999123').owner_id, user.get_id)

        self.assertRegistrationInvitation(
            status=SelfRegistrationInvitation.STATUS_REGISTERED, )
Example #3
0
    def test_custom_message_for_normal_workflow(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        # Initiate Registration Workflow
        SelfRegistrationInvitation.initiate_workflow(
            self.domain,
            [SelfRegistrationUserInfo('999123')],
            app_id=self.app_id,
            custom_first_message='Custom Message',
        )

        self.assertLastOutgoingSMS('+999123', ['Custom Message'])
Example #4
0
    def test_custom_message_for_normal_workflow(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        # Initiate Registration Workflow
        with patch.object(SelfRegistrationInvitation,
                          'get_app_odk_url',
                          return_value=DUMMY_APP_ODK_URL):
            SelfRegistrationInvitation.initiate_workflow(
                self.domain,
                [SelfRegistrationUserInfo('999123')],
                app_id=self.app_id,
                custom_first_message='Custom Message',
            )

        self.assertLastOutgoingSMS('+999123', ['Custom Message'])
Example #5
0
    def test_resend_install_link(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        with patch.object(SelfRegistrationInvitation, 'get_app_info_url', return_value=DUMMY_APP_INFO_URL):
            success_numbers, invalid_format_numbers, error_numbers = SelfRegistrationInvitation.send_install_link(
                self.domain,
                [SelfRegistrationUserInfo('999123')],
                self.app_id
            )
            self.assertEqual(success_numbers, ['999123'])
            self.assertEqual(invalid_format_numbers, [])
            self.assertEqual(error_numbers, [])

        self.assertLastOutgoingSMS('+999123', [
            _MESSAGES[MSG_REGISTRATION_INSTALL_COMMCARE].format(GOOGLE_PLAY_STORE_COMMCARE_URL),
            '[commcare app - do not delete] {}'.format(DUMMY_APP_INFO_URL),
        ])
Example #6
0
    def test_resend_install_link_with_custom_message(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        with patch.object(SelfRegistrationInvitation, 'get_app_info_url', return_value=DUMMY_APP_INFO_URL):
            success_numbers, invalid_format_numbers, error_numbers = SelfRegistrationInvitation.send_install_link(
                self.domain,
                [SelfRegistrationUserInfo('999123')],
                self.app_id,
                custom_message='Click here to reinstall CommCare: {}'
            )
            self.assertEqual(success_numbers, ['999123'])
            self.assertEqual(invalid_format_numbers, [])
            self.assertEqual(error_numbers, [])

        self.assertLastOutgoingSMS('+999123', [
            'Click here to reinstall CommCare: {}'.format(GOOGLE_PLAY_STORE_COMMCARE_URL),
            '[commcare app - do not delete] {}'.format(DUMMY_APP_INFO_URL_B64),
        ])
Example #7
0
    def test_custom_message_for_android_only_workflow(self):
        self.domain_obj.sms_mobile_worker_registration_enabled = True
        self.domain_obj.enable_registration_welcome_sms_for_mobile_worker = True
        self.domain_obj.save()

        # Initiate Registration Workflow
        with patch.object(SelfRegistrationInvitation, 'get_app_odk_url', return_value=DUMMY_APP_ODK_URL), \
                patch.object(SelfRegistrationInvitation, 'get_user_registration_url', return_value=DUMMY_REGISTRATION_URL), \
                patch.object(SelfRegistrationInvitation, 'get_app_info_url', return_value=DUMMY_APP_INFO_URL):
            SelfRegistrationInvitation.initiate_workflow(
                self.domain,
                [SelfRegistrationUserInfo('999123')],
                app_id=self.app_id,
                android_only=True,
                custom_first_message='Sign up here: {}',
            )

        self.assertLastOutgoingSMS('+999123', [
            'Sign up here: {}'.format(DUMMY_REGISTRATION_URL),
            '[commcare app - do not delete] {}'.format(DUMMY_APP_INFO_URL),
        ])
Example #8
0
    def test_parameter_passing(self):

        with patch.object(SelfRegistrationInvitation, 'initiate_workflow', return_value=([], [], [])) as init:

            response = self.make_api_post(
                self.domain1,
                'admin@reg-api-test-1',
                'admin@reg-api-test-1-password',
                {
                    'app_id': '123',
                    'users': [{'phone_number': '999123'}],
                },
            )
            self.assertEqual(response.status_code, 200)
            init.assert_called_once_with(
                self.domain1.name,
                [SelfRegistrationUserInfo('999123')],
                app_id='123',
                custom_first_message=None,
                android_only=False,
                require_email=False,
            )

        with patch.object(SelfRegistrationInvitation, 'initiate_workflow', return_value=([], [], [])) as init:

            response = self.make_api_post(
                self.domain1,
                'admin@reg-api-test-1',
                'admin@reg-api-test-1-password',
                {
                    'app_id': '123',
                    'users': [
                        {'phone_number': '999123',
                         'custom_user_data': {'abc': 'def'}}
                    ]
                },
            )
            self.assertEqual(response.status_code, 200)
            init.assert_called_once_with(
                self.domain1.name,
                [SelfRegistrationUserInfo('999123', {'abc': 'def'})],
                app_id='123',
                custom_first_message=None,
                android_only=False,
                require_email=False,
            )

        with patch.object(SelfRegistrationInvitation, 'initiate_workflow', return_value=([], [], [])) as init:

            response = self.make_api_post(
                self.domain1,
                'admin@reg-api-test-1',
                'admin@reg-api-test-1-password',
                {
                    'app_id': '123',
                    'users': [{'phone_number': '999123'}],
                    'android_only': True,
                },
            )
            self.assertEqual(response.status_code, 200)
            init.assert_called_once_with(
                self.domain1.name,
                [SelfRegistrationUserInfo('999123')],
                app_id='123',
                custom_first_message=None,
                android_only=True,
                require_email=False,
            )

        with patch.object(SelfRegistrationInvitation, 'initiate_workflow', return_value=([], [], [])) as init:

            response = self.make_api_post(
                self.domain1,
                'admin@reg-api-test-1',
                'admin@reg-api-test-1-password',
                {
                    'app_id': '123',
                    'users': [{'phone_number': '999123'}],
                    'require_email': True,
                },
            )
            self.assertEqual(response.status_code, 200)
            init.assert_called_once_with(
                self.domain1.name,
                [SelfRegistrationUserInfo('999123')],
                app_id='123',
                custom_first_message=None,
                android_only=False,
                require_email=True,
            )

        with patch.object(SelfRegistrationInvitation, 'initiate_workflow', return_value=([], [], [])) as init:

            response = self.make_api_post(
                self.domain1,
                'admin@reg-api-test-1',
                'admin@reg-api-test-1-password',
                {
                    'app_id': '123',
                    'users': [{'phone_number': '999123'}],
                    'custom_registration_message': 'Hello',
                },
            )
            self.assertEqual(response.status_code, 200)
            init.assert_called_once_with(
                self.domain1.name,
                [SelfRegistrationUserInfo('999123')],
                app_id='123',
                custom_first_message='Hello',
                android_only=False,
                require_email=False,
            )