Skip to content

Commit

Permalink
Atualiza testes
Browse files Browse the repository at this point in the history
  • Loading branch information
nYCSTs committed Dec 4, 2023
1 parent 54acfe3 commit 8af8e37
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
57 changes: 32 additions & 25 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
invalid_connection = {"name": "Mike", "email": "[email protected]", "connection": "INVALID", "password": "123456"}
invalid_pass_length = {"name": "Victor", "email": "[email protected]", "connection": "SERVIDOR", "password": "123"}
invalid_pass = {"name": "Luisa", "email": "[email protected]", "connection": "SERVIDOR", "password": "123abc"}
valid_social_user = { "name": "Paulo Kogos", "email": "[email protected]" }

total_registed_users = 5

client = TestClient(app)

Expand Down Expand Up @@ -89,6 +92,14 @@ def setup(self, session_mocker):
TestAuth.__user_access_token__ = data['access_token']
TestAuth.__user_refresh_token__ = data['access_token']

# login social - criação conta (nova)
response = client.post('/api/auth/login/social', json=valid_social_user)
data = response.json()
assert response.status_code == 200
assert data["access_token"] != None
assert data["token_type"] == "bearer"
assert data["is_new_user"] == True

# Atualiza role do active_user_admin de USER para ADMIN
with engine.connect() as connection:
query = "UPDATE users SET role = 'ADMIN' WHERE id = 1;"
Expand Down Expand Up @@ -144,6 +155,15 @@ def test_auth_login_not_active(self, setup):
assert response.status_code == 401
assert data['detail'] == errorMessages.ACCOUNT_IS_NOT_ACTIVE

def test_auth_login_social(self, setup):
response = client.post('/api/auth/login/social', json=valid_social_user)
data = response.json()
assert response.status_code == 200
assert data["access_token"] != None
assert data["refresh_token"] != None
assert data["token_type"] == "bearer"
assert data["is_new_user"] == False

# RESEND CODE
def test_auth_resend_code_user_not_found(self, setup):
response = client.post("/api/auth/resend-code", json={"email": invalid_connection['email']})
Expand Down Expand Up @@ -304,38 +324,25 @@ def test_utils_validate_dotenv(self, setup):
assert str(exc_info.value) == "SOME ENVIRONMENT VALUES WERE NOT DEFINED (missing: SECRET)"

os.environ["SECRET"] = environment_secret_value

def test_auth_sso_google_callback_error(self, setup):
response = client.get("/auth/callback")
data = response.json()
assert response.status_code == 400
assert data['detail'] == "'code' parameter was not found in callback request"

def test_auth_sso_google_login_success_load(self, setup):
response = client.get("/callback")
data = response.json()
assert response.status_code == 400
assert data['detail'] == "'code' parameter was not found in callback request"


@pytest.mark.asyncio
async def test_auth_send_mail_send_verification_code_success(self, setup):
send_mail.fm.config.SUPPRESS_SEND = 1
with send_mail.fm.record_messages() as outbox:
response = await send_mail.send_verification_code(valid_user_active_admin['email'], 123456)
assert response.status_code == 200
assert len(outbox) == 1
assert outbox[0]['from'] == f'UNB TV <{os.environ["MAIL_FROM"]}>'
assert outbox[0]['To'] == valid_user_active_admin['email']
response = await send_mail.send_verification_code(valid_user_active_admin['email'], 123456)

assert response.status_code == 200
assert len(outbox) == 1
assert outbox[0]['from'] == f'UNB TV <{os.environ["MAIL_FROM"]}>'
assert outbox[0]['To'] == valid_user_active_admin['email']

@pytest.mark.asyncio
async def test_auth_send_reset_password_code_success(self, setup):
send_mail.fm.config.SUPPRESS_SEND = 1
with send_mail.fm.record_messages() as outbox:
response = await send_mail.send_reset_password_code(valid_user_active_admin['email'], 123456)
assert response.status_code == 200
assert len(outbox) == 1
assert outbox[0]['from'] == f'UNB TV <{os.environ["MAIL_FROM"]}>'
assert outbox[0]['To'] == valid_user_active_admin['email']
response = await send_mail.send_reset_password_code(valid_user_active_admin['email'], 123456)

assert response.status_code == 200
assert len(outbox) == 1
assert outbox[0]['from'] == f'UNB TV <{os.environ["MAIL_FROM"]}>'
assert outbox[0]['To'] == valid_user_active_admin['email']
18 changes: 10 additions & 8 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
invalid_pass = test_auth.invalid_pass
valid_user_to_be_deleted = test_auth.valid_user_to_be_deleted

total_registed_users = test_auth.total_registed_users

client = TestClient(app)

class TestUser:
Expand All @@ -27,22 +29,22 @@ def setup(self):
response = client.get("/api/users/", headers=headers)
data = response.json()
assert response.status_code == 200
assert len(data) == 4
assert response.headers['x-total-count'] == '4'
assert len(data) == total_registed_users
assert response.headers['x-total-count'] == str(total_registed_users)

# Get Users - Offset
response = client.get("/api/users/?offset=1", headers=headers)
data = response.json()
assert response.status_code == 200
assert len(data) == 3
assert response.headers['x-total-count'] == '4'
assert len(data) == 4
assert response.headers['x-total-count'] == str(total_registed_users)

# Get Users - Limit
response = client.get("/api/users/?limit=1", headers=headers)
data = response.json()
assert response.status_code == 200
assert len(data) == 1
assert response.headers['x-total-count'] == '4'
assert response.headers['x-total-count'] == str(total_registed_users)

# Get Users - Filtrar Name
response = client.get(f"/api/users/?name={valid_user_active_admin['name']}", headers=headers)
Expand Down Expand Up @@ -81,7 +83,7 @@ def setup(self):
# Read User
def test_user_read_user_not_found(self, setup):
headers={'Authorization': f'Bearer {test_auth.TestAuth.__admin_access_token__}'}
response = client.get("/api/users/5", headers=headers)
response = client.get("/api/users/10", headers=headers)
data = response.json()

assert response.status_code == 404
Expand Down Expand Up @@ -127,7 +129,7 @@ def test_user_partial_update_user_invalid_connection(self, setup):

def test_user_partial_update_user_not_found(self, setup):
headers={'Authorization': f'Bearer {test_auth.TestAuth.__admin_access_token__}'}
response = client.patch(f"/api/users/5", json=valid_user_active_admin, headers=headers)
response = client.patch(f"/api/users/10", json=valid_user_active_admin, headers=headers)
data = response.json()

assert response.status_code == 404
Expand Down Expand Up @@ -178,7 +180,7 @@ def test_user_update_role_success(self, setup):
# Delete User
def test_user_delete_user_not_found(self, setup):
headers={'Authorization': f'Bearer {test_auth.TestAuth.__admin_access_token__}'}
response = client.delete(f"/api/users/5", headers=headers)
response = client.delete(f"/api/users/10", headers=headers)
data = response.json()

assert response.status_code == 404
Expand Down

0 comments on commit 8af8e37

Please sign in to comment.