Ejemplo n.º 1
0
    def test_simple_login(self, connection_mock):
        conn = connection_mock.instance()
        connection_mock.return_value = conn
        db = conn[self.settings["mongodb.db_name"]]
        backend = MongoDBRegistrationBackend(self.settings, self.config)

        password = "******"
        stored_user = {"username":"******",
                "password":_hash_pw("testpassword"),
                "email":"*****@*****.**"}

        # Test situation where user exists and we do password hash comparison
        db.users.find_one.return_value = stored_user
        self.assertTrue(backend.simple_login(stored_user["username"], password))

        # Test situation where user does not exist
        db.users.find_one.return_value = False
        self.assertFalse(backend.simple_login(stored_user["username"], password))
Ejemplo n.º 2
0
 def test_add_user(self, connection_mock):
     # Test bad username
     struct = {"username":"******"}
     conn = connection_mock.instance()
     connection_mock.return_value = conn
     db = conn[self.settings["mongodb.db_name"]]
     backend = MongoDBRegistrationBackend(self.settings, self.config)
     self.assertRaises(colander.Invalid, backend.add_user, struct)
     # Test good username that already exists in DB
     struct["username"] = "******"
     db.users.find_one.return_value = {"username":struct["username"]}
     self.assertRaises(colander.Invalid, backend.add_user, struct)
     # Test good, available username, writing it to DB
     db.users.find_one.return_value = None
     utcnow = datetime.datetime.utcnow()
     with patch("pyramid_registration.mongodb.make_access_token"):
         with patch("datetime.datetime"):
             testtoken = "testtoken"
             datetime.datetime.utcnow.return_value = utcnow
             mongodb.make_access_token.return_value = testtoken
             
             backend.add_user(struct)
             db.users.insert.assert_called_once_with({"username":struct["username"],
                 "access_tokens":[{"created_timestamp":utcnow,"token":testtoken}]},
                     safe=True)
             # Test writing bcrypted password value to DB
             struct["password"] = "******"
             hashed_pw = _hash_pw(struct["password"])
             # patch bcrypt module to return the hash we just generated
             with patch("pyramid_registration.mongodb.bcrypt.hashpw"):
                 mongodb.bcrypt.hashpw.return_value = hashed_pw
                 backend.add_user(struct)
                 db.users.insert.assert_called_with({"username":struct["username"],"password":hashed_pw,
                     "access_tokens":[{"created_timestamp":utcnow,"token":testtoken}]},
                         safe=True)
             # Test writing email to DB
             struct["email"] = "*****@*****.**"
             del struct["password"]
             backend.add_user(struct)
             db.users.insert.assert_called_with({"username":struct["username"],"email":struct["email"], 
                 "access_tokens":[{"created_timestamp":utcnow,"token":testtoken}]},
                     safe=True)
Ejemplo n.º 3
0
    def test_simple_login(self, connection_mock):
        conn = connection_mock.instance()
        connection_mock.return_value = conn
        db = conn[self.settings["mongodb.db_name"]]
        backend = MongoDBRegistrationBackend(self.settings, self.config)

        password = "******"
        stored_user = {
            "username": "******",
            "password": _hash_pw("testpassword"),
            "email": "*****@*****.**"
        }

        # Test situation where user exists and we do password hash comparison
        db.users.find_one.return_value = stored_user
        self.assertTrue(backend.simple_login(stored_user["username"],
                                             password))

        # Test situation where user does not exist
        db.users.find_one.return_value = False
        self.assertFalse(
            backend.simple_login(stored_user["username"], password))
Ejemplo n.º 4
0
 def test_password_hash(self):
     p = "password"
     h = _hash_pw(p)
     self.assertTrue(_check_pw(p, h))
Ejemplo n.º 5
0
    def test_add_user(self, connection_mock):
        # Test bad username
        struct = {"username": "******"}
        conn = connection_mock.instance()
        connection_mock.return_value = conn
        db = conn[self.settings["mongodb.db_name"]]
        backend = MongoDBRegistrationBackend(self.settings, self.config)
        self.assertRaises(colander.Invalid, backend.add_user, struct)
        # Test good username that already exists in DB
        struct["username"] = "******"
        db.users.find_one.return_value = {"username": struct["username"]}
        self.assertRaises(colander.Invalid, backend.add_user, struct)
        # Test good, available username, writing it to DB
        db.users.find_one.return_value = None
        utcnow = datetime.datetime.utcnow()
        with patch("pyramid_registration.mongodb.make_access_token"):
            with patch("datetime.datetime"):
                testtoken = "testtoken"
                datetime.datetime.utcnow.return_value = utcnow
                mongodb.make_access_token.return_value = testtoken

                backend.add_user(struct)
                db.users.insert.assert_called_once_with(
                    {
                        "username":
                        struct["username"],
                        "access_tokens": [{
                            "created_timestamp": utcnow,
                            "token": testtoken
                        }]
                    },
                    safe=True)
                # Test writing bcrypted password value to DB
                struct["password"] = "******"
                hashed_pw = _hash_pw(struct["password"])
                # patch bcrypt module to return the hash we just generated
                with patch("pyramid_registration.mongodb.bcrypt.hashpw"):
                    mongodb.bcrypt.hashpw.return_value = hashed_pw
                    backend.add_user(struct)
                    db.users.insert.assert_called_with(
                        {
                            "username":
                            struct["username"],
                            "password":
                            hashed_pw,
                            "access_tokens": [{
                                "created_timestamp": utcnow,
                                "token": testtoken
                            }]
                        },
                        safe=True)
                # Test writing email to DB
                struct["email"] = "*****@*****.**"
                del struct["password"]
                backend.add_user(struct)
                db.users.insert.assert_called_with(
                    {
                        "username":
                        struct["username"],
                        "email":
                        struct["email"],
                        "access_tokens": [{
                            "created_timestamp": utcnow,
                            "token": testtoken
                        }]
                    },
                    safe=True)
Ejemplo n.º 6
0
 def test_password_hash(self):
     p = "password"
     h = _hash_pw(p)
     self.assertTrue(_check_pw(p, h))