示例#1
0
    def test_retrieve_users(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)
        # username = random_lower_string()
        # password = random_lower_string()
        data = _MakeRandomNormalUserFactory()
        user_in = UserCreate(email=data.email, password=data.password)
        user = crud.user.create(db, user_in=user_in)
        # username2 = random_lower_string()
        # password2 = random_lower_string()
        data2 = _MakeRandomNormalUserFactory()
        user_in2 = UserCreate(email=data2.email, password=data2.password)
        user2 = crud.user.create(db, user_in=user_in2)

        r = fastapi_client.get(
            f"{server_api}{settings.API_V1_STR}/users/", headers=superuser_token_headers
        )
        all_users = r.json()

        assert len(all_users) > 1
        for user in all_users:
            assert "email" in user
示例#2
0
    def test_create_user_existing_username(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)
        # username = random_lower_string()
        # username = email
        # password = random_lower_string()
        data = _MakeRandomNormalUserFactory()

        user_in = UserCreate(email=data.email, password=data.password)
        user = crud.user.create(db, user_in=user_in)
        # data = {"email": username, "password": password}
        r = fastapi_client.post(
            f"{server_api}{settings.API_V1_STR}/users/",
            headers=superuser_token_headers,
            json=data.dict(),  # pylint: disable=no-member
        )
        created_user = r.json()
        assert r.status_code == 400
        assert "_id" not in created_user
示例#3
0
    def test_update_user_me_by_normal_user(
        self, mocker: MockFixture, fastapi_client: TestClient, db: Session
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)

        # create basic user via crud
        data = _MakeRandomNormalUserFactory()
        logger.debug("data : {}".format(data))
        user_in = UserCreate(email=data.email, password=data.password)
        logger.debug("user_in : {}".format(user_in))
        user_create_crud = crud.user.create(db, user_in=user_in)
        logger.debug("user_create_crud : {}".format(jsonable_encoder(user_create_crud)))

        # get normal user token for update request
        user_token_headers = user_authentication_headers(
            server_api, data.email, data.password
        )

        user_update = UserUpdate(
            email=data.email, password=data.password, full_name=data.full_name
        )
        logger.debug("user_update : {}".format(user_update))

        json_encoded_user_update = jsonable_encoder(user_update)

        r = fastapi_client.put(
            f"{server_api}{settings.API_V1_STR}/users/me",
            headers=user_token_headers,
            json=json_encoded_user_update,
        )

        assert 200 == r.status_code
        r.json()
示例#4
0
    def test_get_existing_user(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)
        # username = random_lower_string()
        # password = random_lower_string()

        data = _MakeRandomNormalUserFactory()

        user_in = UserCreate(email=data.email, password=data.password)
        user = crud.user.create(db, user_in=user_in)
        user_id = user.id
        r = fastapi_client.get(
            f"{server_api}{settings.API_V1_STR}/users/{user_id}",
            headers=superuser_token_headers,
        )
        assert 200 <= r.status_code < 300
        api_user = r.json()
        user = crud.user.get_by_email(db, email=data.email)
        assert user.email == api_user["email"]
示例#5
0
    def test_create_user_new_email_with_emails_enabled(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        monkeypatch: MonkeyPatch,
        mock_email: Callable,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)

        data = _MakeRandomNormalUserFactory()

        r = fastapi_client.post(
            f"{server_api}{settings.API_V1_STR}/users/",
            headers=superuser_token_headers,
            json=data.dict(),  # pylint: disable=no-member
        )
        assert 200 <= r.status_code < 300
        created_user = r.json()
        user = crud.user.get_by_email(db, email=data.email)
        assert user.email == created_user["email"]
        mock_email.assert_called_once_with(
            email_to=data.email, username=data.email, password=data.password
        )
示例#6
0
 def test_create_user_by_normal_user(
     self, mocker: MockFixture, fastapi_client: TestClient, db: Session
 ) -> None:
     server_api = get_server_api()
     logger.debug("server_api : %s", server_api)
     # username = random_lower_string()
     # password = random_lower_string()
     data = _MakeRandomNormalUserFactory()
     user_in = UserCreate(email=data.email, password=data.password)
     user = crud.user.create(db, user_in=user_in)
     user_token_headers = user_authentication_headers(
         server_api, data.email, data.password
     )
     # data = {"email": username, "password": password}
     r = fastapi_client.post(
         f"{server_api}{settings.API_V1_STR}/users/",
         headers=user_token_headers,
         json=data.dict(),  # pylint: disable=no-member
     )
     assert r.status_code == 400
示例#7
0
    def test_create_user_new_email_with_starlette_client(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        # SOURCE: https://github.com/tiangolo/fastapi/issues/300
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)

        data = _MakeRandomNormalUserFactory()
        r = fastapi_client.post(
            f"{server_api}{settings.API_V1_STR}/users/",
            headers=superuser_token_headers,
            json=data.dict(),  # pylint: disable=no-member
        )
        assert 200 <= r.status_code < 300
        created_user = r.json()
        user = crud.user.get_by_email(db, email=data.email)
        assert user.email == created_user["email"]
示例#8
0
    def test_create_user_new_email(
        self,
        superuser_token_headers: Dict[str, str],
        mocker: MockFixture,
        fastapi_client: TestClient,
        db: Session,
    ) -> None:
        server_api = get_server_api()
        logger.debug("server_api : %s", server_api)
        # username = random_lower_string()
        # password = random_lower_string()

        data = _MakeRandomNormalUserFactory()
        # data = {"email": data.email, "password": data.password}

        r = fastapi_client.post(
            f"{server_api}{settings.API_V1_STR}/users/",
            headers=superuser_token_headers,
            json=data.dict(),  # pylint: disable=no-member
        )
        assert 200 <= r.status_code < 300
        created_user = r.json()
        user = crud.user.get_by_email(db, email=data.email)
        assert user.email == created_user["email"]
示例#9
0
def test_user(db: SessionLocal) -> UserInDB:
    data: UserCreate = _MakeRandomNormalUserFactory()
    user = crud.user.create(db, user_in=data)
    return user
示例#10
0
    def test_cli_user_create_with_debug_and_json_output(
            self, request, monkeypatch) -> None:
        # reset global config singleton
        config._CONFIG = None
        fixture_path = fixtures_path / "isolated_config_dir"
        print(fixture_path)
        runner = CliRunner()
        with runner.isolated_filesystem() as isolated_dir:
            # Populate filesystem folders
            isolated_base_dir = isolated_dir
            isolated_xdg_config_home_dir = os.path.join(
                isolated_dir, ".config")
            isolated_ultron_config_dir = os.path.join(
                isolated_xdg_config_home_dir, "ultron8")
            isolated_ultron_config_path = os.path.join(
                isolated_ultron_config_dir, "smart.yaml")

            # create base dirs
            os.makedirs(isolated_xdg_config_home_dir)

            # request.cls.home = isolated_base_dir
            # request.cls.xdg_config_home = isolated_xdg_config_home_dir
            # request.cls.ultron_config_dir = isolated_ultron_config_dir
            # request.cls.ultron_config_path = isolated_ultron_config_path

            # monkeypatch env vars to trick intgr tests into running only in isolated file system
            monkeypatch.setenv("HOME", isolated_base_dir)
            monkeypatch.setenv("XDG_CONFIG_HOME", isolated_xdg_config_home_dir)
            monkeypatch.setenv("ULTRON8DIR", isolated_ultron_config_dir)

            # Copy the project fixture into the isolated filesystem dir.
            shutil.copytree(fixture_path, isolated_ultron_config_dir)

            # Grab access token
            r = get_superuser_jwt_request()
            tokens = r.json()
            a_token = tokens["access_token"]

            # create factory user
            factory_user = _MakeRandomNormalUserFactory()
            example_payload_json = jsonable_encoder(factory_user)
            path_to_payload = os.path.join(isolated_base_dir,
                                           "test_payload.json")

            # write factory user data to disk
            with open(path_to_payload, "w") as outfile:
                json.dump(example_payload_json, outfile)

            example_data = """
clusters_path: clusters/
cache_path: cache/
workspace_path: workspace/
templates_path: templates/

flags:
    debug: 0
    verbose: 0
    keep: 0
    stderr: 0
    repeat: 1

clusters:
    instances:
        local:
            url: http://localhost:11267
            token: '{}'
    """.format(a_token)

            # overwrite smart.yaml w/ config that has auth token in it.
            helper_write_yaml_to_disk(example_data,
                                      isolated_ultron_config_path)

            # Monkeypatch a helper method onto the runner to make running commands
            # easier.
            runner.run = lambda command: runner.invoke(cli, command.split())

            # And another for checkout the text output by the command.
            runner.output_of = lambda command: runner.run(command).output

            # Run click test client
            result = runner.invoke(
                cli,
                [
                    "--debug",
                    "user",
                    "--cluster",
                    "local",
                    "create",
                    "--payload",
                    path_to_payload,
                ],
            )

            # verify results
            assert result.exit_code == 0