Coverage for api/tests/test_email.py: 100%

# Coverage for api/tests/test_email.py : 100%

## 0 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# """

2# This file (test_email.py) contains the unit tests for

3# functions defined in email.py which is used for sending

4# user email confirmation

5# """

6# import pytest

7# from flask_mailman import EmailMessage

8# from modules.box__default.auth.email import _send_email_helper

9# from modules.box__default.auth.email import send_async_email

10# from flask_login import UserMixin

11# from init import db

12# from shopyo.api.models import PkModel

13# class ExampleUserModel(PkModel, UserMixin):

14# """Example user model for testing purposes"""

15# __tablename__ = "testusers_email"

16# password = db.Column(db.String(100), unique=True, nullable=False)

17# email = db.Column(db.String(120), unique=True, nullable=False)

18# @pytest.mark.parametrize(

19# "email_config",

20# [("MAIL_DEFAULT_SENDER", "remove"), ("MAIL_DEFAULT_SENDER", None)],

21# indirect=True,

22# )

23# def test_send_email_with_no_default_sender(capfd, email_config):

24# user = User.create(email="test@gmail.com", password="pass")

25# token = "sometoken"

26# template = "auth/emails/activate_user"

27# subject = "Please confirm your email"

28# context = {"token": token, "user": user}

29# send_async_email(user.email, subject, template, **context)

30# captured = capfd.readouterr()

31# assert "Shopyo Error: MAIL_DEFAULT_SENDER not configured" in captured.out

32# @pytest.mark.parametrize(

33# "email_config",

34# [

35# ("MAIL_USERNAME", "remove"),

36# ("MAIL_PASSWORD", "remove"),

37# ("MAIL_USERNAME", None),

38# ("MAIL_PASSWORD", None),

39# ],

40# indirect=True,

41# )

42# def test_send_email_with_no_username_or_password_set(capfd, email_config):

43# user = User.create(email="test@gmail.com", password="pass")

44# token = "sometoken"

45# template = "auth/emails/activate_user"

46# subject = "Please confirm your email"

47# context = {"token": token, "user": user}

48# thread = send_async_email(user.email, subject, template, **context)

49# thread.join()

50# captured = capfd.readouterr()

51# assert (

52# "Shopyo Error: MAIL_USERNAME, and/or MAIL_PASSWORD not configured"

53# in captured.out

54# )

55# def test_send_email_using_template_on_valid_credentials(capfd):

56# user = User.create(email="to@gmail.com", password="pass")

57# token = "sometoken"

58# template = "auth/emails/activate_user"

59# subject = "Please confirm your email"

60# from_email = "from@gmail.com"

61# context = {"token": token, "user": user}

62# thread = send_async_email(

63# user.email, subject, template, from_email=from_email, **context

64# )

65# thread.join()

66# captured = capfd.readouterr()

67# assert "Please confirm your email" in captured.out

68# assert "sometoken" in captured.out

69# assert "to@gmail.com" in captured.out

70# assert "from@gmail.com" in captured.out

71# assert "Welcome to Shopyo" in captured.out

72# assert "To confirm your account please click on" in captured.out

73# assert "The Shopyo Team" in captured.out

74# def test_send_using_helper_function(test_client, flask_app, capfd):

75# msg = EmailMessage(

76# subject="subject of email",

77# body="body of email",

78# to=["to@test.com"],

79# from_email="from@test.com",

80# )

81# _send_email_helper(flask_app, msg)

82# captured = capfd.readouterr()

83# assert "to@test.com" in captured.out

84# assert "from@test.com" in captured.out

85# assert "subject of email" in captured.out

86# assert "body of email" in captured.out