def recover_password_send_link(cls, email):
        if not cls.is_valid_email(email):
            raise EmailInvalid("%s is not a valid email address" % email)

        code = keyvalue.get(email, namespace="recover_password_link")
        if code is not None:
            return code  # an email has already been sent out

        entity = cls.get_by_email(email)

        if entity:
            code = "%040x" % random.getrandbits(160)
            keyvalue.set(code,
                         email,
                         namespace="recover_password_link",
                         expires=60 * 60)

            mail.send_mail(sender=environment.SENDER,
                           to=email,
                           subject="Password recovery",
                           body="""
Please use the following link to reset your password.

%s/%s

""" % (environment.URL, code))

            return code
    def test_expires(self):
        keyvalue.set("my_key_with_expires", "my_value", expires=1)
        self.assertEqual(keyvalue.get("my_key_with_expires"), "my_value")

        time.sleep(1)

        self.assertEqual(keyvalue.get("my_key_with_expires"), None)
    def test_set(self):
        key = keyvalue.set("my_key", "my_value")
        self.assertTrue(key is not None)
        self.assertEqual(keyvalue.get("my_key"), "my_value")

        keyvalue.set("my_key", "my_value2")
        self.assertEqual(keyvalue.get("my_key"), "my_value2")

        self.assertEqual(keyvalue.get("nothing_on_this_key"), None)
    def test_namespace(self):
        keyvalue.set("my_key", "my_value", namespace="my_namespace")
        self.assertEqual(keyvalue.get("my_key", namespace="my_namespace"),
                         "my_value")

        keyvalue.set("my_key", "my_value2", namespace="my_namespace2")
        self.assertEqual(keyvalue.get("my_key", namespace="my_namespace"),
                         "my_value")
        self.assertEqual(keyvalue.get("my_key", namespace="my_namespace2"),
                         "my_value2")
 def test_recover_password(self):
     self.api_mock.post("/api/user.create",
                        dict(email="*****@*****.**", password="******"))
     resp = self.api_mock.post("/api/user.recover_password_send_link",
                               dict(email="*****@*****.**"))
     self.assertEqual(resp.get("error"), None)
     code = "test"
     keyvalue.set(code, "*****@*****.**", namespace="recover_password_link")
     messages = self.mail_stub.get_sent_messages(to="*****@*****.**")
     self.assertEqual(1, len(messages))
     resp = self.api_mock.post("/api/user.recover_password",
                               dict(code=code, password="******"))
     self.assertEqual(resp.get("error"), None)
    def verify_email_send_code(self):
        code = keyvalue.get(self.email, namespace="verify_email_code")
        if code is not None:
            return code  # an email has already been sent out

        code = ("%040x" % random.getrandbits(160))[0:5].upper()
        keyvalue.set(self.email,
                     code,
                     namespace="verify_email_code",
                     expires=10 * 60)

        mail.send_mail(sender=environment.SENDER,
                       to=self.email,
                       subject="Verify your email: %s" % code,
                       body="""
Thank you for signing up! Please verify your email address using the code below:

%s

""" % (code))

        return code
 def test_delete(self):
     keyvalue.set("my_key", "my_value")
     self.assertEqual(keyvalue.get("my_key"), "my_value")
     keyvalue.delete("my_key")
     self.assertEqual(keyvalue.get("my_key"), None)