def test_load_user_file(self): user = UserInfo("no_date", "Luke Murphey", ["admin", "power"]) user.save(self.tmp_dir) # Try to load the given user loaded_user = UserInfo.getUserInfo(user.username, self.tmp_dir) self.assertEqual(user.username, loaded_user.username)
def test_clear_user_cache_by_time(self): # Make some users old_account = UserInfo("old_account", "Luke Murphey", ["admin", "power"], 1000) old_account2 = UserInfo("old_account2", "Luke Murphey", ["admin", "power"], 2000) no_date = UserInfo("no_date", "Luke Murphey", ["admin", "power"]) new_account = UserInfo("new_account", "Luke Murphey", ["admin", "power"]) new_account.updateLastLogin() old_account.save(self.tmp_dir ) old_account2.save(self.tmp_dir ) no_date.save(self.tmp_dir ) new_account.save(self.tmp_dir ) # Confirm the number of entries after creation self.assertEqual(len(UserInfo.getAllUsers(self.tmp_dir)), 4) # Clear the cache removed_entries = UserInfo.clearCache(30, self.tmp_dir) self.assertEqual(len(removed_entries), 2) # Confirm the number of entries after deletion users = UserInfo.getAllUsers(self.tmp_dir) self.assertEqual(len(users), 2) # Clear all entries from the cache removed_entries = UserInfo.clearCache(0, self.tmp_dir) self.assertEqual(len(removed_entries), 2) # Confirm the number of entries after deletion users = UserInfo.getAllUsers(self.tmp_dir) self.assertEqual(len(users), 0)
def test_load_users(self): user_lmurphey = UserInfo("lmurphey", "Luke Murphey", ["admin", "power"]) self.assertTrue(user_lmurphey.save(self.tmp_dir ), "File was not saved properly" ) user_jdoe = UserInfo("jdoe", "Jane Doe", ["admin", "power"]) self.assertTrue(user_jdoe.save(self.tmp_dir ), "File was not saved properly" ) users = UserInfo.getAllUsers(self.tmp_dir ) self.assertEqual(len(users), 2) for user in users: if user.username == "lmurphey": self.assertEqual(user_lmurphey.load_signature, user.load_signature) elif user.username == "jdoe": self.assertEqual(user_jdoe.load_signature, user.load_signature)
def test_get_users(self): # Create a user-info object to load user_info = UserInfo("jdoe", "John Doe", ["admin", "power"]) self.assertTrue(user_info.save(self.tmp_dir ), "File was not saved properly" ) user_info2 = UserInfo("alincoln", "Abraham Lincoln", ["power"]) self.assertTrue(user_info2.save(self.tmp_dir ), "File was not saved properly" ) # Redirect output to a string so that we can test it out = StringIO() # Try to get the users list getUsers(None, out, self.tmp_dir ) # Test the output if out.getvalue().strip() not in ["--status=success --userInfo=;alincoln;Abraham Lincoln;power --userInfo=;jdoe;John Doe;admin:power", "--status=success --userInfo=;jdoe;John Doe;admin:power --userInfo=;alincoln;Abraham Lincoln;power"]: self.fail("The output of getUsers() was not what was expected: " + out.getvalue().strip())
def test_clear_user_cache(self): # Save a user to the cache ui_before = UserInfo("lmurphey2", "Luke Murphey", ["admin", "power"]) self.assertTrue(ui_before.save(self.tmp_dir ), "File was not saved properly") # Clear the user info self.assertTrue(UserInfo.clearUserInfo("lmurphey2", os.path.join(self.tmp_dir))) # Verify the file doesn't exist self.assertFalse(os.path.isfile(os.path.join(self.tmp_dir, "70fce03a92dd14d910839bccbd1b474b.json")))
def test_save_and_load(self): ui_before = UserInfo("lmurphey", "Luke Murphey", ["admin", "power"]) self.assertTrue(ui_before.save(self.tmp_dir ), "File was not saved properly" ) ui_after = UserInfo.load("lmurphey", self.tmp_dir) self.assertEqual(ui_after.realname, ui_before.realname) self.assertEqual(ui_after.username, ui_before.username) self.assertEqual(ui_after.roles, ui_before.roles) self.assertEqual(ui_after.load_signature, ui_before.load_signature)
def test_save_and_load_file(self): ui_before = UserInfo("lmurphey", "Luke Murphey", ["admin", "power"]) ui_before.updateLastLogin() ui_before.updateLoadSignature() self.assertTrue(ui_before.save(self.tmp_dir ), "File was not saved properly" ) ui_after = UserInfo.loadFile(os.path.join(self.tmp_dir, "3f0d712f3d3d99e26aa3a45a1f37494c.json") ) self.assertEqual(ui_after.realname, ui_before.realname) self.assertEqual(ui_after.username, ui_before.username) self.assertEqual(ui_after.roles, ui_before.roles) self.assertEqual(ui_after.lastLoginTime, ui_before.lastLoginTime) self.assertEqual(ui_after.load_signature, ui_before.load_signature)
def test_get_user_info(self): # Create a user-info object to load user_info = UserInfo(self.username, "John Doe", ["admin", "power"]) self.assertTrue(user_info.save(self.tmp_dir ), "File was not saved properly") # Redirect output to a string so that we can test it out = StringIO() # Build the input args = {} args[USERNAME] = self.username # Try to get the user info getUserInfo(args, out, self.tmp_dir) # Test the output self.assertEqual(out.getvalue().strip(), "--status=success --userInfo=;" + bytesToString(self.username) + ";John Doe;admin:power")