-
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
69d31b5
commit faebd98
Showing
5 changed files
with
98 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import uuid | ||
import bcrypt | ||
from tools import users_collection, sessions_collection | ||
from bson import ObjectId | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -24,16 +25,52 @@ def fixtureuser() -> dict: | |
return user_data | ||
|
||
|
||
@pytest.fixture | ||
def fixtureuser2() -> dict: | ||
users_collection.find_one_and_delete({"email": "[email protected]"}) | ||
hashed_pswd = bcrypt.hashpw("Kennwort1!".encode("utf-8"), bcrypt.gensalt(5)).decode( | ||
"utf-8" | ||
) | ||
user_data = { | ||
"password": hashed_pswd, | ||
"email": "[email protected]", | ||
"username": "FixtureUser1", | ||
"createdAt": datetime.datetime.now(), | ||
} | ||
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]: | ||
sessions_collection.delete_many({}) | ||
# Generate a new session token | ||
session_token = str(uuid.uuid4()) | ||
# Persist the session | ||
session_id = sessions_collection.insert_one( | ||
{ | ||
"session_token": session_token, | ||
"user_id": ObjectId(fixtureuser["_id"]), | ||
"createdAt": datetime.datetime.now(), | ||
"device_information": {}, | ||
} | ||
) | ||
return session_token, fixtureuser, session_id.inserted_id | ||
|
||
|
||
@pytest.fixture | ||
def fixturesessiontoken_user2(fixtureuser2) -> tuple[str, dict]: | ||
# Generate a new session token | ||
session_token = str(uuid.uuid4()) | ||
# Persist the session | ||
sessions_collection.insert_one( | ||
session_id = sessions_collection.insert_one( | ||
{ | ||
"session_token": session_token, | ||
"user_id": fixtureuser["_id"], | ||
"user_id": ObjectId(fixtureuser2["_id"]), | ||
"createdAt": datetime.datetime.now(), | ||
"device_information": {}, | ||
} | ||
) | ||
return session_token, fixtureuser | ||
return session_token, fixtureuser2, session_id.inserted_id |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
from .main import app | ||
|
||
|
||
client = TestClient(app) | ||
|
||
|
||
# SUCCESSFUL TESTS | ||
def test_get_sessions(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.get("/sessions") | ||
resp_js = response.json() | ||
assert response.status_code == 200 | ||
assert isinstance(resp_js.get("sessions"), list) | ||
assert len(resp_js.get("sessions")) == 1 | ||
# Don't leak out session_token | ||
assert resp_js.get("sessions")[0].get("session_token") is None | ||
|
||
|
||
def test_delete_session(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.delete("/sessions/" + str(fixturesessiontoken_user[2])) | ||
assert response.status_code == 204 | ||
|
||
|
||
def test_delete_session_otheracc(fixturesessiontoken_user, fixturesessiontoken_user2): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.delete("/sessions/" + str(fixturesessiontoken_user2[2])) | ||
assert response.status_code == 404 | ||
|
||
|
||
def test_delete_session_nonexistent(fixturesessiontoken_user): | ||
client.cookies.set("session", fixturesessiontoken_user[0]) | ||
response = client.delete("/sessions/test") | ||
assert response.status_code == 404 |
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