def post_password_reset(): try: email = request.form['email'] json_resp, status = api_post('/api/authentication/token', json={"email": email}) form = FormValidator('An error has occurred') form.add_validator('email', email, [email_validator, is_not_empty], empty_msg='Enter your email address') if status != 200 or not form.is_valid(): raise ValueError token = json_resp['token'] decoded_jwt, status = api_get('/api/authentication/token/' + token + '?validation-type=reset-password') if status != 200: current_app.logger.error('Token has failed validation:' + decoded_jwt['error']) raise ValueError first_name = decoded_jwt['principle']['first_name'] url = current_app.config.get( "SITE_URL") + "/password/change?t=" + token current_app.logger.info(url) template_id = current_app.config.get("RESET_PASSWORD_TEMPLATE") _, email_status = api_post('/api/notifications', json={ "email_address": email, "template_id": template_id, "personalisation": { "first_name": first_name, "last_name": decoded_jwt['principle']['surname'], "change_password_link": url }, "reference": "password-reset" }, headers={'Accept': 'application/json'}) if email_status != 201: raise ValueError return render_template('app/password/email_sent.html', email=email) except ValueError: form = FormValidator('An error has occurred') form.add_validator('email', email, [is_not_empty, email_validator], empty_msg='Enter your email address') return render_template( 'app/password/reset.html', error_title="There was a problem", fields=form.validate(), )
def request_payment(amount, reference, description, user_type): if user_type == 'personal': return_url = app.config.get("SITE_URL") + url_for( 'registration_personal.handle_verification') else: return_url = app.config.get("SITE_URL") + \ url_for('registration_overseas_org.handle_verification_overseas') body = { 'amount': amount, 'reference': reference, 'description': description, 'return_url': return_url } current_app.logger.info( 'Requesting GovPay payment with reference {}'.format(reference)) response, status_code = api_post(gov_pay_url, json=body, headers=_make_headers(), external=True) if status_code != 201: current_app.logger.error( 'Fail response from GovPay API: {}'.format(response)) return None else: return response
def handle_expired_link(token): try: current_app.logger.info( 'resetting expired link for token: {}'.format(token)) decoded_jwt, status = api_get('/api/authentication/token/' + token + '?validation-type=expired-token') if status != 200: current_app.logger.error( 'Error decoding the jwt: {}'.format(decoded_jwt)) return False api_url = '/api/account/users/{}/activate'.format(decoded_jwt['sub']) resp, code = api_post(api_url, headers={'Content-Type': 'application/json'}) if code < 299: return True else: current_app.logger.error( 'Error activating the users account: {}'.format(resp)) return False except Exception as e: current_app.logger.error(e) return False
def _send_confirmation_email(template_id, email, personalisation, reference): body, _ = api_post('/api/notifications', json={ "email_address": email, "template_id": template_id, "personalisation": personalisation, "reference": reference }, headers={'Accept': 'application/json'}) current_app.logger.info(body)
def test_api_post_fail_status_code_500(self, mock_req, mock_requests): response = mock_requests.Response() response.status_code = 500 response.json.return_value = {} mock_req.post.return_value = response test_response, test_code = api_post('/url', {'post': 'true'}) self.assertEqual(test_response, {}) self.assertEqual(test_code, 500)
def test_api_post_ok(self, mock_req, mock_requests): response = mock_requests.Response() response.status_code = 200 response.json.return_value = {'post': 'true'} mock_req.post.return_value = response test_response, test_code = api_post('url', {'post': 'true'}) self.assertEqual(test_response, {'post': 'true'}) self.assertEqual(test_code, 200)
def test_api_post_fail_value_error(self, mock_req, mock_requests): response = mock_requests.Response() response.status_code = 205 response.json.side_effect = ValueError mock_req.post.return_value = response test_response, test_code = api_post('/url', {'post': 'true'}) self.assertEqual(test_response, {}) self.assertEqual(test_code, 205)
def test_api_post_fail_status_code_401_not_authorized( self, mock_req, mock_requests): response = mock_requests.Response() response.status_code = 401 response.json.return_value = {} mock_req.post.return_value = response test_response, test_code = api_post('/url') self.assertEqual(test_response, {}) self.assertEqual(test_code, 401)