def _insert_user(): """Insert starting default entries into the User table. Args: None Returns: None """ # Insert into User if user.idx_exists(1) is False: password = ''.join( random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(50)) user.insert_row( DbRowUser(username='******', password=data.hashstring(password), first_name='pattoo', last_name='pattoo', user_type=1, change_password=1, enabled=0)) # Insert admin into User table if user.idx_exists(2) is False: password = '******' user.insert_row( DbRowUser(username='******', password=data.hashstring(password), first_name='admin', last_name='admin', user_type=0, change_password=0, enabled=1))
def _insert_user(): """Insert starting default entries into the User table. Args: None Returns: default_users: dictionary containing the default user credentials """ default_users = [] # Insert into User if user.idx_exists(1) is False: # Creating initial password password = data.hashstring(''.join(random.SystemRandom().choice( string.ascii_uppercase + string.digits) for _ in range(50))) # Inserting default user user.insert_row( DbRowUser( username='******', password=password, first_name='pattoo', last_name='pattoo', role=1, password_expired=1, enabled=0) ) default_users.append(('pattoo', password, 1)) # Insert admin into User table if user.idx_exists(2) is False: # Creating initial password password = data.hashstring(''.join(random.SystemRandom().choice( string.ascii_uppercase + string.digits) for _ in range(50))) user.insert_row( DbRowUser( username='******', password=password, first_name='admin', last_name='admin', role=0, password_expired=1, enabled=1) ) default_users.append(('admin', password, 0)) return default_users
def main(): """Test all the pattoo modules with unittests. Args: None Returns: None """ # Set up parser parser = argparse.ArgumentParser() parser.add_argument( '--username', '-u', help='Username to create', type=str, default='*****@*****.**') args = parser.parse_args() # Determine whether user already exists username = args.username if bool(user.exists(username)) is False: row = DbRowUser( username=username, password=data.hashstring(str(random())), first_name='Pattoo: {}'.format(username), last_name='Test: {}'.format(username), enabled=True, ) user.insert_row(row)
def test_exists(self): """Testing method or function named "exists".""" # Create a translation uname = data.hashstring(str(random())) passwrd = data.hashstring(str(random())) f_name = data.hashstring(str(random())) l_name = data.hashstring(str(random())) # Make sure it does not exist result = user.exists(uname) self.assertFalse(bool(result)) # Add database row user.insert_row( DbRowUser( username=uname, password=passwrd, first_name=f_name, last_name=l_name, role=1, password_expired=0, enabled=0 ) ) # Make sure it exists result = user.exists(uname) self.assertTrue(bool(result))
def test_idx_exists(self): """Testing method or function named "idx_exists".""" # Add entry to database uname = data.hashstring(str(random())) passwrd = data.hashstring(str(random())) f_name = data.hashstring(str(random())) l_name = data.hashstring(str(random())) user.insert_row( DbRowUser( username=uname, password=passwrd, first_name=f_name, last_name=l_name, role=1, password_expired=1, enabled=0 ) ) # Make sure it exists idx_user = user.exists(uname) # Verify that the index exists result = user.idx_exists(idx_user) self.assertTrue(result)
def _random_user(): """Create a random user in the database. Args: value: New value to apply Returns: result: DbRowUser object """ result = DbRowUser(username=data.hashstring(str(random())), password=data.hashstring(str(random())), first_name=data.hashstring(str(random())), last_name=data.hashstring(str(random())), role=1, password_expired=1, enabled=0) return result
def test_exists(self): """Testing method or function named "exists".""" # Add user entry to database uname = data.hashstring(str(random())) passwrd = data.hashstring(str(random())) salt_ = data.hashstring(str(random())) f_name = data.hashstring(str(random())) l_name = data.hashstring(str(random())) user.insert_row( DbRowUser( username=uname, password=passwrd, first_name=f_name, role=0, password_expired=1, last_name=l_name, enabled=0, )) # Make sure user entry exists idx_user = user.exists(uname) # Add chart entry to database chart_name = data.hashstring(str(random())) chart_checksum = data.hashstring(str(random())) chart.insert_row( DbRowChart(name=chart_name, checksum=chart_checksum, enabled=0)) # Make sure chart entry exists idx_chart = chart.exists(chart_checksum) # Make sure favorite does not exist result = favorite.exists(idx_chart, idx_user) self.assertFalse(bool(result)) # Add favorite to database favorite.insert_row( DbRowFavorite(idx_chart=idx_chart, idx_user=idx_user, order=0, enabled=0)) # Make sure favorite exists result = favorite.exists(idx_chart, idx_user) self.assertTrue(bool(result))
def test_idx_exists(self): """Testing method or function named "idx_exists".""" # Add user entry to database uname = data.hashstring(str(random())) passwrd = data.hashstring(str(random())) f_name = data.hashstring(str(random())) l_name = data.hashstring(str(random())) user.insert_row( DbRowUser(username=uname, password=passwrd, first_name=f_name, last_name=l_name, user_type=1, change_password=1, enabled=0)) # Make sure user entry exists idx_user = user.exists(uname) self.assertTrue(bool(idx_user)) # Add chart entry to database chart_name = data.hashstring(str(random())) chart_checksum = data.hashstring(str(random())) chart.insert_row( DbRowChart(name=chart_name, checksum=chart_checksum, enabled=0)) # Make sure chart entry exists idx_chart = chart.exists(chart_checksum) self.assertTrue(bool(idx_chart)) # Add favorite to database favorite.insert_row( DbRowFavorite(idx_chart=idx_chart, idx_user=idx_user, order=0, enabled=0)) # Make sure the favorite exists idx_favorite = favorite.exists(idx_chart, idx_user) # Verify that the index exists result = favorite.idx_exists(idx_favorite) self.assertTrue(result)
def test_insert_row(self): """Testing method or function named "insert_row".""" # Add an entry to the database uname = data.hashstring(str(random())) password = data.hashstring(str(random())) expected = DbRowUser( username=uname, password=password, first_name=data.hashstring(str(random())), last_name=data.hashstring(str(random())), role=1, password_expired=1, enabled=0 ) user.insert_row(expected) # Make sure it exists idx_user = user.exists(uname) # Verify the index exists result = user.idx_exists(idx_user) self.assertTrue(result) # Verify that all the parameters match with db.db_query(20091) as session: row = session.query( User).filter(User.username == uname.encode()).one() self.assertEqual(expected.username, row.username.decode()) self.assertEqual(expected.first_name, row.first_name.decode()) self.assertEqual(expected.last_name, row.last_name.decode()) self.assertEqual(expected.role, row.role) self.assertEqual(expected.password_expired, row.password_expired) self.assertEqual(expected.enabled, row.enabled) # Test password salt = row.password.decode().split('$')[2] password_hash = crypt.crypt(password, '$6${}'.format(salt)) self.assertEqual(row.password.decode(), password_hash)
def test_route_graphql(self): """Testing method / function add_url_rule (graphql).""" # Initialize key variables _data = [] pattoo_checksum = data.hashstring(str(random())) pattoo_key = data.hashstring(str(random())) agent_id = data.hashstring(str(random())) pattoo_agent_polled_target = data.hashstring(str(random())) _pi = 300 * 1000 data_type = DATA_FLOAT timestamp = int(time.time()) * 1000 pattoo_value = round(uniform(1, 100), 5) insert = PattooDBrecord( pattoo_checksum=pattoo_checksum, pattoo_key=pattoo_key, pattoo_agent_id=agent_id, pattoo_agent_polling_interval=_pi, pattoo_timestamp=timestamp, pattoo_data_type=data_type, pattoo_value=pattoo_value, pattoo_agent_polled_target=pattoo_agent_polled_target, pattoo_agent_program='pattoo_agent_program', pattoo_agent_hostname='pattoo_agent_hostname', pattoo_metadata=[]) # Create checksum entry in the DB, then update the data table idx_datapoint = datapoint.idx_datapoint(insert) _data.append( IDXTimestampValue(idx_datapoint=idx_datapoint, polling_interval=_pi, timestamp=timestamp, value=pattoo_value)) # Insert rows of new data lib_data.insert_rows(_data) # Creating required test admin test_admin = { "username": "******", "first_name": "Pattoo Test", "last_name": "Pattoo Test", "password": "******", "role": 0, "password_expired": 0, "enabled": 1 } user.insert_row(DbRowUser(**test_admin)) # Get accesss token to make test queries acesss_query = ('''\ mutation{ authenticate(Input: {username: "******", password: "******"}) { accessToken refreshToken } } ''') # Replacing username and password in access_query acesss_query = acesss_query.replace("USERNAME", test_admin['username']) acesss_query = acesss_query.replace("PASSWORD", test_admin['password']) access_request = _query(acesss_query, query_type="Mutation") acesss_token = access_request['data']['authenticate']['accessToken'] # Test query = ('''\ { allDatapoints(idxDatapoint: "IDX", token: "TOKEN") { edges { node { checksum } } } } ''') # Replacing IDX and TOKEN in query query = query.replace("IDX", str(idx_datapoint)) query = query.replace("TOKEN", str(acesss_token)) # Test graphql_result = _query(query) result = graphql_result['data']['allDatapoints']['edges'][0]['node'] self.assertEqual(result['checksum'], pattoo_checksum)