Skip to content

Commit

Permalink
fixed token test view
Browse files Browse the repository at this point in the history
  • Loading branch information
joseevilasio committed Sep 15, 2023
1 parent e96a089 commit 37058e3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 33 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
150 changes: 123 additions & 27 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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))

Expand All @@ -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)

Expand All @@ -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",
},
)
Expand All @@ -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

Expand All @@ -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",
},
)
Expand All @@ -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"

Expand All @@ -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"

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))

Expand All @@ -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)

Expand All @@ -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))

Expand All @@ -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)

Expand All @@ -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",
},
)
Expand All @@ -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"}

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 37058e3

Please sign in to comment.