0

I am creating an application that receives some parameters and sends an email using a company server, when I do the test using postman it works, but using the same parameters to perform a test happens an error and I can't debug, only 500 error appears, can i see the test django server log?

urls.py

urlpatterns = [
    path('modules/email/send', views_email.email_send.as_view(), name='email_send'),
]

views_email.py

class email_send(APIView):
    permission_classes = (IsAuthenticated,)

    def post(self, request):    # ver anexo e como carregar os componentes
        try:
            body = json.loads(request.body.decode('utf-8'))

            fromaddr = body.get("from")
            to = body.get("to")
            cc = body.get("cc")
            subject = body.get("subject")
            level = body.get("level")
            level_color = body.get("level_color")
            alert_name = body.get("alert_name")
            body_html = body.get('body_html')

            #Arquivos html
            index_file = body.get("index_file")
            header_file = body.get("header_file")
            footer_file = body.get("footer_file")
            # body_file = body.get("body_file")

            message = MIMEMultipart("alternative")
            message["Subject"] = subject
            message["From"] = fromaddr
            message["To"] = to
            message["CC"] = cc 

            ....

            part1 = MIMEText(text, "plain")
            part2 = MIMEText(html_string, "html")

            message.attach(part1)
            message.attach(part2)

            server = smtplib.SMTP('server.com.br', 25)
            response = server.sendmail(fromaddr, toaddr, message.as_string())
            if response == {}:
                response = 200
            else:
                pass
            return Response(response, content_type="text/plain", status=status.HTTP_200_OK)

        except Exception as e:
            return Response(str(e), content_type="text/plain", status=status.HTTP_400_BAD_REQUEST)

test_urls.py

from rest_framework.test import RequestsClient
from rest_framework.authtoken.models import Token
import requests

client = RequestsClient()
token = Token.objects.get(user__username='admin')

class EmailSendTest(TestCase):

    def test_email_send(self):

        headers = {
            'Content-Type': "application/json",
            'Authorization': "Token " + token.key
        }

        payload = {
            "from": "@",
            "to": "@",
            "cc": "",
            "subject": "Test views_email",
            "alert_name": "Test views_email",
            "level": "level",
            "level_color": "default",

            "index_file": "index.html",
            "header_file": "header.html",
            "footer_file": "footer.html",
            "body_html": "Test"
        }

        response = client.post(
            'http://testserver/motor-alertas/modules/email/send',
            json=payload,
            headers=headers,
        )
        self.assertEquals(response.content, 200)

I run the tests with the following command:

python3 -m pytest --cov=. && \
python3 -m coverage xml -i
Hiago Bonamelli
  • 371
  • 5
  • 15
  • 1
    if its an external service, its better to mock it – ruddra Oct 28 '19 at 11:50
  • 2
    You're gonna need to use mocking - that is, return a "fake" response whenever you call `client.post`. This way, instead of checking whether the service is up or not, your code checks whether the function was properly called, and whether your solutions for different "fake" responses (200, 500, 404, ...) work as you expect. Here are some resources about mocking: [1](https://realpython.com/testing-third-party-apis-with-mocks/), [2](https://stackoverflow.com/questions/15753390/how-can-i-mock-requests-and-the-response), [3](https://docs.python.org/3/library/unittest.mock.html). – jfaccioni Oct 28 '19 at 11:52

0 Answers0