Skip to content

Commit f7b6e5e

Browse files
test(auth): cases for admin and non admin
1 parent 25eabec commit f7b6e5e

File tree

3 files changed

+119
-31
lines changed

3 files changed

+119
-31
lines changed

tests/conftest.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
from tests.config import TEST_USER_EMAIL, TEST_USER_PASSWORD
1111

1212

13+
@pytest.fixture(scope='session')
14+
def auth():
15+
return make_auth()
16+
17+
18+
@pytest.fixture(scope='session')
19+
def auth_admin():
20+
return make_auth(True)
21+
22+
1323
@pytest.fixture(scope='session')
1424
def db():
1525
# To make it easier to test, we keep the test restricted to firebase_tests
@@ -20,11 +30,6 @@ def db():
2030
make_db(service_account=True).child('firebase_tests').remove()
2131

2232

23-
@pytest.fixture(scope='session')
24-
def auth():
25-
return make_auth(True)
26-
27-
2833
@pytest.fixture(scope='session')
2934
def email():
3035
return TEST_USER_EMAIL

tests/test_auth.py

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66

77

88
import pytest
9+
import requests.exceptions
910

1011

1112
class TestAuth:
1213

1314
user = None
1415
anonymous_user = None
15-
custom_token = None
16-
custom_token_with_claims = None
17-
custom_user = None
18-
custom_user_with_claims = None
1916

20-
@pytest.mark.xfail
2117
def test_sign_in_with_non_existing_account_email_and_password(self, auth, email, password):
22-
assert auth.sign_in_with_email_and_password(email, password)
18+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
19+
auth.sign_in_with_email_and_password(email, password)
20+
assert "EMAIL_NOT_FOUND" in str(exc_info.value)
2321

2422
def test_create_user_with_email_and_password(self, auth, email, password):
2523
assert auth.create_user_with_email_and_password(email, password)
2624

27-
@pytest.mark.xfail
2825
def test_create_user_with_existing_email_and_password(self, auth, email, password):
29-
assert auth.create_user_with_email_and_password(email, password)
26+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
27+
auth.create_user_with_email_and_password(email, password)
28+
assert "EMAIL_EXISTS" in str(exc_info.value)
3029

31-
@pytest.mark.xfail
3230
def test_sign_in_with_email_and_wrong_password(self, auth, email):
33-
assert auth.sign_in_with_email_and_password(email, 'WrongPassword123')
31+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
32+
auth.sign_in_with_email_and_password(email, 'WrongPassword123')
33+
assert "INVALID_PASSWORD" in str(exc_info.value)
3434

3535
def test_sign_in_with_email_and_password(self, auth, email, password):
3636
user = auth.sign_in_with_email_and_password(email, password)
@@ -43,24 +43,16 @@ def test_sign_in_anonymous(self, auth):
4343
assert user
4444

4545
def test_create_custom_token(self, auth):
46-
token = auth.create_custom_token('CreateCustomToken1')
47-
self.__class__.custom_token = token
48-
assert token
46+
with pytest.raises(AttributeError):
47+
auth.create_custom_token('CreateCustomToken1')
4948

5049
def test_create_custom_token_with_claims(self, auth):
51-
token = auth.create_custom_token('CreateCustomToken2', {'premium': True})
52-
self.__class__.custom_token_with_claims = token
53-
assert token
50+
with pytest.raises(AttributeError):
51+
auth.create_custom_token('CreateCustomToken2', {'premium': True})
5452

5553
def test_sign_in_with_custom_token(self, auth):
56-
user1 = auth.sign_in_with_custom_token(self.__class__.custom_token)
57-
user2 = auth.sign_in_with_custom_token(self.__class__.custom_token_with_claims)
58-
59-
self.__class__.custom_user = user1
60-
self.__class__.custom_user_with_claims = user2
61-
62-
assert user1
63-
assert user2
54+
with pytest.raises(requests.exceptions.HTTPError):
55+
auth.sign_in_with_custom_token(None)
6456

6557
def test_refresh(self, auth):
6658
assert auth.refresh(self.__class__.user.get('refreshToken'))
@@ -87,5 +79,89 @@ def test_update_profile_display_name(self, auth):
8779
def test_delete_user_account(self, auth):
8880
assert auth.delete_user_account(self.__class__.user.get('idToken'))
8981
assert auth.delete_user_account(self.__class__.anonymous_user.get('idToken'))
90-
assert auth.delete_user_account(self.__class__.custom_user.get('idToken'))
91-
assert auth.delete_user_account(self.__class__.custom_user_with_claims.get('idToken'))
82+
83+
84+
class TestAuthAdmin:
85+
86+
user = None
87+
anonymous_user = None
88+
custom_token = None
89+
custom_token_with_claims = None
90+
custom_user = None
91+
custom_user_with_claims = None
92+
93+
def test_sign_in_with_non_existing_account_email_and_password(self, auth_admin, email, password):
94+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
95+
auth_admin.sign_in_with_email_and_password(email, password)
96+
assert "EMAIL_NOT_FOUND" in str(exc_info.value)
97+
98+
def test_create_user_with_email_and_password(self, auth_admin, email, password):
99+
assert auth_admin.create_user_with_email_and_password(email, password)
100+
101+
def test_create_user_with_existing_email_and_password(self, auth_admin, email, password):
102+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
103+
auth_admin.create_user_with_email_and_password(email, password)
104+
assert "EMAIL_EXISTS" in str(exc_info.value)
105+
106+
def test_sign_in_with_email_and_wrong_password(self, auth_admin, email):
107+
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
108+
auth_admin.sign_in_with_email_and_password(email, 'WrongPassword123')
109+
assert "INVALID_PASSWORD" in str(exc_info.value)
110+
111+
def test_sign_in_with_email_and_password(self, auth_admin, email, password):
112+
user = auth_admin.sign_in_with_email_and_password(email, password)
113+
self.__class__.user = user
114+
assert user
115+
116+
def test_sign_in_anonymous(self, auth_admin):
117+
user = auth_admin.sign_in_anonymous()
118+
self.__class__.anonymous_user = user
119+
assert user
120+
121+
def test_create_custom_token(self, auth_admin):
122+
token = auth_admin.create_custom_token('CreateCustomToken1')
123+
self.__class__.custom_token = token
124+
assert token
125+
126+
def test_create_custom_token_with_claims(self, auth_admin):
127+
token = auth_admin.create_custom_token('CreateCustomToken2', {'premium': True})
128+
self.__class__.custom_token_with_claims = token
129+
assert token
130+
131+
def test_sign_in_with_custom_token(self, auth_admin):
132+
user1 = auth_admin.sign_in_with_custom_token(self.__class__.custom_token)
133+
user2 = auth_admin.sign_in_with_custom_token(self.__class__.custom_token_with_claims)
134+
135+
self.__class__.custom_user = user1
136+
self.__class__.custom_user_with_claims = user2
137+
138+
assert user1
139+
assert user2
140+
141+
def test_refresh(self, auth_admin):
142+
assert auth_admin.refresh(self.__class__.user.get('refreshToken'))
143+
144+
def test_get_account_info(self, auth_admin):
145+
assert auth_admin.get_account_info(self.__class__.user.get('idToken'))
146+
147+
def test_send_email_verification(self, auth_admin):
148+
assert auth_admin.send_email_verification(self.__class__.user.get('idToken'))
149+
150+
def test_send_password_reset_email(self, auth_admin):
151+
assert auth_admin.send_password_reset_email(self.__class__.user.get('email'))
152+
153+
@pytest.mark.xfail
154+
def test_verify_password_reset_code(self, auth_admin):
155+
assert auth_admin.verify_password_reset_code('123456', 'NewTestPassword123')
156+
157+
def test_update_profile_display_name(self, auth_admin):
158+
new_name = 'Test User'
159+
user = auth_admin.update_profile(self.__class__.user.get('idToken'), display_name=new_name)
160+
assert user
161+
assert new_name == user['displayName']
162+
163+
def test_delete_user_account(self, auth_admin):
164+
assert auth_admin.delete_user_account(self.__class__.user.get('idToken'))
165+
assert auth_admin.delete_user_account(self.__class__.anonymous_user.get('idToken'))
166+
assert auth_admin.delete_user_account(self.__class__.custom_user.get('idToken'))
167+
assert auth_admin.delete_user_account(self.__class__.custom_user_with_claims.get('idToken'))

tests/test_setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ def test_initiate_app_with_service_account_file():
1616

1717

1818
def test_setup_auth():
19+
auth = make_auth()
20+
user = auth.sign_in_anonymous()
21+
22+
assert auth.delete_user_account(user['idToken'])
23+
24+
25+
def test_setup_auth_admin():
1926
auth = make_auth(True)
2027
user = auth.sign_in_anonymous()
2128

0 commit comments

Comments
 (0)