11import datetime
2- from unittest .mock import Mock
2+ from unittest .mock import patch
33
44import pytest
55from fastapi .testclient import TestClient
66from sqlalchemy import create_engine
77from sqlalchemy .orm import sessionmaker
88from starlette import status
99
10- import auth_backend .auth_plugins .email
1110from auth_backend .models import AuthMethod , User
1211from auth_backend .models .db import Group , UserSession , UserGroup
1312from auth_backend .routes .base import app
1413from auth_backend .settings import get_settings
15- import auth_backend .utils .security
1614
1715
18- @pytest .fixture ( scope = "session" )
16+ @pytest .fixture
1917def client ():
20- auth_backend .auth_plugins .email .send_confirmation_email = Mock (return_value = None )
21- auth_backend .auth_plugins .email .send_change_password_confirmation = Mock (return_value = None )
22- auth_backend .auth_plugins .email .send_changes_password_notification = Mock (return_value = None )
23- auth_backend .auth_plugins .email .send_reset_email = Mock (return_value = None )
24- auth_backend .utils .security .UnionAuth .__call__ = Mock (return_value = {"id" : 0 , "email" : "" })
18+ patcher1 = patch ("auth_backend.auth_plugins.email.send_confirmation_email" )
19+ patcher2 = patch ("auth_backend.auth_plugins.email.send_change_password_confirmation" )
20+ patcher3 = patch ("auth_backend.auth_plugins.email.send_changes_password_notification" )
21+ patcher4 = patch ("auth_backend.auth_plugins.email.send_reset_email" )
22+ patcher5 = patch ("auth_backend.utils.security.UnionAuth.__call__" )
23+ patcher1 .start ()
24+ patcher2 .start ()
25+ patcher3 .start ()
26+ patcher4 .start ()
27+ patcher5 .start ()
28+ patcher1 .return_value = None
29+ patcher2 .return_value = None
30+ patcher3 .return_value = None
31+ patcher4 .return_value = None
32+ patcher5 .return_value = {"id" : 0 , "email" : None }
2533 client = TestClient (app )
2634 yield client
35+ patcher1 .stop ()
36+ patcher2 .stop ()
37+ patcher3 .stop ()
38+ patcher4 .stop ()
39+ patcher5 .stop ()
40+
41+
42+ @pytest .fixture
43+ def client_auth ():
44+ patcher1 = patch ("auth_backend.auth_plugins.email.send_confirmation_email" )
45+ patcher2 = patch ("auth_backend.auth_plugins.email.send_change_password_confirmation" )
46+ patcher3 = patch ("auth_backend.auth_plugins.email.send_changes_password_notification" )
47+ patcher4 = patch ("auth_backend.auth_plugins.email.send_reset_email" )
48+ patcher1 .start ()
49+ patcher2 .start ()
50+ patcher3 .start ()
51+ patcher4 .start ()
52+ patcher1 .return_value = None
53+ patcher2 .return_value = None
54+ patcher3 .return_value = None
55+ patcher4 .return_value = None
56+ client = TestClient (app )
57+ yield client
58+ patcher1 .stop ()
59+ patcher2 .stop ()
60+ patcher3 .stop ()
61+ patcher4 .stop ()
2762
2863
2964@pytest .fixture (scope = 'session' )
@@ -35,10 +70,10 @@ def dbsession():
3570
3671
3772@pytest .fixture ()
38- def user_id (client : TestClient , dbsession ):
73+ def user_id (client_auth : TestClient , dbsession ):
3974 time = datetime .datetime .utcnow ()
4075 body = {"email" : f"user{ time } @example.com" , "password" : "string" }
41- client .post ("/email/registration" , json = body )
76+ client_auth .post ("/email/registration" , json = body )
4277 db_user : AuthMethod = (
4378 dbsession .query (AuthMethod ).filter (AuthMethod .value == body ['email' ], AuthMethod .param == 'email' ).one ()
4479 )
@@ -54,15 +89,15 @@ def user_id(client: TestClient, dbsession):
5489
5590
5691@pytest .fixture ()
57- def user (client : TestClient , dbsession ):
92+ def user (client_auth : TestClient , dbsession ):
5893 url = "/email/login"
5994 time = datetime .datetime .utcnow ()
6095 body = {"email" : f"user{ time } @example.com" , "password" : "string" }
61- client .post ("/email/registration" , json = body )
96+ client_auth .post ("/email/registration" , json = body )
6297 db_user : AuthMethod = (
6398 dbsession .query (AuthMethod ).filter (AuthMethod .value == body ['email' ], AuthMethod .param == 'email' ).one ()
6499 )
65- response = client .post (url , json = body )
100+ response = client_auth .post (url , json = body )
66101 assert response .status_code == status .HTTP_401_UNAUTHORIZED
67102 token = (
68103 dbsession .query (AuthMethod )
@@ -73,9 +108,9 @@ def user(client: TestClient, dbsession):
73108 )
74109 .one ()
75110 )
76- response = client .get (f"/email/approve?token={ token .value } " )
111+ response = client_auth .get (f"/email/approve?token={ token .value } " )
77112 assert response .status_code == status .HTTP_200_OK
78- response = client .post (url , json = body )
113+ response = client_auth .post (url , json = body )
79114 assert response .status_code == status .HTTP_200_OK
80115 yield {"user_id" : db_user .user_id , "body" : body , "login_json" : response .json ()}
81116 session = dbsession .query (UserSession ).filter (UserSession .user_id == db_user .user_id ).all ()
@@ -88,7 +123,7 @@ def user(client: TestClient, dbsession):
88123 dbsession .commit ()
89124
90125
91- @pytest .fixture ( scope = "module" )
126+ @pytest .fixture
92127def parent_id (client , dbsession ):
93128 time = datetime .datetime .utcnow ()
94129 body = {"name" : f"group{ time } " , "parent_id" : None }
0 commit comments