def test_get_single(db, session): # pylint: disable=unused-argument """Verify that the get_single() returns correct results as expected from database table. """ # add a notification for user user_id = 'notf-user' request_id = 225 request_type = 'registration' request_status = 3 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) notification.add() notification_id = get_notification_id(session, request_id) notification_data = Notification.get_single(notification_id) notification_data_raw = get_single_notification(session, notification_id) # verify results of both assert notification_data_raw.id == notification_data.id assert notification_data_raw.user_id == notification_data.user_id assert notification_data_raw.request_id == notification_data.request_id assert notification_data_raw.request_status == notification_data.request_status assert notification_data_raw.message == notification_data.message
def put(self, **kwargs): """Put method handler for notification route.""" user_id = kwargs.get('user_id') notification_id = kwargs.get('notification_id') if NotificationModel.exist_users(user_id): if NotificationModel.exists(notification_id): notification = NotificationModel.get_single(notification_id) if notification.user_id == user_id: NotificationModel.mark_read(notification_id) return Response(status=201, mimetype='application/json') else: res = {'error': ['invalid user id']} return Response(json.dumps(ErrorResponse().dump(res).data), status=400, mimetype='application/json') else: res = { 'error': [ 'notification {id} does not exists'.format( id=notification_id) ] } return Response(json.dumps(ErrorResponse().dump(res).data), status=400, mimetype='application/json') else: res = {'error': ['user {id} does not exists'.format(id=user_id)]} return Response(json.dumps(ErrorResponse().dump(res).data), status=400, mimetype='application/json')
def test_mark_notification_as_read_api(flask_app, db): # pylint: disable=unused-argument """Verify that a generated notification is not return when mark as read.""" # generate a registration request type notification user_id = 'test-user1' notification = Notification( user_id=user_id, request_id=1342, request_type='registration_request', request_status=6, message='a test notification has been generated') notification.add() api_url = '{api}?user_id={user_id}'.format(api=NOTIFICATION_API, user_id=user_id) rv = flask_app.get(api_url) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) notification_id = data['notifications'][0]['id'] # mark notification as read body_data = {'notification_id': notification_id, 'user_id': user_id} headers = {'Content-Type': 'application/json'} rv = flask_app.put(NOTIFICATION_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201
def test_with_existing_notification_data(flask_app, db): # pylint: disable=unused-argument """Verify that the api responds correctly when notification data exists in table.""" # generate a registration request type notification user_id = 'test-user1' notification = Notification(user_id=user_id, request_id=1342, request_type='registration_request', request_status=6, message='a test notification has been generated') notification.add() # get notification for user id = test-user1 api_url = '{api}?user_id={user_id}'.format(api=NOTIFICATION_API, user_id=user_id) rv = flask_app.get(api_url) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert len(data.get('notifications')) == 1 data = data.get('notifications')[0] assert data.get('request_id') == 1342 assert data.get('request_status') == 6 assert data.get('request_type') == 'registration_request' assert data.get('id') == 1 assert data.get('message') == 'a test notification has been generated' # generate multiple notifications for the same user notification = Notification(user_id=user_id, request_id=1343, request_type='de_registration_request', request_status=7, message='a test notification has been generated') notification.add() api_url = '{api}?user_id={user_id}'.format(api=NOTIFICATION_API, user_id=user_id) rv = flask_app.get(api_url) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) assert len(data.get('notifications')) == 2
def test_registration_request_information_requested(flask_app, db): # pylint: disable=unused-argument """Verify that when any of the section is marked as information requested the system automatically mark the whole request as information requested. """ headers = {'Content-Type': 'application/json'} data = { 'device_count': 1, 'imei_per_device': 1, 'imeis': "[['11110673016888']]", 'm_location': 'local', 'user_name': 'reg req approve rev user 7', 'user_id': 'reg-req-approve-rev-user-7' } request = create_assigned_dummy_request(data, 'Registration', 'reg-req-approve-rev-user-7', 'reg req approve rev user 7') assert request request_id = request.id user_id = request.user_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) 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-7', 'reg req approve rev user 7', 6, request_id) RegComments.add('device_description', 'test comment on section', 'reg-req-approve-rev-user-7', 'reg req approve rev user 7', 6, request_id) RegComments.add('imei_classification', 'test comment on section', 'reg-req-approve-rev-user-7', 'reg req approve rev user 7', 5, request_id) RegComments.add('imei_registration', 'test comment on section', 'reg-req-approve-rev-user-7', 'reg req approve rev user 7', 6, request_id) RegComments.add('approval_documents', 'test comment on section', 'reg-req-approve-rev-user-7', 'reg req approve rev user 7', 6, request_id) body_data = { 'request_id': request_id, 'request_type': 'registration_request', 'reviewer_id': 'reg-req-approve-rev-user-7' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201 data = json.loads(rv.data.decode('utf-8')) assert data['status'] == 5 assert data['request_id'] == request_id assert data['message'] == 'case {0} updated successfully'.format(request_id) assert data['request_type'] == 'registration_request' assert Notification.exist_users(user_id)
def test_get(db, session): # pylint: disable=unused-argument """Verify that the get() returns correct results as expected.""" # get from method for notf-user user_id = 'notf-user' method_res = Notification.get(user_id) query_res = get_user_notifications( session, user_id) # check if the results are really same assert len(query_res) == len(method_res)
def get(self, **kwargs): """Get method handler for notification route.""" user_id = kwargs.get('user_id') if user_id: notifications = NotificationModel.get(user_id) response = Notifications().dump(dict(notifications=notifications)).data return Response(json.dumps(response), status=200, mimetype='application/json') else: err = {'error': ['user_id cannot be empty']} return Response(json.dumps(ErrorResponse().dump(err).data), status=422, mimetype='application/json')
def test_exist_users(db, session): # pylint: disable=unused-argument """Verify that the exist_user() work correctly.""" # add a notification for user user_id = 'notf-user' request_id = 223 request_type = 'registration' request_status = 3 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) notification.add() user_exists_bool = Notification.exist_users(user_id) # check if it really exists in db res = exists_user_notifications(session, user_id) assert res is user_exists_bool # remove the above notification and then check again assert delete_user_notifications(session, user_id) user_exists_bool = Notification.exist_users(user_id) res = exists_user_notifications(session, user_id) assert res is user_exists_bool
def test_registration_request_rejected_section(flask_app, db): # pylint: disable=unused-argument """Verify that the registration request gets rejected when anyone of the section is marked as rejected, imeis are removed from approvedimeis and notification is being generated. """ # only one section is reviewed and rejected headers = {'Content-Type': 'application/json'} data = { 'device_count': 1, 'imei_per_device': 1, 'imeis': "[['23010403010533']]", 'm_location': 'local', 'user_name': '23423423rev user 1', 'user_id': 'assign-rev23442342-user-1' } request = create_assigned_dummy_request(data, 'Registration', 'rev230987', 'rev 230987') assert request request_id = request.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) assert request # add one section with rejected status RegComments.add('device_quota', 'test comment on section', 'rev230987', 'rev 230987', 7, request_id) body_data = { 'request_id': request_id, 'request_type': 'registration_request', 'reviewer_id': 'rev230987' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201 response = json.loads(rv.data.decode('utf-8')) assert response['status'] == 7 assert response['request_id'] == request_id assert response['message'] == 'case {0} updated successfully'.format( request_id) assert response['request_type'] == 'registration_request' imei = ApprovedImeis.get_imei('23010403010533') assert imei.status == 'removed' assert imei.delta_status == 'remove' assert Notification.exist_users('assign-rev23442342-user-1')
def test_notification_put_api_with_invalid_data(flask_app, db): # pylint: disable=unused-argument """Verify that the notification put api responds correctly in case of invalid input.""" # invalid user id user_id = 'abcd-user' body_data = {'notification_id': 2, 'user_id': user_id} headers = {'Content-Type': 'application/json'} rv = flask_app.put(NOTIFICATION_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 400 data = json.loads(rv.data.decode('utf-8')) assert data.get('error')[0] == 'user {0} does not exists'.format(user_id) # generate a notification with the same user id notification = Notification(user_id=user_id, request_id=1342, request_type='registration_request', request_status=6, message='a test notification has been generated') notification.add() # invalid notification id test notification_id = 35 body_data = {'notification_id': notification_id, 'user_id': user_id} rv = flask_app.put(NOTIFICATION_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 400 data = json.loads(rv.data.decode('utf-8')) assert data.get('error')[0] == 'notification {0} does not exists'.format(notification_id) # wrong user for notification test user_id_2 = 'test-user1' notification = Notification(user_id=user_id_2, request_id=13, request_type='registration_request', request_status=6, message='a test notification has been generated') notification.add() # get notification id rv = flask_app.get('{0}?user_id={1}'.format(NOTIFICATION_API, user_id_2)) assert rv.status_code == 200 data = json.loads(rv.data.decode('utf-8')) notification_id = data['notifications'][0]['id'] # try to mark notification as read with wrong user body_data = {'notification_id': notification_id, 'user_id': user_id} rv = flask_app.put(NOTIFICATION_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 400 data = json.loads(rv.data.decode('utf-8')) assert data.get('error')[0] == 'invalid user id' # string arguments test body_data = {'notification_id': '', 'user_id': ''} rv = flask_app.put(NOTIFICATION_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 422 data = json.loads(rv.data.decode('utf-8')) assert data['notification_id'][0] == "Bad 'notification_id':'' argument format. Accepts only integer"
def test_exists(db, session): # pylint: disable=unused-argument """Verify that exists() responds correctly while checking for existence of notifications.""" # add a new notification user_id = 'notf-user' request_id = 224 request_type = 'registration' request_status = 7 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) notification.add() # get notification id notification_id = get_notification_id(session, request_id) notification_bool = Notification.exists(notification_id) # check if it really exists res = exists_notification(session, notification_id) assert res is notification_bool
def test_de_registration_request_rejected_section(flask_app, db, app): """Verify that the de_registration request gets rejected when anyone of the section is marked as rejected, imeis are removed from approvedimeis and notification is being generated. """ # de registration request headers = {'Content-Type': 'application/json'} de_registration_data = { 'file': 'de-reg-test-file.txt', 'device_count': 1, 'user_id': 'dereg-section-submit-assign-rev-user-1', 'user_name': 'submit assign rev user 1', 'reason': 'because we have to run tests successfully' } request = create_assigned_dummy_request(de_registration_data, 'De_Registration', 'dereg-rejected-section-rev', 'de reg rev') device_data = { 'devices': """[ { "tac": "95762201", "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=['957622010005119'], file_path='{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, de_registration_data.get('file'))) assert request request_id = request.id DeRegComments.add('device_description', 'test comment on section', 'dereg-rejected-section-rev', 'rev 230987', 7, request_id) body_data = { 'request_id': request_id, 'request_type': 'de_registration_request', 'reviewer_id': 'dereg-rejected-section-rev' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201 response = json.loads(rv.data.decode('utf-8')) assert response['status'] == 7 assert response['request_id'] == request_id assert response['message'] == 'case {0} updated successfully'.format(request_id) assert response['request_type'] == 'de_registration_request' assert Notification.exist_users('dereg-section-submit-assign-rev-user-1')
def test_add_notification(db, session): # pylint: disable=unused-argument """Verify that the notifications model add functionality works correctly.""" # add a notification using model user_id = 'test-user' request_id = 213 request_type = 'registration' request_status = 3 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) notification.add() # check entry in database res = session.execute( text("""SELECT * FROM public.notification WHERE request_id='213'""")).fetchone() assert res.user_id == user_id assert res.request_id == request_id assert res.request_type == request_type assert res.request_status == 3 assert res.message == message # marked_read should be false initially assert not res.marked_read # notification is not added without committing to the db using add() user_id = 'test-user' request_id = 2100 request_type = 'de_registration' request_status = 7 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) res = session.execute( text("""SELECT * FROM public.notification WHERE request_id='2100'""")).fetchone() assert not res
def test_mark_read(db, session): # pylint: disable=unused-argument """Verify that mark_read() mark marked_read column as True.""" # add a notification user_id = 'notf-user' request_id = 226 request_type = 'registration' request_status = 3 message = 'this is a test notification' notification = Notification(user_id=user_id, request_id=request_id, request_type=request_type, request_status=request_status, message=message) notification.add() # get notification id notification_id = get_notification_id(session, request_id) notification_data = get_single_notification(session, notification_id) assert notification_data.marked_read is False # call mark_read() Notification.mark_read(notification_id) notification_data = get_single_notification(session, notification_id) assert notification_data.marked_read is True
def test_registration_request_approval(flask_app, db): # pylint: disable=unused-argument """Verify that the api behave properly upon approval of the request.""" # only one section is reviewed and rejected headers = {'Content-Type': 'application/json'} data = { 'device_count': 1, 'imei_per_device': 1, 'imeis': "[['94310813016000']]", 'm_location': 'local', 'user_name': 'reg req approve rev user 5', 'user_id': 'reg-req-approve-rev-user-5' } request = create_assigned_dummy_request(data, 'Registration', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5') assert request request_id = request.id user_id = request.user_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) assert request # create user quota quota = DeviceQuota.create(user_id, 'individual') assert quota user_reg_quota = quota.reg_quota # approve all sections manually RegComments.add('device_quota', 'test comment on section', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5', 6, request_id) RegComments.add('device_description', 'test comment on section', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5', 6, request_id) RegComments.add('imei_classification', 'test comment on section', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5', 6, request_id) RegComments.add('imei_registration', 'test comment on section', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5', 6, request_id) RegComments.add('approval_documents', 'test comment on section', 'reg-req-approve-rev-user-5', 'reg req approve rev user 5', 6, request_id) body_data = { 'request_id': request_id, 'request_type': 'registration_request', 'reviewer_id': 'reg-req-approve-rev-user-5' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201 data = json.loads(rv.data.decode('utf-8')) assert data['status'] == 6 assert data['request_id'] == request_id assert data['message'] == 'case {0} updated successfully'.format(request_id) assert data['request_type'] == 'registration_request' imei = ApprovedImeis.get_imei('94310813016000') assert imei.status == 'whitelist' assert imei.delta_status == 'add' assert Notification.exist_users(user_id) assert DeviceQuota.get(user_id).reg_quota == user_reg_quota - 1
def put(): """PUT method handler, updates documents.""" reg_id = request.form.to_dict().get('reg_id', None) if not reg_id or not reg_id.isdigit() or not RegDetails.exists(reg_id): return Response(app.json_encoder.encode(REG_NOT_FOUND_MSG), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) try: schema = RegistrationDocumentsUpdateSchema() time = datetime.now().strftime('%Y%m%d%H%M%S') args = request.form.to_dict() args = Utilities.update_args_with_file(request.files, args) reg_details = RegDetails.get_by_id(reg_id) if reg_details: args.update({ 'reg_details_id': reg_details.id, 'status': reg_details.status }) else: args.update({'reg_details_id': ''}) validation_errors = schema.validate(args) if validation_errors: return Response(app.json_encoder.encode(validation_errors), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) tracking_id = reg_details.tracking_id updated = RegDocuments.bulk_update(request.files, reg_details, time) response = Utilities.store_files(request.files, tracking_id, time) if response: return Response(json.dumps(response), status=CODES.get("UNPROCESSABLE_ENTITY"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) if reg_details.status == Status.get_status_id( 'Information Requested'): reg_details.update_status('In Review') message = 'The request {id} has been updated.'.format( id=reg_details.id) notification = Notification(reg_details.reviewer_id, reg_details.id, 'registration_request', reg_details.status, message) notification.add() else: reg_details.update_status('Pending Review') response = schema.dump(updated, many=True).data db.session.commit() return Response(json.dumps(response), status=CODES.get("OK"), mimetype=MIME_TYPES.get("APPLICATION_JSON")) except Exception as e: # pragma: no cover db.session.rollback() app.logger.exception(e) data = { 'message': _('request document updation failed, please try again later.') } return Response(app.json_encoder.encode(data), status=CODES.get('INTERNAL_SERVER_ERROR'), mimetype=MIME_TYPES.get('APPLICATION_JSON')) finally: db.session.close()