def test_exists(db, session): # pylint: disable=unused-argument """Verify that the exists function works correctly.""" # if there is no imei in the table imei_norm = '64728204390652' model_exists = ApprovedImeis.exists(imei_norm) query_exists = session.execute(text("""SELECT EXISTS( SELECT 1 FROM public.approvedimeis WHERE imei='{0}' AND removed IS NOT TRUE )""".format(imei_norm))).fetchone() assert query_exists[0] == model_exists # add a record and than check imei_norm = '64728204390652' request_id = 237654998 status = 'status' delta_status = 'delta status' approved_imei = ApprovedImeis(imei_norm, request_id, status, delta_status) approved_imei.add() assert ApprovedImeis.exists(imei_norm) # set existing record's removed=True than check res = session.execute(text("""UPDATE public.approvedimeis SET removed=True WHERE imei='{0}' AND request_id='{1}'""".format(imei_norm, request_id))) assert res assert not ApprovedImeis.exists(imei_norm)
def test_get_imei(db, session): # pylint: disable=unused-argument """Verify that the get_imei() return the same imei which is not removed.""" imei_norm = '23456421264573' request_id = 237654998 status = 'status' delta_status = 'delta status' approved_imei = ApprovedImeis(imei_norm, request_id, status, delta_status) approved_imei.add() # get imei imei_data = ApprovedImeis.get_imei(imei_norm) assert imei_data.imei == imei_norm assert imei_data.request_id == request_id assert imei_data.status == status assert imei_data.delta_status == delta_status assert not imei_data.removed # update removed=True and check again res = session.execute( text("""UPDATE public.approvedimeis SET removed=True WHERE imei='{0}' AND request_id='{1}'""". format(imei_norm, request_id))) assert res imei_data = ApprovedImeis.get_imei(imei_norm) assert not imei_data
def test_registration_request_duplicated_imeis(flask_app, db): # pylint: disable=unused-argument """Verify that the system does not allow the request approval when there are duplicated imeis in the request and responds properly. """ headers = {'Content-Type': 'application/json'} data = { 'device_count': 1, 'imei_per_device': 1, 'imeis': "[['00010673016999']]", 'm_location': 'local', 'user_name': 'reg req approve rev user 6', 'user_id': 'reg-req-approve-rev-user-6' } request = create_assigned_dummy_request(data, 'Registration', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6') assert request request_id = request.id user_id = request.user_id # add duplicated imei to approvedimeis approved_imei = ApprovedImeis('00010673016999', 673739, 'whitelist', 'update') approved_imei.add() device_data = { 'brand': 'samsung', 'operating_system': 'android', 'model_name': 's9', 'model_num': '30jjd', 'device_type': 'Smartphone', 'technologies': '2G,3G,4G', 'reg_id': request.id } request = create_dummy_devices(device_data, 'Registration', request) assert request # create user quota quota = DeviceQuota.create(user_id, 'individual') assert quota # approve all sections manually RegComments.add('device_quota', 'test comment on section', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6', 6, request_id) RegComments.add('device_description', 'test comment on section', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6', 6, request_id) RegComments.add('imei_classification', 'test comment on section', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6', 6, request_id) RegComments.add('imei_registration', 'test comment on section', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6', 6, request_id) RegComments.add('approval_documents', 'test comment on section', 'reg-req-approve-rev-user-6', 'reg req approve rev user 6', 6, request_id) body_data = { 'request_id': request_id, 'request_type': 'registration_request', 'reviewer_id': 'reg-req-approve-rev-user-6' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 400 data = json.loads(rv.data.decode('utf-8')) assert data['error'] == ['unable to approve case {0}, duplicated imeis found'.format(request_id)]
def sync_bulk_create(cls, reg_details, reg_device_id, app, ussd=None): """Create devices in bulk.""" try: flatten_imeis = [] if ussd is None: imeis_lists = ast.literal_eval(reg_details.imeis) else: imeis_lists = [] ims = reg_details.imeis imeis_lists.append(list(ims.strip('}{').split(","))) for device_imeis in imeis_lists: device = cls(device_imeis[0][:8], reg_details.id, reg_device_id) device.save() for imei in device_imeis: flatten_imeis.append(imei) imei_device = ImeiDevice(imei, device.id) db.session.add(imei_device) imei_object = ApprovedImeis.query.filter_by( imei=imei[:14]).first() if not imei_object: approved_imei = ApprovedImeis(imei[:14], reg_details.id, 'pending', 'add') db.session.add(approved_imei) elif imei_object and imei_object.status == 'removed': imei_object.status = 'pending' imei_object.removed = False imei_object.request_id = reg_details.id db.session.add(imei_object) reg_details.update_processing_status('Processed') reg_details.update_report_status('Processing') db.session.commit() task_id = Utilities.generate_summary(flatten_imeis, reg_details.tracking_id) if task_id: Utilities.pool_summary_request(task_id, reg_details, app) else: reg_details.update_report_status('Failed') db.session.commit() exit() if app.config['AUTOMATE_IMEI_CHECK'] or ussd: if Device.auto_approve(task_id, reg_details, flatten_imeis, app): app.logger.info( "Auto Approved/Rejected Registration Application Id:" + str(reg_details.id)) except Exception as e: # pragma: no cover reg_details.update_processing_status('Failed') reg_details.update_report_status('Failed') app.logger.exception(e) db.session.commit()
def sync_bulk_create(cls, reg_details, reg_device_id, app): """Create devices in bulk.""" try: flatten_imeis = [] imeis_lists = ast.literal_eval(reg_details.imeis) for device_imeis in imeis_lists: device = cls(device_imeis[0][:8], reg_details.id, reg_device_id) device.save() for imei in device_imeis: flatten_imeis.append(imei) imei_device = ImeiDevice(imei, device.id) db.session.add(imei_device) imei_object = ApprovedImeis.query.filter_by(imei=imei[:14]).first() if not imei_object: approved_imei = ApprovedImeis(imei[:14], reg_details.id, 'pending', 'add') db.session.add(approved_imei) elif imei_object and imei_object.status == 'removed': imei_object.status = 'pending' imei_object.removed = False imei_object.request_id = reg_details.id db.session.add(imei_object) reg_details.update_processing_status('Processed') reg_details.update_report_status('Processing') db.session.commit() task_id = Utilities.generate_summary(flatten_imeis, reg_details.tracking_id) if task_id: Utilities.pool_summary_request(task_id, reg_details, app) else: reg_details.update_report_status('Failed') db.session.commit() except Exception: reg_details.update_processing_status('Failed') db.session.commit()
def test_bulk_insert_imeis(db, session): # pylint: disable=unused-argument """Verify that the bulk_insert_imeis() works as expected.""" imei_norm1 = '67895678901234' imei_norm2 = '76545678906548' imei_norm3 = '54375699900000' request_id = 2376322 status = 'status' delta_status = 'delta status' imeis = [ ApprovedImeis(imei_norm1, request_id, status, delta_status), ApprovedImeis(imei_norm2, request_id, status, delta_status), ApprovedImeis(imei_norm3, request_id, status, delta_status) ] ApprovedImeis.bulk_insert_imeis(imeis) assert ApprovedImeis.exists(imei_norm1) assert ApprovedImeis.exists(imei_norm2) assert ApprovedImeis.exists(imei_norm3)
def test_imeis_to_export(db, session): # pylint: disable=unused-argument """Verify that imeis_to_export() works as expected.""" imei_norm1 = '67890000001234' imei_norm2 = '71111178906548' imei_norm3 = '54322333900000' request_id = 2376322 status = 'status' delta_status = 'delta status' imeis = [ ApprovedImeis(imei_norm1, request_id, status, delta_status), ApprovedImeis(imei_norm2, request_id, status, delta_status), ApprovedImeis(imei_norm3, request_id, status, delta_status) ] ApprovedImeis.bulk_insert_imeis(imeis) imeis_to_export = ApprovedImeis.imei_to_export() assert imeis_to_export for imei in imeis_to_export: assert imei.removed is False
def async_bulk_create(cls, reg_details, reg_device_id, app): # pragma: no cover """Create devices async.""" with app.app_context(): from app import db try: filename = reg_details.file tracking_id = reg_details.tracking_id args = { 'imei_per_device': reg_details.imei_per_device, 'device_count': reg_details.device_count } response = Utilities.process_reg_file(filename, tracking_id, args) imeis = [] for device_imeis in response: tac = Utilities.get_imei_tac(device_imeis[0]) device = cls(tac, reg_details.id, reg_device_id) db.session.add(device) db.session.flush() imeis = imeis + device_imeis for imei in device_imeis: imei_device = ImeiDevice(imei, device.id) db.session.add(imei_device) imei_object = ApprovedImeis.query.filter_by( imei=imei[:14]).first() if not imei_object: approved_imei = ApprovedImeis( imei[:14], reg_details.id, 'pending', 'add') db.session.add(approved_imei) elif imei_object and imei_object.status == 'removed': imei_object.status = 'pending' imei_object.removed = False imei_object.request_id = reg_details.id db.session.add(imei_object) db.session.commit() reg_details.update_processing_status('Processed') db.session.commit() task_id = Utilities.generate_summary(imeis, reg_details.tracking_id) app.logger.info( 'task with task_id: {0} initiated'.format(task_id)) if not task_id: reg_details.update_report_status('Failed') app.logger.info( 'task with task_id: {0} failed'.format(task_id)) db.session.commit() else: Utilities.pool_summary_request(task_id, reg_details, app) except Exception as e: app.logger.exception(e) db.session.rollback() reg_details.update_report_status('Failed') reg_details.update_processing_status('Failed') db.session.commit() raise e
def test_add(db, session): # pylint: disable=unused-argument """Verify that the approved imeis add function works correctly.""" # add data using model's add function imei_norm = '12345678901234' request_id = 2376322 status = 'status' delta_status = 'delta status' approved_imei = ApprovedImeis(imei_norm, request_id, status, delta_status) approved_imei.add() # extract data and verify imei_data = session.execute( text("""SELECT * FROM public.approvedimeis WHERE imei='{0}'""".format( imei_norm))).fetchone() assert imei_data assert imei_data.imei == imei_norm assert imei_data.request_id == request_id assert imei_data.status == status assert imei_data.delta_status == delta_status # check some default params assert not imei_data.exported # exported should be false as default assert imei_data.exported_at is None # when not exported than exported_at should be none assert not imei_data.removed # removed should be false as default # verify that exported and removed params can be altered by add function imei_norm = '23456789064573' request_id = 23765466 status = 'status' delta_status = 'delta status' approved_imei = ApprovedImeis(imei_norm, request_id, status, delta_status, True, True) approved_imei.add() imei_data = session.execute( text("""SELECT * FROM public.approvedimeis WHERE imei='{0}'""".format( imei_norm))).fetchone() assert imei_data.exported assert imei_data.removed
def test_bulk_insert_imeis(): # pylint: disable=unused-argument """Verify that the bulk_insert_imeis() works as expected.""" imei_list = [ '67895678901234', '76545678906548', '54375699900000', '35282004000637', '35282004000638', '35282004000639', '35282004000640', '35282004000641', '35282004000642', '35282004000643', '35282004000644', '35282004000645', '35282004000646', '35282004000647', '35282004000648', '35282004000649', '35282004000650', '35282004000651', '35282004000652' ] request_id = 2376322 status = 'whitelist' delta_status = 'delta status' imeis = [ ApprovedImeis(imei, request_id, status, delta_status) for imei in imei_list ] ApprovedImeis.bulk_insert_imeis(imeis)
def test_duplicated_imeis_count(flask_app, db, app): # pylint: disable=unused-argument """Verify that the system detects duplicate imeis and generated a related file.""" registration_data = { 'device_count': 3, 'imei_per_device': 1, 'imeis': "[['86800103015010', '8683342039012345', '868000039111111']]", 'm_location': 'local', 'user_name': 'usr-2-02-test-002', 'user_id': 'usr-2-02-test-002' } request = create_dummy_request(registration_data, 'Registration') tracking_id = request.tracking_id device_data = { 'brand': 'samsung', 'operating_system': 'android', 'model_name': 's9', 'model_num': '30jjd', 'device_type': 'Smartphone', 'technologies': '2G,3G,4G', 'reg_id': request.id } request = create_dummy_devices(device_data, 'Registration', request) # add an imei to approved imeis approved_imei = ApprovedImeis('86800003911111', 332332332, 'whitelist', 'update') approved_imei.add() rv = flask_app.get( '{0}?request_id={1}&request_type=registration_request'.format( IMEIS_STATUS_API, request.id)) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['duplicated'] == 1 duplicated_file_path = '{0}/{1}/duplicated_imeis.txt'.format( app.config['DRS_UPLOADS'], tracking_id) assert os.path.exists(duplicated_file_path) # read file and check the content with open(duplicated_file_path, 'r') as f: assert f.readline().split('\n')[0] == '86800003911111'
def test_pending_registration_status(flask_app, db, app): """Verify that api returns correct imeis count.""" # registration request registration_data = { 'device_count': 2, 'imei_per_device': 1, 'imeis': "[['86834403015010', '868344039012345']]", 'm_location': 'local', 'user_name': 'imei stat user 1', 'user_id': 'imei-stat-user-1' } request = create_assigned_dummy_request(registration_data, 'Registration', 'dev-descp-1', 'dev descp') device_data = { 'brand': 'samsung', 'operating_system': 'android', 'model_name': 's9', 'model_num': '30jjd', 'device_type': 'Smartphone', 'technologies': '2G,3G,4G', 'reg_id': request.id } request = create_dummy_devices(device_data, 'Registration', request) rv = flask_app.get( '{0}?request_id={1}&request_type=registration_request'.format( IMEIS_STATUS_API, request.id)) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['not_registered'] == 0 assert data['pending_registration'] == 2 assert data['registered'] == 0 # de-registration request de_registration_data = { 'file': 'de-reg-test-file.txt', 'device_count': 1, 'user_id': 'imei-stat-user-2', 'user_name': 'assign rev user 1', 'reason': 'because we have to run tests successfully' } request = create_assigned_dummy_request(de_registration_data, 'De_Registration', 'dereg-rev', 'de reg rev') device_data = { 'devices': """[ { "tac": "35732108", "model_name": "TA-1034", "brand_name": "NOKIA", "model_num": "TA-1034", "technology": "NONE", "device_type": "Mobile Phone/Feature phone", "count": 2, "operating_system": "N/A" } ]""", 'dereg_id': request.id } request = create_dummy_devices(device_data, 'De_Registration', request, db, file_content=['357321082345123'], file_path='{0}/{1}/{2}'.format( app.config['DRS_UPLOADS'], request.tracking_id, de_registration_data.get('file'))) approved_imei = ApprovedImeis('35732108234512', 332332, 'whitelist', 'update') approved_imei.add() rv = flask_app.get( '{0}?request_id={1}&request_type=de_registration_request'.format( IMEIS_STATUS_API, request.id)) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert data['pending_registration'] == 2 assert data['not_registered'] == 0 assert data['registered'] == 0