def registerUser(request): if not settings.REGISTRATION_OPEN: return HttpResponseRedirect(reverse('registration_disallowed')) return render(request, 'NMTK_server/registration_closed.html') template = 'NMTK_server/registration_form.html' site = get_current_site(request) # Returns 0 if there is no terms of service page content... terms_of_service = models.PageContent.objects.filter( page__name='terms_of_service').count() if request.method == 'POST': userform = forms.NMTKRegistrationForm(request.POST, tos=terms_of_service) if userform.is_valid(): user = userform.save() salt = hashlib.sha1(str(random.random())).hexdigest()[:5] username = user.username if isinstance(user.username, unicode): username = username.encode('utf-8') activation_key = hashlib.sha1(salt + username).hexdigest() profile = registration_models.RegistrationProfile( user=user, activation_key=activation_key) profile.save() profile.send_activation_email(site, request) return render(request, 'NMTK_server/registration_complete.html') else: userform = forms.NMTKRegistrationForm(tos=terms_of_service) return render(request, template, {'form': userform, 'site': site})
def test_sends_proper_arguments_to_send_mail_command(self, settings_mock, get_subject, get_message): registration_profile = models.RegistrationProfile(user=self.sample_user) registration_profile.send_activation_email(Mock()) self.assertEqual([( get_subject.return_value, get_message.return_value, settings_mock.DEFAULT_FROM_EMAIL, ), {}], registration_profile.user.email_user.call_args)
def test_renders_email_subject_to_string(self, render_to_string): render_to_string.return_value = "Activation email" site_mock = Mock() subject = models.RegistrationProfile()._get_activation_subject(site_mock) self.assertEqual([( models.RegistrationProfile.activation_subject_template_name, {'site': site_mock}, ), {}], render_to_string.call_args) self.assertEqual(render_to_string.return_value, subject)
def test_renders_email_message_to_string(self, render_to_string, settings_mock): profile = models.RegistrationProfile(activation_key=Mock()) current_site = Mock() message = profile._get_activation_message(current_site) self.assertEqual([( models.RegistrationProfile.activation_template_name, { 'site': current_site, 'activation_key': profile.activation_key, 'expiration_days': settings_mock.ACCOUNT_ACTIVATION_DAYS, }, ), {}], render_to_string.call_args) self.assertEqual(render_to_string.return_value, message)
def test_forces_email_subject_to_be_one_line(self, render_to_string): render_to_string.return_value = "Activation \nemail" subject = models.RegistrationProfile()._get_activation_subject(Mock) self.assertEqual("Activation email", subject)
def test_gets_current_site_and_sends_to_get_templates(self, get_subject, get_message): registration_profile = models.RegistrationProfile(user=self.sample_user) current_site = Mock() registration_profile.send_activation_email(current_site) self.assertEqual([(current_site,), {}], get_subject.call_args) self.assertEqual([(current_site,), {}], get_message.call_args)
def test_uses_user_in_string_representation(self): user = User.objects.create_user("aaron", "secret", "*****@*****.**") profile = models.RegistrationProfile(user=user) self.assertTrue(str(user) in str(profile))