Coverage for portality/events/consumers/account_passwordreset_email.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-11-09 15:10 +0000

1# from flask import url_for 

2from portality.util import url_for 

3 

4from portality.events.consumer import EventConsumer 

5from portality import constants 

6from portality import app_email 

7from portality import models 

8from portality.core import app 

9from portality.bll.exceptions import NoSuchPropertyException 

10 

11 

12class AccountPasswordResetEmail(EventConsumer): 

13 ID = "account:password_reset:email" 

14 

15 @classmethod 

16 def consumes(cls, event): 

17 return event.id == constants.EVENT_ACCOUNT_PASSWORD_RESET and event.context.get("account") is not None 

18 

19 @classmethod 

20 def consume(cls, event): 

21 acc = models.Account(**event.context.get("account")) 

22 if not acc.reset_token or not acc.email: 

23 raise NoSuchPropertyException("Account {x} does not have a reset_token and/or an email address".format(x=acc.id)) 

24 cls._send_password_reset_email(acc) 

25 

26 @classmethod 

27 def _send_password_reset_email(cls, account: models.Account): 

28 reset_url = app.config.get('BASE_URL', "https://doaj.org") + url_for('account.reset', reset_token=account.reset_token) 

29 

30 to = [account.email] 

31 fro = app.config.get('SYSTEM_EMAIL_FROM', app.config['ADMIN_EMAIL']) 

32 subject = app.config.get("SERVICE_NAME", "") + " - password reset" 

33 

34 app_email.send_mail(to=to, 

35 fro=fro, 

36 subject=subject, 

37 template_name="email/account_password_reset.jinja2", 

38 email=account.email, 

39 reset_url=reset_url, 

40 forgot_pw_url=app.config.get('BASE_URL', "https://doaj.org") + url_for('account.forgot') 

41 )