Esempio n. 1
0
    def test_group_membership_acceptance_via_email_link(self):
        # here we are testing group_membership view function which is invoked
        # when the user clicks the link provided in the email

        # create a group
        new_group = self._create_group()

        # test user accepting group owner's invitation
        # check mike is no more a member of the group
        self.assertNotIn(self.mike, new_group.gaccess.members)
        # let john invite mike to join group
        membership_request = self.john.uaccess.create_group_membership_request(
            new_group, self.mike)
        # create the link that mike should find in his email
        uidb36 = int_to_base36(self.mike.id)
        token = default_token_generator.make_token(self.mike)
        url_params = {
            "uidb36": uidb36,
            "token": token,
            "membership_request_id": membership_request.id
        }
        url = reverse('group_membership', kwargs=url_params)
        # due to session requirement of the view being tested, using the Client class
        client = Client()
        # let mike click the link in the email
        response = client.get(url)
        redirect_url = '/group/{}/'.format(new_group.id)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertTrue(response['Location'].endswith(redirect_url))
        # check mike is now a member of the group
        self.assertIn(self.mike, new_group.gaccess.members)

        # test group owner (john) accepting user (mike) request to join a group

        # remove mike from group
        self.john.uaccess.unshare_group_with_user(new_group, self.mike)
        # check mike is no more a member of the group
        self.assertNotIn(self.mike, new_group.gaccess.members)
        # let mike make a request to join group
        membership_request = self.mike.uaccess.create_group_membership_request(
            new_group)
        # create the link that john should find in his email
        uidb36 = int_to_base36(self.john.id)
        token = default_token_generator.make_token(self.john)
        url_params = {
            "uidb36": uidb36,
            "token": token,
            "membership_request_id": membership_request.id
        }
        url = reverse('group_membership', kwargs=url_params)
        # let john click the link
        response = client.get(url)
        redirect_url = '/group/{}/'.format(new_group.id)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertTrue(response['Location'].endswith(redirect_url))
        # check mike is now a member of the group
        self.assertIn(self.mike, new_group.gaccess.members)
Esempio n. 2
0
    def test_group_membership_acceptance_via_email_link(self):
        # here we are testing group_membership view function which is invoked
        # when the user clicks the link provided in the email

        # create a group
        new_group = self._create_group()

        # test user accepting group owner's invitation
        # check mike is no more a member of the group
        self.assertNotIn(self.mike, new_group.gaccess.members)
        # let john invite mike to join group
        membership_request = self.john.uaccess.create_group_membership_request(new_group, self.mike)
        # create the link that mike should find in his email
        uidb36 = int_to_base36(self.mike.id)
        token = default_token_generator.make_token(self.mike)
        url_params = {"uidb36": uidb36, "token": token, "membership_request_id": membership_request.id}
        url = reverse('group_membership', kwargs=url_params)
        # due to session requirement of the view being tested, using the Client class
        client = Client()
        # let mike click the link in the email
        response = client.get(url)
        redirect_url = '/group/{}/'.format(new_group.id)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertTrue(response['Location'].endswith(redirect_url))
        # check mike is now a member of the group
        self.assertIn(self.mike, new_group.gaccess.members)

        # test group owner (john) accepting user (mike) request to join a group

        # remove mike from group
        self.john.uaccess.unshare_group_with_user(new_group, self.mike)
        # check mike is no more a member of the group
        self.assertNotIn(self.mike, new_group.gaccess.members)
        # let mike make a request to join group
        membership_request = self.mike.uaccess.create_group_membership_request(new_group)
        # create the link that john should find in his email
        uidb36 = int_to_base36(self.john.id)
        token = default_token_generator.make_token(self.john)
        url_params = {"uidb36": uidb36, "token": token, "membership_request_id": membership_request.id}
        url = reverse('group_membership', kwargs=url_params)
        # let john click the link
        response = client.get(url)
        redirect_url = '/group/{}/'.format(new_group.id)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertTrue(response['Location'].endswith(redirect_url))
        # check mike is now a member of the group
        self.assertIn(self.mike, new_group.gaccess.members)
Esempio n. 3
0
def send_action_to_take_email(request, user, action_type, **kwargs):
    """
    Sends an email with an action link to a user.
    The actual action takes place when the user clicks on the link

    The ``action_type`` arg is both the name of the urlpattern for
    the action link, as well as the names of the email templates
    to use. Additional context variable needed in the email template can be
    passed using the kwargs

    for action_type == 'group_membership', an instance of GroupMembershipRequest and
    instance of Group are expected to
    be passed into this function
    """
    email_to = kwargs.get('group_owner', user)
    context = {'request': request, 'user': user}
    if action_type == 'group_membership':
        membership_request = kwargs['membership_request']
        action_url = reverse(
            action_type,
            kwargs={
                "uidb36": int_to_base36(email_to.id),
                "token": default_token_generator.make_token(email_to),
                "membership_request_id": membership_request.id
            }) + "?next=" + (next_url(request) or "/")

        context['group'] = kwargs.pop('group')
    elif action_type == 'group_auto_membership':
        context['group'] = kwargs.pop('group')
        action_url = ''
    else:
        action_url = reverse(
            action_type,
            kwargs={
                "uidb36": int_to_base36(email_to.id),
                "token": default_token_generator.make_token(email_to)
            }) + "?next=" + (next_url(request) or "/")

    context['action_url'] = action_url
    context.update(kwargs)

    subject_template_name = "email/%s_subject.txt" % action_type
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject,
                       "email/%s" % action_type,
                       settings.DEFAULT_FROM_EMAIL,
                       email_to.email,
                       context=context)
Esempio n. 4
0
def send_verification_mail_for_email_update(request, user, new_email,
                                            verification_type):
    """
    Sends an email with a verification link to users when
    they update their email. The email is sent to the new email.
    The actual update of the email happens only after
    the verification link is clicked.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    verify_url = reverse(verification_type,
                         kwargs={
                             "uidb36": int_to_base36(user.id),
                             "token": default_token_generator.make_token(user),
                             "new_email": new_email
                         }) + "?next=" + (next_url(request) or "/")
    context = {
        "request": request,
        "user": user,
        "new_email": new_email,
        "verify_url": verify_url,
    }
    subject_template_name = "email/%s_subject.txt" % verification_type
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject,
                       "email/%s" % verification_type,
                       settings.DEFAULT_FROM_EMAIL,
                       new_email,
                       context=context)
Esempio n. 5
0
def send_verification_mail_for_email_update(request, user, new_email, verification_type):
    """
    Sends an email with a verification link to users when
    they update their email. The email is sent to the new email.
    The actual update of the email happens only after
    the verification link is clicked.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    verify_url = reverse(verification_type, kwargs={
        "uidb36": int_to_base36(user.id),
        "token": default_token_generator.make_token(user),
        "new_email": new_email
    }) + "?next=" + (next_url(request) or "/")
    context = {
        "request": request,
        "user": user,
        "new_email": new_email,
        "verify_url": verify_url,
    }
    subject_template_name = "email/%s_subject.txt" % verification_type
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject, "email/%s" % verification_type,
                       settings.DEFAULT_FROM_EMAIL, new_email,
                       context=context)
Esempio n. 6
0
def send_verification_mail_for_password_reset(request, user):
    """
    Sends an email with a verification link to users when
    they request to reset forgotten password to their email. The email is sent to the new email.
    The actual reset of of password will begin after the user clicks the link
    provided in the email.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    reset_url = reverse('email_verify_password_reset',
                        kwargs={
                            "uidb36": int_to_base36(user.id),
                            "token": default_token_generator.make_token(user)
                        }) + "?next=" + (next_url(request) or "/")
    context = {"request": request, "user": user, "reset_url": reset_url}
    subject_template_name = "email/reset_password_subject.txt"
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject,
                       "email/reset_password",
                       settings.DEFAULT_FROM_EMAIL,
                       user.email,
                       context=context)
Esempio n. 7
0
def send_verification_mail_for_password_reset(request, user):
    """
    Sends an email with a verification link to users when
    they request to reset forgotten password to their email. The email is sent to the new email.
    The actual reset of of password will begin after the user clicks the link
    provided in the email.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    reset_url = reverse('email_verify_password_reset', kwargs={
        "uidb36": int_to_base36(user.id),
        "token": default_token_generator.make_token(user)
    }) + "?next=" + (next_url(request) or "/")
    context = {
        "request": request,
        "user": user,
        "reset_url": reset_url
    }
    subject_template_name = "email/reset_password_subject.txt"
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject, "email/reset_password",
                       settings.DEFAULT_FROM_EMAIL, user.email,
                       context=context)