Esempio n. 1
0
  def test_can_revoke_invite(self):
    """ Test it's possible to revoke invitation """
    self.test_can_invite_user()

    mail.outbox = []
    self.assertTrue(len(mail.outbox) == 0)
    self.assertTrue(OrganizationInvite.objects.filter(revoked_date=None).count() == 2)
    response = self.client.post(reverse("organization-revoke-invite", ["test-organization"]), {"email": "*****@*****.**"}, format="json")
    self.assertTrue(response.status_code == 200)
    self.assertTrue(response.data["detail"] == "Invite has been revoked.")
    self.assertTrue(OrganizationInvite.objects.filter(revoked_date=None).count() == 1)

    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userInvitedRevoked-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvitedRevoked-toUser", "Your invitation to an organization has been revoked"))
    if is_email_enabled("default", "userInvitedRevoked-toOwnerInviter"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvitedRevoked-toOwnerInviter", "You have revoked an user invitation"))


    mail.outbox = []
    self.client.force_authenticate(User.objects.get(email="*****@*****.**"))
    response = self.client.post(reverse("organization-revoke-invite", ["test-organization"]), {"email": "*****@*****.**"}, format="json")

    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userInvitedRevoked-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvitedRevoked-toUser", "Your invitation to an organization has been revoked"))
    if is_email_enabled("default", "userInvitedRevoked-toOwner"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvitedRevoked-toOwner", "An invitation to join your organization has been revoked"))
    if is_email_enabled("default", "userInvitedRevoked-toMemberInviter"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvitedRevoked-toMemberInviter", "You have revoked an user invitation"))
Esempio n. 2
0
  def test_can_join_if_invited(self):
    """ Test it's possible to join organization if invited """
    self.test_can_invite_user()
    self.assertTrue(self.user2 not in self.organization.members.all())

    mail.outbox = []
    self.assertTrue(len(mail.outbox) == 0)

    client = APIClient()
    client.force_authenticate(self.user2)
    response = client.post(reverse("organization-join", ["test-organization"]), {}, format="json")
    self.assertTrue(response.status_code == 200)
    self.assertTrue(response.data["detail"] == "Joined organization.")
    self.assertTrue(self.user2 in self.organization.members.all())

    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userJoined-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userJoined-toUser", "You have joined an organization"))
    if is_email_enabled("default", "userJoined-toOwner"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userJoined-toOwner", "An user has joined an organization you own"))

    # Don't allow joining twice or has after invite used
    response = client.post(reverse("organization-join", ["test-organization"]), {}, format="json")
    self.assertTrue(response.status_code == 403)

    self.organization.members.remove(self.user2)
    self.assertTrue(self.user2 not in self.organization.members.all())
    response = client.post(reverse("organization-join", ["test-organization"]), {}, format="json")
    self.assertTrue(response.status_code == 403)
Esempio n. 3
0
  def test_can_remove_member(self):
    """ Test it's possible to remove a member """
    mail.outbox = []
    self.client.force_authenticate(self.user)
    response = self.client.post(reverse("organization-remove-member", ["test-organization"]), {"email": "*****@*****.**"}, format="json")

    self.assertTrue(response.status_code == 200)
    self.assertTrue(response.data["detail"] == "Member was removed.")

    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userRemoved-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userRemoved-toUser", "You have have been removed from an organization"))
    if is_email_enabled("default", "userRemoved-toOwner"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userRemoved-toOwner", "You have removed an user from an organization you own"))
Esempio n. 4
0
  def test_can_leave_organization(self):
    """ Test it's possible to leave the organization """
    mail.outbox = []
    self.assertTrue(len(mail.outbox) == 0)
    self.assertTrue(self.user2 in self.organization.members.all())
    self.client.force_authenticate(self.user2)
    response = self.client.post(reverse("organization-leave", ["test-organization"]), {}, format="json")

    self.assertTrue(response.status_code == 200)
    self.assertTrue(response.data["detail"] == "You've left the organization.")
    self.assertTrue(self.user2 not in self.organization.members.all())


    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userLeft-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userLeft-toUser", "You have left an organization"))
    if is_email_enabled("default", "userLeft-toOwner"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userLeft-toOwner", "An user has left an organization you own"))
Esempio n. 5
0
 def test_organization_creation_trigger_email(self):
     """Assert that email is triggered when creating an organization"""
     if is_email_enabled("default",
                         "organizationCreated"):  # pragma: no cover
         self.assertTrue(len(mail.outbox) == 1)
         self.assertTrue(mail.outbox[0].subject == get_email_subject(
             "default", "organizationCreated",
             "Your organization was created"))
     else:  # pragma: no cover
         self.assertTrue(len(mail.outbox) == 0)
Esempio n. 6
0
  def test_can_invite_user(self):
    """ Test it's possible to invite user """
    mail.outbox = []
    self.assertTrue(OrganizationInvite.objects.all().count() == 0)
    self.assertTrue(len(mail.outbox) == 0)

    response = self.client.post(reverse("organization-invite-user", ["test-organization"]), {"email": "*****@*****.**"}, format="json")
    self.assertTrue(response.status_code == 200)
    self.assertTrue(response.data["detail"] == "User invited.")

    self.assertTrue(OrganizationInvite.objects.all().count() == 1)
    invite = OrganizationInvite.objects.last()
    self.assertTrue(invite.invitator == self.user)
    self.assertTrue(invite.invited == self.user2)

    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userInvited-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvited-toUser", "You are invited to an organization"))
    if is_email_enabled("default", "userInvited-toOwnerInviter"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvited-toOwnerInviter", "You invited a member to an organization you own"))


    third_user = User(email="*****@*****.**")
    third_user.save(object_channel="default")

    fourth_user = User(email="*****@*****.**")
    fourth_user.save(object_channel="default")

    self.organization.members.add(third_user)
    self.client.force_authenticate(third_user)

    mail.outbox = []
    self.assertTrue(len(mail.outbox) == 0)
    response = self.client.post(reverse("organization-invite-user", ["test-organization"]), {"email": "*****@*****.**"}, format="json")


    subjects = [x.subject for x in mail.outbox]
    if is_email_enabled("default", "userInvited-toUser"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvited-toUser", "You are invited to an organization"))
    if is_email_enabled("default", "userInvited-toOwner"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvited-toOwner", "A member has been invited to your organization"))
    if is_email_enabled("default", "userInvited-toMemberInviter"): # pragma: no cover
      self.assertTrue(get_email_subject("default", "userInvited-toInviter", "You invited a member to an organization you are part of"))
Esempio n. 7
0
    def test_unapply_trigger_email(self):
        """Assert that applying to project trigger one email to volunteer and one to project owner"""
        user = User.objects.create_user(email="*****@*****.**",
                                        password="******",
                                        object_channel="default")
        volunteer = User.objects.create_user(
            email="*****@*****.**",
            password="******",
            object_channel="default")
        project = Project.objects.create(name="test project",
                                         slug="test project",
                                         details="abc",
                                         description="abc",
                                         owner=user,
                                         object_channel="default")

        mail.outbox = []  # Mails sent before applying don't matter
        apply = Apply(project=project, user=volunteer, email=volunteer.email)
        apply.save(object_channel="default")
        apply.status = "unapplied"
        apply.save()

        recipients = [x.to[0] for x in mail.outbox]
        subjects = [x.subject for x in mail.outbox]

        if is_email_enabled(
                "default",
                "volunteerUnapplied-ToVolunteer"):  # pragma: no cover
            self.assertTrue(
                get_email_subject("default", "volunteerUnapplied-ToVolunteer",
                                  "Unapplied from project") in subjects)
            self.assertTrue("*****@*****.**" in recipients)

        if is_email_enabled("default",
                            "volunteerUnapplied-ToOwner"):  # pragma: no cover
            self.assertTrue(
                get_email_subject("default", "volunteerUnapplied-ToOwner",
                                  "Volunteer unapplied from project") in
                subjects)
            self.assertTrue("*****@*****.**" in recipients)
Esempio n. 8
0
    def test_organization_publishing_trigger_email(self):
        """Assert that email is triggered when publishing an organization"""
        mail.outbox = []
        self.organization.published = True
        self.organization.save()

        if is_email_enabled("default",
                            "organizationPublished"):  # pragma: no cover
            self.assertTrue(len(mail.outbox) == 1)
            self.assertTrue(mail.outbox[0].subject == get_email_subject(
                "default", "organizationPublished",
                "Your organization was published"))
        else:  # pragma: no cover
            self.assertTrue(len(mail.outbox) == 0)
Esempio n. 9
0
    def sendEmail(self, template_name, subject, context={}):
        if not is_email_enabled(self.channel, template_name):
            return False

        # Inject extra context
        ctx = inject_client_url(self.channel, context)
        ctx["extend"] = {
            "html": "{}/email/base-body.html".format(self.channel),
            "txt": "{}/email/base-body.txt".format(self.channel)
        }

        self.__setLocale()
        subject = get_email_subject(self.channel, template_name, subject)
        text_content, html_content = self.__render(template_name, ctx)
        self.__resetLocale()

        from_email = self.all_emails.get(self.channel, self.from_email)
        from_user = self.all_users.get(self.channel, settings.EMAIL_HOST_USER)
        from_password = self.all_passwords.get(self.channel,
                                               settings.EMAIL_HOST_PASSWORD)
        connection = get_connection(
            username=from_user,
            password=from_password,
        )
        msg = EmailMultiAlternatives(subject,
                                     text_content,
                                     from_email, [self.email_address],
                                     connection=connection)
        msg.attach_alternative(html_content, "text/html")

        async_flag = None
        if self.async_mail:
            async_flag = "async"
        elif self.async_mail == None:
            async_flag = getattr(settings, "DEFAULT_SEND_EMAIL", "async")

        if async_flag == "async":
            t = EmailThread(msg)
            t.start()
            result = t
        else:
            result = msg.send() > 0

        return result
Esempio n. 10
0
    def test_project_creation_trigger_email(self):
        """Assert that email is triggered when creating a project"""
        user = User.objects.create_user(email="*****@*****.**",
                                        password="******",
                                        object_channel="default")
        mail.outbox = []  # Mails sent before creating don't matter
        project = Project.objects.create(name="test project",
                                         slug="test project",
                                         details="abc",
                                         description="abc",
                                         owner=user,
                                         object_channel="default")

        if is_email_enabled("default", "projectCreated"):
            self.assertTrue(len(mail.outbox) == 1)
            self.assertTrue(mail.outbox[0].subject == get_email_subject(
                "default", "projectCreated", "Project created"))
        else:  # pragma: no cover
            self.assertTrue(len(mail.outbox) == 0)