def request_remote_newuser(request): """Sends an email to the user with a link to newuser-remote""" if request.method == "POST": form = RemoteEmailRequestForm(request.POST) if form.is_valid(): email = form.cleaned_data["email"] token = newuser_token_generator.make_token(email) html_message = render_to_string( "newuser_email.html", {"email": email, "token": token} ) if not email_exists(email): if email.endswith("@berkeley.edu"): send_mail( subject="CSUA New User Creation Link", message=strip_tags(html_message), html_message=html_message, from_email="*****@*****.**", recipient_list=[email], ) messages.info(request, "Email sent!") else: messages.error( request, "Email must be @berkeley.edu. Please contact us if this is an issue.", ) else: messages.error(request, "Email exists in system!") else: form = RemoteEmailRequestForm() return render(request, "newuserremoterequest.html", {"form": form})
def test_remote_newuser_flow(self, subprocess_run): logging.disable(logging.CRITICAL) url = "/newuser/remote/" email = "*****@*****.**" resp = self.client.get(url) resp = self.client.post(url, {"email": email}) self.assertContains(resp, "Email sent") self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [email]) token_url = reverse( "newuser-remote", kwargs={ "email": email, "token": newuser_token_generator.make_token(email) }, ) full_url = "https://www.csua.berkeley.edu" + token_url self.assertIn(full_url, mail.outbox[0].body) subprocess_run.return_value.returncode = 0 self.assertFalse(email_exists(email)) resp = self.client.get(token_url) resp = self.client.post( token_url, { "full_name": "Phillip E. Nunez II", "student_id": 3114201612, "email": email, "username": "******", "password": "******", "enroll_jobs": False, "agree_rules": True, }, ) self.assertEqual(len(subprocess_run.call_args_list), 1) self.assertNotContains(resp, "failed") args = subprocess_run.call_args_list[0][0][0] self.assertIs(type(args), list) for arg in args: self.assertIs(type(arg), str) self.assertTrue(email_exists(email)) logging.disable(logging.NOTSET)
def _make_hash_value(self, email, timestamp): return str(email) + str(email_exists(email)) + str(timestamp)