def setUp(self): super().setUp() self.alice = get_user('*****@*****.**') self.bob = get_user('*****@*****.**') self.thread = Thread.objects.create(subject='test subject', sender=self.alice, receiver=self.bob) self.last_message = self.thread.add_message(self.thread.sender, 'test message')
def _inner(sender, receiver, *args, **kwargs): # import here to prevent circular imports from ecs.core.models import Submission submission = kwargs.get('submission', None) if isinstance(sender, str): sender = get_user(sender) if isinstance(receiver, str): receiver = get_user(receiver) kwargs['submission'] = Submission.objects.get(pk=int(submission)) if isinstance(submission, (str, int)) else submission args = [sender, receiver] + list(args) return func(*args, **kwargs)
def save(self, **kwargs): if not self.presenter_id: self.presenter = get_current_user() if not self.submission.is_transient: for x, org in (('submitter', 'submitter_organisation'), ('sponsor', 'sponsor_name')): email = getattr(self, '{0}_email'.format(x)) if email: try: user = get_user(email) except User.DoesNotExist: user = create_phantom_user(email, role=x) user.first_name = getattr( self, '{0}_contact_first_name'.format(x)) user.last_name = getattr( self, '{0}_contact_last_name'.format(x)) user.save() profile = user.profile profile.title = getattr(self, '{0}_contact_title'.format(x)) profile.gender = getattr( self, '{0}_contact_gender'.format(x)) or 'f' profile.organisation = getattr(self, org) profile.save() setattr(self, x, user) return super().save(**kwargs)
def test_registration(self): '''Tests the registration process by registering as a new user, following the link in the registration mail message, setting a password and comparing the provided user data afterwards. ''' # create user workflow auth_groups() response = self.client.post(reverse('ecs.users.views.register'), { 'gender': 'm', 'first_name': 'New', 'last_name': 'User', 'email': '*****@*****.**', }) self.assertEqual(response.status_code, 200) mimetype, message = self.get_mimeparts(self.queue_get(0), "text", "html")[0] # XXX: how do we get the right url without knowing its path-prefix? (FMD1) match = re.search(rb'href="https?://[\w.]+(/activate/[^"]+)"', message) self.assertTrue(match) activation_url = match.group(1) response = self.client.get(activation_url) self.assertEqual(response.status_code, 200) response = self.client.post(activation_url, { 'password': '******', 'password_again': 'password', }) self.assertEqual(response.status_code, 200) user = get_user('*****@*****.**') self.assertEqual(user.first_name, 'New') self.assertEqual(user.profile.gender, 'm') self.assertTrue(user.check_password('password'))
def test_password_reset(self): '''Makes sure that a user can reset his password, by following the link in a password reset mail message, setting a new password and performing a test login with the newly set password. ''' user = create_user('*****@*****.**') user.set_password('password') user.save() response = self.client.post(reverse('ecs.users.views.request_password_reset'), { 'email': '*****@*****.**', }) self.assertEqual(response.status_code, 200) mimetype, message = self.get_mimeparts(self.queue_get(0), "text", "html")[0] # XXX: how do we get the right url without knowing its path-prefix? (FMD1) match = re.search(rb'href="https?://[^/]+(/password-reset/[^"]+)"', message) self.assertTrue(match) password_reset_url = match.group(1) response = self.client.get(password_reset_url) self.assertEqual(response.status_code, 200) self.assertTrue('form' in response.context) response = self.client.post(password_reset_url, { 'new_password1': '12345678', 'new_password2': '12345678', }) self.assertEqual(response.status_code, 200) user = get_user('*****@*****.**') self.assertTrue(user.check_password('12345678')) response = self.client.get(password_reset_url) self.assertEqual(response.status_code, 200) self.assertFalse('form' in response.context)
def value_from_datadict(self, data, files, name): val = data.get(name, '') if not val: return None try: return get_user(val.strip()).pk except User.DoesNotExist: return None
def clean_email(self): email = self.cleaned_data['email'] try: user = get_user(email) raise forms.ValidationError( _('There is already a user with this email address.')) except User.DoesNotExist: pass return email
def setUp(self): self.logger = logging.getLogger() self.create_user('alice', profile_extra={'is_internal': True}) for name in ( 'bob', 'unittest', ): self.create_user(name) user = get_user('*****@*****.**') user.is_superuser = True user.save()
def send_vote_reminder(vote, subject, template, recipients): sender = get_user('*****@*****.**') submission = vote.get_submission() subject = subject.format(ec_number=submission.get_ec_number_display()) template = 'votes/messages/{}'.format(template) for recipient in recipients: send_message_template(sender, recipient, subject, template, { 'vote': vote, 'submission': submission, }, submission=submission)
def save(self, **kwargs): if self.email and not self.submission_form.submission.is_transient: try: user = get_user(self.email) except User.DoesNotExist: user = create_phantom_user(self.email, role='investigator') user.first_name = self.contact_first_name user.last_name = self.contact_last_name user.save() profile = user.profile profile.title = self.contact_title profile.gender = self.contact_gender profile.organisation = self.organisation profile.save() self.user = user return super().save(**kwargs)
def send_task_message(task, subject, template, extra_ctx={}): if task.content_type.natural_key() == ('core', 'submission'): submission = task.data elif task.content_type.natural_key() == ('checklists', 'checklist'): submission = task.data.submission else: assert False subject = subject.format(task=task.task_type) template = 'tasks/messages/{}'.format(template) ctx = {'task': task} ctx.update(extra_ctx) send_message_template(get_user('*****@*****.**'), task.created_by, subject, template, ctx, submission=submission)
def handle(self, *emails, **options): verbosity = int(options['verbosity']) activate_internal = options['activate_internal'] activate_testusers = options['activate_testusers'] activate_boardmember = options['activate_board'] remove_user = options['remove_user'] if activate_internal is None and activate_testusers is None and activate_boardmember is None and not emails: raise CommandError("nothing to do") if remove_user and not emails: raise CommandError("nobody to remove") group = Group.objects.get(name='Userswitcher Target') boardmember_group = Group.objects.get(name='Board Member') internal_users = User.objects.filter( profile__is_internal=True).exclude(profile__is_testuser=True) testusers = User.objects.filter(profile__is_testuser=True) boardmembers = User.objects.filter(groups=boardmember_group.id) def _toggle(user, activate): if verbosity >= 2: print(' {0}{1}'.format('+' if activate else '-', user.email)) (group.user_set.add if activate else group.user_set.remove)(user) def _toggle_set(name, users, activate): if not activate is None: if verbosity >= 1: print(('+' if activate else '-') + name) for user in users: _toggle(user, activate) _toggle_set('internal_users', internal_users, activate_internal) _toggle_set('testusers', testusers, activate_testusers) _toggle_set('boardmembers', boardmembers, activate_boardmember) for email in emails: user = get_user(email) _toggle(user, not remove_user)
def send_download_warnings(): now = timezone.now() hist = ( DownloadHistory.objects .filter(user__profile__is_internal=False, downloaded_at__range=(now - timedelta(days=7), now)) .order_by('user_id') .values_list('user_id') .annotate(Count('id')) .filter(id__count__gt=WEEKLY_DOWNLOAD_THRESHOLD) ) sender = get_user('*****@*****.**') receiver = get_office_user() for user_id, count in hist: user = User.objects.get(id=user_id) subject = _('Large number of downloads by {user}').format(user=user) send_message_template(sender, receiver, subject, 'documents/messages/download_warning.txt', {'user': user,'count': count})
def test_password_change(self): '''Makes sure that a password can be changed, by changing a password and performing a test-login afterwards with the changed password. ''' user = create_user('*****@*****.**') user.set_password('test') user.save() self.client.login(email='*****@*****.**', password='******') url = reverse('ecs.users.views.change_password') response = self.client.get(url) self.assertEqual(response.status_code, 200) response = self.client.post(url, { 'old_password': '******', 'new_password1': '12345678', 'new_password2': '12345678', }) self.assertEqual(response.status_code, 200) self.client.logout() user = get_user('*****@*****.**') self.assertTrue(user.check_password('12345678'))
def send_system_message_template(*args, **kwargs): kwargs.setdefault('reply_receiver', get_current_user()) return send_message_template(get_user('*****@*****.**'), *args, **kwargs)
def advanced_settings(): default_contact = get_user('*****@*****.**') AdvancedSettings.objects.get_or_create( pk=1, defaults={'default_contact': default_contact})
def setUp(self): super().setUp() self.user = get_user('*****@*****.**') self.client.login(email='*****@*****.**', password='******')