def setUp(self): self.db = Database() admin.set_db() self.email = "*****@*****.**" self.password = "******" self.device_name = "samsung" self.providername = "test" self.providerpassword = "******" self.providerdevice = "techno" self.reward = 20 self.dog_breed = "bulldog" self.breed_image = "dog.jpg" self.stub = admin.get_session_manager_stub()
def setUp(self): urllib.request.urlretrieve( "https://www.statnews.com/wp-content/uploads/2019/08/AdobeStock_182445295-645x645.jpeg", "dog.jpg") self.channel = grpc.insecure_channel('localhost:5001') self.stub = sm_pb2_grpc.SessionManagerStub(self.channel) self.email = "*****@*****.**" self.password = "******" self.device_name = "samsungnew1" self.img = "dog.jpg" self.access_token = "0A" self.timeout = 20 self.min_token = 23 self.db = Database()
class HealthServicer(heartb_pb2_grpc.HealthServicer): def __init__(self, db_file="sessions.db", db_create=False): self.db = Database(db_file=db_file, db_create=db_create) def Check(self, request, context): response = heartb_pb2.HealthCheckResponse() response.status = HealthStatus.UNKNOWN if request.service == "opencog_services": ocs = self.db.query(Service, service_name="OPENCOG_SERVICES") if ocs: s = self.create_health_stub(ocs.service_host, ocs.service_port) response.status = self.get_server_status(s) else: response.status = HealthStatus.NOT_SERVING return response @staticmethod def create_health_stub(host, port): try: channel = grpc.insecure_channel("{}:{}".format(host, port)) stub = heartb_pb2_grpc.HealthStub(channel) return stub except Exception as e: log.error(e) @staticmethod def get_server_status(stub): try: stub.Check(heartb_pb2.HealthCheckRequest(service=""), timeout=10) return HealthStatus.SERVING except Exception as e: log.error(e) return HealthStatus.NOT_SERVING
def __init__( self, port=nunet_port, ): self.db = Database() self.port = port self.server = None self.timeout = 30
def __init__(self, db_file="sessions.db", db_create=False, port=50000): self.db_file = db_file self.db_create = db_create self.db = Database(db_file=db_file, db_create=db_create) self.port = port self.server = None self.timeout = 30
def test_update(self): self.db.add(ConsumerDevice, device_name=self.device_name, access_token=secrets.token_urlsafe(), email=self.email) before_update_tkn = self.db.query(ConsumerDevice, device_name=self.device_name, email=self.email).access_token devices = self.db.query_all(ConsumerDevice, email=self.email) self.assertEqual(devices[0].email, self.email) access_token = secrets.token_urlsafe() self.db.update(ConsumerDevice, where={ "email": self.email, }, update={"access_token": access_token}) after_update_tkn = Database().query(ConsumerDevice, device_name=self.device_name, email=self.email).access_token self.assertNotEqual(before_update_tkn, after_update_tkn)
def set_db(logger=None): global db db = Database(logger)
def setUp(self, db_file="sessions.db", db_create=True, logger=None): global db db = Database(db_file, db_create, logger)
def set_db(db_file, db_create=False, logger=None): global db db = Database(db_file, db_create, logger)
def __init__(self, db_file="sessions.db", db_create=False): self.db = Database(db_file=db_file, db_create=db_create)
class TestSessionManager(unittest.TestCase): def setUp(self): self.db = Database() admin.set_db() self.email = "*****@*****.**" self.password = "******" self.device_name = "samsung" self.providername = "test" self.providerpassword = "******" self.providerdevice = "techno" self.reward = 20 self.dog_breed = "bulldog" self.breed_image = "dog.jpg" self.stub = admin.get_session_manager_stub() def test_a_add_credential(self): #before adding credential test_1 = self.db.query(ConsumerCredential, email=self.email) self.assertIsNone(test_1) #after adding credential admin.add_credential(self.email, self.password) test_1 = self.db.query(ConsumerCredential, email=self.email) self.assertIsNotNone(test_1) self.assertEqual(test_1.email, self.email) def test_b_add_device(self): #before adding device test_1 = self.db.query(ConsumerDevice, email=self.email) self.assertIsNone(test_1) #after adding device admin.add_device(self.email, self.password, self.device_name) test_1 = self.db.query(ConsumerDevice, email=self.email) self.assertIsNotNone(test_1.device_name) self.assertEqual(test_1.device_name, self.device_name) def test_c_activate_device(self): admin.activate_device(self.email, self.password, self.device_name) test_1 = self.db.query(ConsumerCredential, email=self.email, active_device=self.device_name) self.assertEqual(test_1.active_device, self.device_name) def test_d_add_providerdevice(self): #before adding test_1 = self.db.query(ProviderDevice, memory_limit=1.1) self.assertIsNone(test_1) #after adding admin.add_provider_device(device_name=self.providerdevice, memory_limit=1.1, net_limit=1.1, cpu_limit=1.1, up_time_limit=1.1, cpu_price=1.1, ram_price=1.1, net_price=1.1) test_1 = self.db.query(ProviderDevice, memory_limit=1.1) self.assertEqual(test_1.device_name, self.providerdevice) def test_e_add_dog_breeds(self): #before adding self.db.delete(RewardTable, breed_image=self.breed_image) test_1 = self.db.query(RewardTable, breed_image=self.breed_image, reward=self.reward) self.assertIsNone(test_1) #after adding admin.add_dog_breeds(self.dog_breed, self.breed_image, self.reward) test_1 = self.db.query(RewardTable, breed_image=self.breed_image, reward=self.reward) self.assertIsNotNone(test_1) self.assertEqual(test_1.breed_name, self.dog_breed) def test_f_delete_one_credential(self): #before adding credential new_email = "*****@*****.**" test_1 = self.db.query(ConsumerCredential, email=new_email) self.assertIsNone(test_1) #after adding credential admin.add_credential(new_email, self.password) test_1 = self.db.query(ConsumerCredential, email=new_email) self.assertIsNotNone(test_1) self.assertEqual(test_1.email, new_email) #after deleting credential admin.delete_one_credential(new_email) test_1 = self.db.query(ConsumerCredential, email=new_email) self.assertIsNone(test_1) def test_g_admin_credential(self): admin.delete_all_credentials() #before adding credential test_1 = self.db.query_all(ConsumerCredential, email=self.email) self.assertEqual([], test_1) #adding credential admin.add_credential(self.email, self.password) test_1 = self.db.query(ConsumerCredential, email=self.email) self.assertEqual(test_1.email, self.email) #after deleting credential admin.delete_all_credentials() test_1 = self.db.query(ConsumerCredential, email=self.email) self.assertIsNone(test_1) def test_e2_delete_device(self): #after deletion admin.delete_device(self.email, self.password, self.device_name) test_1 = self.db.query(ConsumerDevice, email=self.email) self.assertIsNone(test_1)
def setUp(self): self.db = Database() self.email = "*****@*****.**" self.password = "******" self.device_name = "samsung"
class TestSessionManager(unittest.TestCase): def setUp(self): self.db = Database() self.email = "*****@*****.**" self.password = "******" self.device_name = "samsung" def test_create(self): #ConsumerCredential #should save ConsumerCredential self.db.add(ConsumerCredential, email=self.email, password=self.db.hash_password(self.password)) test_1 = self.db.query(ConsumerCredential, email=self.email) print(test_1.email) self.assertIsNotNone(test_1) self.assertEqual(test_1.email, self.email) #should not save again self.db.add(ConsumerCredential, email=self.email, password=self.db.hash_password(self.password)) test_1 = self.db.query_all(ConsumerCredential, email=self.email) self.assertEqual(len(test_1), 1) def test_hashpassword(self): expected = bcrypt.hashpw(self.password.encode('utf-8'), bcrypt.gensalt()) self.assertIsNotNone(self.db.hash_password(self.password)) self.assertEqual(len(expected), len(self.db.hash_password(self.password))) def test_checkpassword(self): self.assertTrue( self.db.check_password(self.password, self.db.hash_password(self.password))) self.assertFalse( self.db.check_password("@1efakepassword", self.db.hash_password(self.password))) def test_query(self): lattest_email = "*****@*****.**" lattest_password = "******" self.db.add(ConsumerCredential, email=lattest_email, password=self.db.hash_password(lattest_password)) test_1 = self.db.query(ConsumerCredential, email=lattest_email) self.assertEqual(test_1.email, lattest_email) self.assertNotEqual(test_1.email, self.email) test_1 = self.db.query(ConsumerCredential, email=lattest_email, password=lattest_password) self.assertEqual(test_1.email, lattest_email) test_1 = self.db.query(ConsumerCredential, email="*****@*****.**", password=lattest_password) self.assertIsNone(test_1) def test_update(self): self.db.add(ConsumerDevice, device_name=self.device_name, access_token=secrets.token_urlsafe(), email=self.email) before_update_tkn = self.db.query(ConsumerDevice, device_name=self.device_name, email=self.email).access_token devices = self.db.query_all(ConsumerDevice, email=self.email) self.assertEqual(devices[0].email, self.email) access_token = secrets.token_urlsafe() self.db.update(ConsumerDevice, where={ "email": self.email, }, update={"access_token": access_token}) after_update_tkn = Database().query(ConsumerDevice, device_name=self.device_name, email=self.email).access_token self.assertNotEqual(before_update_tkn, after_update_tkn) def test_delete(self): self.db.add(ConsumerDevice, device_name="device2", access_token=secrets.token_urlsafe(), email=self.email) before_delete = self.db.query(ConsumerDevice, device_name="device2", email=self.email) self.db.delete(ConsumerDevice, device_name="device2", email=self.email) after_delete = self.db.query(ConsumerDevice, device_name="device2", email=self.email) self.assertNotEqual(before_delete, after_delete) def drop(self): self.db.drop
class TestSessionManagerServer(unittest.TestCase): sms = None @classmethod def setUpClass(self): self.sms = SessionManagerServer(port=5001) self.sms.start_server() @classmethod def tearDownClass(self): self.sms = None def setUp(self): urllib.request.urlretrieve( "https://www.statnews.com/wp-content/uploads/2019/08/AdobeStock_182445295-645x645.jpeg", "dog.jpg") self.channel = grpc.insecure_channel('localhost:5001') self.stub = sm_pb2_grpc.SessionManagerStub(self.channel) self.email = "*****@*****.**" self.password = "******" self.device_name = "samsungnew1" self.img = "dog.jpg" self.access_token = "0A" self.timeout = 20 self.min_token = 23 self.db = Database() def test_a_signup(self): self.db.query(ConsumerCredential, email=self.email) with grpc.insecure_channel("localhost:{}".format(5001)) as channel: user_1 = sampleclient.signup(self.stub, self.email, self.password) cred = self.db.query(ConsumerCredential, email=self.email, password=self.password) self.assertIsNotNone(cred) self.assertEqual(cred.email, self.email) self.assertEqual(user_1.status, Status.OK) def test_b_login(self): device_1 = sampleclient.login(self.stub, self.email, self.password, self.device_name) self.assertIsNotNone(device_1) device = self.db.query(ConsumerDevice, email=self.email, device_name=self.device_name) self.assertEqual(self.device_name, device.device_name) def test_c_logout(self): #before logout device_1 = sampleclient.login(self.stub, self.email, self.password, self.device_name) login_1 = sampleclient.login(self.stub, self.email, self.password, self.device_name) self.assertEqual(login_1, device_1) #after logout sampleclient.logout(self.stub, self.device_name, device_1) login_1 = sampleclient.login(self.stub, self.email, self.password, self.device_name) self.assertNotEqual(login_1, self.access_token) self.db.delete(ConsumerCredential, email=self.email, password=self.password)