Esempio n. 1
0
def test_auth_wrong_username():
    with database_mock() as mock:
        mock.collection("users").insert(mocks.users.alice.json(by_alias=True))
        response = client.post(
            "/auth/", data={"username": "******", "password": ALICE_PASSWORD},
        )
        assert response.status_code == 401
Esempio n. 2
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. 3
0
def test_update_ok():
    with database_mock() as db:
        db: StandardDatabase
        helper = setup_helper_and_db(db)
        loremham = LoremOut(**{**lorembacon.dict(by_alias=True), "dolor": 88})
        result = helper.update(
            db,
            mocks.users.john,
            lorembacon.object_key,
            LoremPatch(dolor=loremham.dolor),
        ).dict(by_alias=True)
        result_update_date = result["updated_at"]
        for key, value in {
                "dolor": loremham.dolor,
                "sit": loremham.sit,
                "owner_key": loremham.owner_key,
        }.items():
            assert result[key] == value

        assert result_update_date.date() == datetime.now().date()
        assert result_update_date.hour == datetime.now().hour
        assert result_update_date.minute == datetime.now().minute
        parsed_full_key = parse(OBJECT_KEY_FORMAT, result["_key"])
        assert parsed_full_key.named["owner"] == lorembacon.owner_key
        assert parsed_full_key.named["object"] == result["object_key"]
Esempio n. 4
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. 5
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. 6
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. 7
0
def test_delete_ok():
    with database_mock() as db:
        db: StandardDatabase
        helper = setup_helper_and_db(db)
        collection_length_before_deletion = db.collection(
            "ipsum").all().count()
        assert (helper.delete(db, mocks.users.john,
                              lorembacon.object_key).status_code == 204)
        assert (db.collection("ipsum").all().count() ==
                collection_length_before_deletion - 1)
        assert db.collection("ipsum").get(lorembacon._key) is None
Esempio n. 8
0
def test_auth_ok():
    # login
    with database_mock() as mock:
        mock.collection("users").insert(mocks.users.alice.json(by_alias=True))
        response = client.post(
            "/auth/",
            data={"username": mocks.users.alice.username, "password": ALICE_PASSWORD},
        )
        assert response.status_code == 200
        assert "access_token" in response.json().keys()
        assert "token_type" in response.json().keys()
Esempio n. 9
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. 10
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. 11
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. 12
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
Esempio n. 13
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. 14
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. 15
0
def test_get_not_owned():
    with database_mock() as db:
        db: StandardDatabase
        helper = setup_helper_and_db(db)
        try_with_key = loremeggs.object_key
        with raises(fastapi.exceptions.HTTPException) as error:
            helper.get(db, mocks.users.john, try_with_key)

        assert error.value.status_code == 404
        assert (
            error.value.detail ==
            f"No lorem with key {OBJECT_KEY_FORMAT.format(owner=mocks.users.john.key, object=try_with_key)} found"
        )
Esempio n. 16
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. 17
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. 18
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. 19
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. 20
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. 21
0
def test_create_user_username_disallowed():
    with database_mock() as db:
        assert db.collection("users").all().count() == 0

        response = client.post(
            "/users/",
            json={
                "username": "******",
                "password": ALICE_PASSWORD,
                "email": mocks.users.alice.email,
            },
        )

        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert "username is not allowed" in response.json()["detail"]
Esempio n. 22
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. 23
0
def test_create_user_email_taken():
    with database_mock() as db:
        db: StandardDatabase
        db.collection("users").insert(mocks.users.alice.json(by_alias=True))

        response = client.post(
            "/users/",
            json={
                "username": "******",
                "password": ALICE_PASSWORD,
                "email": mocks.users.alice.email,
            },
        )

        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert "email is already taken" in response.json()["detail"]
Esempio n. 24
0
def test_create_user_password_too_simple():
    with database_mock() as db:
        assert db.collection("users").all().count() == 0

        response = client.post(
            "/users/",
            json={
                "username": mocks.users.alice.username,
                "password": "******",
                "email": mocks.users.alice.email,
            },
        )

        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert "password is not strong enough" in response.json()["detail"]["message"]
        assert "analysis" in response.json()["detail"].keys()
Esempio n. 25
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. 26
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. 27
0
def test_delete_not_found():
    with database_mock() as db:
        db: StandardDatabase
        helper = setup_helper_and_db(db)
        collection_length_before_deletion = db.collection(
            "ipsum").all().count()
        try_with_key = objectbarekey()
        with raises(fastapi.exceptions.HTTPException) as error:
            helper.delete(db, mocks.users.john, try_with_key)

        assert error.value.status_code == 404
        assert (
            error.value.detail ==
            f"No lorem with key {OBJECT_KEY_FORMAT.format(owner=mocks.users.john.key, object=try_with_key)} found"
        )
        assert db.collection(
            "ipsum").all().count() == collection_length_before_deletion
        assert db.collection("ipsum").get(try_with_key) is None
Esempio n. 28
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. 29
0
def test_create_user_ok():
    with database_mock() as db:
        assert db.collection("users").all().count() == 0

        response = client.post(
            "/users/",
            json={
                "username": mocks.users.alice.username,
                "password": ALICE_PASSWORD,
                "email": mocks.users.alice.email,
            },
        )

        assert response.status_code == status.HTTP_201_CREATED
        assert response.json()["username"] == mocks.users.alice.username
        assert response.json()["email"] == mocks.users.alice.email
        assert not response.json()["email_is_confirmed"]
        joined_at = isodatetime.parse_datetime(response.json()["joined_at"])
        assert joined_at.today
        assert "_key" in response.json().keys()
Esempio n. 30
0
def test_update_not_owned():
    with database_mock() as db:
        db: StandardDatabase
        helper = setup_helper_and_db(db)
        loremham = LoremOut(**{**lorembacon.dict(by_alias=True), "dolor": 88})
        try_with_key = loremeggs.object_key

        with raises(fastapi.exceptions.HTTPException) as error:
            helper.update(
                db,
                mocks.users.john,
                try_with_key,
                LoremPatch(dolor=loremham.dolor),
            )

        assert error.value.status_code == 404
        assert (
            error.value.detail ==
            f"No lorem with key {OBJECT_KEY_FORMAT.format(owner=mocks.users.john.key, object=try_with_key)} found"
        )
        assert db.collection("ipsum").get(try_with_key) is None