如何测试访问外部服务的api?

时间:2019-10-28 11:44:35

标签: python django django-rest-framework

我正在创建一个接收一些参数并使用公司服务器发送电子邮件的应用程序,当我使用邮递员进行测试时,它可以工作,但是使用相同的参数执行测试会发生错误,并且我无法调试,仅出现500个错误,我可以看到测试django服务器日志吗?

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)

我使用以下命令运行测试:

python3 -m pytest --cov=. && \
python3 -m coverage xml -i

0 个答案:

没有答案