Refactoring the get_captcha function and adding the related tests