From 3f3f1066caa28e6530f2eda625e5f01caff3d04e Mon Sep 17 00:00:00 2001 From: Annielated Date: Fri, 28 May 2021 19:01:55 +0200 Subject: [PATCH 1/5] aggiunti test sui tweet --- nest_backend/test/test_6_tweet.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 nest_backend/test/test_6_tweet.py diff --git a/nest_backend/test/test_6_tweet.py b/nest_backend/test/test_6_tweet.py new file mode 100644 index 0000000..a8f822d --- /dev/null +++ b/nest_backend/test/test_6_tweet.py @@ -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 From 4411f0310c7202f7f3f362b9d2e416acfd694e0d Mon Sep 17 00:00:00 2001 From: stefanogoldoni Date: Fri, 28 May 2021 19:51:12 +0200 Subject: [PATCH 2/5] fixato un po' di smell code segnalato da sonarqube --- .../repository/authorizations/authorization.py | 8 ++++---- .../authorizations/repository_authorizations.py | 16 ++++++++-------- nest_backend/routes/users/login.py | 4 ++-- nest_backend/routes/users/user.py | 14 +++++++------- nest_backend/routes/users/users.py | 8 ++++---- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/nest_backend/routes/repository/authorizations/authorization.py b/nest_backend/routes/repository/authorizations/authorization.py index cf6b932..0602813 100644 --- a/nest_backend/routes/repository/authorizations/authorization.py +++ b/nest_backend/routes/repository/authorizations/authorization.py @@ -3,7 +3,7 @@ from nest_backend.database import * from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -42,12 +42,12 @@ def page_authorization(rid, email): repository = Repository.query.filter_by(id=rid, is_deleted=False).first() user = find_user(get_jwt_identity()) 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: - 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() 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": ext.session.delete(authorization) ext.session.commit() diff --git a/nest_backend/routes/repository/authorizations/repository_authorizations.py b/nest_backend/routes/repository/authorizations/repository_authorizations.py index 9afc0a2..724e081 100644 --- a/nest_backend/routes/repository/authorizations/repository_authorizations.py +++ b/nest_backend/routes/repository/authorizations/repository_authorizations.py @@ -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 flask_cors import cross_origin from nest_backend.gestione import hashtag_validator -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -114,24 +114,24 @@ def page_repository_authorizations(rid): repository = Repository.query.filter_by(id=rid, is_deleted=False).first() 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()) 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": try: return json_success([a.to_json() for a in repository.authorizations]) 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: - 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"): - 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() 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: - 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": authorization = Authorization(email=request.json.get('email'), rid=repository.id) ext.session.add(authorization) diff --git a/nest_backend/routes/users/login.py b/nest_backend/routes/users/login.py index 0178c44..e99f03d 100644 --- a/nest_backend/routes/users/login.py +++ b/nest_backend/routes/users/login.py @@ -4,7 +4,7 @@ from nest_backend.gestione import * from flask_jwt_extended import create_access_token from flask_cors import cross_origin from datetime import timedelta, datetime -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -43,4 +43,4 @@ def page_login(): access_token = create_access_token(identity=email, expires_delta=delta) user = find_user(email) 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 diff --git a/nest_backend/routes/users/user.py b/nest_backend/routes/users/user.py index 475ee44..63ceb1b 100644 --- a/nest_backend/routes/users/user.py +++ b/nest_backend/routes/users/user.py @@ -3,7 +3,7 @@ from nest_backend.database import * from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -118,16 +118,16 @@ def page_user(email): user = find_user(get_jwt_identity()) target = find_user(email) 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 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()) elif request.method == "DELETE": 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: - 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 for repository in repos: repository.owner_id = user.email @@ -140,11 +140,11 @@ def page_user(email): ext.session.commit() except Exception as e: 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." elif request.method == "PATCH": 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) if request.json.get("username"): target.username = request.json.get("username") diff --git a/nest_backend/routes/users/users.py b/nest_backend/routes/users/users.py index 3225876..19e546e 100644 --- a/nest_backend/routes/users/users.py +++ b/nest_backend/routes/users/users.py @@ -3,7 +3,7 @@ from nest_backend.database import * from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -69,11 +69,11 @@ def page_users(): return json_success([user.to_json() for user in users]), 200 if request.method == "POST": 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"): - 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(): - 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")), username=request.json.get("username")) ext.session.add(new_user) From 02b22140eb175dd40b673fc43fc9707fd84847d5 Mon Sep 17 00:00:00 2001 From: stefanogoldoni Date: Fri, 28 May 2021 19:53:31 +0200 Subject: [PATCH 3/5] fixato un po' di smell code segnalato da sonarqube --- nest_backend/routes/repository/repositories.py | 8 ++++---- nest_backend/routes/repository/repository.py | 18 +++++++++--------- .../repository/tweets/repository_tweets.py | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/nest_backend/routes/repository/repositories.py b/nest_backend/routes/repository/repositories.py index 0da0ff6..d66af95 100644 --- a/nest_backend/routes/repository/repositories.py +++ b/nest_backend/routes/repository/repositories.py @@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * import datetime 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 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. if not request.json.get("name") or not request.json.get("conditions") or not str( 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") try: evaluation_mode = ConditionMode(request.json['evaluation_mode']) 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: return json_error("Unknown error: " + str(e)), 400 repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode) @@ -108,7 +108,7 @@ def page_repositories(): try: type_ = ConditionType(c['type']) 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.commit() repository.is_active = True diff --git a/nest_backend/routes/repository/repository.py b/nest_backend/routes/repository/repository.py index 68c4d3e..a9a35e2 100644 --- a/nest_backend/routes/repository/repository.py +++ b/nest_backend/routes/repository/repository.py @@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin import datetime -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -156,11 +156,11 @@ def page_repository(rid): user = find_user(get_jwt_identity()) repository = Repository.query.filter_by(id=rid, is_deleted=False).first() 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": return json_success(repository.to_json()), 200 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": if 'name' in request.json: repository.name = request.json['name'] @@ -173,7 +173,7 @@ def page_repository(rid): try: evaluation_mode = ConditionMode(request.json['evaluation_mode']) 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 ext.session.commit() return json_success(repository.to_json()), 204 @@ -183,16 +183,16 @@ def page_repository(rid): ext.session.commit() except Exception as e: 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 elif request.method == "PUT": 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. try: evaluation_mode = ConditionMode(request.json['evaluation_mode']) 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.name = request.json['name'] repository.is_active = request.json['is_active'] @@ -210,14 +210,14 @@ def page_repository(rid): ext.session.delete(c) ext.session.commit() 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 for c in request.json['conditions']: if not c['id']: try: type_ = ConditionType(c['type']) 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'] if type_ == ConditionType.hashtag: content = hashtag_validator(content) diff --git a/nest_backend/routes/repository/tweets/repository_tweets.py b/nest_backend/routes/repository/tweets/repository_tweets.py index ce8d427..7da1f81 100644 --- a/nest_backend/routes/repository/tweets/repository_tweets.py +++ b/nest_backend/routes/repository/tweets/repository_tweets.py @@ -5,7 +5,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, Con from nest_backend.database import ext from flask_cors import cross_origin from nest_backend.gestione import hashtag_validator -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -45,11 +45,11 @@ def page_repository_tweets(rid): repository = Repository.query.filter_by(id=rid, is_deleted=False).first() 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()) 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": return json_success([t.tweet.to_json() for t in repository.tweets]) From 12e79f241ee89495a5915d6380b7013a7708ddb5 Mon Sep 17 00:00:00 2001 From: stefanogoldoni Date: Fri, 28 May 2021 19:56:42 +0200 Subject: [PATCH 4/5] fixato un po' di smell code segnalato da sonarqube --- .../routes/repository/alerts/alert.py | 30 +++++++++---------- .../repository/alerts/repository_alerts.py | 24 +++++++-------- .../repository_authorizations.py | 2 +- .../routes/repository/conditions/condition.py | 14 ++++----- .../conditions/repository_conditions.py | 16 +++++----- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/nest_backend/routes/repository/alerts/alert.py b/nest_backend/routes/repository/alerts/alert.py index a5f2e90..e6f65fc 100644 --- a/nest_backend/routes/repository/alerts/alert.py +++ b/nest_backend/routes/repository/alerts/alert.py @@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin import datetime -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -150,16 +150,16 @@ def page_alert(aid): user = find_user(get_jwt_identity()) alert = Alert.query.filter_by(id=aid).first() 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: - 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": return json_success(alert.to_json()), 200 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.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: alert.name = request.json['name'] @@ -171,9 +171,9 @@ def page_alert(aid): try: alert.evaluation_mode = ConditionMode(request.json['evaluation_mode']) 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: - return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 + return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400 ext.session.commit() return json_success(alert.to_json()), 200 elif request.method == "DELETE": @@ -191,14 +191,14 @@ def page_alert(aid): ext.session.delete(alert) ext.session.commit() 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 elif request.method == "PUT": 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): - 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.name = request.json['name'] alert.window_size = request.json['window_size'] @@ -206,9 +206,9 @@ def page_alert(aid): try: alert.evaluation_mode = ConditionMode(mode) 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: - 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: # Wow very pythonic so much wow # Obtain list of no longer needed connections @@ -223,15 +223,15 @@ def page_alert(aid): for c in request.json['conditions']: if not c.get("id"): 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: type_ = ConditionType(type_) 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: return json_error("Unknown error: " + str(e)), 400 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: content = hashtag_validator(content) con = Condition(content=content, type=type_, repository_id=alert.repository_id) diff --git a/nest_backend/routes/repository/alerts/repository_alerts.py b/nest_backend/routes/repository/alerts/repository_alerts.py index 785d2f9..90b8d64 100644 --- a/nest_backend/routes/repository/alerts/repository_alerts.py +++ b/nest_backend/routes/repository/alerts/repository_alerts.py @@ -3,7 +3,7 @@ from nest_backend.database import * from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -73,30 +73,30 @@ def page_repository_alerts(rid): repository = Repository.query.filter_by(id=rid, is_deleted=False).first() 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()) 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": return json_success([alert.to_json() for alert in repository.alerts]) if request.method == "POST": 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: - 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: - 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: try: mode = ConditionMode(mode) 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: - return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 + return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400 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'], repository_id=rid, evaluation_mode=mode) @@ -105,15 +105,15 @@ def page_repository_alerts(rid): if request.json['conditions'] is not None: for condition in request.json['conditions']: 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: type_ = ConditionType(type_) 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: return json_error("Unknown error: " + str(e)), 400 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: content = hashtag_validator(content) c = Condition(content=content, type=type_) diff --git a/nest_backend/routes/repository/authorizations/repository_authorizations.py b/nest_backend/routes/repository/authorizations/repository_authorizations.py index 724e081..7dd3f57 100644 --- a/nest_backend/routes/repository/authorizations/repository_authorizations.py +++ b/nest_backend/routes/repository/authorizations/repository_authorizations.py @@ -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 flask_cors import cross_origin from nest_backend.gestione import hashtag_validator -import nest_backend.errors as errors +import nest_backend.errors as errors @cross_origin() diff --git a/nest_backend/routes/repository/conditions/condition.py b/nest_backend/routes/repository/conditions/condition.py index 57a8e38..68fda3f 100644 --- a/nest_backend/routes/repository/conditions/condition.py +++ b/nest_backend/routes/repository/conditions/condition.py @@ -3,7 +3,7 @@ from nest_backend.database import * from flask_jwt_extended import jwt_required, get_jwt_identity from nest_backend.gestione import * from flask_cors import cross_origin -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -107,25 +107,25 @@ def page_condition(cid): condition = Condition.query.filter_by(id=cid).first() user = find_user(get_jwt_identity()) 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: - 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": return json_success(condition.to_json()), 200 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.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: try: type_ = ConditionType(type_) condition.type = type_ 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: - 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"): condition.content = content diff --git a/nest_backend/routes/repository/conditions/repository_conditions.py b/nest_backend/routes/repository/conditions/repository_conditions.py index 6cca26e..773c0fc 100644 --- a/nest_backend/routes/repository/conditions/repository_conditions.py +++ b/nest_backend/routes/repository/conditions/repository_conditions.py @@ -5,7 +5,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, Con from nest_backend.database import ext from flask_cors import cross_origin from nest_backend.gestione import hashtag_validator -from nest_backend.errors import * +import nest_backend.errors as errors @cross_origin() @@ -75,34 +75,34 @@ def page_repository_conditions(rid): repository = Repository.query.filter_by(id=rid, is_deleted=False).first() 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()) if request.method == "GET": try: return json_success([u.to_json() for u in repository.conditions]) 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: - 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.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: - return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 + return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400 try: type_ = ConditionType(type_) 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: return json_error("Unknown error: " + str(e)), 400 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: content = hashtag_validator(content) condition = Condition(content=content, type=type_, repository_id=rid) From 40ea776f3e5c5b629798d48a9f0bf0279fc29859 Mon Sep 17 00:00:00 2001 From: stefanogoldoni Date: Fri, 28 May 2021 20:40:01 +0200 Subject: [PATCH 5/5] aggiunti nuovi test sugli alert --- nest_backend/test/test_4_alert.py | 108 +++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/nest_backend/test/test_4_alert.py b/nest_backend/test/test_4_alert.py index 49a8877..0e393d3 100644 --- a/nest_backend/test/test_4_alert.py +++ b/nest_backend/test/test_4_alert.py @@ -199,7 +199,113 @@ class TestOneAlertOfARepository: # test PUT 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.json["result"] == "failure"