2021-05-07 17:46:14 +00:00
|
|
|
import pytest
|
|
|
|
import uuid
|
2021-05-08 22:47:56 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2021-05-07 17:46:14 +00:00
|
|
|
from nest_backend.app import app
|
|
|
|
from nest_backend.gestione import gen_password
|
|
|
|
from nest_backend.database import ext
|
|
|
|
from nest_backend.database.tables import User
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="package", autouse=True)
|
|
|
|
def flask_client():
|
|
|
|
# Enter testing mode
|
|
|
|
app.config["TESTING"] = True
|
|
|
|
|
|
|
|
# Get environment variables
|
|
|
|
app.config.from_envvar("FLASK_CONFIG")
|
|
|
|
|
|
|
|
# Initialize database
|
|
|
|
with app.app_context():
|
|
|
|
# Use unique db schemas for each test session
|
|
|
|
uniq_schema = f"test_{uuid.uuid4()}"
|
|
|
|
ext.engine.execute(f"""CREATE SCHEMA "{uniq_schema}";""")
|
|
|
|
for table in ext.Model.metadata.tables.values():
|
|
|
|
table.schema = uniq_schema
|
|
|
|
|
|
|
|
ext.create_all(app=app)
|
|
|
|
if not User.query.filter_by(isAdmin=True).all():
|
|
|
|
ext.session.add(
|
|
|
|
User(email="admin@admin.com", password=gen_password("password"), username="admin", isAdmin=True))
|
|
|
|
ext.session.commit()
|
|
|
|
|
|
|
|
# Prepare test client
|
|
|
|
with app.test_client(use_cookies=False) as client:
|
|
|
|
yield client
|
|
|
|
|
|
|
|
# Teardown schema
|
|
|
|
with app.app_context():
|
|
|
|
ext.engine.execute(f"""DROP SCHEMA "{uniq_schema}" CASCADE;""")
|
2021-05-07 19:22:45 +00:00
|
|
|
|
|
|
|
|
2021-05-10 22:16:02 +00:00
|
|
|
@pytest.fixture(scope="package")
|
2021-05-08 15:02:53 +00:00
|
|
|
def admin_access_token(flask_client):
|
2021-05-07 19:22:45 +00:00
|
|
|
response = flask_client.post("/api/v1/login", json={
|
2021-05-07 19:24:33 +00:00
|
|
|
"email": "admin@admin.com",
|
2021-05-07 19:22:45 +00:00
|
|
|
"password": "password"
|
|
|
|
})
|
|
|
|
assert response.json is not None
|
|
|
|
assert "result" in response.json
|
|
|
|
assert response.json["result"] == "success"
|
|
|
|
assert "data" in response.json
|
|
|
|
data = response.json["data"]
|
|
|
|
assert "access_token" in data
|
|
|
|
return data["access_token"]
|
2021-05-08 14:36:50 +00:00
|
|
|
|
|
|
|
|
2021-05-10 22:16:02 +00:00
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def user_exists(admin_headers, flask_client):
|
|
|
|
flask_client.post(f'/api/v1/users/', headers=admin_headers, json={
|
|
|
|
'email': 'utente_test@nest.com',
|
|
|
|
'password': 'password',
|
|
|
|
'username': 'utente_test'
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-05-11 21:53:36 +00:00
|
|
|
@pytest.fixture(scope="package")
|
2021-05-12 18:20:51 +00:00
|
|
|
def repository_exists(user_headers, flask_client):
|
|
|
|
r = flask_client.post(f'/api/v1/repositories/', headers=user_headers, json={
|
|
|
|
'conditions': [
|
|
|
|
{
|
|
|
|
'content': 'PdS2021',
|
|
|
|
'id': 0,
|
|
|
|
'type': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'evaluation_mode': 0,
|
|
|
|
'name': 'repo_1'
|
2021-05-11 21:53:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-05-10 22:16:02 +00:00
|
|
|
@pytest.fixture(scope="package")
|
|
|
|
def user_access_token(flask_client, user_exists):
|
2021-05-08 15:02:53 +00:00
|
|
|
response = flask_client.post("/api/v1/login", json={
|
|
|
|
"email": "utente_test@nest.com",
|
|
|
|
"password": "password"
|
|
|
|
})
|
|
|
|
assert response.json is not None
|
|
|
|
assert "result" in response.json
|
|
|
|
assert response.json["result"] == "success"
|
|
|
|
assert "data" in response.json
|
|
|
|
data = response.json["data"]
|
|
|
|
assert "access_token" in data
|
|
|
|
return data["access_token"]
|
|
|
|
|
|
|
|
|
2021-05-10 22:16:02 +00:00
|
|
|
@pytest.fixture(scope="package")
|
2021-05-08 15:02:53 +00:00
|
|
|
def admin_headers(admin_access_token):
|
|
|
|
admin_headers = {'Authorization': f"Bearer {admin_access_token}"}
|
|
|
|
return admin_headers
|
|
|
|
|
|
|
|
|
2021-05-10 22:16:02 +00:00
|
|
|
@pytest.fixture(scope="package")
|
2021-05-08 15:02:53 +00:00
|
|
|
def user_headers(user_access_token):
|
|
|
|
user_headers = {'Authorization': f"Bearer {user_access_token}"}
|
|
|
|
return user_headers
|