def test_get_services(notify_api, notify_db, notify_db_session, sample_user): sample_service = create_sample_service(notify_db, notify_db_session, user=sample_user) assert Service.query.count() == 1 assert len(get_model_services()) == 1 service_name = "Another service" sample_service = create_sample_service(notify_db, notify_db_session, service_name=service_name, user=sample_user) assert Service.query.count() == 2 assert len(get_model_services()) == 2
def update_service(service_id): try: service = get_model_services(service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 if request.method == 'DELETE': status_code = 202 delete_model_service(service) else: status_code = 200 # TODO there has got to be a better way to do the next three lines upd_serv, errors = service_schema.load(request.get_json()) if errors: return jsonify(result="error", message=errors), 400 update_dict, errors = service_schema.dump(upd_serv) # TODO FIX ME # Remove update_service model which is added to db.session db.session.rollback() try: save_model_service(service, update_dict=update_dict) except DAOException as e: return jsonify(result="error", message=str(e)), 400 return jsonify(data=service_schema.dump(service).data), status_code
def test_get_user_service(notify_api, notify_db, notify_db_session, sample_user): assert Service.query.count() == 0 service_name = "Random service" sample_service = create_sample_service(notify_db, notify_db_session, service_name=service_name, user=sample_user) assert get_model_services(service_id=sample_service.id).name == service_name assert Service.query.count() == 1
def get_service(service_id=None): user_id = request.args.get('user_id', None) try: services = get_model_services(service_id=service_id, user_id=user_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 data, errors = services_schema.dump(services) if isinstance(services, list) else service_schema.dump(services) return jsonify(data=data)
def test_get_user_service(notify_api, notify_db, notify_db_session, sample_user): assert Service.query.count() == 0 service_name = "Random service" sample_service = create_sample_service(notify_db, notify_db_session, service_name=service_name, user=sample_user) assert get_model_services( service_id=sample_service.id).name == service_name assert Service.query.count() == 1
def get_service(service_id=None): user_id = request.args.get('user_id', None) try: services = get_model_services(service_id=service_id, user_id=user_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 data, errors = services_schema.dump(services) if isinstance( services, list) else service_schema.dump(services) return jsonify(data=data)
def create_template(service_id): try: service = get_model_services(service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 template, errors = template_schema.load(request.get_json()) if errors: return jsonify(result="error", message=errors), 400 template.service = service # I believe service is already added to the session but just needs a # db.session.commit save_model_template(template) return jsonify(data=template_schema.dump(template).data), 201
def test_get_services_for_user(notify_api, notify_db, notify_db_session, sample_service): assert Service.query.count() == 1 service_name = "Random service" second_user = create_sample_user(notify_db, notify_db_session, '*****@*****.**') create_sample_service(notify_db, notify_db_session, service_name='another service', user=second_user) sample_service = create_sample_service(notify_db, notify_db_session, service_name=service_name, user=sample_service.users[0]) assert Service.query.count() == 3 services = get_model_services(user_id=sample_service.users[0].id) assert len(services) == 2 assert service_name in [x.name for x in services] assert 'Sample service' in [x.name for x in services]
def get_service_by_user_id(user_id, service_id=None): try: user = get_model_users(user_id=user_id) except DataError: return jsonify(result="error", message="Invalid user id"), 400 except NoResultFound: return jsonify(result="error", message="User not found"), 404 try: services = get_model_services(user_id=user.id, service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 services, errors = services_schema.dump(services) if isinstance(services, list) else service_schema.dump(services) return jsonify(data=services)
def get_service_by_user_id(user_id, service_id=None): try: user = get_model_users(user_id=user_id) except DataError: return jsonify(result="error", message="Invalid user id"), 400 except NoResultFound: return jsonify(result="error", message="User not found"), 404 try: services = get_model_services(user_id=user.id, service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 services, errors = services_schema.dump(services) if isinstance( services, list) else service_schema.dump(services) return jsonify(data=services)
def renew_api_key(service_id=None): try: service = get_model_services(service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 try: # create a new one # TODO: what validation should be done here? secret_name = request.get_json()['name'] key = ApiKey(service=service, name=secret_name) save_model_api_key(key) except DAOException as e: return jsonify(result='error', message=str(e)), 400 unsigned_api_key = get_unsigned_secret(key.id) return jsonify(data=unsigned_api_key), 201
def get_api_keys(service_id, key_id=None): try: service = get_model_services(service_id=service_id) except DataError: return jsonify(result="error", message="Invalid service id"), 400 except NoResultFound: return jsonify(result="error", message="Service not found"), 404 try: if key_id: api_keys = [get_model_api_keys(service_id=service_id, id=key_id)] else: api_keys = get_model_api_keys(service_id=service_id) except DAOException as e: return jsonify(result='error', message=str(e)), 400 except NoResultFound: return jsonify(result="error", message="API key not found"), 404 return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200
def validate_to(json_body, service_id): errors = {"to": []} mob = json_body.get('to', None) if not mob: errors['to'].append('Required data missing') else: if not mobile_regex.match(mob): errors['to'].append('invalid phone number, must be of format +441234123123') if service_id != current_app.config.get('ADMIN_CLIENT_USER_NAME'): service = services_dao.get_model_services(service_id=service_id) if service.restricted: valid = False for usr in service.users: if mob == usr.mobile_number: valid = True break if not valid: errors['to'].append('Invalid phone number for restricted service') return mob, errors
def validate_to(json_body, service_id): errors = {"to": []} mob = json_body.get('to', None) if not mob: errors['to'].append('Required data missing') else: if not mobile_regex.match(mob): errors['to'].append( 'invalid phone number, must be of format +441234123123') if service_id != current_app.config.get('ADMIN_CLIENT_USER_NAME'): service = services_dao.get_model_services(service_id=service_id) if service.restricted: valid = False for usr in service.users: if mob == usr.mobile_number: valid = True break if not valid: errors['to'].append( 'Invalid phone number for restricted service') return mob, errors