Ejemplo n.º 1
0
def test_create_grades():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "grades")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.post(
                "/grades/",
                json={
                    "subject_key": mocks.subjects.sciences_de_l_ingénieur._key,
                    "title": "Cinématique",
                    "unit": 95,
                    "details": "Aliquip sit aute ea pariatur.",
                },
                **params,
            )

            assert response.status_code == 201
            for key, value in {
                    "subject_key": mocks.subjects.sciences_de_l_ingénieur._key,
                    "title": "Cinématique",
                    "unit": 95,
                    "details": "Aliquip sit aute ea pariatur.",
            }.items():
                assert response.json()[key] == value

            assert "_key" in response.json().keys()

            assert db.collection("grades").all().count() == 5
            assert db.collection("grades").get(
                response.json()["_key"]) is not None
Ejemplo n.º 2
0
def test_create_subject():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.post(
                "/subjects/",
                json={
                    "color": "red",
                    "name": "Sciences de l'ingénieur",
                    "weight": 1.0,
                    "goal": 3.67e-1,
                },
                **params,
            )

            assert response.status_code == 201
            for key, value in {
                    "color": "red",
                    "name": "Sciences de l'ingénieur",
                    "slug": "sciences-de-l-ingenieur",
                    "weight": 1.0,
                    "goal": 3.67e-1,
                    "location": "",
            }.items():
                assert response.json()[key] == value

            assert "_key" in response.json().keys()

            assert db.collection("subjects").all().count() == 1
            assert db.collection("subjects").get(
                response.json()["_key"]) is not None
Ejemplo n.º 3
0
def test_reset_setting_unknown_setting_key():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "settings")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            result = client.delete("/settings/lorem_ipsum", **params)
            assert result.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
Ejemplo n.º 4
0
def test_get_settings():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "settings")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            result = client.get("/settings", **params).json()
            assert result == json.loads(mocks.settings.alice.json(by_alias=True))
Ejemplo n.º 5
0
def test_read_subject_not_found():
    with database_mock() as db:
        db: StandardDatabase

        insert_mocks(db, "users")
        db.collection("subjects").insert(mocks.subjects.mathematiques.json())

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/subjects/{mocks.subjects.français.object_key}/", **params)
            assert response.status_code == 404
Ejemplo n.º 6
0
def test_reset_all_settings():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "settings")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            result = client.delete("/settings", **params)
            assert result.status_code == 200
            for key, value in json.loads(
                get_default_settings().json(by_alias=True)
            ).items():
                assert result.json()[key] == value
Ejemplo n.º 7
0
def test_get():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        default_settings = InSettings()
        # for now users have no tied settings
        result = schoolsyst_api.settings.get(db, mocks.users.john)
        for prop in default_settings.dict(by_alias=True).keys():
            assert getattr(result, prop) == getattr(default_settings, prop)

        # test that the user now has a settings object tied to him in the db
        assert db.collection("settings").get(mocks.users.john.key) is not None
Ejemplo n.º 8
0
def test_delete_subject():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        db.collection("subjects").insert(mocks.subjects.mathematiques.json())
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.delete(
                f"/subjects/{mocks.subjects.mathematiques.object_key}",
                **params)

            assert response.status_code == status.HTTP_204_NO_CONTENT
            assert not response.text
            assert db.collection("subjects").all().count() == 0
Ejemplo n.º 9
0
def test_reset_setting():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "settings")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            result = client.delete("/settings/offdays", **params)
            expected = {
                **json.loads(mocks.settings.alice.json(by_alias=True)),
                "offdays": json.loads(get_default_settings().json())["offdays"],
            }
            assert result.status_code == 200
            assert result.json() == expected
Ejemplo n.º 10
0
def test_read_grades_not_owned_by_current_user():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        db.collection("grades").insert(mocks.grades.john_nosubject.json())

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/grades/{mocks.grades.john_nosubject.object_key}/",
                **params,
            )

            assert response.status_code == 404
Ejemplo n.º 11
0
def test_read_subject_ok():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "subjects")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/subjects/{mocks.subjects.mathematiques.object_key}/",
                **params)

            assert response.status_code == 200
            assert response.json() == json.loads(
                mocks.subjects.mathematiques.json())
Ejemplo n.º 12
0
def test_read_subject_not_owned_by_current_user():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        db.collection("subjects").insert(
            mocks.subjects.sciences_de_l_ingénieur.json())

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/subjects/{mocks.subjects.sciences_de_l_ingénieur.object_key}/",
                **params,
            )

            assert response.status_code == 404
Ejemplo n.º 13
0
def test_read_grades_not_found():
    with database_mock() as db:
        db: StandardDatabase

        insert_mocks(db, "users")
        db.collection("grades").insert(
            mocks.grades.alice_nietzsche.json(by_alias=True))

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/grades/{mocks.grades.alice_trigo.object_key}",
                **params,
            )
            assert response.status_code == 404
Ejemplo n.º 14
0
def test_read_grades_ok():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        db.collection("grades").insert(mocks.grades.alice_trigo.json())

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get(
                f"/grades/{mocks.grades.alice_trigo.object_key}/",
                **params,
            )

            assert response.status_code == 200
            assert response.json() == json.loads(
                mocks.grades.alice_trigo.json())
Ejemplo n.º 15
0
def test_list_grades():
    with database_mock() as db:
        db: StandardDatabase

        assert db.collection("grades").all().count() == 0
        insert_mocks(db, "users")
        insert_mocks(db, "grades")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get("/grades/", **params)

            assert response.status_code == 200
            assert len(response.json()) == 3
            assert mocks.grades.john_nosubject._key not in [
                v["_key"] for v in response.json()
            ]
Ejemplo n.º 16
0
def test_delete_grades():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "grades")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.delete(
                f"/grades/{mocks.grades.alice_trigo.object_key}",
                **params,
            )

            assert response.status_code == status.HTTP_204_NO_CONTENT
            assert not response.text
            assert db.collection("grades").all().count() == 3
            assert db.collection("grades").get(
                mocks.grades.alice_trigo._key) is None
Ejemplo n.º 17
0
def test_update_a_subject():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "subjects")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.patch(
                f"/subjects/{mocks.subjects.français.object_key}",
                json={"name": "François"},
                **params,
            )

            assert response.status_code == 200
            assert response.json()["color"] == str(
                mocks.subjects.français.color)
            assert response.json()["name"] == "François"
            assert response.json()["slug"] == "francois"
Ejemplo n.º 18
0
def test_list_subjects():
    with database_mock() as db:
        db: StandardDatabase

        assert db.collection("subjects").all().count() == 0
        insert_mocks(db, "users")
        insert_mocks(db, "subjects")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.get("/subjects/", **params)
            keys = [s["_key"] for s in response.json()]

            assert response.status_code == 200
            assert len(response.json()) == 2
            assert mocks.subjects.sciences_de_l_ingénieur._key not in keys
            assert all([
                s._key in keys for s in (mocks.subjects.français,
                                         mocks.subjects.mathematiques)
            ])
Ejemplo n.º 19
0
def test_update_grade():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "grades")

        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            response = client.patch(
                f"/grades/{mocks.grades.alice_unobtained.object_key}",
                json={"actual": 0.5},
                **params,
            )

            assert response.status_code == 200
            assert response.json()["actual"] == 0.5
            assert response.json(
            )["subject_key"] == mocks.subjects.français._key
            obtained_at = datetime.fromisoformat(
                response.json()["obtained_at"])
            assert abs(datetime.now().timestamp() -
                       obtained_at.timestamp()) < 1
Ejemplo n.º 20
0
def test_update_settings():
    with database_mock() as db:
        db: StandardDatabase
        insert_mocks(db, "users")
        insert_mocks(db, "settings")
        with authed_request(client, "alice", ALICE_PASSWORD) as params:
            result = client.patch("/settings", json={"grades_unit": 10}, **params)
            expected = {
                **json.loads(mocks.settings.alice.json(by_alias=True)),
                "grades_unit": 10,
            }

            assert result.status_code == 200
            assert result.json()["grades_unit"] == 10
            for key in InSettings().dict(by_alias=True).keys():
                if key in ("grades_unit", "updated_at"):
                    pass
                assert result.json()[key] == expected[key]
            updated_at = Settings(**result.json()).updated_at
            assert updated_at.date() == datetime.now().date()
            assert updated_at.hour == datetime.now().hour
            assert updated_at.minute == datetime.now().minute
            assert updated_at.second == datetime.now().second
Ejemplo n.º 21
0
def setup_helper_and_db(db) -> ResourceRoutesGenerator:
    db.create_collection("ipsum")
    db.collection("ipsum").insert(loremeggs.json(by_alias=True))
    db.collection("ipsum").insert(lorembacon.json(by_alias=True))
    insert_mocks(db, "users")
    return ResourceRoutesGenerator("lorem", "ipsum", Lorem, LoremOut)