def test_sets_csrf_token_in_headers(self) -> None: mock_session = MagicMock() request_preparer = MilMoveRequestPreparer(env=MilMoveEnv.LOCAL) create_user(request_preparer=request_preparer, session=mock_session, user_type=UserType.MILMOVE) mock_session.headers.update.assert_called_once_with( {"x-csrf-token": mock_session.cookies.get.return_value})
def on_start(self): """ Creates a login right at the start of the TaskSet and stops task execution if the login fails. """ success = create_user(request_preparer=self.request_preparer, session=self.client, user_type=UserType.TOO) if not success: logger.error("Failed to create a user") self.interrupt()
def test_makes_expected_requests(self, user_type: UserType, base_domain: str) -> None: mock_session = MagicMock() request_preparer = MilMoveRequestPreparer(env=MilMoveEnv.LOCAL) create_user(request_preparer=request_preparer, session=mock_session, user_type=user_type) mock_session.get.assert_called_once_with( url=f"{base_domain}/devlocal-auth/login") mock_session.post.assert_called_once_with( url=f"{base_domain}/devlocal-auth/create", data={ "userType": user_type.value, "gorilla.csrf.Token": mock_session.cookies.get.return_value, }, )
def test_returns_bool_indicating_success_based_on_status_code( self, status_code: int, expected_result: bool) -> None: mock_session = MagicMock() mock_session.post.return_value.status_code = status_code request_preparer = MilMoveRequestPreparer(env=MilMoveEnv.LOCAL) success = create_user(request_preparer=request_preparer, session=mock_session, user_type=UserType.MILMOVE) assert success == expected_result
def ghc_api_client(self, user_type: UserType) -> LocustGHCApiClient: session = requests.Session() if not create_user(self.request_preparer, session, user_type): raise Exception(f"Cannot create user: {user_type}") host = self.request_preparer.form_ghc_path("") req = requests.Request("GET", host) cookie = requests.cookies.get_cookie_header(session.cookies, req) configuration = internal_client.Configuration(host=host) api_client = LocustGHCApiClient(configuration, cookie=cookie) for k, v in session.headers.items(): api_client.set_default_header(k, v) api_client.rest_client.pool_manager = self.new_pool_manager() return api_client