def test_dispatch_and_register(self): """ Long-form test that tests creating and sending an invite, and then registering a user by following the referral link in the invitation email. """ countries = [ Country(gwno=i, name=str(i), shape={}, simpleshape={}) for i in range(5) ] for c in countries: c.save() i = Invitation(email="*****@*****.**") i.save() i.countries.set(countries) dispatchInvitation(i) m = mail.outbox[0] # Mailed flag self.assertTrue(i.mailed) # Email has proper title self.assertEqual(m.subject, settings.DEFAULT_EMAIL_TITLE) # Email contains ref. key self.assertIsNotNone(re.search(i.refkey, m.body)) r = self.client.get(i.invitationLink(), follow=True) location, *_ = r.redirect_chain[-1] soup = BeautifulSoup(r.content, features="html.parser") regform = soup.find("form") usernameInput = regform.find("input", attrs={"name": "username"}) # Email is in the username form self.assertEqual(usernameInput["value"], i.email) url = regform.action if regform.action else location method = regform.method if regform.method else "POST" getattr(self.client, method.lower())(url, { "username": usernameInput["value"], "password1": hashlib.md5(b"1234").hexdigest(), "password2": hashlib.md5(b"1234").hexdigest() }) # User was created try: u = User.objects.get(email=i.email) except User.DoesNotExist: self.fail("User was not created") # Make sure all countries were added self.assertEqual({c.pk for c in countries}, {c.pk for c in u.profile.countries.all()})
def share(request: HttpRequest)->HttpResponse: try: data = json.loads(request.body) except json.JSONDecodeError: return JsonResponse({"status":"error","errors":"failed to decode request data"},status=401) try: sender = request.user.email if request.user.email else None reciever = data["email"] msg = data["message"] if data["message"] else "" except Exception as e: return JsonResponse({"status":"error","errors":str(e)},status=401) try: invitation = Invitation.objects.create(email=reciever,invitedBy=sender, customemail = msg) except IntegrityError: try: invitation = Invitation.objects.get(email=reciever) invitation.invitedBy = sender invitation.customemail = msg invitation.save() dispatchInvitation(invitation) return JsonResponse({"status":"ok","message":"User was already invited!"},status=200) except Exception as e: return JsonResponse({"status":"error","message":str(e)},status=401) else: dispatchInvitation(invitation) return JsonResponse({"status":"ok","message":"invitation sent"},status=200)
def test_custom_invite_text(self): et = EmailTemplate.objects.create(active=True, subject="Inv", message="Please join my survey") inv = Invitation.objects.create( email="*****@*****.**", customemail="Hey yee! Please join my survey", customsig="Sincerely, Haw.") dispatchInvitation(inv) self.assertEqual(len(mail.outbox), 1) m = mail.outbox.pop() for msg, mtype in m.alternatives: if mtype == "text/html": self.assertIsNotNone( re.search("Hey yee! Please join my survey", msg)) self.assertIsNotNone(re.search(inv.refkey, msg)) self.assertIsNotNone(re.search("Sincerely, Haw", msg))
def test_rendered_email(self): et = EmailTemplate(active=True, subject="An invitation", message=""" # You are hereby invited to my survey Please fill it out! [here]({{link}}) Best regards The Team """) et.save() inv = Invitation(email="*****@*****.**") dispatchInvitation(inv) self.assertEqual(len(mail.outbox), 1) m = mail.outbox[0] with open("/tmp/m.pckl", "wb") as f: pickle.dump(m, f) self.assertEqual(m.subject, et.subject) self.assertIsNotNone(re.search(inv.refkey, m.body)) self.assertIsNotNone( re.search("You are hereby invited to my survey", m.body))
def dispatch_invitation(modeladmin, request, queryset): """ Send invitation email """ for invitation in queryset: dispatchInvitation(invitation)