def create_accounts(self): """ Creates accounts with users bucket lists and bucket list items :return: dictionary with the 2 new unique users to test """ user1_profile, user2_profile = self.create_user_profiles() user1_account = UserAccount(username="******", email="*****@*****.**", password="******", registered_on=datetime.now(), user_profile_id=user1_profile.id) user2_account = UserAccount(username="******", email="*****@*****.**", password="******", registered_on=datetime.now(), user_profile_id=user2_profile.id) try: db.session.add(user2_account) db.session.add(user1_account) db.session.commit() except IntegrityError as ie: app_logger.exception("Encountered error creating values {}".format(ie)) db.session.rollback() return user1_account, user2_account
def test_valid_confirmation_token(self): """Test generated token for a user can be confirmed to belong to them""" u = UserAccount(email="*****@*****.**", username="******", password='******') db.session.add(u) db.session.commit() token = u.generate_confirmation_token() self.assertTrue(u.confirm_token(token))
def test_ping(self): """Test pinging a user updates their last seen timestamp""" u = UserAccount(email="*****@*****.**", username="******", password='******') db.session.add(u) db.session.commit() time.sleep(2) last_seen_before = u.last_seen u.ping() self.assertTrue(u.last_seen > last_seen_before)
def test_valid_reset_token(self): """Test a user can reset their password given a valid reset token""" u = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u) db.session.commit() token = u.generate_reset_token() self.assertTrue(u.reset_password(token, 'dog')) self.assertTrue(u.verify_password('dog'))
def test_valid_email_change_token(self): """Test valid email change token allows user to change their email""" u1 = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u1) db.session.commit() token = u1.generate_email_change_token("*****@*****.**") self.assertTrue(u1.change_email(token)) self.assertEqual(u1.email, "*****@*****.**")
def test_expired_confirmation_token(self): """Test a user can not confirm their token when expiration elapses""" u = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u) db.session.commit() token = u.generate_confirmation_token(1) time.sleep(2) self.assertFalse(u.confirm_token(token))
def test_to_json(self): u = UserAccount(username="******", email='*****@*****.**', password='******') db.session.add(u) db.session.commit() json_user = u.to_json() expected_keys = [ "id", 'uuid', 'username', 'member_since', 'last_seen', 'profile_id', 'account_status_id', 'email', "date_created", "date_modified", "registered_on", "confirmed", "confirmed_on" ] self.assertEqual(sorted(json_user.keys()), sorted(expected_keys))
def test_duplicate_email_change_token(self): """Test user can not change their current email to that of another existing email""" u1 = UserAccount(username="******", email='*****@*****.**', password='******') u2 = UserAccount(username="******", email='*****@*****.**', password='******') db.session.add(u1) db.session.add(u2) db.session.commit() token = u2.generate_email_change_token('*****@*****.**') self.assertFalse(u2.change_email(token)) self.assertTrue(u2.email == '*****@*****.**')
def test_password_getter(self): """Test that user password can not be retrieved""" user_account = UserAccount(password="******") with self.assertRaises(AttributeError) as ctx: user_account.password self.assertIn("Password is not a readable attribute", ctx.exception)
def test_invalid_reset_token(self): """Test that valid reset token from one user can not reset another users password""" u1 = UserAccount(username="******", email="*****@*****.**", password='******') u2 = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u1) db.session.add(u2) db.session.commit() token = u1.generate_reset_token() self.assertFalse(u2.reset_password(token, "horse")) self.assertTrue(u2.verify_password("doge"))
def test_invalid_confirmation_token(self): """Test one user token can not confirm another user's token""" u1 = UserAccount(email="*****@*****.**", username="******", password='******') u2 = UserAccount(email="*****@*****.**", username="******", password='******') db.session.add(u1) db.session.add(u2) db.session.commit() token = u1.generate_confirmation_token() self.assertFalse(u2.confirm_token(token))
def test_invalid_email_change_token(self): """Test valid email change token from 1 user can not update another users email""" u1 = UserAccount(username="******", email="*****@*****.**", password='******') u2 = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u1) db.session.add(u2) db.session.commit() token = u2.generate_email_change_token("*****@*****.**") self.assertFalse(u1.change_email(token)) self.assertEqual(u1.email, "*****@*****.**")
def test_password_verification(self): """_____Successful password decryption should equal entered password""" author = UserAccount(password='******') self.assertTrue(author.verify_password('cat')) self.assertFalse(author.verify_password('dog'))
def test_password_salts_are_random(self): """Test that password salts are always random for 2 users""" user_account = UserAccount(password="******") user_account_1 = UserAccount(password="******") self.assertNotEqual(user_account.password_hash, user_account_1.password_hash)
def test_password_verification(self): """Test password verification""" user_account = UserAccount(password="******") self.assertTrue(user_account.verify_password("dog")) self.assertFalse(user_account.verify_password("cat"))
def test_password_setter(self): """Test that password is set on registration of a new user""" user_account = UserAccount(password="******") self.assertIsNotNone(user_account.password_hash)
def test_timestamps(self): """Test member since attribute is updateed when a user registers""" u = UserAccount(username="******", email="*****@*****.**", password='******') db.session.add(u) db.session.commit() self.assertTrue((datetime.now() - u.member_since).total_seconds() < 3)