Clean up and adding tests for the device email endpoint
parent
577c470d90
commit
b68dabe0b9
|
@ -1,30 +0,0 @@
|
|||
from flask import Flask
|
||||
from flask_restful import Api
|
||||
from selene.api import JSON_MIMETYPE, output_json
|
||||
|
||||
from public_api.endpoints.device_email import DeviceEmailEndpoint
|
||||
from .endpoints.device import DeviceEndpoint
|
||||
from .endpoints.device_setting import DeviceSettingEndpoint
|
||||
from .endpoints.device_skill import DeviceSkillEndpoint
|
||||
from .endpoints.device_skills import DeviceSkillsEndpoint
|
||||
from selene.api.base_config import get_base_config
|
||||
|
||||
from .endpoints.device_subscription import DeviceSubscriptionEndpoint
|
||||
from .endpoints.open_weather_map import OpenWeatherMapEndpoint
|
||||
from .endpoints.wolfram_alpha import WolframAlphaEndpoint
|
||||
|
||||
public = Flask(__name__)
|
||||
public.config.from_object(get_base_config())
|
||||
public_api = Api(public)
|
||||
public_api.representations[JSON_MIMETYPE] = output_json
|
||||
|
||||
public_api.representations.update()
|
||||
|
||||
public_api.add_resource(DeviceSkillsEndpoint, '/device/<string:device_id>/skill')
|
||||
public_api.add_resource(DeviceSkillEndpoint, '/device/<string:device_id>/userSkill')
|
||||
public_api.add_resource(DeviceEndpoint, '/device/<string:device_id>')
|
||||
public_api.add_resource(DeviceSettingEndpoint, '/device/<string:device_id>/setting')
|
||||
public_api.add_resource(DeviceSubscriptionEndpoint, '/device/<string:device_id>/subscription')
|
||||
public_api.add_resource(WolframAlphaEndpoint, '/wa') # TODO: change this path in the API v2
|
||||
public_api.add_resource(OpenWeatherMapEndpoint, '/owm/<path:path>') # TODO: change this path in the API v2
|
||||
public_api.add_resource(DeviceEmailEndpoint, '/device/<string:device_id>/email')
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import smtplib
|
||||
|
||||
from flask import Flask
|
||||
|
||||
|
@ -24,6 +25,15 @@ public.config.from_object(get_base_config())
|
|||
public.config['GOOGLE_STT_KEY'] = os.environ['GOOGLE_STT_KEY']
|
||||
public.config['SELENE_CACHE'] = SeleneCache()
|
||||
|
||||
# Initializing email client
|
||||
host = os.environ['EMAIL_SERVICE_HOST']
|
||||
port = os.environ['EMAIL_SERVICE_PORT']
|
||||
user = os.environ['EMAIL_SERVICE_USER']
|
||||
password = os.environ['EMAIL_SERVICE_PASSWORD']
|
||||
email_client = smtplib.SMTP(host, port)
|
||||
email_client.login(user, password)
|
||||
public.config['EMAIL_CLIENT'] = email_client
|
||||
|
||||
public.response_class = SeleneResponse
|
||||
public.register_blueprint(selene_api)
|
||||
|
||||
|
|
|
@ -4,13 +4,12 @@ from http import HTTPStatus
|
|||
|
||||
from schematics import Model
|
||||
from schematics.types import StringType
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
|
||||
from selene.api import SeleneEndpoint
|
||||
import smtplib
|
||||
|
||||
from selene.data.device import DeviceRepository
|
||||
from selene.util.db import get_db_connection
|
||||
from email.message import EmailMessage
|
||||
|
||||
|
||||
class SendEmail(Model):
|
||||
|
@ -24,12 +23,7 @@ class DeviceEmailEndpoint(SeleneEndpoint):
|
|||
|
||||
def __init__(self):
|
||||
super(DeviceEmailEndpoint, self).__init__()
|
||||
host = os.environ['EMAIL_SERVICE_HOST']
|
||||
port = os.environ['EMAIL_SERVICE_PORT']
|
||||
user = os.environ['EMAIL_SERVICE_USER']
|
||||
password = os.environ['EMAIL_SERVICE_PASSWORD']
|
||||
self.email_client = smtplib.SMTP(host, port)
|
||||
self.email_client.login(user, password)
|
||||
self.email_client: smtplib.SMTP = self.config['EMAIL_CLIENT']
|
||||
|
||||
def post(self, device_id):
|
||||
payload = json.loads(self.request.data)
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
Feature: Send email to a to the account that owns a device
|
||||
Test the email endpoint
|
||||
|
||||
Scenario: an email payload is passed to the email endpoint
|
||||
Given a device pairing code
|
||||
When a device is added to an account using the pairing code
|
||||
And device is activated
|
||||
And an email message is sent to the email endpoint
|
||||
Then an email should be sent to the user's account that owns the device
|
||||
|
||||
Scenario: an email payload is passed to the the email endpoint using a nonexistent device
|
||||
When the email endpoint is called for a nonexistent device
|
||||
Then 204 status code should be returned
|
|
@ -0,0 +1,52 @@
|
|||
import json
|
||||
import uuid
|
||||
from email.message import EmailMessage
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from behave import when, then
|
||||
from hamcrest import assert_that, equal_to
|
||||
|
||||
email_request = dict(
|
||||
title='this is a test',
|
||||
sender='test@test.com',
|
||||
body='body message'
|
||||
)
|
||||
|
||||
|
||||
@when('an email message is sent to the email endpoint')
|
||||
@patch('smtplib.SMTP')
|
||||
def send_email(context, email_client):
|
||||
context.client_config['EMAIL_CLIENT'] = email_client
|
||||
context.email_response = context.client.post(
|
||||
'/device/{uuid}/email'.format(uuid=context.device_id),
|
||||
data=json.dumps(email_request),
|
||||
content_type='application_json'
|
||||
)
|
||||
|
||||
|
||||
@then('an email should be sent to the user\'s account that owns the device')
|
||||
def validate_response(context):
|
||||
response = context.email_response
|
||||
assert_that(response.status_code, equal_to(HTTPStatus.OK))
|
||||
email_client: MagicMock = context.client_config['EMAIL_CLIENT']
|
||||
email_client.send_message.assert_called()
|
||||
|
||||
|
||||
@when('the email endpoint is called for a nonexistent device')
|
||||
@patch('smtplib.SMTP')
|
||||
def send_email_invalid_device(context, email_client):
|
||||
context.client_config['EMAIL_CLIENT'] = email_client
|
||||
context.email_invalid_response = context.client.post(
|
||||
'/device/{uuid}/email'.format(uuid=str(uuid.uuid4())),
|
||||
data=json.dumps(email_request),
|
||||
content_type='application_json'
|
||||
)
|
||||
|
||||
|
||||
@then('204 status code should be returned')
|
||||
def validate_response_invalid_device(context):
|
||||
response = context.email_invalid_response
|
||||
assert_that(response.status_code, equal_to(HTTPStatus.NO_CONTENT))
|
||||
email_client: MagicMock = context.client_config['EMAIL_CLIENT']
|
||||
email_client.send_message.assert_not_called()
|
Loading…
Reference in New Issue