Esempio 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
Esempio 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
Esempio 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
Esempio 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))
Esempio 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
Esempio n. 6
0
def test_read_users_self_logged_in():
    with database_mock() as db:
        db.collection("users").insert(mocks.users.alice.json(by_alias=True))

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

        assert response.status_code == 200
        assert response.json() == json.loads(
            User(**mocks.users.alice.dict(by_alias=True)).json(by_alias=True)
        )
Esempio n. 7
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
Esempio 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
Esempio 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
Esempio 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
Esempio 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())
Esempio 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
Esempio 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
Esempio 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())
Esempio 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()
            ]
Esempio 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
Esempio 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"
Esempio 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)
            ])
Esempio 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
Esempio 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