def prepare_confirmation(user, token): """ Prepare to confirmation. """ tokens_dict = get_confirmation_dict() tokens_dict[token] = {'user_id': user.pk} if settings.USE_CELERY: send_confirmation_email.delay(user, token) else: send_confirmation_email(user, token)
def create_user_by_api(self, **user_data): register_url = reverse('user-register') response = self._client.post(register_url, user_data, format='json') try: content_data = json.loads(response.content) except ValueError: raise AssertionError( _(u'Invalid response content format, not a JSON')) user_id = content_data.get('id') if not user_id: raise AssertionError(_(u'Not a User ID in data')) confirm_token = content_data.get('confirm_token') if not confirm_token: raise AssertionError(_(u'Not a Confirm Token in data')) try: new_user = User.objects.get(pk=user_id) except User.DoesNotExist: raise AssertionError(_(u'User has not been created')) else: if new_user.is_active: raise AssertionError( _(u'New User is active after registration')) tokens_dict = get_confirmation_dict() user_info = tokens_dict.get(confirm_token) if not user_info: raise AssertionError(_(u'Confirmation token has not beed added')) if not isinstance(user_info, dict) or 'user_id' not in user_info: raise AssertionError(_(u'Invalid confirmation token')) confirm_url = reverse('user-confirm', args=[confirm_token]) response = self._client.get(confirm_url, format='json') assert response.status_code == status.HTTP_200_OK, \ _(u'Invalid HTTp response code') try: new_user = User.objects.get(pk=user_id) except User.DoesNotExist: raise AssertionError(_(u'User has not been created')) else: if not new_user.is_active: raise AssertionError( _(u'New User is not active after confirmation')) return new_user return None
def test_user_register(self): """ Test the new User registration. """ register_url = reverse('user-register') response = self._client.post(register_url, NEW_USER_DATA, format='json') try: content_data = json.loads(response.content) except ValueError: raise AssertionError( _(u'Invalid response content format, not a JSON')) user_id = content_data.get('id') if not user_id: raise AssertionError(_(u'Not a User ID in data')) confirm_token = content_data.get('confirm_token') if not confirm_token: raise AssertionError(_(u'Not a Confirm Token in data')) try: new_user = User.objects.get(pk=user_id) except User.DoesNotExist: raise AssertionError(_(u'User has not been created')) else: if new_user.is_active: raise AssertionError( _(u'New User is active after registration')) tokens_dict = get_confirmation_dict() user_info = tokens_dict.get(confirm_token) if not user_info: raise AssertionError(_(u'Confirmation token has not beed added')) if not isinstance(user_info, dict) or 'user_id' not in user_info: raise AssertionError(_(u'Invalid confirmation token')) confirm_url = reverse('user-confirm', args=[confirm_token]) response = self._client.get(confirm_url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) try: new_user = User.objects.get(pk=user_id) except User.DoesNotExist: raise AssertionError(_(u'User has not been created')) else: if not new_user.is_active: raise AssertionError( _(u'New User is not active after confirmation'))
def confirm(request, token): """ Confirn the new User. """ # Get default Redis List for confirmation tokens tokens_dict = get_confirmation_dict() # Get user info from list user_info = tokens_dict.get(token) result = False if user_info and isinstance(user_info, dict) and 'user_id' in user_info: # Retrieve the User by Id try: user = User.objects.get(pk=user_info['user_id']) except User.DoesNotExist: pass else: # Activate User user.is_active = True user.save() tokens_dict.discard(token) result = True return {'result_ok': result}