class OptoutTest(TestCase): view = 'patchwork.views.mail.optout' url = reverse(view) def setUp(self): self.email = u'*****@*****.**' self.conf = EmailConfirmation(type = 'optout', email = self.email) self.conf.save() def testOptoutValidHash(self): url = reverse('patchwork.views.confirm', kwargs = {'key': self.conf.key}) response = self.client.get(url) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/optout.html') self.assertTrue(self.email in response.content) # check that we've got an optout in the list self.assertEquals(EmailOptout.objects.count(), 1) self.assertEquals(EmailOptout.objects.all()[0].email, self.email) # check that the confirmation is now inactive self.assertFalse(EmailConfirmation.objects.get( pk = self.conf.pk).active)
class UserPersonConfirmTest(TestCase): def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.client.login(username = self.user.username, password = self.user.password) self.conf = EmailConfirmation(type = 'userperson', email = self.user.secondary_email, user = self.user.user) self.conf.save() def testUserPersonConfirm(self): self.assertEquals(Person.objects.count(), 0) response = self.client.get(_confirmation_url(self.conf)) self.assertEquals(response.status_code, 200) # check that the Person object has been created and linked self.assertEquals(Person.objects.count(), 1) person = Person.objects.get(email = self.user.secondary_email) self.assertEquals(person.email, self.user.secondary_email) self.assertEquals(person.user, self.user.user) # check that the confirmation has been marked as inactive. We # need to reload the confirmation to check this. conf = EmailConfirmation.objects.get(pk = self.conf.pk) self.assertEquals(conf.active, False)
def test_patch_submitter_expiry(self): # someone submits a patch... patch = create_patch() submitter = patch.submitter # ... then starts registration... date = ((datetime.datetime.utcnow() - EmailConfirmation.validity) - datetime.timedelta(hours=1)) user = create_user(link_person=False, email=submitter.email) user.is_active = False user.date_joined = user.last_login = date user.save() conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.date = date conf.save() # ... which expires expire_notifications() # we should see no matching user self.assertFalse( User.objects.filter(email=patch.submitter.email).exists()) # but the patch and person should still be present self.assertTrue(Person.objects.filter(pk=submitter.pk).exists()) self.assertTrue(Patch.objects.filter(pk=patch.pk).exists()) # and there should be no user associated with the person self.assertEqual(Person.objects.get(pk=submitter.pk).user, None)
def link(request): context = {} if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): conf = EmailConfirmation(type='userperson', user=request.user, email=form.cleaned_data['email']) conf.save() context['confirmation'] = conf subject = render_to_string('patchwork/mails/user-link-subject.txt') message = render_to_string('patchwork/mails/user-link.txt', context, request=request) try: send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [form.cleaned_data['email']]) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ('An error occurred during confirmation. ' 'Please try again later') else: form = EmailForm() context['linkform'] = form return render(request, 'patchwork/user-link.html', context)
def link(request): context = PatchworkRequestContext(request) if request.method == 'POST': form = UserPersonLinkForm(request.POST) if form.is_valid(): conf = EmailConfirmation(type='userperson', user=request.user, email=form.cleaned_data['email']) conf.save() context['confirmation'] = conf try: send_mail( 'Patchwork email address confirmation', render_to_string('patchwork/user-link.mail', context), settings.DEFAULT_FROM_EMAIL, [form.cleaned_data['email']]) except Exception: context['confirmation'] = None context['error'] = 'An error occurred during confirmation. ' + \ 'Please try again later' else: form = UserPersonLinkForm() context['linkform'] = form return render_to_response('patchwork/user-link.html', context)
class UserPersonConfirmTest(TestCase): def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.client.login(username=self.user.username, password=self.user.password) self.conf = EmailConfirmation(type='userperson', email=self.user.secondary_email, user=self.user.user) self.conf.save() def testUserPersonConfirm(self): self.assertEqual(Person.objects.count(), 0) response = self.client.get(_confirmation_url(self.conf)) self.assertEqual(response.status_code, 200) # check that the Person object has been created and linked self.assertEqual(Person.objects.count(), 1) person = Person.objects.get(email=self.user.secondary_email) self.assertEqual(person.email, self.user.secondary_email) self.assertEqual(person.user, self.user.user) # check that the confirmation has been marked as inactive. We # need to reload the confirmation to check this. conf = EmailConfirmation.objects.get(pk=self.conf.pk) self.assertEqual(conf.active, False)
def test_patch_submitter_expiry(self): # someone submits a patch... patch = create_patch() submitter = patch.submitter # ... then starts registration... date = ((datetime.datetime.utcnow() - EmailConfirmation.validity) - datetime.timedelta(hours=1)) user = create_user(link_person=False, email=submitter.email) user.is_active = False user.date_joined = user.last_login = date user.save() conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.date = date conf.save() # ... which expires expire_notifications() # we should see no matching user self.assertFalse(User.objects.filter(email=patch.submitter.email) .exists()) # but the patch and person should still be present self.assertTrue(Person.objects.filter(pk=submitter.pk).exists()) self.assertTrue(Patch.objects.filter(pk=patch.pk).exists()) # and there should be no user associated with the person self.assertEqual(Person.objects.get(pk=submitter.pk).user, None)
def link(request): context = {} if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): conf = EmailConfirmation(type='userperson', user=request.user, email=form.cleaned_data['email']) conf.save() context['confirmation'] = conf try: send_mail('Patchwork email address confirmation', render_to_string('patchwork/user-link.mail', context, request=request), settings.DEFAULT_FROM_EMAIL, [form.cleaned_data['email']]) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ('An error occurred during confirmation. ' 'Please try again later') else: form = EmailForm() context['linkform'] = form return render(request, 'patchwork/user-link.html', context)
def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.conf = EmailConfirmation(type = 'userperson', email = self.user.secondary_email, user = self.user.user) self.conf.save()
def setUp(self): self.user = create_user() self.secondary_email = _generate_secondary_email(self.user) self.conf = EmailConfirmation(type='userperson', email=self.secondary_email, user=self.user) self.conf.save()
def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.client.login(username=self.user.username, password=self.user.password) self.conf = EmailConfirmation(type='userperson', email=self.user.secondary_email, user=self.user.user) self.conf.save()
def register(self, date): user = create_user() user.is_active = False user.date_joined = user.last_login = date user.save() conf = EmailConfirmation(type="registration", user=user, email=user.email) conf.date = date conf.save() return (user, conf)
def register(self, date): user = create_user() user.is_active = False user.date_joined = user.last_login = date user.save() conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.date = date conf.save() return (user, conf)
def setUp(self): self.user = create_user(link_person=False) self.password = User.objects.make_random_password() self.user.set_password(self.password) self.user.save() self.client.login(username=self.user.username, password=self.password) self.secondary_email = _generate_secondary_email(self.user) self.conf = EmailConfirmation(type='userperson', email=self.secondary_email, user=self.user) self.conf.save()
def register(request): context = {} if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # create inactive user user = auth.models.User.objects.create_user( data['username'], data['email'], data['password'] ) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation conf = EmailConfirmation( type='registration', user=user, email=user.email ) conf.save() context['confirmation'] = conf # send email subject = render_to_string( 'patchwork/mails/activation-subject.txt' ) message = render_to_string( 'patchwork/mails/activation.txt', {'site': Site.objects.get_current(), 'confirmation': conf}, ) try: send_mail( subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email] ) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ( 'An error occurred during registration. ' 'Please try again later' ) else: form = RegistrationForm() context['form'] = form return render(request, 'patchwork/registration.html', context)
def testUnlinkNonPost(self): user = TestUser() self.client.login(username=user.username, password=user.password) conf = EmailConfirmation(type='userperson', email=user.secondary_email, user=user.user) conf.save() self.client.get(_confirmation_url(conf)) person = Person.objects.get(email=user.secondary_email) response = self.client.get(self.form_url.format(pid=str(person.id))) self.assertRedirects(response, self.done_url) person = Person.objects.get(email=user.secondary_email) self.assertEqual(person.user, user.user)
def testUnlinkNonPost(self): user = TestUser() self.client.login(username=user.username, password=user.password) conf = EmailConfirmation(type='userperson', email=user.secondary_email, user=user.user) conf.save() self.client.get(_confirmation_url(conf)) person = Person.objects.get(email=user.secondary_email) response = self.client.get(self.form_url.format(pid=str(person.id))) self.assertRedirects(response, self.done_url) person = Person.objects.get(email=user.secondary_email) self.assertEquals(person.user, user.user)
def register(request): context = PatchworkRequestContext(request) if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # create inactive user user = auth.models.User.objects.create_user( data['username'], data['email'], data['password']) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.save() # send email mail_ctx = { 'site': Site.objects.get_current(), 'confirmation': conf } subject = render_to_string( 'patchwork/activation_email_subject.txt', mail_ctx).replace('\n', ' ').strip() message = render_to_string('patchwork/activation_email.txt', mail_ctx) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email]) # setting 'confirmation' in the template indicates success context['confirmation'] = conf else: form = RegistrationForm() return render_to_response('patchwork/registration_form.html', {'form': form}, context_instance=context)
def testPatchSubmitterExpiry(self): defaults.project.save() defaults.patch_author_person.save() # someone submits a patch... patch = Patch(project=defaults.project, msgid='*****@*****.**', name='test patch', submitter=defaults.patch_author_person, content=defaults.patch) patch.save() # ... then starts registration... date = ((datetime.datetime.now() - EmailConfirmation.validity) - datetime.timedelta(hours=1)) userid = 'test-user' user = User.objects.create_user(userid, defaults.patch_author_person.email, userid) user.is_active = False user.date_joined = user.last_login = date user.save() self.assertEqual(user.email, patch.submitter.email) conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.date = date conf.save() # ... which expires do_expiry() # we should see no matching user self.assertFalse( User.objects.filter(email=patch.submitter.email).exists()) # but the patch and person should still be present self.assertTrue( Person.objects.filter(pk=defaults.patch_author_person.pk).exists()) self.assertTrue(Patch.objects.filter(pk=patch.pk).exists()) # and there should be no user associated with the person self.assertEqual( Person.objects.get(pk=defaults.patch_author_person.pk).user, None)
def testUnlinkAnotherUser(self): user = TestUser() self.client.login(username=user.username, password=user.password) other_user = TestUser('testuser_other', '*****@*****.**', '*****@*****.**') conf = EmailConfirmation(type='userperson', email=other_user.email, user=other_user.user) conf.save() self.client.get(_confirmation_url(conf)) person = Person.objects.get(email=other_user.email) response = self.client.post(self.form_url.format(pid=str(person.id))) self.assertRedirects(response, self.done_url) person = Person.objects.get(email=other_user.email) self.assertEqual(person.user, None)
def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.client.login(username = self.user.username, password = self.user.password) self.conf = EmailConfirmation(type = 'userperson', email = self.user.secondary_email, user = self.user.user) self.conf.save()
def register(request): context = PatchworkRequestContext(request) if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # create inactive user user = auth.models.User.objects.create_user(data['username'], data['email'], data['password']) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.save() # send email mail_ctx = {'site': Site.objects.get_current(), 'confirmation': conf} subject = render_to_string( 'patchwork/activation_email_subject.txt', mail_ctx).replace('\n', ' ').strip() message = render_to_string('patchwork/activation_email.txt', mail_ctx) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email]) # setting 'confirmation' in the template indicates success context['confirmation'] = conf else: form = RegistrationForm() return render_to_response('patchwork/registration_form.html', {'form': form}, context_instance=context)
def testPatchSubmitterExpiry(self): defaults.project.save() defaults.patch_author_person.save() # someone submits a patch... patch = Patch(project=defaults.project, msgid='*****@*****.**', name='test patch', submitter=defaults.patch_author_person, content=defaults.patch) patch.save() # ... then starts registration... date = ((datetime.datetime.now() - EmailConfirmation.validity) - datetime.timedelta(hours=1)) userid = 'test-user' user = User.objects.create_user( userid, defaults.patch_author_person.email, userid) user.is_active = False user.date_joined = user.last_login = date user.save() self.assertEqual(user.email, patch.submitter.email) conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.date = date conf.save() # ... which expires do_expiry() # we should see no matching user self.assertFalse(User.objects.filter(email=patch.submitter.email) .exists()) # but the patch and person should still be present self.assertTrue(Person.objects.filter( pk=defaults.patch_author_person.pk).exists()) self.assertTrue(Patch.objects.filter(pk=patch.pk).exists()) # and there should be no user associated with the person self.assertEqual(Person.objects.get( pk=defaults.patch_author_person.pk).user, None)
def testUnlinkAnotherUser(self): user = TestUser() self.client.login(username=user.username, password=user.password) other_user = TestUser('testuser_other', '*****@*****.**', '*****@*****.**') conf = EmailConfirmation(type='userperson', email=other_user.email, user=other_user.user) conf.save() self.client.get(_confirmation_url(conf)) person = Person.objects.get(email=other_user.email) response = self.client.post(self.form_url.format(pid=str(person.id))) self.assertRedirects(response, self.done_url) person = Person.objects.get(email=other_user.email) self.assertEquals(person.user, None)
def _optinout(request, action): context = {} mail_template = 'patchwork/mails/%s-request.txt' % action mail_subject_template = 'patchwork/mails/%s-request-subject.txt' % action html_template = 'patchwork/%s-request.html' % action if request.method != 'POST': return HttpResponseRedirect(reverse(settings)) form = EmailForm(data=request.POST) if not form.is_valid(): context['error'] = ('There was an error in the form. Please review ' 'and re-submit.') context['form'] = form return render(request, html_template, context) email = form.cleaned_data['email'] if action == 'optin' and EmailOptout.objects.filter( email=email).count() == 0: context['error'] = ("The email address %s is not on the patchwork " "opt-out list, so you don't need to opt back in" % email) context['form'] = form return render(request, html_template, context) conf = EmailConfirmation(type=action, email=email) conf.save() context['confirmation'] = conf subject = render_to_string(mail_subject_template) message = render_to_string(mail_template, context, request=request) try: send_mail(subject, message, conf_settings.DEFAULT_FROM_EMAIL, [email]) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ('An error occurred during confirmation . ' 'Please try again later.') context['admins'] = conf_settings.ADMINS return render(request, html_template, context)
class OptoutTest(TestCase): def setUp(self): self.email = u'*****@*****.**' self.conf = EmailConfirmation(type='optout', email=self.email) self.conf.save() def test_valid_hash(self): url = reverse('confirm', kwargs={'key': self.conf.key}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/optout.html') self.assertContains(response, self.email) # check that we've got an optout in the list self.assertEqual(EmailOptout.objects.count(), 1) self.assertEqual(EmailOptout.objects.all()[0].email, self.email) # check that the confirmation is now inactive self.assertFalse(EmailConfirmation.objects.get(pk=self.conf.pk).active)
def optinout(request, action, description): context = {'site': Site.objects.get_current()} mail_template = 'patchwork/%s-request.mail' % action html_template = 'patchwork/%s-request.html' % action if request.method != 'POST': return HttpResponseRedirect(reverse(settings)) form = OptinoutRequestForm(data=request.POST) if not form.is_valid(): context['error'] = ('There was an error in the %s form. ' + 'Please review the form and re-submit.') % \ description context['form'] = form return render(request, html_template, context) email = form.cleaned_data['email'] if action == 'optin' and \ EmailOptout.objects.filter(email=email).count() == 0: context['error'] = ('The email address %s is not on the ' + 'patchwork opt-out list, so you don\'t ' + 'need to opt back in') % email context['form'] = form return render(request, html_template, context) conf = EmailConfirmation(type=action, email=email) conf.save() context['confirmation'] = conf mail = render_to_string(mail_template, context) try: send_mail('Patchwork %s confirmation' % description, mail, conf_settings.DEFAULT_FROM_EMAIL, [email]) context['email'] = mail context['email_sent'] = True except Exception: context['error'] = ('An error occurred during confirmation . ' 'Please try again later.') context['admins'] = conf_settings.ADMINS return render(request, html_template, context)
def optinout(request, action, description): context = PatchworkRequestContext(request) mail_template = 'patchwork/%s-request.mail' % action html_template = 'patchwork/%s-request.html' % action if request.method != 'POST': return HttpResponseRedirect(reverse(settings)) form = OptinoutRequestForm(data=request.POST) if not form.is_valid(): context['error'] = ('There was an error in the %s form. ' + 'Please review the form and re-submit.') % \ description context['form'] = form return render_to_response(html_template, context) email = form.cleaned_data['email'] if action == 'optin' and \ EmailOptout.objects.filter(email=email).count() == 0: context['error'] = ('The email address %s is not on the ' + 'patchwork opt-out list, so you don\'t ' + 'need to opt back in') % email context['form'] = form return render_to_response(html_template, context) conf = EmailConfirmation(type=action, email=email) conf.save() context['confirmation'] = conf mail = render_to_string(mail_template, context) try: send_mail('Patchwork %s confirmation' % description, mail, conf_settings.DEFAULT_FROM_EMAIL, [email]) context['email'] = mail context['email_sent'] = True except Exception: context['error'] = ('An error occurred during confirmation . ' 'Please try again later.') context['admins'] = conf_settings.ADMINS return render_to_response(html_template, context)
class OptinTest(TestCase): def setUp(self): self.email = u'*****@*****.**' self.optout = EmailOptout(email=self.email) self.optout.save() self.conf = EmailConfirmation(type='optin', email=self.email) self.conf.save() def testOptinValidHash(self): url = reverse('confirm', kwargs={'key': self.conf.key}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/optin.html') self.assertContains(response, self.email) # check that there's no optout remaining self.assertEqual(EmailOptout.objects.count(), 0) # check that the confirmation is now inactive self.assertFalse(EmailConfirmation.objects.get(pk=self.conf.pk).active)
class InvalidConfirmationTest(TestCase): def setUp(self): self.user = create_user() self.secondary_email = _generate_secondary_email(self.user) self.conf = EmailConfirmation(type='userperson', email=self.secondary_email, user=self.user) self.conf.save() def test_inactive_confirmation(self): self.conf.active = False self.conf.save() response = self.client.get(_confirmation_url(self.conf)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/confirm-error.html') self.assertEqual(response.context['error'], 'inactive') self.assertEqual(response.context['conf'], self.conf) def test_expired_confirmation(self): self.conf.date -= self.conf.validity self.conf.save() response = self.client.get(_confirmation_url(self.conf)) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/confirm-error.html') self.assertEqual(response.context['error'], 'expired') self.assertEqual(response.context['conf'], self.conf)
class InvalidConfirmationTest(TestCase): def setUp(self): EmailConfirmation.objects.all().delete() Person.objects.all().delete() self.user = TestUser() self.conf = EmailConfirmation(type = 'userperson', email = self.user.secondary_email, user = self.user.user) self.conf.save() def testInactiveConfirmation(self): self.conf.active = False self.conf.save() response = self.client.get(_confirmation_url(self.conf)) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/confirm-error.html') self.assertEqual(response.context['error'], 'inactive') self.assertEqual(response.context['conf'], self.conf) def testExpiredConfirmation(self): self.conf.date -= self.conf.validity self.conf.save() response = self.client.get(_confirmation_url(self.conf)) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/confirm-error.html') self.assertEqual(response.context['error'], 'expired') self.assertEqual(response.context['conf'], self.conf)
class OptinTest(TestCase): def setUp(self): self.email = u'*****@*****.**' self.optout = EmailOptout(email=self.email) self.optout.save() self.conf = EmailConfirmation(type='optin', email=self.email) self.conf.save() def test_valid_hash(self): response = self.client.get( reverse('confirm', kwargs={'key': self.conf.key})) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'patchwork/optin.html') self.assertContains(response, self.email) # check that there's no optout remaining self.assertEqual(EmailOptout.objects.count(), 0) # check that the confirmation is now inactive self.assertFalse(EmailConfirmation.objects.get( pk=self.conf.pk).active)
def setUp(self): self.email = u'*****@*****.**' self.optout = EmailOptout(email = self.email) self.optout.save() self.conf = EmailConfirmation(type = 'optin', email = self.email) self.conf.save()
def setUp(self): self.url = reverse('patchwork.views.mail.optout') self.email = u'*****@*****.**' self.conf = EmailConfirmation(type = 'optout', email = self.email) self.conf.save()
def _create_confirmation(self, email): conf = EmailConfirmation(type='userperson', email=email, user=self.user) conf.save() self.client.get(_confirmation_url(conf))