def test_get_notifications_with_a_team_api_key_type( notify_db, notify_db_session, sample_service, sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_api_key.id, key_type=sample_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_team_api_key.id, key_type=sample_team_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_test_api_key.id, key_type=sample_test_api_key.key_type ) # only those created with team API key, no jobs all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, key_type=KEY_TYPE_TEAM).items assert len(all_notifications) == 1 # only those created with team API key, no jobs, even when requested all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True, key_type=KEY_TYPE_TEAM).items assert len(all_notifications) == 1
def get_all_notifications(): data = notifications_filter_schema.load(request.args).data include_jobs = data.get("include_jobs", False) page = data.get("page", 1) page_size = data.get("page_size", current_app.config.get("API_PAGE_SIZE")) limit_days = data.get("limit_days") pagination = notifications_dao.get_notifications_for_service( str(authenticated_service.id), personalisation=True, filter_dict=data, page=page, page_size=page_size, limit_days=limit_days, key_type=api_user.key_type, include_jobs=include_jobs, ) return ( jsonify( notifications=notification_with_personalisation_schema.dump( pagination.items, many=True).data, page_size=page_size, total=pagination.total, links=pagination_links(pagination, ".get_all_notifications", **request.args.to_dict()), ), 200, )
def get_all_notifications_for_service(service_id): data = notifications_filter_schema.load(request.args).data if data.get('to'): notification_type = data.get('template_type')[0] if data.get('template_type') else None return search_for_notification_by_to_field(service_id=service_id, search_term=data['to'], statuses=data.get('status'), notification_type=notification_type) page = data['page'] if 'page' in data else 1 page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE') limit_days = data.get('limit_days') include_jobs = data.get('include_jobs', True) include_from_test_key = data.get('include_from_test_key', False) pagination = notifications_dao.get_notifications_for_service( service_id, filter_dict=data, page=page, page_size=page_size, limit_days=limit_days, include_jobs=include_jobs, include_from_test_key=include_from_test_key ) kwargs = request.args.to_dict() kwargs['service_id'] = service_id return jsonify( notifications=notification_with_template_schema.dump(pagination.items, many=True).data, page_size=page_size, total=pagination.total, links=pagination_links( pagination, '.get_all_notifications_for_service', **kwargs ) ), 200
def test_should_return_notifications_including_jobs(notify_db, notify_db_session, sample_service): assert len(Notification.query.all()) == 0 job = sample_job(notify_db, notify_db_session) with_job = sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), status="delivered", job=job ) all_notifications = Notification.query.all() assert len(all_notifications) == 1 all_notifications = get_notifications_for_service(sample_service.id).items assert len(all_notifications) == 0 all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items assert len(all_notifications) == 1 assert all_notifications[0].id == with_job.id
def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, notify_db_session, sample_service): assert len(Notification.query.all()) == 0 # create one notification a day between 1st and 9th for i in range(1, 11): past_date = '2016-01-{0:02d}'.format(i) with freeze_time(past_date): sample_notification(notify_db, notify_db_session, created_at=datetime.utcnow(), status="failed") all_notifications = Notification.query.all() assert len(all_notifications) == 10 all_notifications = get_notifications_for_service(sample_service.id, limit_days=10).items assert len(all_notifications) == 10 all_notifications = get_notifications_for_service(sample_service.id, limit_days=1).items assert len(all_notifications) == 2
def test_get_notifications_by_reference(notify_db, notify_db_session, sample_service): client_reference = 'some-client-ref' assert len(Notification.query.all()) == 0 sample_notification(notify_db, notify_db_session, client_reference=client_reference) sample_notification(notify_db, notify_db_session, client_reference=client_reference) sample_notification(notify_db, notify_db_session, client_reference='other-ref') all_notifications = get_notifications_for_service(sample_service.id, client_reference=client_reference).items assert len(all_notifications) == 2
def get_all_notifications_for_service(service_id): data = notifications_filter_schema.load(request.args).data if data.get("to"): notification_type = data.get("template_type")[0] if data.get( "template_type") else None return search_for_notification_by_to_field( service_id=service_id, search_term=data["to"], statuses=data.get("status"), notification_type=notification_type, ) page = data["page"] if "page" in data else 1 page_size = data[ "page_size"] if "page_size" in data else current_app.config.get( "PAGE_SIZE") limit_days = data.get("limit_days") include_jobs = data.get("include_jobs", True) include_from_test_key = data.get("include_from_test_key", False) include_one_off = data.get("include_one_off", True) count_pages = data.get("count_pages", True) pagination = notifications_dao.get_notifications_for_service( service_id, filter_dict=data, page=page, page_size=page_size, count_pages=count_pages, limit_days=limit_days, include_jobs=include_jobs, include_from_test_key=include_from_test_key, include_one_off=include_one_off, ) kwargs = request.args.to_dict() kwargs["service_id"] = service_id if data.get("format_for_csv"): notifications = [ notification.serialize_for_csv() for notification in pagination.items ] else: notifications = notification_with_template_schema.dump( pagination.items, many=True).data return ( jsonify( notifications=notifications, page_size=page_size, total=pagination.total, links=pagination_links(pagination, ".get_all_notifications_for_service", **kwargs), ), 200, )
def test_get_notifications_created_by_api_or_csv_are_returned_correctly_excluding_test_key_notifications( notify_db, notify_db_session, sample_service, sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_api_key.id, key_type=sample_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_team_api_key.id, key_type=sample_team_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_test_api_key.id, key_type=sample_test_api_key.key_type ) all_notifications = Notification.query.all() assert len(all_notifications) == 4 # returns all real API derived notifications all_notifications = get_notifications_for_service(sample_service.id).items assert len(all_notifications) == 2 # returns all API derived notifications, including those created with test key all_notifications = get_notifications_for_service(sample_service.id, include_from_test_key=True).items assert len(all_notifications) == 3 # all real notifications including jobs all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items assert len(all_notifications) == 3
def get_notifications(): _data = request.args.to_dict(flat=False) # flat=False makes everything a list, but we only ever allow one value for "older_than" if "older_than" in _data: _data["older_than"] = _data["older_than"][0] # and client reference if "reference" in _data: _data["reference"] = _data["reference"][0] if "include_jobs" in _data: _data["include_jobs"] = _data["include_jobs"][0] data = validate(_data, get_notifications_request) paginated_notifications = notifications_dao.get_notifications_for_service( str(authenticated_service.id), filter_dict=data, key_type=api_user.key_type, personalisation=True, older_than=data.get("older_than"), client_reference=data.get("reference"), page_size=current_app.config.get("API_PAGE_SIZE"), include_jobs=data.get("include_jobs"), ) def _build_links(notifications): _links = { "current": url_for(".get_notifications", _external=True, **data), } if len(notifications): next_query_params = dict(data, older_than=notifications[-1].id) _links["next"] = url_for(".get_notifications", _external=True, **next_query_params) return _links return ( jsonify( notifications=[ notification.serialize() for notification in paginated_notifications.items ], links=_build_links(paginated_notifications.items), ), 200, )
def test_should_exclude_test_key_notifications_by_default( notify_db, notify_db_session, sample_service, sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_api_key.id, key_type=sample_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_team_api_key.id, key_type=sample_team_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_test_api_key.id, key_type=sample_test_api_key.key_type ) all_notifications = Notification.query.all() assert len(all_notifications) == 4 all_notifications = get_notifications_for_service(sample_service.id, limit_days=1).items assert len(all_notifications) == 2 all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True).items assert len(all_notifications) == 3 all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, key_type=KEY_TYPE_TEST).items assert len(all_notifications) == 1
def test_get_notifications_with_a_live_api_key_type( notify_db, notify_db_session, sample_service, sample_job, sample_api_key, sample_team_api_key, sample_test_api_key ): sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_api_key.id, key_type=sample_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_team_api_key.id, key_type=sample_team_api_key.key_type ) sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), api_key_id=sample_test_api_key.id, key_type=sample_test_api_key.key_type ) all_notifications = Notification.query.all() assert len(all_notifications) == 4 # only those created with normal API key, no jobs all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, key_type=KEY_TYPE_NORMAL).items assert len(all_notifications) == 1 # only those created with normal API key, with jobs all_notifications = get_notifications_for_service(sample_service.id, limit_days=1, include_jobs=True, key_type=KEY_TYPE_NORMAL).items assert len(all_notifications) == 2
def test_should_return_notifications_excluding_jobs_by_default(notify_db, notify_db_session, sample_service): assert len(Notification.query.all()) == 0 job = sample_job(notify_db, notify_db_session) with_job = sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), status="delivered", job=job ) without_job = sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), status="delivered" ) all_notifications = Notification.query.all() assert len(all_notifications) == 2 all_notifications = get_notifications_for_service(sample_service.id).items assert len(all_notifications) == 1 assert all_notifications[0].id == without_job.id
def get_notifications(): _data = request.args.to_dict(flat=False) # flat=False makes everything a list, but we only ever allow one value for "older_than" if 'older_than' in _data: _data['older_than'] = _data['older_than'][0] # and client reference if 'reference' in _data: _data['reference'] = _data['reference'][0] if 'template_id' in _data: _data['template_id'] = _data['template_id'][0] if 'include_jobs' in _data: _data['include_jobs'] = _data['include_jobs'][0] data = validate(_data, get_notifications_request) paginated_notifications = notifications_dao.get_notifications_for_service( str(authenticated_service.id), filter_dict=data, key_type=api_user.key_type, personalisation=True, older_than=data.get('older_than'), client_reference=data.get('reference'), page_size=current_app.config.get('API_PAGE_SIZE'), include_jobs=data.get('include_jobs', 'true').lower() == 'true', ) def _build_links(notifications): _links = { 'current': url_for(".get_notifications", _external=True, **data), } if len(notifications): next_query_params = dict(data, older_than=notifications[-1].id) _links['next'] = url_for(".get_notifications", _external=True, **next_query_params) return _links return jsonify( notifications=[notification.serialize() for notification in paginated_notifications.items], links=_build_links(paginated_notifications.items) ), 200
def get_notifications(): _data = request.args.to_dict(flat=False) # flat=False makes everything a list, but we only ever allow one value for "older_than" if 'older_than' in _data: _data['older_than'] = _data['older_than'][0] # and client reference if 'reference' in _data: _data['reference'] = _data['reference'][0] data = validate(_data, get_notifications_request) paginated_notifications = notifications_dao.get_notifications_for_service( str(api_user.service_id), filter_dict=data, key_type=api_user.key_type, personalisation=True, older_than=data.get('older_than'), client_reference=data.get('reference') ) def _build_links(notifications): _links = { 'current': url_for(".get_notifications", _external=True, **data), } if len(notifications): next_query_params = dict(data, older_than=notifications[-1].id) _links['next'] = url_for(".get_notifications", _external=True, **next_query_params) return _links return jsonify( notifications=[notification.serialize() for notification in paginated_notifications.items], links=_build_links(paginated_notifications.items) ), 200