def test_check_logged_user_props(self): u = { 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' } keys = ['_id', 'public_id', 'token', 'username', 'exp', 'login_time'] User().insert(u) user = User().find({'username': '******'}) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Logged user obj not created') self.assertIsInstance(logged_user.data, dict, 'User data is not a dict') self.assertListEqual(list(logged_user.data.keys()), keys, 'Keys are not equal') self.assertEqual(logged_user.data['public_id'], user.data['public_id'], 'Public id not equal') self.assertEqual(logged_user.data['username'], user.data['username'], 'Username not equal')
def test_token(self): u = { 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' } keys = ['_id', 'public_id', 'token', 'username', 'exp', 'login_time'] User().insert(u) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Logged user obj not created') self.assertIsInstance(logged_user.data, dict, 'User data is not a dict') self.assertListEqual(list(logged_user.data.keys()), keys, 'Keys are not equal') token = logged_user.data['token'] data = jwt.decode(token, JWT_ENC_KEY, algorithms=['HS256']) self.assertEqual(logged_user.data['public_id'], data['public_id'], 'Public id from token not equal')
def test_status(self): Customer().set_customer(TESTING_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'status', 'last_name': 'status', 'username': '******', 'email': 'status', 'password': '******' }) logged_user = Login().login(Auth('status', 'status')) self.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] } response = self.app.get('/status', headers=self.headers) self.assertEqual(response.status_code, 200, 'Status not found') data = json.loads(response.data) keys = ['is_up', 'data_usage', 'info'] self.assertEqual('docker' in data, True, 'Missing docker status') self.assertEqual('mongo' in data, True, 'Missing mongo status') for key in keys: self.assertEqual(key in data['docker'], True, key + ' missing in docker status') self.assertEqual(key in data['mongo'], True, key + ' missing in mongo status')
def test_login_existing_user(self): User().insert({ 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' }) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Existing user not logged in')
def setUp(self): Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'customers') User().insert({ 'type': 'admin', 'first_name': 'admin-user', 'last_name': 'admin-user', 'username': '******', 'email': 'admin-user', 'password': '******' }) logged_user = Login().login(Auth('admin-user', 'admin-user')) self.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] }
def __new__(cls, *args, **kwargs): if not cls.engine: cls.engine = super(TestingLogin, cls).__new__(cls, *args, **kwargs) Customer().set_customer(TESTING_DATABASE) MongoEngine().drop(TESTING_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'admin', 'last_name': 'admin', 'username': '******', 'email': '*****@*****.**', 'password': '******' }) logged_user = Login().login(Auth('admin', 'admin')) cls.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] } return cls.engine
def test_login_non_existing_user(self): logged_user = Login().login(Auth('123', '456')) self.assertEqual(logged_user, False, 'Non existing user logged in')