Skip to content

Commit

Permalink
updating tox
Browse files Browse the repository at this point in the history
  • Loading branch information
sabuhish committed Jan 20, 2021
1 parent 0b20537 commit c69e531
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 17 deletions.
8 changes: 6 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ uvicorn app:app --port 8000 --reload
```



NOTE: Be sure to merge the latest from "upstream" before making a pull request!
`


### Testing
------------

For testing use **tox**
72 changes: 72 additions & 0 deletions examples/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form
from starlette.responses import JSONResponse
from .templates import html, template,bulkmail
from .schema import EmailSchema, EmailStr
from starlette.requests import Request
from fastapi_mail import FastMail, MessageSchema,ConnectionConfig


conf = ConnectionConfig(
MAIL_USERNAME = "your_username",
MAIL_PASSWORD = "strong_password",
MAIL_FROM = "[email protected]",
MAIL_PORT = 587,
MAIL_SERVER = "your mail server",
MAIL_TLS = True,
MAIL_SSL = False
)

app = FastAPI()

#test email standart sending mail
@app.post("/email")
async def simple_send(email: EmailSchema) -> JSONResponse:

message = MessageSchema(
subject="Fastapi-Mail module",
recipients=email.dict().get("email"),
body=html,
subtype="html"
)

fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})



#this mail sending using starlettes background tasks, faster than the above one
@app.post("/emailbackground")
async def send_in_background(background_tasks: BackgroundTasks,email: EmailSchema) -> JSONResponse:

message = MessageSchema(
subject="Fastapi mail module",
recipients=email.dict().get("email"),
body="Simple background task ",
)

fm = FastMail(conf)

background_tasks.add_task(fm.send_message,message)

return JSONResponse(status_code=200, content={"message": "email has been sent"})



#an example of sending attachments
@app.post("/file")
async def send_file(background_tasks: BackgroundTasks,file: UploadFile = File(...),email:EmailStr = Form(...)) -> JSONResponse:

message = MessageSchema(
subject="Fastapi mail module",
recipients=[email],
body="Simple background task ",
attachments=[file]
)

fm = FastMail(conf)

background_tasks.add_task(fm.send_message,message)

return JSONResponse(status_code=200, content={"message": "email has been sent"})

4 changes: 3 additions & 1 deletion fastapi_mail/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from mimetypes import MimeTypes
from fastapi_mail.errors import WrongFile


class MultipartSubtypeEnum(Enum):
'''
for more info about Multipart subtypes visit: https://en.wikipedia.org/wiki/MIME#Multipart_subtypes
'''
mixed = "mixed"
digest = "digest"
alternative = "alternative"
Expand Down
19 changes: 10 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import pytest
import fakeredis.aioredis
from pytest_mock import mock

from fastapi_mail.email_utils import DefaultChecker
from fastapi_mail.config import ConnectionConfig

Expand All @@ -29,14 +27,17 @@ async def redis_checker(scope="redis_config"):
def mail_config():
env = {
"MAIL_USERNAME": "[email protected]",
"MAIL_PASSWORD": "strong password",
"MAIL_PASSWORD":"strong",
"MAIL_FROM": "[email protected]",
"MAIL_PORT": "587",
"MAIL_SERVER": "smtp.gmail.com",
"MAIL_TLS": "True",
"MAIL_SSL": "False",
"SUPPRESS_SEND": "1",
"USE_CREDENTIALS": "True"
"MAIL_FROM_NAME": "example",
"MAIL_PORT": 25,
"MAIL_SERVER": "localhost",
"MAIL_TLS": False,
"MAIL_SSL": False,
"MAIL_DEBUG": 0,
"SUPPRESS_SEND": 1,
"USE_CREDENTIALS": False,
"TEMPLATE_FOLDER": None,
}

yield env
18 changes: 18 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from fastapi_mail import FastMail, MessageSchema,ConnectionConfig

@pytest.mark.asyncio
async def test_connection(mail_config):
message = MessageSchema(
subject="test subject",
recipients=["[email protected]"],
body="test",
subtype="plain"
)
conf = ConnectionConfig(**mail_config)

fm = FastMail(conf)
await fm.send_message(message)



8 changes: 7 additions & 1 deletion tests/test_fastapi_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
def test_configuration(mail_config):
conf = ConnectionConfig(**mail_config)
assert conf.MAIL_USERNAME == "[email protected]"
assert conf.MAIL_PORT == 587
assert conf.MAIL_PORT == 25






15 changes: 11 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py{36,37,38}
envlist = py{36,37,38,39}
; skipsdist = True
recreate = True

Expand All @@ -16,20 +16,27 @@ basepython =
py36: python3.6
py37: python3.7
py38: python3.8
py39: python3.9

platform = linux
usedevelop = true
commands_pre =
pip install -U setuptools

pip install tox-venv
deps=
-r{toxinidir}/tests/requirements.testing.txt

extras =
testing
extra

commands = {posargs:pytest}
; whitelist_externals = {homedir}/fastapi-mail/.tox/py39*
; allowlist_externals = make
; /bin/bash

commands =
; sudo python -m smtpd -n -c DebuggingServer localhost:25
{posargs:pytest}



; [testenv:docs]
Expand Down

0 comments on commit c69e531

Please sign in to comment.