def get_storyboard_info(): test_account = request.args.get('account', default='no_input', type=str) test_password = request.args.get('password', type=str) mydlink_id = request.args.get('mydlink_id', type=str) start_ts = request.args.get('start', type=int) end_ts = request.args.get('end', type=int) if test_account == 'no_input': return render_template('getStoryBoard.html', account=test_account, error_info='NO ERROR') uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(test_account, test_password) uap.cnvr_get_storyboard_info(mydlink_id, [start_ts, end_ts]) res = uap.res.json() try: res_json = res['data'] except KeyError: if 'error' in res: return render_template('getStoryBoard.html', error_info=res['error']['message']) else: return render_template( 'getStoryBoard.html', error_info=f'Something wrong subid[{subs_uid}] sid[{sid}]') return render_template('getStoryBoard.html', storyboardProperty=res_json, account=test_account, mydlink_id=mydlink_id, start=start_ts, end=end_ts, error_info='NO ERROR')
def get_event_clip(): test_account = request.args.get('account', default='no_input', type=str) test_password = request.args.get('password', type=str) mydlink_id = request.args.get('mydlink_id', type=str) timestamp = request.args.get('timestamp', type=int) if test_account == 'no_input': return render_template('getEventClip.html', account=test_account, error_info='NO ERROR') uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(test_account, test_password) subs_uid = 'Not exist' uap.cnvr_query_subscription() try: subscription_list = uap.res.json()['data'] for subscription in subscription_list: if mydlink_id in subscription['devices']: subs_uid = subscription['subs_uid'] except KeyError: return render_template('getEventClip.html', account='no_input', error_info='Need to subscribe test DEV first.') uap.cnvr_init_playlist_session_from_timestamp(mydlink_id, subs_uid, timestamp) res = uap.res.json() sid = 'Not exist' try: res_json = res['data'] sid = res_json['session_id'] uap.cnvr_fetch_playlist(sid) url_pattern = re.compile(r'(https://\S*)') return_body = uap.res.text url_list = url_pattern.findall(return_body) except KeyError: if 'error' in res: return render_template('getEventClip.html', error_info=res['error']['message']) else: return render_template( 'getEventClip.html', error_info=f'Something wrong subid[{subs_uid}] sid[{sid}]') return render_template('getEventClip.html', url_list=url_list, account=test_account, mydlink_id=mydlink_id, timestamp=timestamp, error_info='NO ERROR')
class UserRelatedTest(BaseApiTest): uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) def setUp(self): super().setUp() self.uap.get_user_token(ACCOUNT, PASSWORD) def tearDown(self): super().tearDown() def test_get_user_info(self): self.uap.get_user_info() self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'email': ACCOUNT, }, ) res_json = self.uap.res.json()['data'] self.assertIn('first_name', res_json) @unittest.skip("Just skip") def test_update_account(self): f_name = 'Bruce' + str(int(time.time())) l_name = 'Wayne' + str(int(time.time())) # Set new name self.uap.update_account(ACCOUNT, PASSWORD, f_name, l_name) self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'first_name': f_name, 'last_name': l_name, }, ) # Check if setting successful self.uap.get_user_info() self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'first_name': f_name, 'last_name': l_name, }, )
def list_event(): test_account = request.args.get('account', default='no_input', type=str) test_password = request.args.get('password', type=str) sTs = request.args.get('start', type=int) eTs = request.args.get('end', type=int) if test_account == 'no_input': return render_template( 'listEvent.html', account=test_account, ) uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(test_account, test_password) event_data = [] file_property = [] event_property = {} try: uap.cnvr_list_event(sTs, eTs) data = uap.res.json()['data'] for row in data: if row['type'] == 'file': file_property.append(row) else: event_property['date'] = row['date'] event_property['first_event_ts'] = row['first_event_ts'] event_property['last_event_ts'] = row['last_event_ts'] event_property['has_more'] = row['has_more'] event_property['num'] = row['num'] raw_data = row['data'] for event in raw_data: tmp_dict = {} for key in event: tmp_dict[key] = json.dumps(event[key]) event_data.append(tmp_dict) except KeyError: event_data = 'NoData' print(file_property) return render_template( 'listEvent.html', start=sTs, end=eTs, event=event_data, eventProverty=event_property, fileProverty=file_property, eventNum=len(event_data), fileNum=len(file_property), account=test_account, )
def subs_dev(mydlink_id): uap = agent.Uap( 'qa-us-openapi.auto.mydlink.com', parameter.APP_ID(), parameter.APP_SECRET() ) uap.get_user_token(f'testqaid+{mydlink_id}@sqadt1.mydlink.com', 'mydlink') uap.cnvr_query_subscription() try: subs_uid = uap.res.json()['data'][0]['subs_uid'] if uap.res.json()['data']: print('Add dev to exist plan.') uap.cnvr_enable_device_subscription(subs_uid, [mydlink_id]) else: print('Create free trial and add dev to it.') uap.cnvr_subscribe_freetrial([mydlink_id]) except (IndexError): print('Create free trial and add dev to it.') uap.cnvr_subscribe_freetrial([mydlink_id]) uap.cnvr_query_subscription()
def dev_info(): test_account = request.args.get('account', default='no_input', type=str) test_password = request.args.get('password', type=str) if test_account == 'no_input': return render_template('getDevInfo.html', account=test_account, password=test_password) uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(test_account, test_password) try: uap.list_device() data = uap.res.json()['data'] dev_id_list = [] for dev in data: dev_id_list.append(dev["mydlink_id"]) dev_num = len(dev_id_list) start_idx = 0 dev_data = {} while start_idx < dev_num: uap.get_device_info(dev_id_list[start_idx:start_idx + 3]) res = uap.res.json()['data'] for dev in res: tmp_list = [] for key, val in dev.items(): tmp_list.append('{}: {}'.format(key, val)) dev_data[dev['mydlink_id']] = tmp_list start_idx += 3 except KeyError: dev_data = 'NoData' return render_template('getDevInfo.html', account=test_account, password=test_password, devData=dev_data)
# -------------------CONFIG---------------------------- MYDLINK_ID = '*************' ACCOUNT = '*************' PASSWORD = '******' URI = '*************' SOCKET_URL = "*************" CLIENT_ID = '*************' CLIENT_SECRET = '*************' CA_FILE = "*************" KEY_FILE = MYDLINK_ID + ".key" CERT_FILE = MYDLINK_ID + ".pem" # -------------------CONFIG---------------------------- uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(ACCOUNT, PASSWORD) import pathlib import ssl ssl.match_hostname = lambda cert, hostname: True ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ssl_context.load_cert_chain(certfile=CERT_FILE, keyfile=KEY_FILE, password=get_ssl_key_password(MYDLINK_ID)) ssl_context.load_verify_locations(CA_FILE) ssl_context.verify_mode = ssl.CERT_REQUIRED async def get_policy(): async with websockets.connect( SOCKET_URL, close_timeout=0, ssl=ssl_context,
class CnvrRelatedTest(BaseApiTest): uap = agent.Uap(URI, CLIENT_ID, CLIENT_SECRET) uap.get_user_token(ACCOUNT, PASSWORD) uap.cnvr_query_subscription() try: return_data = uap.res.json()['data'] SUBS_ID = return_data[0]['subs_uid'] DEV_ID = return_data[0]['devices'][0] except (AttributeError, IndexError): SUBS_ID = 'Query subcription fail' DEV_ID = 'Query subcription fail' end_time = datetime.now() start_time = end_time - timedelta(minutes=30) START_TS = int(start_time.timestamp()) END_TS = int(end_time.timestamp()) NO_EVENT = 'No record found for the given information.' def setUp(self): super().setUp() def tearDown(self): super().tearDown() def test_favorite_behavior(self): # Get event to add to favorite fav_id_list = [] event_ts = None self.uap.cnvr_list_event(self.START_TS, self.END_TS) res_json = self.uap.res.json()['data'][0]['data'] for event in res_json: try: if event['act'][0]['mydlink_id'] == self.DEV_ID and \ event['act'][0]['subs_uid'] == self.SUBS_ID: event_ts = event['act'][0]['timestamp'] except (KeyError, IndexError): continue if event_ts is None: return # Add event to favorite self.uap.cnvr_add_favorite(self.SUBS_ID, self.DEV_ID, event_ts) self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'result': True, }, ) # Check favorite event self.uap.cnvr_query_favorite_list(start_ts=self.START_TS, end_ts=self.END_TS) self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'result': True, }, ) res_json = self.uap.res.json()['data']['list'] self.assertIsInstance(res_json, (list)) ts_has_find = False for favorite in res_json: if int(favorite['timestamp']) == int(event_ts): fav_id_list.append(favorite['act'][0]['fav_id']) ts_has_find = True self.assertTrue(ts_has_find) # Remove favorite event self.assertRes( self.uap.res, 'data', { 'stat_code': 200, 'result': True, }, ) # Check favorite event remove self.uap.cnvr_query_favorite_list(start_ts=self.START_TS, end_ts=self.END_TS) res_json = self.uap.res.json()['data']['list'] ts_has_find = False for favorite in res_json: if int(favorite['timestamp']) == int(event_ts): ts_has_find = True self.assertFalse(res_json)