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_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 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_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_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