Spaces:
Build error
Build error
File size: 7,096 Bytes
b7a7f32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
import json
import time
import base64
import pytest
import requests
from core.config import settings
from fastapi.testclient import TestClient
import email
from cruds import crud_user
import re
import os
MAILHOG_URL = f"http://{settings.SMTP_HOST}:8025/api"
headers = {
"accept": "application/json",
"Content-Type": "application/json",
}
def test_signup(client: TestClient) -> None:
mailhog_history_clear = requests.delete(f"{MAILHOG_URL}/v1/messages")
assert mailhog_history_clear.status_code == 200, "Mailhog email delete failed"
data = {
"email": "[email protected]",
"full_name": "Test User",
"address": "Testland",
"group_id": 1,
"contact_number": "9841111111",
"dob": "2000-01-01",
"join_year": 2015,
"password": "testold",
}
response = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/signup/",
headers=headers,
json=data,
)
assert response.status_code == 200
def test_verification_email(client: TestClient) -> None:
mailhog_email_req = requests.get(f"{MAILHOG_URL}/v2/messages")
assert mailhog_email_req.status_code == 200, "Mailhog email request failed"
emails = mailhog_email_req.json()
assert emails.get("total") == 1, f'Received {emails.get("total")} emails! Expected 1!'
verification_email = emails.get("items")[0]
content = email.message_from_string(verification_email["Raw"]["Data"])
decoded_email_content = None
for part in content.walk():
payload = part.get_payload(decode=True)
if payload:
decoded_email_content = payload.decode()
link = re.findall(
settings.FRONTEND_URL_BASE+"/verify\?token=.{40}", decoded_email_content
)[0]
token = link[-40:]
assert token, "Token of length 40 not found in the email"
params = {"token": token}
verify_req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/verify/",
params=params,
)
assert verify_req.status_code == 200
def test_web_session_authentication(
client: TestClient, username=None, password=None, remember_me=None
) -> None:
data = {
"username": username or "[email protected]",
"password": password or "testold",
"remember_me": remember_me or True,
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
json=data,
headers=headers,
)
assert req.status_code == 200
assert req.cookies.get("session"), "Cookie not returned!"
def test_web_session_authentication_fail(client: TestClient) -> None:
data = {
"username": "[email protected]",
"password": "test123",
"remember_me": True,
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
json=data,
headers=headers,
)
assert req.status_code == 401
assert not req.cookies.get("session"), "Cookie returned on login fail!"
def test_send_reset_email(client: TestClient) -> None:
data = {
"email": "[email protected]",
}
response = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/password-recovery/",
headers=headers,
params=data,
)
assert response.status_code == 200
def test_reset_password(client: TestClient) -> None:
mailhog_email_req = requests.get(f"{MAILHOG_URL}/v2/messages")
assert mailhog_email_req.status_code == 200, "Mailhog email request failed"
emails = mailhog_email_req.json()
reset_email = emails.get("items")[0]
content = email.message_from_string(reset_email["Raw"]["Data"])
decoded_email_content = None
for part in content.walk():
payload = part.get_payload(decode=True)
if payload:
decoded_email_content = payload.decode()
link = re.findall(
settings.FRONTEND_URL_BASE+"/reset\?token=.{40}", decoded_email_content
)[0]
token = link[-40:]
assert token, "Token of length 40 not found in the email"
data = {"token": token, "new_password": "test"}
reset_response = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/reset-password/",
headers=headers,
json=data,
)
assert reset_response.status_code == 200
def test_login_with_old_password(client: TestClient):
data = {
"username": "[email protected]",
"password": "testold",
"remember_me": True,
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
json=data,
headers=headers,
)
assert req.status_code != 200, "Logged in with old password after reset"
assert not req.cookies.get("session"), "Cookie returned with old password"
def test_login_with_new_password(client: TestClient):
data = {
"username": "[email protected]",
"password": "test",
"remember_me": True,
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
json=data,
headers=headers,
)
assert req.status_code == 200, "Couldn't login with new password"
assert req.cookies.get("session"), "Cookie not returned with new password"
def test_change_password(client: TestClient):
wrong_data = {
"current_password": "testwrong",
"new_password": "newtest",
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/change-password/",
json=wrong_data,
headers=headers,
)
assert req.status_code != 200
data = {
"current_password": "test",
"new_password": "newtest",
}
req = client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/change-password/",
json=data,
headers=headers,
)
assert req.status_code == 200
test_web_session_authentication(
client=client,
username="[email protected]",
password="newtest",
remember_me=True,
)
def test_logout(client: TestClient) -> None:
response = client.get(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/logout/",
headers=headers,
)
assert response.status_code == 200
def test_delete_user(db, super_user_client: TestClient):
data = {
"username": "[email protected]",
"password": "test",
"remember_me": False,
}
req = super_user_client.post(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
json=data,
headers=headers,
)
assert req.status_code == 200, "Superadmin login failed"
assert req.cookies.get("session"), "Superadmin cookie not returned!"
user = crud_user.get_by_email(db, email="[email protected]")
delete_req = super_user_client.delete(
f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/users/{user.id}/",
)
assert delete_req.status_code == 200
|