-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7412087
commit 500567b
Showing
9 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import pytest | ||
import datetime | ||
import uuid | ||
import bcrypt | ||
from tools import users_collection | ||
from tools import users_collection, sessions_collection | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -15,7 +16,24 @@ def fixtureuser() -> dict: | |
"email": "[email protected]", | ||
"username": "FixtureUser", | ||
"createdAt": datetime.datetime.now(), | ||
"test": True, | ||
} | ||
users_collection.insert_one(user_data) | ||
result = users_collection.insert_one(user_data) | ||
user_data["password"] = "Kennwort1!" | ||
user_data["_id"] = str(result.inserted_id) | ||
return user_data | ||
|
||
|
||
@pytest.fixture | ||
def fixturesessiontoken_user(fixtureuser) -> tuple[str, dict]: | ||
# Generate a new session token | ||
session_token = str(uuid.uuid4()) | ||
# Persist the session | ||
sessions_collection.insert_one( | ||
{ | ||
"session_token": session_token, | ||
"user_id": fixtureuser["_id"], | ||
"createdAt": datetime.datetime.now(), | ||
} | ||
) | ||
return session_token, fixtureuser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from fastapi.testclient import TestClient | ||
from crud.user import get_dangerous_user | ||
|
||
from .main import app | ||
|
||
client = TestClient(app) | ||
|
||
|
||
# Unauthorized Route Test | ||
def test_unauthorized_profile(fixtureuser): | ||
response = client.get("/profile") | ||
assert response.status_code == 401 | ||
|
||
|
||
# Successfull tests | ||
def test_get_profile(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.get("/profile") | ||
resp_json = response.json() | ||
assert response.status_code == 200 | ||
assert resp_json.get("email") == fixturesessiontoken_user[1]["email"] | ||
assert resp_json.get("username") == fixturesessiontoken_user[1]["username"] | ||
assert resp_json.get("createdAt") is not None | ||
assert resp_json.get("password") is None | ||
assert resp_json.get("_id") is None | ||
|
||
|
||
def test_update_username_valid(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.patch("/profile", json={"username": "newusername"}) | ||
resp_json = response.json() | ||
assert response.status_code == 200 | ||
assert resp_json.get("email") == fixturesessiontoken_user[1]["email"] | ||
assert resp_json.get("username") == "newusername" | ||
assert resp_json.get("createdAt") is not None | ||
|
||
|
||
def test_update_email(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# E-Mail should not be updateable through this endpoint | ||
response = client.patch("/profile", json={"email": "[email protected]"}) | ||
resp_json = response.json() | ||
assert response.status_code == 200 | ||
assert resp_json.get("email") == fixturesessiontoken_user[1]["email"] | ||
assert resp_json.get("username") == fixturesessiontoken_user[1]["username"] | ||
assert resp_json.get("createdAt") is not None | ||
|
||
|
||
def test_update_password(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# Password should not be updateable through this endpoint | ||
response = client.patch("/profile", json={"password": "Kennwort2!"}) | ||
assert response.status_code == 200 | ||
assert ( | ||
get_dangerous_user(fixturesessiontoken_user[1]["_id"])["password"] | ||
!= "Kennwort2!" | ||
) | ||
|
||
|
||
def test_update_createdAt(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# Password should not be updateable through this endpoint | ||
response = client.patch("/profile", json={"createdAt": "1921"}) | ||
assert response.status_code == 200 | ||
assert get_dangerous_user(fixturesessiontoken_user[1]["_id"])["createdAt"] != "1921" | ||
|
||
|
||
def test_update_additional_data_valid(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# Data that is not existent, but updateable should be possible | ||
response = client.patch("/profile", json={"test2": "1921"}) | ||
assert response.status_code == 200 | ||
assert get_dangerous_user(fixturesessiontoken_user[1]["_id"]).get("test2") == "1921" | ||
|
||
|
||
def test_update_nonexistent_data(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# Additional data should not be updateable through this endpoint | ||
response = client.patch("/profile", json={"sas": "1921"}) | ||
assert response.status_code == 200 | ||
assert get_dangerous_user(fixturesessiontoken_user[1]["_id"]).get("sas") is None | ||
|
||
|
||
def test_update_additional_existent_data(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
# Data that is additional but already exists should also be updateable | ||
response = client.patch("/profile", json={"test": "Ya"}) | ||
assert response.status_code == 200 | ||
assert get_dangerous_user(fixturesessiontoken_user[1]["_id"]).get("test") == "Ya" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from fastapi.testclient import TestClient | ||
from tools import users_collection | ||
|
||
from .main import app | ||
|
||
|
@@ -37,6 +38,12 @@ def test_create_account_additional_data(): | |
assert response.status_code == 200 | ||
assert resp_js.get("session_token") is not None | ||
assert int(resp_js.get("expires")) is not None | ||
# Check which aditional data was saved | ||
usr = users_collection.find_one({"email": "[email protected]"}) | ||
# Check which data should be saved (testing_conf.json) | ||
assert usr.get("test") is True | ||
assert usr.get("test2") is None | ||
assert usr.get("test3") is None | ||
|
||
|
||
# MISSING DATA TESTS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters