diff --git a/requirements.txt b/requirements.txt index 94529aa..1ea1d73 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ Flask==2.1.0 -pytest==5.4.3 +pytest==7.2.1 Flask-PyMongo==2.3.0 pymongo[srv]==4.0.1 flask-cors==3.0.10 diff --git a/tests/conftest.py b/tests/conftest.py index aaecef2..2e869b2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,6 +14,7 @@ def teardown(): mock.patch('api_hoco.connect2db.DB', DB_TEST).start() mock.patch('api_hoco.models.Activity.DB', DB_TEST).start() + mock.patch('api_hoco.models.Question.DB', DB_TEST).start() yield @@ -23,3 +24,11 @@ def controller_activity(): yield activity from api_hoco.models import Activity Activity.DB.activity.delete_many({}) + + +@pytest.fixture +def controller_questions(): + from api_hoco.controllers import questions + yield questions + from api_hoco.models import Question + Question.DB.question.delete_many({}) \ No newline at end of file diff --git a/tests/resources/file.pdf b/tests/resources/file.pdf new file mode 100644 index 0000000..2076775 Binary files /dev/null and b/tests/resources/file.pdf differ diff --git a/tests/resources/image.png b/tests/resources/image.png new file mode 100644 index 0000000..7409dc4 Binary files /dev/null and b/tests/resources/image.png differ diff --git a/tests/test_controllers_activity.py b/tests/test_controllers_activity.py index 2e66767..845ed77 100644 --- a/tests/test_controllers_activity.py +++ b/tests/test_controllers_activity.py @@ -3,6 +3,20 @@ import pytest +@mock.patch('api_hoco.models.Activity.Activity.save') +@mock.patch('api_hoco.models.Activity.Activity.get_all') +def test_register_activity_successful(mock_save, mock_get_all, controller_activity): + certificate = None + data = { + "e-mail": "email" + } + expected_return = {"category": "category", "time": "", "filename": "file.pdf", "credits": "0", "e-mail": "email", "title": "title"} + mock_save.return_value = {"category": "category", "time": "", "filename": "file.pdf", "credits": "0", "e-mail": "email", "title": "title"} + mock_get_all.return_value = expected_return + result = controller_activity.register_activity(certificate, data) + assert result == expected_return + + @mock.patch('api_hoco.models.Activity.Activity.remove') def test_remove_activity_id_invalido(mock_remove, controller_activity): mock_remove.return_value = None diff --git a/tests/test_controllers_questions.py b/tests/test_controllers_questions.py new file mode 100644 index 0000000..1f7865e --- /dev/null +++ b/tests/test_controllers_questions.py @@ -0,0 +1,25 @@ +from unittest import mock + +import pytest + +@mock.patch('api_hoco.controllers.questions.register_question') +@mock.patch('api_hoco.controllers.questions.get_questions') +def test_register_question_successful(mock_register_question, mock_get_questions, controller_questions): + question = "Question?" + answer = "answer" + expected_return = [{'_id': "ID", "question": "Question?", "answer": "answer"}] + mock_register_question = {"question": "Question?", "answer": "answer"} + mock_get_questions.return_value = expected_return + result = controller_questions.register_question(question, answer) + assert result == expected_return + + +@mock.patch('api_hoco.controllers.questions.remove_question') +@mock.patch('api_hoco.controllers.questions.get_questions') +def test_remove_question_successful(mock_remove_question, mock_get_questions, controller_questions): + name = "name" + expected_return = {'_id':'ID', 'name':'name'} + mock_get_questions.return_value = expected_return + mock_remove_question.return_value = 1 + result = controller_questions.remove_question(name) + assert result == expected_return diff --git a/tests/test_routes_activities.py b/tests/test_routes_activities.py index ee10e0f..f6094d6 100644 --- a/tests/test_routes_activities.py +++ b/tests/test_routes_activities.py @@ -1,4 +1,5 @@ from unittest import mock +from pathlib import Path import pytest @@ -11,6 +12,7 @@ def app(): "TESTING": True, }) + yield app @@ -20,6 +22,73 @@ def client(app): return client +@mock.patch('api_hoco.routes.activities.register_activity') +def test_create_activity(mock_register_activity, client): + resources = Path(__file__).parent / "resources" + expected_return = { + "_id": "ID", + "category": "category", + "file": "file.pdf", + "credits": "0", + "e-mail": "email", + "time": "", + "title": "title" + } + mock_register_activity.return_value = expected_return + data_form = {"category": "category", "time": "", "file": (resources / "file.pdf").open("rb"), "credits": "0", "e-mail": "email", "title": "title"} + response = client.post("/activity", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 201 + assert response_json == expected_return + + +def test_create_activity_missing_email(client): + resources = Path(__file__).parent / "resources" + + data_form = {"category": "category", "file": (resources / "file.pdf").open("rb"), "credits": "0", "title": "title"} + response = client.post("/activity", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['e-mail (str)']" + + +def test_create_activity_missing_credits(client): + resources = Path(__file__).parent / "resources" + + data_form = {"category": "category", "file": (resources / "file.pdf").open("rb"), "e-mail": "email", "title": "title"} + response = client.post("/activity", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 400 + assert response_json == None + + +def test_create_activity_missing_certificate(client): + resources = Path(__file__).parent / "resources" + + data_form = {"category": "category", "time": "", "credits": "0", "e-mail": "email", "title": "title"} + response = client.post("/activity", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 400 + assert response_json == None + + +@mock.patch('api_hoco.routes.activities.register_activity') +def test_create_activity_server_erro(mock_register_activity, client): + resources = Path(__file__).parent / "resources" + exception_msg = "uma exceção ocorreu no controller" + mock_register_activity.side_effect = Exception(exception_msg) + data_form = {"category": "category", "time": "", "file": (resources / "file.pdf").open("rb"), "credits": "0", "e-mail": "email", "title": "title"} + response = client.post("/activity", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 500 + assert "Error:" in response_json + + def test_remove_activity_email_nao_informado(client): id_activity = "id" response = client.delete(f"/activity/{id_activity}") diff --git a/tests/test_routes_orgs.py b/tests/test_routes_orgs.py new file mode 100644 index 0000000..fb16e9f --- /dev/null +++ b/tests/test_routes_orgs.py @@ -0,0 +1,73 @@ +from unittest import mock +from pathlib import Path + +import pytest + +from api_hoco import create_app + +@pytest.fixture() +def app(): + app = create_app() + app.config.update({ + "TESTING": True, + }) + + yield app + + +@pytest.fixture +def client(app): + client = app.test_client() + return client + + +def test_create_org_missing_params(client): + resources = Path(__file__).parent / "resources" + + data_form = {"image": (resources / "image.png").open("rb")} + response = client.post("/org", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['name (str)', 'org_url (str)', 'image (file)']" + + +def test_create_org_missing_params_url(client): + resources = Path(__file__).parent / "resources" + + data_form = {"image": (resources / "image.png").open("rb"), "name":"ORG"} + response = client.post("/org", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['name (str)', 'org_url (str)', 'image (file)']" + +@mock.patch('api_hoco.routes.orgs.register_org') +def test_create_org(mock_register_org, client): + resources = Path(__file__).parent / "resources" + expected_return = { + "_id": 'ID', + "name": "ORG", + "org_url": "", + "image": "image.png" + } + mock_register_org.return_value = expected_return + data_form = {"image": (resources / "image.png").open("rb") ,"name": "ORG", "org_url": "",} + response = client.post("/org", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 201 + assert response_json == expected_return + + +@mock.patch('api_hoco.routes.orgs.register_org') +def test_create_org_server_erro(mock_register_org, client): + resources = Path(__file__).parent / "resources" + exception_msg = "uma exceção ocorreu no controller" + mock_register_org.side_effect = Exception(exception_msg) + data_form = {"image": (resources / "image.png").open("rb") ,"name": "ORG", "org_url": "",} + response = client.post("/org", data=data_form, content_type='multipart/form-data') + response_json = response.json + + assert response.status_code == 500 + assert "Error:" in response_json diff --git a/tests/test_routes_questions.py b/tests/test_routes_questions.py new file mode 100644 index 0000000..177362c --- /dev/null +++ b/tests/test_routes_questions.py @@ -0,0 +1,99 @@ +from unittest import mock + +import pytest + +from api_hoco import create_app + +@pytest.fixture() +def app(): + app = create_app() + app.config.update({ + "TESTING": True, + }) + + + yield app + + +@pytest.fixture +def client(app): + client = app.test_client() + return client + + +@mock.patch('api_hoco.routes.questions.register_question') +def test_create_question(mock_register_question, client): + expected_return = { + "_id": "ID", + "question": "question?", + "answer": "answer" + } + mock_register_question.return_value = expected_return + data_json = {"question": "question?", "answer": "answer"} + response = client.post("/question", json=data_json, content_type='application/json') + response_json = response.json + + assert response.status_code == 201 + assert response_json == expected_return + + +def test_create_question_missing_question(client): + data_json = {"question": "", "answer": "answer"} + response = client.post("/question", json=data_json, content_type='application/json') + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['question (str)', 'answer (str)']" + + +def test_create_question_missing_answer(client): + data_json = {"question": "question?", "answer": ""} + response = client.post("/question", json=data_json, content_type='application/json') + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['question (str)', 'answer (str)']" + + +@mock.patch('api_hoco.routes.questions.register_question') +def test_register_question_excecao_no_controller(mock_register_question, client): + exception_msg = "uma exceção ocorreu no controller" + mock_register_question.side_effect = Exception(exception_msg) + data_json = {"question": "question?", "answer": "answer"} + response = client.post("/question", json=data_json, content_type='application/json') + response_json = response.json + + assert response.status_code == 500 + assert "Error:" in response_json + + +@mock.patch('api_hoco.routes.questions.remove_question') +def test_remove_question(mock_remove_question, client): + expected_return = {'isso': 'funcionou'} + mock_remove_question.return_value = expected_return + id_question = "ID" + response = client.delete(f"/question?id={id_question}") + response_json = response.json + + assert response.status_code == 200 + assert response_json == expected_return + + +def test_remove_question_missing_name(client): + response = client.delete(f"/question") + response_json = response.json + + assert response.status_code == 400 + assert response_json == "Parameters required: ['id (str)']" + + +@mock.patch('api_hoco.routes.questions.remove_question') +def test_remove_question_excecao_no_controller(mock_remove_question, client): + exception_msg = "uma exceção ocorreu no controller" + mock_remove_question.side_effect = Exception(exception_msg) + id_question = "ID" + response = client.delete(f"/question?id={id_question}") + response_json = response.json + + assert response.status_code == 500 + assert "Error:" in response_json