Skip to content

Commit

Permalink
For #26 create custom Exceptions for send news job; add tests;change …
Browse files Browse the repository at this point in the history
…user factory (to be inherit by others)
  • Loading branch information
lissa3 committed Aug 6, 2023
1 parent 528fac6 commit cfc8d25
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 96 deletions.
3 changes: 2 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
env_path=./.env
DJANGO_SETTINGS_MODULE=sandbox.conf.dev
python_files=tests.py test_*.py *_tests.py

addopts = -p no:warnings --reuse-db
norecursedirs = venv dist

filterwarnings=ignore::DeprecationWarning
10 changes: 2 additions & 8 deletions src/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class Meta:
django_get_or_create = ("username", "email")


class AdminSupUserFactory(factory.django.DjangoModelFactory):
username = factory.Sequence(lambda n: f"user-{n}")
email = factory.LazyAttribute(lambda _: faker.unique.email())
password = factory.PostGenerationMethodCall("set_password", "12345abc")
class AdminSupUserFactory(UserFactory):
is_staff = True
is_superuser = True

Expand All @@ -29,10 +26,7 @@ class Meta:
django_get_or_create = ("username", "email", "is_staff", "is_superuser")


class StaffUserFactory(factory.django.DjangoModelFactory):
username = factory.Sequence(lambda n: f"user-{n}")
email = factory.LazyAttribute(lambda _: faker.unique.email())
password = factory.PostGenerationMethodCall("set_password", "12345abc")
class StaffUserFactory(UserFactory):
is_staff = True
is_superuser = False

Expand Down
Empty file.
42 changes: 0 additions & 42 deletions src/accounts/tests/factories/user_factory.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from src.profiles.models import Profile

from .factories.user_factory import UserFactory
from .factories import UserFactory

User = get_user_model()

Expand Down
2 changes: 1 addition & 1 deletion src/accounts/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.urls import reverse
from django_webtest import WebTest

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory

User = get_user_model()

Expand Down
24 changes: 24 additions & 0 deletions src/contacts/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class NewsFansNotFoundException(Exception):
"""
get triggered manager Profile model
can't find users who wants to get newsletter
"""

pass


class LetterNotFoundException(Exception):
"""
get triggered when there is no newsletter to send
by cron jobs
"""

pass


class JobError(Exception):
"""
get triggered if sending newsletter by cron failed
"""

pass
21 changes: 15 additions & 6 deletions src/contacts/jobs/send_news.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from smtplib import SMTPException

from django.conf import settings
from django.core import mail
from django.template.loader import render_to_string
from django.utils import timezone
from django_extensions.management.jobs import WeeklyJob

from src.contacts.exceptions import * # noqa
from src.contacts.models import NewsLetter
from src.posts.models.post_model import Post
from src.profiles.models import Profile
Expand All @@ -28,11 +31,11 @@ def execute(self):
profiles = Profile.objects.send_news().select_related("user")
letter = NewsLetter.objects.filter(letter_status=1).last()
ctx = {"letter": letter, "domain": domain}
posts = Post.objects.filter(send_status=1, letter=letter)
if letter and profiles:
if letter.posts:
ctx.update({"posts": letter.posts.all()})
if profiles and letter:
try:
posts = Post.objects.filter(send_status=1, letter=letter)
if letter.posts:
ctx.update({"posts": letter.posts.all()})
for profile in profiles:
ctx.update({"uuid": profile.uuid})
text_msg = render_to_string("contacts/emails/letter.txt", ctx)
Expand All @@ -52,6 +55,12 @@ def execute(self):
post.send_status = 2
post.save()

except Exception as e:
print(e)
except SMTPException as e:
print("smth went wrong ", e)
# TODO: add Log

else:
if not profiles:
raise NewsFansNotFoundException("No profiles not send news")
elif not letter:
raise LetterNotFoundException("No letter to send")
16 changes: 2 additions & 14 deletions src/contacts/tests/test_jobs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
import time_machine
from django.conf import settings
from django.urls import reverse

from src.accounts.models import User
from src.contacts.exceptions import * # noqa
from src.contacts.jobs.send_news import Job as SendMailJob
from src.contacts.models import NewsLetter
from src.posts.models.post_model import Post
Expand All @@ -13,20 +15,6 @@


class TestSendEmailJob:
@time_machine.travel("2024-04-23 00:00 +0000")
def test_no_news_inactive_user(self, mailoutbox):
"""
no letter for inactive user
"""
profile = ProfileFactory(want_news=True)
user = User.objects.get(profile=profile)
user.is_active = False
user.save()
send_mail_job = SendMailJob()
send_mail_job.execute()

assert len(mailoutbox) == 0

@time_machine.travel("2023-07-17 00:00 +0000")
def test_send_news_with_posts_links(self, mailoutbox):
"""
Expand Down
36 changes: 36 additions & 0 deletions src/contacts/tests/test_web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.contrib.auth import get_user_model
from django.test import override_settings
from django.urls import reverse
from django_webtest import WebTest

from src.accounts.tests.factories import UserFactory

User = get_user_model()


@override_settings(LANGUAGE_CODE="en", LANGUAGES=(("en", "English"),))
class SubscribeLinkTest(WebTest):
def setUp(self):
super().setUp()
self.user = UserFactory()
self.url = reverse("home")

def test_auth_user_has_subscr_link_in_menu(self):
"""Auth user has link to subscribe for a newsletter"""
self.app.set_user(self.user)
self.response = self.app.get(self.url)
self.assertEqual(self.response.status_code, 200)

subs_link = self.response.html.find("a", id="subLink")

self.assertIsNotNone(subs_link)

def test_unauth_user_no_link_in_menu(self):
"""Unauth used - no link in menu to subscribe for a newsletter"""

self.response = self.app.get(self.url)
self.assertEqual(self.response.status_code, 200)

subs_link = self.response.html.find("a", id="subLink")

self.assertIsNone(subs_link)
5 changes: 2 additions & 3 deletions src/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
class Subscribe(LRM, View):
def get(self, request):
"""
only autent-ed users can subscribe to a news letter
only auth-ed users can subscribe to a news letter
via menu dropdown item
"""
return render(
request,
"contacts/subscription/\
confirmation.html",
"contacts/subscription/confirmation.html",
)

def post(self, request, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion src/posts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from faker import Faker
from taggit.models import Tag

from src.accounts.tests.factories.user_factory import StaffUserFactory
from src.accounts.tests.factories import StaffUserFactory
from src.posts.models.categ_model import Category
from src.posts.models.post_model import Post

Expand Down Expand Up @@ -37,6 +37,7 @@ class PostFactory(DjangoModelFactory):
status = factory.fuzzy.FuzzyChoice(Post.CurrentStatus.values)
title = factory.Faker("word")
content = factory.Faker("paragraph")

# category = factory.SubFactory(Category) default

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion src/profiles/tests/factories/profile_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import factory
from django.db.models.signals import post_save

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory
from src.profiles.models import Profile


Expand Down
3 changes: 0 additions & 3 deletions src/profiles/tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import pytest

from src.profiles.models import Profile
from src.profiles.tests.factories.profile_factory import ProfileFactory


@pytest.mark.django_db
class TestProfile:
def test_factory(self):
profile = ProfileFactory()
Expand Down
2 changes: 1 addition & 1 deletion src/profiles/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time_machine
from django.test import TestCase

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory
from src.profiles.models import Profile


Expand Down
2 changes: 1 addition & 1 deletion src/profiles/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db.models.signals import post_delete, post_save
from django.test import TestCase

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory
from src.profiles.models import Profile
from src.profiles.tests.factories.profile_factory import ProfileFactory

Expand Down
2 changes: 1 addition & 1 deletion src/profiles/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory
from src.core.utils.base import get_temporary_image, get_temporary_text_file
from src.profiles.forms import ProfileForm
from src.profiles.models import Profile
Expand Down
2 changes: 1 addition & 1 deletion src/profiles/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django_webtest import WebTest
from webtest import Upload

from src.accounts.tests.factories.user_factory import UserFactory
from src.accounts.tests.factories import UserFactory
from src.core.utils.base import get_temp_img_bytes
from src.profiles.models import Profile

Expand Down
26 changes: 26 additions & 0 deletions src/static/images/ico/site.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{
"name":"Med Sandox",
"short_name":"sanbox",
"icons":[
{"src":"./android-chrome-192x192.png",
"sizes":"192x192",
"type":"image/png",
"purporse":"maskable"
},
{"src":"./android-chrome-512x512.png",
"sizes":"512x512",
"type":"image/png",
"purporse":"maskable"
},
{"src":"./apple-touch-icon.png",
"sizes":"512x512",
"type":"image/png",
"purporse":"maskable"
}

],
"display":"standalone",
"description":"Medical education",
"theme_color":"#f1dfc5"
}
Loading

0 comments on commit cfc8d25

Please sign in to comment.