1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-25 14:34:19 +00:00

Merge branch 'main' of zero.ryg.one:nest/g2-progetto

This commit is contained in:
FlaviaC-uni 2021-05-28 21:51:00 +02:00
commit b9a9e4cfdb
14 changed files with 215 additions and 84 deletions

View file

@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
import datetime import datetime
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -150,16 +150,16 @@ def page_alert(aid):
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
alert = Alert.query.filter_by(id=aid).first() alert = Alert.query.filter_by(id=aid).first()
if not alert or alert.repository.is_deleted: if not alert or alert.repository.is_deleted:
return json_error("Could not find alert.", ALERT_NOT_FOUND), 404 return json_error("Could not find alert.", errors.ALERT_NOT_FOUND), 404
if alert.repository not in [a.repository for a in user.authorizations] + user.owner_of: if alert.repository not in [a.repository for a in user.authorizations] + user.owner_of:
return json_error("You are not authorized to proceed.", USER_NOT_AUTHORIZED), 403 return json_error("You are not authorized to proceed.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET": if request.method == "GET":
return json_success(alert.to_json()), 200 return json_success(alert.to_json()), 200
if alert.repository not in user.owner_of: if alert.repository not in user.owner_of:
return json_error("You are not authorized to proceed.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized to proceed.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "PATCH": if request.method == "PATCH":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if 'name' in request.json: if 'name' in request.json:
alert.name = request.json['name'] alert.name = request.json['name']
@ -171,9 +171,9 @@ def page_alert(aid):
try: try:
alert.evaluation_mode = ConditionMode(request.json['evaluation_mode']) alert.evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError: except KeyError:
return json_error("Unknown `evaluation_mode` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `evaluation_mode` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
ext.session.commit() ext.session.commit()
return json_success(alert.to_json()), 200 return json_success(alert.to_json()), 200
elif request.method == "DELETE": elif request.method == "DELETE":
@ -191,14 +191,14 @@ def page_alert(aid):
ext.session.delete(alert) ext.session.delete(alert)
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
return json_error("Something went wrong while deleting alert.", ALERT_DELETION_FAILURE), 500 return json_error("Something went wrong while deleting alert.", errors.ALERT_DELETION_FAILURE), 500
return json_success("Deletion completed."), 204 return json_success("Deletion completed."), 204
elif request.method == "PUT": elif request.method == "PUT":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if not json_request_authorizer(request.json, alert): if not json_request_authorizer(request.json, alert):
return json_error("Missing one or more parameters in alert json.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing one or more parameters in alert json.", errors.GENERIC_MISSING_FIELDS), 400
alert.limit = request.json['limit'] alert.limit = request.json['limit']
alert.name = request.json['name'] alert.name = request.json['name']
alert.window_size = request.json['window_size'] alert.window_size = request.json['window_size']
@ -206,9 +206,9 @@ def page_alert(aid):
try: try:
alert.evaluation_mode = ConditionMode(mode) alert.evaluation_mode = ConditionMode(mode)
except KeyError: except KeyError:
return json_error("Unknown `evaluation_mode` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `evaluation_mode` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if request.json['conditions'] is not None: if request.json['conditions'] is not None:
# Wow very pythonic so much wow # Wow very pythonic so much wow
# Obtain list of no longer needed connections # Obtain list of no longer needed connections
@ -223,15 +223,15 @@ def page_alert(aid):
for c in request.json['conditions']: for c in request.json['conditions']:
if not c.get("id"): if not c.get("id"):
if (type_ := c.get("type")) is None: if (type_ := c.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := c.get("content")): if not (content := c.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
con = Condition(content=content, type=type_, repository_id=alert.repository_id) con = Condition(content=content, type=type_, repository_id=alert.repository_id)

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -73,30 +73,30 @@ def page_repository_alerts(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "GET": if request.method == "GET":
return json_success([alert.to_json() for alert in repository.alerts]) return json_success([alert.to_json() for alert in repository.alerts])
if request.method == "POST": if request.method == "POST":
if 'name' not in request.json: if 'name' not in request.json:
return json_error("Missing name.", ALERT_NO_NAME), 400 return json_error("Missing name.", errors.ALERT_NO_NAME), 400
if 'limit' not in request.json: if 'limit' not in request.json:
return json_error('Missing limit', ALERT_NO_LIMIT), 400 return json_error('Missing limit', errors.ALERT_NO_LIMIT), 400
if 'window_size' not in request.json: if 'window_size' not in request.json:
return json_error('Missing window size', ALERT_NO_WINDOW), 400 return json_error('Missing window size', errors.ALERT_NO_WINDOW), 400
if (mode := request.json.get("evaluation_mode")) is not None: if (mode := request.json.get("evaluation_mode")) is not None:
try: try:
mode = ConditionMode(mode) mode = ConditionMode(mode)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
else: else:
return json_error("Evaluation mode was not provided.", ALERT_NO_EVALUATION), 400 return json_error("Evaluation mode was not provided.", errors.ALERT_NO_EVALUATION), 400
alert = Alert(name=request.json['name'], limit=request.json['limit'], window_size=request.json['window_size'], alert = Alert(name=request.json['name'], limit=request.json['limit'], window_size=request.json['window_size'],
repository_id=rid, evaluation_mode=mode) repository_id=rid, evaluation_mode=mode)
@ -105,15 +105,15 @@ def page_repository_alerts(rid):
if request.json['conditions'] is not None: if request.json['conditions'] is not None:
for condition in request.json['conditions']: for condition in request.json['conditions']:
if (type_ := condition.get("type")) is None: if (type_ := condition.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := condition.get("content")): if not (content := condition.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
c = Condition(content=content, type=type_) c = Condition(content=content, type=type_)

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -42,12 +42,12 @@ def page_authorization(rid, email):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if not repository: if not repository:
return json_error("Could not find the repository.", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find the repository.", errors.REPOSITORY_NOT_FOUND), 404
if user != repository.owner: if user != repository.owner:
return json_error("You are not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("You are not authorized.", errors.USER_NOT_AUTHORIZED), 403
authorization = Authorization.query.filter_by(rid=rid, email=email).first() authorization = Authorization.query.filter_by(rid=rid, email=email).first()
if not authorization: if not authorization:
return json_error("Could not find the authorization", AUTHORIZATION_NOT_FOUND), 404 return json_error("Could not find the authorization", errors.AUTHORIZATION_NOT_FOUND), 404
if request.method == "DELETE": if request.method == "DELETE":
ext.session.delete(authorization) ext.session.delete(authorization)
ext.session.commit() ext.session.commit()

View file

@ -4,7 +4,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, fin
from nest_backend.database import ext, User, Authorization, Repository from nest_backend.database import ext, User, Authorization, Repository
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator from nest_backend.gestione import hashtag_validator
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -114,24 +114,24 @@ def page_repository_authorizations(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "GET": if request.method == "GET":
try: try:
return json_success([a.to_json() for a in repository.authorizations]) return json_success([a.to_json() for a in repository.authorizations])
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if not request.json.get("email"): if not request.json.get("email"):
return json_error("Missing user email.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing user email.", errors.GENERIC_MISSING_FIELDS), 400
target = User.query.filter_by(email=request.json.get('email')).first() target = User.query.filter_by(email=request.json.get('email')).first()
if not target: if not target:
return json_error("User could not be located", USER_NOT_FOUND), 400 return json_error("User could not be located", errors.USER_NOT_FOUND), 400
if target == user: if target == user:
return json_error("Owner cannot be a spectator", GENERIC_ALREADY_EXISTS), 406 return json_error("Owner cannot be a spectator", errors.GENERIC_ALREADY_EXISTS), 406
if request.method == "POST": if request.method == "POST":
authorization = Authorization(email=request.json.get('email'), rid=repository.id) authorization = Authorization(email=request.json.get('email'), rid=repository.id)
ext.session.add(authorization) ext.session.add(authorization)

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -107,25 +107,25 @@ def page_condition(cid):
condition = Condition.query.filter_by(id=cid).first() condition = Condition.query.filter_by(id=cid).first()
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if not condition or condition.repository.is_deleted: if not condition or condition.repository.is_deleted:
return json_error("Could not find the condition.", CONDITION_NOT_FOUND), 404 return json_error("Could not find the condition.", errors.CONDITION_NOT_FOUND), 404
if condition.repository not in [a.repository for a in user.authorizations] + user.owner_of and not user.isAdmin: if condition.repository not in [a.repository for a in user.authorizations] + user.owner_of and not user.isAdmin:
return json_error("You lack the authorization to proceed, pal.", USER_NOT_AUTHORIZED), 403 return json_error("You lack the authorization to proceed, pal.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET": if request.method == "GET":
return json_success(condition.to_json()), 200 return json_success(condition.to_json()), 200
if condition.repository not in user.owner_of and not user.isAdmin: if condition.repository not in user.owner_of and not user.isAdmin:
return json_error("You lack the authorization to proceed, pal.", USER_NOT_AUTHORIZED), 403 return json_error("You lack the authorization to proceed, pal.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "PATCH": if request.method == "PATCH":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if (type_ := request.json.get("type")) is not None: if (type_ := request.json.get("type")) is not None:
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
condition.type = type_ condition.type = type_
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if content := request.json.get("content"): if content := request.json.get("content"):
condition.content = content condition.content = content

View file

@ -5,7 +5,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, Con
from nest_backend.database import ext from nest_backend.database import ext
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator from nest_backend.gestione import hashtag_validator
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -75,34 +75,34 @@ def page_repository_conditions(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if request.method == "GET": if request.method == "GET":
try: try:
return json_success([u.to_json() for u in repository.conditions]) return json_success([u.to_json() for u in repository.conditions])
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "POST": if request.method == "POST":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if (type_ := request.json.get("type")) is None: if (type_ := request.json.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := request.json.get("content")): if not (content := request.json.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
condition = Condition(content=content, type=type_, repository_id=rid) condition = Condition(content=content, type=type_, repository_id=rid)

View file

@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
import datetime import datetime
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
from nest_crawler.repo_search import search_repo_conditions from nest_crawler.repo_search import search_repo_conditions
import threading import threading
@ -85,12 +85,12 @@ def page_repositories():
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now. # Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
if not request.json.get("name") or not request.json.get("conditions") or not str( if not request.json.get("name") or not request.json.get("conditions") or not str(
request.json.get("evaluation_mode")): request.json.get("evaluation_mode")):
return json_error("Missing arguments.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing arguments.", errors.GENERIC_MISSING_FIELDS), 400
name = request.json.get("name") name = request.json.get("name")
try: try:
evaluation_mode = ConditionMode(request.json['evaluation_mode']) evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode) repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode)
@ -108,7 +108,7 @@ def page_repositories():
try: try:
type_ = ConditionType(c['type']) type_ = ConditionType(c['type'])
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id)) ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id))
ext.session.commit() ext.session.commit()
repository.is_active = True repository.is_active = True

View file

@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
import datetime import datetime
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -156,11 +156,11 @@ def page_repository(rid):
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository.", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository.", errors.REPOSITORY_NOT_FOUND), 404
if request.method == "GET": if request.method == "GET":
return json_success(repository.to_json()), 200 return json_success(repository.to_json()), 200
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not the owner of this repository.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not the owner of this repository.", errors.REPOSITORY_NOT_OWNER), 403
elif request.method == "PATCH": elif request.method == "PATCH":
if 'name' in request.json: if 'name' in request.json:
repository.name = request.json['name'] repository.name = request.json['name']
@ -173,7 +173,7 @@ def page_repository(rid):
try: try:
evaluation_mode = ConditionMode(request.json['evaluation_mode']) evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
repository.evaluation_mode = evaluation_mode repository.evaluation_mode = evaluation_mode
ext.session.commit() ext.session.commit()
return json_success(repository.to_json()), 204 return json_success(repository.to_json()), 204
@ -183,16 +183,16 @@ def page_repository(rid):
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
ext.session.rollback() ext.session.rollback()
return json_error("Cant delete repository because of dependencies.", REPOSITORY_DEPENDENCY_FAILURE), 500 return json_error("Cant delete repository because of dependencies.", errors.REPOSITORY_DEPENDENCY_FAILURE), 500
return json_success("Success"), 204 return json_success("Success"), 204
elif request.method == "PUT": elif request.method == "PUT":
if not json_request_authorizer(request.json, repository): if not json_request_authorizer(request.json, repository):
return json_error("Missing one or more parameters in repository json.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing one or more parameters in repository json.", errors.GENERIC_MISSING_FIELDS), 400
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now. # Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
try: try:
evaluation_mode = ConditionMode(request.json['evaluation_mode']) evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
repository.evaluation_mode = evaluation_mode repository.evaluation_mode = evaluation_mode
repository.name = request.json['name'] repository.name = request.json['name']
repository.is_active = request.json['is_active'] repository.is_active = request.json['is_active']
@ -210,14 +210,14 @@ def page_repository(rid):
ext.session.delete(c) ext.session.delete(c)
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
return json_error("Could not delete conditions.", GENERIC_UFO), 500 return json_error("Could not delete conditions.", errors.GENERIC_UFO), 500
# Create brand new conditions # Create brand new conditions
for c in request.json['conditions']: for c in request.json['conditions']:
if not c['id']: if not c['id']:
try: try:
type_ = ConditionType(c['type']) type_ = ConditionType(c['type'])
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
content = c['content'] content = c['content']
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)

View file

@ -5,7 +5,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, Con
from nest_backend.database import ext from nest_backend.database import ext
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator from nest_backend.gestione import hashtag_validator
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -45,11 +45,11 @@ def page_repository_tweets(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if user.email != repository.owner_id and user.email not in [a.email for a in repository.authorizations]: if user.email != repository.owner_id and user.email not in [a.email for a in repository.authorizations]:
return json_error("You are not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("You are not authorized.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET": if request.method == "GET":
return json_success([t.tweet.to_json() for t in repository.tweets]) return json_success([t.tweet.to_json() for t in repository.tweets])

View file

@ -4,7 +4,7 @@ from nest_backend.gestione import *
from flask_jwt_extended import create_access_token from flask_jwt_extended import create_access_token
from flask_cors import cross_origin from flask_cors import cross_origin
from datetime import timedelta, datetime from datetime import timedelta, datetime
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -43,4 +43,4 @@ def page_login():
access_token = create_access_token(identity=email, expires_delta=delta) access_token = create_access_token(identity=email, expires_delta=delta)
user = find_user(email) user = find_user(email)
return json_success({"access_token": access_token, 'user': user.to_json(), "expiration": expiration}), 201 return json_success({"access_token": access_token, 'user': user.to_json(), "expiration": expiration}), 201
return json_error("Bad username or password.", USER_WRONG_CREDENTIALS), 401 return json_error("Bad username or password.", errors.USER_WRONG_CREDENTIALS), 401

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -118,16 +118,16 @@ def page_user(email):
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
target = find_user(email) target = find_user(email)
if not target: if not target:
return json_error("Could not locate the user.", USER_NOT_FOUND), 404 return json_error("Could not locate the user.", errors.USER_NOT_FOUND), 404
if request.method == "GET": if request.method == "GET":
if not email == user.email and not user.isAdmin: if not email == user.email and not user.isAdmin:
return json_error("Thou art not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("Thou art not authorized.", errors.USER_NOT_AUTHORIZED), 403
return json_success(target.to_json()) return json_success(target.to_json())
elif request.method == "DELETE": elif request.method == "DELETE":
if not user.isAdmin: if not user.isAdmin:
return json_error("User is not admin.", USER_NOT_ADMIN), 403 return json_error("User is not admin.", errors.USER_NOT_ADMIN), 403
if user == target: if user == target:
return json_error("The user cant delete himself. Its a sin.", USER_PREVENT_SEPPUKU), 406 return json_error("The user cant delete himself. Its a sin.", errors.USER_PREVENT_SEPPUKU), 406
repos = target.owner_of repos = target.owner_of
for repository in repos: for repository in repos:
repository.owner_id = user.email repository.owner_id = user.email
@ -140,11 +140,11 @@ def page_user(email):
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
ext.session.rollback() ext.session.rollback()
return json_error("Could not delete the user.", USER_DELETION_ERROR), 500 return json_error("Could not delete the user.", errors.USER_DELETION_ERROR), 500
return json_success(""), 204 # "The user has been deleted." return json_success(""), 204 # "The user has been deleted."
elif request.method == "PATCH": elif request.method == "PATCH":
if not email == user.email and not user.isAdmin: if not email == user.email and not user.isAdmin:
return json_error("Thou art not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("Thou art not authorized.", errors.USER_NOT_AUTHORIZED), 403
target = find_user(email) target = find_user(email)
if request.json.get("username"): if request.json.get("username"):
target.username = request.json.get("username") target.username = request.json.get("username")

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -69,11 +69,11 @@ def page_users():
return json_success([user.to_json() for user in users]), 200 return json_success([user.to_json() for user in users]), 200
if request.method == "POST": if request.method == "POST":
if not user.isAdmin: if not user.isAdmin:
return json_error("User is not admin. Thou art not authorized.", USER_NOT_ADMIN), 403 return json_error("User is not admin. Thou art not authorized.", errors.USER_NOT_ADMIN), 403
if not request.json.get("email") or not request.json.get("password") or not request.json.get("username"): if not request.json.get("email") or not request.json.get("password") or not request.json.get("username"):
return json_error("Missing required fields.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing required fields.", errors.GENERIC_MISSING_FIELDS), 400
if User.query.filter_by(email=request.json.get("email")).first(): if User.query.filter_by(email=request.json.get("email")).first():
return json_error("User already exists.", GENERIC_ALREADY_EXISTS), 406 return json_error("User already exists.", errors.GENERIC_ALREADY_EXISTS), 406
new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")), new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")),
username=request.json.get("username")) username=request.json.get("username"))
ext.session.add(new_user) ext.session.add(new_user)

View file

@ -199,7 +199,113 @@ class TestOneAlertOfARepository:
# test PUT # test PUT
def test_put_alert_no_json(self, flask_client: Client, user_headers): def test_put_alert_no_json(self, flask_client: Client, user_headers):
r = flask_client.patch(f'/api/v1/alert/2', headers=user_headers) r = flask_client.put(f'/api/v1/alert/2', headers=user_headers)
assert r.status_code == 400
assert r.json["result"] == "failure"
def test_put_alert_wrong_evaluation_mode(self, flask_client: Client, user_headers):
r = flask_client.put(f'/api/v1/alert/2', headers=user_headers,
json={
"conditions": [
{
"content": "string",
"id": 0,
"type": 0
}
],
"evaluation_mode": 99,
"id": 0,
"limit": 0,
"name": "string",
"notifications": [
{
"id": 0,
"ora": "2021-05-28T18:23:22.324Z",
"repository_id": 0
}
],
"repository_id": 0,
"window_size": 0
})
assert r.status_code == 400
assert r.json["result"] == "failure"
def test_put_alert_empty_conditions_type(self, flask_client: Client, user_headers):
r = flask_client.put(f'/api/v1/alert/2', headers=user_headers,
json={
"conditions": [
{
"content": "string",
"id": 0
}
],
"evaluation_mode": 0,
"id": 0,
"limit": 0,
"name": "string",
"notifications": [
{
"id": 0,
"ora": "2021-05-28T18:23:22.324Z",
"repository_id": 0
}
],
"repository_id": 0,
"window_size": 0
})
assert r.status_code == 400
assert r.json["result"] == "failure"
def test_put_alert_wrong_conditions_type(self, flask_client: Client, user_headers):
r = flask_client.put(f'/api/v1/alert/2', headers=user_headers,
json={
"conditions": [
{
"content": "string",
"id": 0,
"type": 99
}
],
"evaluation_mode": 0,
"id": 0,
"limit": 0,
"name": "string",
"notifications": [
{
"id": 0,
"ora": "2021-05-28T18:23:22.324Z",
"repository_id": 0
}
],
"repository_id": 0,
"window_size": 0
})
assert r.status_code == 400
assert r.json["result"] == "failure"
def test_put_alert_missing_conditions_content(self, flask_client: Client, user_headers):
r = flask_client.put(f'/api/v1/alert/2', headers=user_headers,
json={
"conditions": [
{
"id": 0,
"type": 99
}
],
"evaluation_mode": 0,
"id": 0,
"limit": 0,
"name": "string",
"notifications": [
{
"id": 0,
"ora": "2021-05-28T18:23:22.324Z",
"repository_id": 0
}
],
"repository_id": 0,
"window_size": 0
})
assert r.status_code == 400 assert r.status_code == 400
assert r.json["result"] == "failure" assert r.json["result"] == "failure"

View file

@ -0,0 +1,25 @@
from flask.testing import Client
'''A file that contains tests classes and methods for all the requests concerning Tweets.'''
# TODO capire come passare i Tweet nell'URL
class TestTweetGet:
def test_for_success(self, flask_client: Client, user_headers):
r = flask_client.get(f'/api/v1/repositories/1/tweets/', headers=user_headers)
assert r.status_code == 200
assert r.json["result"] == "success"
def test_repository_not_found(self, flask_client: Client, user_headers):
r = flask_client.get(f'/api/v1/repositories/99/tweets/', headers=user_headers)
assert r.status_code == 404
assert r.json["result"] == "failure"
def test_user_wrong_owner(self, flask_client: Client, user_headers):
r = flask_client.get(f'/api/v1/repositories/2/tweets/', headers=user_headers)
assert r.status_code == 403
assert r.json["result"] == "failure"
def test_user_not_logged(self, flask_client: Client, ):
r = flask_client.get(f'/api/v1/repositories/2/tweets/')
assert r.status_code == 401