Verified Commit 93b14274 authored by Peter Stanko's avatar Peter Stanko
Browse files

Simplified tests for with assert response and unnecessary headers

parent bf102b33
Pipeline #14376 failed with stage
in 37 seconds
......@@ -4,7 +4,9 @@ import pytest
from management.data import DataManagement
from portal import create_app, db
from portal.database import ProjectConfig
from portal.logger import load_config
from tests.utils.ent_mocker import EntitiesMocker
load_config('test')
......@@ -39,3 +41,13 @@ def rest_service():
def ent_mocker(app):
from tests.utils.ent_mocker import EntitiesMocker
return EntitiesMocker(app, db=db)
@pytest.fixture()
def mocked_submission(ent_mocker: EntitiesMocker, rest_service):
submission = ent_mocker.create_submission()
rest_service.submissions.write_entity(submission)
ent_mocker.create_submission_storage(submission=submission)
project_config: ProjectConfig = submission.project.config
project_config.test_files_commit_hash = 'some-random-hash'
return submission
......@@ -4,6 +4,7 @@ import pytest
from portal.database.models import Client, Secret
from tests.rest import rest_tools
from tests.rest.rest_tools import assert_response
@pytest.fixture
......@@ -27,9 +28,8 @@ def worker_credentials():
def test_client_detail_using_student_cred(client, student_credentials):
path = f'/client'
response = rest_tools.make_request(
client, path, 'get', credentials=student_credentials)
assert response.status_code == 200
assert response.mimetype == 'application/json'
client, path, credentials=student_credentials)
assert_response(response, 200)
data = response.json
assert data['username'] == 'student1'
assert data['type'] == 'ClientType.USER'
......@@ -38,9 +38,9 @@ def test_client_detail_using_student_cred(client, student_credentials):
def test_client_detail_using_executor_cred(client, worker_credentials):
path = f'/client'
response = rest_tools.make_request(
client, path, 'get', credentials=worker_credentials)
assert response.status_code == 200
assert response.mimetype == 'application/json'
client, path, credentials=worker_credentials)
assert_response(response, 200)
data = response.json
assert data['name'] == 'executor'
assert data['type'] == 'ClientType.WORKER'
......@@ -50,12 +50,10 @@ def test_create_secret(client):
instance = Client.query.filter_by(codename="executor").first()
request_dict = dict(name="new_secret")
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets',
headers={"content-type": "application/json"},
data=json.dumps(request_dict),
json=request_dict,
method='post')
assert response.status_code == 201
assert response.mimetype == 'application/json'
assert_response(response, 201)
parsed = response.json
assert parsed['value'] is not None
assert parsed['id'] is not None
......@@ -67,12 +65,9 @@ def test_create_secret(client):
def test_list_secret(client):
instance = Client.query.filter_by(codename="executor").first()
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets',
headers={"content-type": "application/json"},
method='get')
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets')
assert response.status_code == 200
assert response.mimetype == 'application/json'
assert_response(response, 200)
secrets = rest_tools.extract_data(response)
assert len(secrets) == 1
......@@ -81,7 +76,6 @@ def test_delete_secret(client):
worker = Client.query.filter_by(codename="executor").first()
secret = Secret.query.filter_by(client=worker).first()
response = rest_tools.make_request(client, f'/clients/{worker.id}/secrets/{secret.id}',
headers={"content-type": "application/json"},
method='delete')
assert response.status_code == 204
......@@ -92,12 +86,9 @@ def test_delete_secret(client):
def test_read_secret(client):
instance = Client.query.filter_by(codename="executor").first()
secret = Secret.query.filter_by(client=instance).first()
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets/{secret.id}',
headers={"content-type": "application/json"},
method='get')
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets/{secret.id}')
assert response.status_code == 200
assert response.mimetype == 'application/json'
assert_response(response, 200)
response_secret = rest_tools.extract_data(response)
assert response_secret['id'] == secret.id
assert response_secret['name'] == secret.name
......@@ -110,11 +101,10 @@ def test_update_secret(client):
request_dict = dict(name="new_name")
secret = Secret.query.filter_by(client=instance).first()
response = rest_tools.make_request(client, f'/clients/{instance.id}/secrets/{secret.id}',
headers={"content-type": "application/json"},
data=json.dumps(request_dict),
json=request_dict,
method='put')
assert response.status_code == 204
assert_response(response, 204)
updated_secret = Secret.query.filter_by(client=instance).first()
assert updated_secret.id == secret.id
assert updated_secret.name == "new_name"
......
import json
from portal.database.models import Course
from portal.service import courses, general
from portal.service.courses import CourseService
from tests.rest.rest_tools import assert_response
from . import rest_tools
def test_list(client):
response = rest_tools.make_request(client, '/courses', method='get')
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, '/courses')
assert_response(response)
courses_response = rest_tools.extract_data(response)
assert len(courses_response) == 2
......@@ -22,16 +20,11 @@ def test_create(client):
request_dict = dict(
name="java",
codename="PA165",
)
)
db_courses_number = len(Course.query.all())
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, '/courses', data=request_json,
headers={"content-type": "application/json"},
method='post')
assert response.status_code == 201
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, '/courses', json=request_dict, method='post')
assert_response(response, 201)
resp_new_course = rest_tools.extract_data(response)
db_courses = Course.query.all()
assert len(db_courses) == db_courses_number + 1
......@@ -43,15 +36,11 @@ def test_create_extra_key(client):
name="java",
codename="PA165",
extra="should not be here"
)
)
db_courses_number = len(Course.query.all())
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, '/courses', data=request_json,
headers={"content-type": "application/json"},
method='post')
assert response.status_code == 201
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, '/courses', json=request_dict, method='post')
assert_response(response, 201)
resp_new_course = rest_tools.extract_data(response)
db_courses = Course.query.all()
......@@ -63,12 +52,9 @@ def test_create_missing_name(client):
request_dict = dict(codename="PA165")
db_courses_number = len(Course.query.all())
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, '/courses', data=request_json,
headers={"content-type": "application/json"},
method='post')
assert response.status_code == 400
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, '/courses', json=request_dict, method='post')
assert_response(response, 400)
assert len(Course.query.all()) == db_courses_number
......@@ -76,24 +62,18 @@ def test_create_invalid_value(client):
request_dict = dict(
name=456,
codename="PA165",
)
)
db_courses_number = len(Course.query.all())
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, '/courses', data=request_json,
headers={"content-type": "application/json"},
method='post')
assert response.status_code == 400
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, '/courses', json=request_dict, method='post')
assert_response(response, 400)
assert len(Course.query.all()) == db_courses_number
def test_read(client):
c = Course.query.filter_by(codename="testcourse1").first()
response = rest_tools.make_request(client, f"/courses/{c.id}", method='get')
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, f"/courses/{c.id}")
assert_response(response, 200)
course = rest_tools.extract_data(response)
rest_tools.assert_course(c, course)
......@@ -104,13 +84,11 @@ def test_update(client):
request_dict = dict(
name="new_cpp",
codename="testcourse1_new",
)
)
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f'/courses/{c.id}', data=request_json,
headers={"content-type": "application/json"}, method='put')
response = rest_tools.make_request(client, f'/courses/{c.id}', data=request_json, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
assert_response(response, 204)
assert not Course.query.filter_by(codename="testcourse1").first()
course_from_db = Course.query.filter_by(codename="testcourse1-new").first()
......@@ -119,22 +97,16 @@ def test_update(client):
def test_delete(client):
c = Course.query.filter_by(codename="testcourse2").first()
response = rest_tools.make_request(client, f'/courses/{c.id}',
headers={"content-type": "application/json"},
method='delete')
assert response.status_code == 204
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, f'/courses/{c.id}', method='delete')
assert_response(response, 204)
assert not Course.query.filter_by(codename="testcourse2").first()
def test_notes_token_read(client):
c = Course.query.filter_by(codename="testcourse2").first()
response = rest_tools.make_request(
client, f"/courses/{c.id}/notes_access_token", method='get')
assert response.status_code == 200
assert response.mimetype == 'application/json'
client, f"/courses/{c.id}/notes_access_token")
assert_response(response, 200)
data = rest_tools.extract_data(response)
assert data == "testcourse2_token"
......@@ -143,10 +115,8 @@ def test_notes_token_read(client):
def test_notes_token_create(client):
c = Course.query.filter_by(codename="testcourse2").first()
response = rest_tools.make_request(client, f"/courses/{c.id}/notes_access_token",
data=json.dumps({"token": "new_token"}),
headers={"content-type": "application/json"}, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
json={"token": "new_token"}, method='put')
assert_response(response, 204)
updated = Course.query.filter_by(codename="testcourse2").first()
assert updated.notes_access_token == "new_token"
......@@ -155,10 +125,8 @@ def test_notes_token_create(client):
def test_notes_token_update(client):
c = Course.query.filter_by(codename="testcourse1").first()
response = rest_tools.make_request(client, f"/courses/{c.id}/notes_access_token",
data=json.dumps({"token": "new_token"}),
headers={"content-type": "application/json"}, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
json={"token": "new_token"}, method='put')
assert_response(response, 204)
updated = Course.query.filter_by(codename="testcourse1").first()
assert updated.notes_access_token == "new_token"
......@@ -173,26 +141,14 @@ def test_copy_from_course(rest_service, client):
"roles": "with_clients",
"groups": "with_users",
"projects": "bare",
}
}
request_json = json.dumps(request_dict)
}
response = rest_tools.make_request(client, f'/courses/{target.codename}/import',
data=request_json,
headers={"content-type": "application/json"},
method='put')
assert response.status_code == 200
assert response.mimetype == "application/json"
json=request_dict, method='put')
assert_response(response, 200)
updated_course: Course = rest_service.find.course(target.codename)
print(
f"CPP projects:{source.projects}\n Target projects:{updated_course.projects}\n\n")
print(
f"CPP roles:{source.roles}\n Target roles:{updated_course.roles}\n\n")
print(
f"CPP groups:{source.groups}\n Target groups:{updated_course.groups}\n\n")
assert_collections(source, updated_course, 'projects')
assert_collections(source, updated_course, 'roles')
assert_collections(source, updated_course, 'groups')
......
import json
from portal.database.models import Course, Group, Role, User
from tests.rest.rest_tools import assert_response
from . import rest_tools
def test_list(client):
cpp = Course.query.filter_by(codename="testcourse1").first()
response = rest_tools.make_request(
client, f'/courses/{cpp.codename}/groups', method='get')
assert response.status_code == 200
assert response.mimetype == 'application/json'
client, f'/courses/{cpp.codename}/groups')
assert_response(response, 200)
groups = rest_tools.extract_data(response)
assert len(groups) == len(cpp.groups)
......@@ -25,11 +25,10 @@ def test_create(client):
name="newgrp",
codename='newgrp'
)
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups", data=request_json,
headers={"content-type": "application/json"}, method='post')
assert response.status_code == 201
assert response.mimetype == "application/json"
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups", json=request_dict,
method='post')
assert_response(response, 201)
resp_new_group = rest_tools.extract_data(response)
cpp_updated = Course.query.filter_by(codename="testcourse1").first()
......@@ -43,7 +42,7 @@ def test_read(client):
cpp = Course.query.filter_by(codename="testcourse1").first()
g = cpp.groups[0]
response = rest_tools.make_request(
client, f"/courses/{cpp.codename}/groups/{g.name}", method='get')
client, f"/courses/{cpp.codename}/groups/{g.name}")
assert response.status_code == 200
assert response.mimetype == 'application/json'
......@@ -59,8 +58,7 @@ def test_update(client):
)
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups/{g.name}",
data=request_json,
headers={"content-type": "application/json"}, method='put')
data=request_json, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
......@@ -87,8 +85,7 @@ def test_delete(client):
def test_list_users(client):
cpp = Course.query.filter_by(codename="testcourse1").first()
g = cpp.groups[0]
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups/{g.id}/users",
method='get')
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups/{g.id}/users")
assert response.status_code == 200
assert response.mimetype == 'application/json'
......@@ -105,8 +102,7 @@ def test_list_users_filter(client):
name="student").first()
students = [user for user in g.users if role in user.roles]
response = rest_tools.make_request(client,
f"/courses/{cpp.codename}/groups/{g.id}/users?role={role.name}",
method='get')
f"/courses/{cpp.codename}/groups/{g.id}/users?role={role.name}")
assert response.status_code == 200
assert response.mimetype == 'application/json'
......@@ -130,8 +126,7 @@ def test_update_users_add(client):
)
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{tc2.codename}/groups/{g.name}/users",
data=request_json,
headers={"content-type": "application/json"}, method='put')
data=request_json, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
......@@ -154,8 +149,7 @@ def test_update_users_add_duplicate(client):
)
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups/{g.name}/users",
data=request_json,
headers={"content-type": "application/json"}, method='put')
data=request_json, method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
......@@ -179,7 +173,7 @@ def test_update_users_remove(client):
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/groups/{g.name}/users",
data=request_json,
headers={"content-type": "application/json"}, method='put')
method='put')
assert response.status_code == 204
assert response.mimetype == 'application/json'
......@@ -299,8 +293,7 @@ def test_import(client):
}
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{c.codename}/groups/import",
data=request_json,
headers={"content-type": "application/json"}, method='put')
data=request_json, method='put')
assert response.status_code == 201
assert response.mimetype == "application/json"
......
# tests for basic login
import json
import pytest
from tests.rest import rest_tools
from .rest_tools import assert_response
@pytest.fixture
......@@ -12,9 +13,8 @@ def tokens(client):
"identifier": "admin",
"secret": "789789"
}
request_json = json.dumps(request_dict)
response = client.post("/api/v1.0/auth/login", data=request_json,
headers={"content-type": "application/json"})
response = client.post("/api/v1.0/auth/login", json=request_dict)
assert_response(response, 200)
data = rest_tools.extract_data(response)
return data
......@@ -25,11 +25,8 @@ def test_login_user_valid(client):
"identifier": "admin",
"secret": "789789"
}
request_json = json.dumps(request_dict)
response = client.post("/api/v1.0/auth/login", data=request_json,
headers={"content-type": "application/json"})
assert response.status_code == 200
assert response.mimetype == "application/json"
response = client.post("/api/v1.0/auth/login", json=request_dict)
assert_response(response, 200)
tokens = rest_tools.extract_data(response)
assert tokens.get('access_token')
......@@ -42,11 +39,8 @@ def test_login_component_valid(client):
"identifier": "executor",
"secret": "executor_secret"
}
request_json = json.dumps(request_dict)
response = client.post(
"/api/v1.0/auth/login", headers={"content-type": "application/json"}, data=request_json)
assert response.status_code == 200
assert response.mimetype == "application/json"
response = client.post("/api/v1.0/auth/login", json=request_dict)
assert_response(response, 200)
def test_refresh_token(client, tokens: dict):
......@@ -55,8 +49,7 @@ def test_refresh_token(client, tokens: dict):
"Authorization": "Bearer " + tokens['refresh_token']
}
response = client.post("/api/v1.0/auth/refresh", headers=headers)
assert response.status_code == 200
assert response.mimetype == "application/json"
assert_response(response, 200)
assert tokens.get('access_token')
......@@ -66,5 +59,4 @@ def test_user_logout(client, tokens):
"Authorization": "Bearer " + tokens['access_token']
}
response = client.post("/api/v1.0/auth/logout", headers=headers)
assert response.status_code == 200
assert response.mimetype == "application/json"
assert_response(response)
from tests.rest import rest_tools
from tests.rest.rest_tools import assert_response
def test_status_endpoint(client):
headers = {"Content-Type": "application/json"}
response = client.get("/api/v1.0/management/status", headers=headers)
assert response.status_code == 200
assert response.mimetype == "application/json"
assert_response(response, 200)
status = rest_tools.extract_data(response=response)
assert status['status'] == 'works'
import json
import pytest
from tests.rest import rest_tools
from tests.rest.rest_tools import get_user_credentials, get_user_secret
from tests.rest.rest_tools import assert_response, get_user_credentials, get_user_secret
@pytest.fixture
......@@ -23,30 +21,23 @@ def teacher_secret() -> str:
def test_project_detail_using_admin_cred(client):
path = f'/courses/testcourse1'
response = rest_tools.make_request(client, path, 'get')
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, path)
assert_response(response, 200)
def test_project_detail_using_student_cred(client, student_credentials):
path = f'/courses/testcourse1'
response = rest_tools.make_request(
client, path, 'get', credentials=student_credentials)
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, path, credentials=student_credentials)
assert_response(response, 200)
def test_project_detail_using_teacher_cred(client, teacher_credentials):
path = f'/courses/testcourse1/projects'
response = rest_tools.make_request(
client, path, 'get', credentials=teacher_credentials)
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, path, credentials=teacher_credentials)
assert_response(response, 200)
def test_project_detail_using_teacher_secret(client, teacher_secret):
path = f'/courses/testcourse1'
response = rest_tools.make_request(
client, path, 'get', credentials=teacher_secret)
assert response.status_code == 200
assert response.mimetype == 'application/json'
response = rest_tools.make_request(client, path, credentials=teacher_secret)
assert_response(response, 200)
......@@ -14,8 +14,7 @@ def test_list(client):
cpp = Course.query.filter_by(codename="testcourse2").first()
cpp_projects = len(cpp.projects)
response = rest_tools.make_request(
client, f'/courses/{cpp.codename}/projects', 'get')
response = rest_tools.make_request(client, f'/courses/{cpp.codename}/projects')
assert_response(response)
projects = rest_tools.extract_data(response)
......@@ -36,8 +35,7 @@ def test_create(rest_service, client):
}
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/projects", method='post',
data=request_json,
headers={"content-type": "application/json"})
data=request_json)
assert_response(response, 201)
new_project = rest_tools.extract_data(response)
......@@ -66,8 +64,7 @@ def test_update(rest_service, client):
request_dict = dict(name="new project name")
request_json = json.dumps(request_dict)
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/projects/{p.name}",
data=request_json, method='put',
headers={"content-type": "application/json"})
data=request_json, method='put')
assert_response(response, 204)
assert Project.query.filter(Project.course_id == cpp.id).filter(
......@@ -118,7 +115,7 @@ def test_config_update(client):
response = rest_tools.make_request(client, f"/courses/{cpp.codename}/projects/{p.name}/config",
data=request_json,
headers={"content-type": "application/json"}, method='put')
method='put')
assert_response(response, 204)
p_updated = Project.query.filter(
......