def test_message_upsert_raise_api_error_if_save_failed(self, mock_save): # Arrange mock_save.side_effect = Exception() # Act # Assert with self.assertRaises(exceptions.ApiError): contact_message_api.upsert(self.mock_message)
def contact(request): """Contact form Parameters: request: Returns: Http response """ if request.method == "POST": contact_form = ContactForm(request.POST) if contact_form.is_valid(): # Call the API contact_message = ContactMessage( name=request.POST["name"], email=request.POST["email"], content=request.POST["message"], ) contact_message_api.upsert(contact_message) messages.add_message( request, messages.INFO, "Your message has been sent to the administrator.", ) return redirect(reverse("core_main_app_homepage")) else: contact_form = ContactForm() return render( request, "core_website_app/user/contact.html", context={"contact_form": contact_form}, )
def test_contact_message_send_mail_admins(self, mock_save): # Arrange mock_save.return_value = self.contact_message # Act contact_message_api.upsert(self.contact_message) # Assert self.assertEqual(len(mail.outbox[0].to), 2) self.assertEqual(mail.outbox[0].to, ["*****@*****.**", "*****@*****.**"])
def test_contact_message_send_mail(self, mock_save): # Arrange mock_save.return_value = self.contact_message # Act contact_message_api.upsert(self.contact_message) # Assert self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, "[Django] New Contact Message")
def test_message_upsert_return_message(self, mock_save): # Arrange mock_save.return_value = self.mock_message # Act result = contact_message_api.upsert(self.mock_message) # Assert self.assertIsInstance(result, ContactMessage)
def post(request): """ Create a message Parameters: { "name": "name", "message": "message", "email": "email" } Args: request: HTTP request Returns: - code: 200 content: Contact message - code: 400 content: Validation error / missing parameters """ try: # Get parameters message_name = request.DATA['name'] message_email = request.DATA['email'] message_content = request.DATA['message'] try: # Create the message and insert it contact_message_object = ContactMessage( name=message_name, email=message_email, content=message_content ) new_contact_message = contact_message_api.upsert(contact_message_object) # Serialize the message serializer = ContactMessageSerializer(new_contact_message) if serializer.is_valid(): return Response(serializer.data, status=status.HTTP_200_OK) else: content = {'message': 'Serialization failed'} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)
def post(request): """ Post a message :param request: :return: """ try: # Get parameters message_name = request.DATA['name'] message_email = request.DATA['email'] message_content = request.DATA['message'] try: # Create the message and insert it contact_message_object = ContactMessage(name=message_name, email=message_email, content=message_content) new_contact_message = contact_message_api.upsert( contact_message_object) # Serialize the message serializer = ContactMessageSerializer(new_contact_message) if serializer.is_valid(): return Response(serializer.data, status=status.HTTP_200_OK) else: content = {'message': 'Serialization failed'} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as api_exception: content = {'message': api_exception.message} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Exception as e: content = {'message': 'Expected parameters not provided.'} logger.exception(e.message) return Response(content, status=status.HTTP_400_BAD_REQUEST)