def setUp(self): super(WebSocketTestCase, self).setUp() DatabaseManager.add('default', ':memory:') User._users = UserManager() Token._tokens = TokenManager() self.username_0 = '*****@*****.**' self.password_0 = 'asdfasdf' self.username_1 = '*****@*****.**' self.password_1 = 'qwerqwer' self.user_0 = User(self.username_0, self.password_0, True) self.user_0.save() self.user_1 = User(self.username_1, self.password_1, True) self.user_1.save() self.token_0 = Token(user=self.user_0) self.token_0.save() self.token_1 = Token(user=self.user_1) self.token_1.save()
def setUp(self): super(HTTPTestCase, self).setUp() DatabaseManager.add("default", ":memory:") User._users = UserManager() Token._tokens = TokenManager() self.username_0 = "*****@*****.**" self.password_0 = "asdfasdf" self.username_1 = "*****@*****.**" self.password_1 = "qwerqwer" self.user_0 = User(self.username_0, self.password_0, True) self.user_0.save() self.user_1 = User(self.username_1, self.password_1, True) self.user_1.save() self.token_0 = Token(user=self.user_0) self.token_0.save() self.token_1 = Token(user=self.user_1) self.token_1.save()
def test_token(self): # The reason we're creating a new UserManager() here is because # there was a previous UserManager() instance created in another # test that was connected to a different database. So, we create # a new UserManager() that connects to the database we've created # in this test. User._users = UserManager() Token._tokens = TokenManager() user_0 = User('jim', 'asdf', True) user_0.save() user_1 = User('jane', 'qwer', False) user_1.save() token_0 = Token(user_0) token_0.save() with self.assertRaises(ValueError): token_0.save() self.assertEqual(token_0.id, 1, 'id is 1') token_0_1 = Token.tokens().get_token_for_user(user_0) self.assertEqual(token_0.id, token_0_1.id, 'ids are equal') self.assertEqual(token_0.user, token_0_1.user, 'users are equal') self.assertEqual(token_0.token, token_0_1.token, 'tokens are equal') self.assertEqual(token_0.created, token_0_1.created, 'created are equal') token_1 = Token(user_1) token_1.save() self.assertEqual(token_1.id, 2, 'id is 2') self.assertEqual(Token.tokens().count(), 2, '2 tokens saved') token_2 = Token(user_0) with self.assertRaises(ValueError): token_2.save() self.assertEqual(Token.tokens().count(), 2, 'still 2 tokens saved') self.assertEqual(len(Token.tokens().all()), 2, 'len of all is 2.') token_1.delete() self.assertEqual(token_1.id, None, 'token has no id') self.assertEqual(token_1.created, None, 'token has no created') self.assertEqual(Token.tokens().count(), 1, '1 token saved') token_1.delete() self.assertEqual(Token.tokens().count(), 1, 'still 1 token saved')
def test_delete_token(self, getuser, getpassword): User._users = None Token._tokens = None getuser.side_effect = ('*****@*****.**', '*****@*****.**',) getpassword.side_effect = ('asdfasdf',) user_0 = User('*****@*****.**', 'asdfasdf', True) user_0.save() user_1 = User('*****@*****.**', 'qwerqwer', False) user_1.save() temp_stdout = io.BytesIO() sys.stdout = temp_stdout self.app.deleteToken(self.args) sys.stdout = sys.__stdout__ temp_stdout.seek(0) self.assertTrue('Delete token: please authenticate...' in temp_stdout.readline()) self.assertTrue('[email protected] has no tokens to delete.' in temp_stdout.readline())
def test_user(self): # I need some way of deferring creation of UserManager # until DatabaseManager has been instantiated. # So that's why the import is inside this function, so # that it's called AFTER I've had a chance to call # DatabaseManager.add('default', ':memory:') from sremote.models.user import User user_0 = User('jack', 'asdf', True) self.assertEqual(user_0.username, 'jack') self.assertEqual(user_0.password, 'asdf') self.assertTrue(user_0.admin) self.assertEqual(user_0.id, None) self.assertEqual(user_0.created, None) self.assertEqual(User.users().count(), 0, 'No users in database.') user_0.save() self.assertEqual(User.users().count(), 1, '1 user in database.') self.assertEqual(user_0.id, 1, 'User has id=1.') self.assertLess(user_0.created, time.time(), 'User was created before now.') user_0.username = '******' user_0.admin = False user_0.save() # Now drop into sql to ensure that we really saved the object's username db = DatabaseManager.instance('default') select_user_query = 'SELECT * FROM user WHERE id=?;' select_result = db.query(select_user_query, (user_0.id,)).nextresult() self.assertEqual(select_result['username'], 'jill') self.assertEqual(select_result['admin'], 0) # Now test the UserManager.get() method: user_0_1 = User.users().get(id=user_0.id) user_0_2 = User.users().get(username=user_0.username) user_0_3 = User.users().get(id=user_0.id, username=user_0.username) self.assertEqual(user_0.username, user_0_1.username, 'usernames equal') self.assertEqual(user_0_1.username, user_0_2.username, 'usernames equal') self.assertEqual(user_0_1.username, user_0_3.username, 'usernames equal') self.assertEqual(user_0.id, user_0_1.id, 'ids equal') self.assertEqual(user_0_1.id, user_0_2.id, 'ids equal') self.assertEqual(user_0_1.id, user_0_3.id, 'ids equal') self.assertEqual(user_0.admin, user_0_1.admin, 'admin equal') self.assertEqual(user_0_1.admin, user_0_2.admin, 'admin equal') self.assertEqual(user_0_1.admin, user_0_3.admin, 'admin equal') self.assertTrue(user_0.authenticate('asdf'), 'User authenticates.') self.assertFalse(user_0.authenticate('qwer'), 'User does not authenticate.') # Now delete the object user_0.delete() self.assertEqual(User.users().count(), 0, 'No more users in database.') self.assertEqual(user_0.id, None, 'id is None') self.assertEqual(user_0.created, None, 'created is None') creation_count = 10 for i in range(creation_count): user = User('user%s' % i, 'asdf', True) user.save() self.assertEqual(User.users().count(), creation_count, 'count() == creation_count') all_users = User.users().all() self.assertEqual(creation_count, len(all_users), 'len == creation_count') user_1 = User('jill', 'qwer', True) self.assertFalse(user_1.authenticate('qwer'), 'User does not authenticate because it has not been saved.') self.assertFalse(user_1.authenticate('asdf'), 'User does not authenticate because it has not been saved.')
class WebSocketTestCase(AsyncHTTPTestCase): def setUp(self): super(WebSocketTestCase, self).setUp() DatabaseManager.add('default', ':memory:') User._users = UserManager() Token._tokens = TokenManager() self.username_0 = '*****@*****.**' self.password_0 = 'asdfasdf' self.username_1 = '*****@*****.**' self.password_1 = 'qwerqwer' self.user_0 = User(self.username_0, self.password_0, True) self.user_0.save() self.user_1 = User(self.username_1, self.password_1, True) self.user_1.save() self.token_0 = Token(user=self.user_0) self.token_0.save() self.token_1 = Token(user=self.user_1) self.token_1.save() def tearDown(self): DatabaseManager.remove('default') # ProcInfo.purge() def get_app(self): app = Application([ (r'/ws/', WSHandler), ]) return app @gen_test def test_wshandler_no_authorization(self): ws_url = 'ws://localhost:' + str(self.get_http_port()) + '/ws/' ws_client = yield websocket_connect(ws_url) ws_client.write_message(json.dumps({'msg':'update'})) response = yield ws_client.read_message() self.assertEqual(response, None, "No response from server because authorization not provided.") # self.assertEqual(len(WSHandler.connections), 0, "0 websocket connections.") @gen_test def test_wshandler_bad_authorization(self): ws_url = 'ws://localhost:' + str(self.get_http_port()) + '/ws/' ws_client = yield websocket_connect(ws_url, headers={'authorization':'asdf'}) ws_client.write_message(json.dumps({'msg':'update'})) response = yield ws_client.read_message() self.assertEqual(response, None, "No response from server because bad authorization provided.") # self.assertEqual(len(WSHandler.connections), 0, "0 websocket connections.") @gen_test def test_wshandler_authorization(self): ws_url = 'ws://localhost:' + str(self.get_http_port()) + '/ws/' ws_client = yield websocket_connect(ws_url, headers={'authorization':self.token_0.token}) ws_client.write_message(json.dumps({'msg':'update'})) response = yield ws_client.read_message() response_data = json.loads(response) self.assertEqual(response_data['msg'], 'updated', "Update response from server.") self.assertEqual(len(WSHandler.connections), 1, "1 websocket connection.")
class HTTPTestCase(AsyncHTTPTestCase): def setUp(self): super(HTTPTestCase, self).setUp() DatabaseManager.add("default", ":memory:") User._users = UserManager() Token._tokens = TokenManager() self.username_0 = "*****@*****.**" self.password_0 = "asdfasdf" self.username_1 = "*****@*****.**" self.password_1 = "qwerqwer" self.user_0 = User(self.username_0, self.password_0, True) self.user_0.save() self.user_1 = User(self.username_1, self.password_1, True) self.user_1.save() self.token_0 = Token(user=self.user_0) self.token_0.save() self.token_1 = Token(user=self.user_1) self.token_1.save() def tearDown(self): DatabaseManager.remove("default") ProcInfo.purge() def get_app(self): return Application( [url(r"/", HTTPVersionHandler), url(r"/status/", HTTPStatusHandler), url(r"/token/", HTTPTokenHandler)] ) def test_http_handler(self): response = self.fetch("/", method="GET") response_data = json.loads(response.body) self.assertTrue("version" in response_data) self.assertEqual(response.code, 200) def test_http_token_handler(self): headers = {"username": self.username_0, "password": self.password_0} response = self.fetch("/token/", method="GET", headers=headers) response_data = json.loads(response.body) self.assertEqual(response.code, 200) self.assertEqual(self.token_0.token, response_data["token"]) def test_http_status_handler(self): ProcInfo("proc_0", "group_0", 1, 20, "RUNNING", time.time()) ProcInfo("proc_1", "group_1", 2, 0, "STOPPED", time.time()) # Update ProcInfo 3 times. num_updates = 3 for i in range(num_updates): ProcInfo.updateall() headers = {"authorization": self.token_0.token} response = self.fetch("/status/", method="GET", headers=headers) response_data = json.loads(response.body) self.assertEqual(response.code, 200) self.assertTrue("processes" in response_data) self.assertEqual(len(response_data["processes"][0]["cpu"]), num_updates, "cpu length is %s" % num_updates) self.assertEqual(len(response_data["processes"][0]["mem"]), num_updates, "mem length is %s" % num_updates)
def test_delete_user(self, getuser, getpassword): getuser.side_effect = ('*****@*****.**', '*****@*****.**',) getpassword.side_effect = ('asdfasdf',) User._users = None Token._tokens = None user_0 = User('*****@*****.**', 'asdfasdf', True) user_0.save() user_1 = User('*****@*****.**', 'qwerqwer', False) user_1.save() user_3 = User('*****@*****.**', 'zxcvzxcv', False) user_3.save() user_4 = User('*****@*****.**', 'wertwert', True) user_4.save() # deleteUser will create a new database connection, so best to null out the current # one first! This will force User.user() to access the new default database. # I hate this hack!!!! User._users = None Token._tokens = None temp_stdout = io.BytesIO() sys.stdout = temp_stdout self.app.deleteUser(self.args) sys.stdout = sys.__stdout__ temp_stdout.seek(0) self.assertEqual(temp_stdout.readline(), 'Delete user: please authenticate...\n') self.assertEqual(temp_stdout.readline(), 'Deleted user [email protected].\n')
def test_list_users(self, getuser, getpassword): User._users = None Token._tokens = None user_0 = User('*****@*****.**', 'asdfasdf', True) user_0.save() user_1 = User('*****@*****.**', 'qwerqwer', False) user_1.save() user_3 = User('*****@*****.**', 'zxcvzxcv', False) user_3.save() user_4 = User('*****@*****.**', 'wertwert', True) user_4.save() temp_stdout = io.BytesIO() sys.stdout = temp_stdout self.app.listUsers(self.args) sys.stdout = sys.__stdout__ temp_stdout.seek(0) self.assertTrue('Username' in temp_stdout.readline()) self.assertTrue('*****@*****.**' in temp_stdout.readline())