From 37058e35dec521043e9ecd785fa7eca8900ad25d Mon Sep 17 00:00:00 2001 From: joseevilasio Date: Fri, 15 Sep 2023 21:33:40 +0100 Subject: [PATCH] fixed token test view --- .github/workflows/main.yml | 7 +- tests/test_views.py | 150 ++++++++++++++++++++++++++++++------- 2 files changed, 124 insertions(+), 33 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f944576..82301b8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -77,12 +77,7 @@ jobs: uses: snok/install-poetry@v1 - name: Install Project - run: poetry install - - - name: Set up environment variables - env: - JWT_TOKEN_TEST: ${{ secrets.JWT_TOKEN_TEST }} - run: echo "TOKEN -> $JWT_TOKEN_TEST" + run: poetry install - name: Run tests run: poetry run pytest -v --forked --junitxml=test-result.xml diff --git a/tests/test_views.py b/tests/test_views.py index e0cbf92..be5af53 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -2,14 +2,11 @@ import pytest +from api.auth import create_user from api.controller import add_new_category, add_new_video +from api.database import mongo from api.plugins import convert_json_for_dict -from tests.constants import ( - CATEGORY_FILE, - JWT_TOKEN_TEST, - VIDEO_FILE, - VIDEO_FILE_2, -) +from tests.constants import CATEGORY_FILE, VIDEO_FILE, VIDEO_FILE_2 @pytest.mark.integration @@ -34,9 +31,13 @@ def test_route_negative(client): def test_positive_list_videos(client): """Test to check if list videos route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + add_new_video(convert_json_for_dict(VIDEO_FILE)) - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} - print(JWT_TOKEN_TEST) + headers = {"Authorization": f"Bearer {token}"} response = client.get("/videos", headers=headers) assert response.status_code == 200 @@ -46,7 +47,12 @@ def test_positive_list_videos(client): def test_negative_list_videos(client): """Test negative to check if list videos route is return OK""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response = client.get("/videos", headers=headers) assert response.status_code == 404 @@ -56,7 +62,12 @@ def test_negative_list_videos(client): def test_one_video_positive_data(client): """Test to check if one video route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} add_new_video(convert_json_for_dict(VIDEO_FILE)) add_new_video(convert_json_for_dict(VIDEO_FILE_2)) @@ -74,7 +85,12 @@ def test_one_video_positive_data(client): def test_one_video_negative_data(client): """Test negative to check if one video route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response_1 = client.get("/videos/1", headers=headers) response_2 = client.get("/videos/2", headers=headers) @@ -87,7 +103,12 @@ def test_one_video_negative_data(client): def test_positive_delete_one_video(client): """Test to check if delete one video route is return OK""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} add_new_video(convert_json_for_dict(VIDEO_FILE)) @@ -102,7 +123,12 @@ def test_positive_delete_one_video(client): def test_negative_delete_one_video(client): """Test negative to check if delete one video route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response_delete = client.delete("/videos/1", headers=headers) @@ -113,12 +139,17 @@ def test_negative_delete_one_video(client): def test_positive_new_video(client): """Test to check if new video route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + with open(VIDEO_FILE, "r") as content: response = client.post( "/videos/new", json=json.load(content), headers={ - "Authorization": f"Bearer {JWT_TOKEN_TEST}", + "Authorization": f"Bearer {token}", "Content-Type": "application/json", }, ) @@ -132,6 +163,11 @@ def test_positive_new_video(client): def test_positive_update_data_video(client): """Test to check if update video route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_data = add_new_video(convert_json_for_dict(VIDEO_FILE)) assert insert_data == 1 @@ -141,7 +177,7 @@ def test_positive_update_data_video(client): "/videos/1", json=data, headers={ - "Authorization": f"Bearer {JWT_TOKEN_TEST}", + "Authorization": f"Bearer {token}", "Content-Type": "application/json", }, ) @@ -155,10 +191,15 @@ def test_positive_update_data_video(client): def test_positive_search_video_query(client): """Test to check if search video route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_data = add_new_video(convert_json_for_dict(VIDEO_FILE)) assert insert_data == 1 - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} word = "github" @@ -175,10 +216,15 @@ def test_positive_search_video_query(client): def test_negative_search_video_query(client): """Test to check if search video route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_data = add_new_video(convert_json_for_dict(VIDEO_FILE)) assert insert_data == 1 - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} word = "carro" @@ -195,8 +241,13 @@ def test_negative_search_video_query(client): def test_positive_list_category(client): """Test to check if list category route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + add_new_category(convert_json_for_dict(CATEGORY_FILE)) - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} response = client.get("/category", headers=headers) assert response.status_code == 200 @@ -206,7 +257,12 @@ def test_positive_list_category(client): def test_negative_list_category(client): """Test negative to check if list category route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response = client.get("/category", headers=headers) assert response.status_code == 404 @@ -216,7 +272,12 @@ def test_negative_list_category(client): def test_positive_one_category_data(client): """Test to check if one category route is return OK""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} add_new_category(convert_json_for_dict(CATEGORY_FILE)) @@ -230,7 +291,12 @@ def test_positive_one_category_data(client): def test_negative_one_category_data(client): """Test negative to check if one category route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response = client.get("/category/1", headers=headers) @@ -241,7 +307,12 @@ def test_negative_one_category_data(client): def test_positive_delete_one_category(client): """Test to check if delete one category route is return OK""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} add_new_category(convert_json_for_dict(CATEGORY_FILE)) @@ -256,7 +327,12 @@ def test_positive_delete_one_category(client): def test_negative_delete_one_category(client): """Test negative to check if delete one category route is return 404""" - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + + headers = {"Authorization": f"Bearer {token}"} response_delete = client.delete("/category/1", headers=headers) @@ -267,12 +343,17 @@ def test_negative_delete_one_category(client): def test_positive_new_category(client): """Test to check if new category route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + with open(CATEGORY_FILE, "r") as content: response = client.post( "/category/new", json=json.load(content), headers={ - "Authorization": f"Bearer {JWT_TOKEN_TEST}", + "Authorization": f"Bearer {token}", "Content-Type": "application/json", }, ) @@ -286,10 +367,15 @@ def test_positive_new_category(client): def test_positive_update_data_category(client): """Test to check if update category route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_data = add_new_category(convert_json_for_dict(CATEGORY_FILE)) assert insert_data == 1 - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} data = {"title": "Terror"} @@ -304,13 +390,18 @@ def test_positive_update_data_category(client): def test_positive_show_videos_by_category(client): """Test to check if show videos by category route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_category = add_new_category(convert_json_for_dict(CATEGORY_FILE)) insert_video = add_new_video(convert_json_for_dict(VIDEO_FILE)) assert insert_category == 1 assert insert_video == 1 - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} response = client.get("/category/1/videos", headers=headers) @@ -322,11 +413,16 @@ def test_positive_show_videos_by_category(client): def test_negative_show_videos_by_category(client): """Test to check if show videos by category route is return OK""" + create_user(username="admin", password="123456") + token = mongo.db.users.find_one( + {"username": "admin"}, projection={"_id": False} + )["token"] + insert_category = add_new_category(convert_json_for_dict(CATEGORY_FILE)) assert insert_category == 1 - headers = {"Authorization": f"Bearer {JWT_TOKEN_TEST}"} + headers = {"Authorization": f"Bearer {token}"} response = client.get("/category/1/videos", headers=headers)