2019-02-21 23:30:02 +00:00
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
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
|
2019-03-07 01:10:59 +00:00
|
|
|
login = context.device_login
|
|
|
|
device_id = login['uuid']
|
|
|
|
access_token = login['accessToken']
|
2019-05-24 14:07:27 +00:00
|
|
|
context.email_response = context.client.put(
|
2019-05-24 13:57:01 +00:00
|
|
|
'/v1/device/{uuid}/message'.format(uuid=device_id),
|
2019-02-21 23:30:02 +00:00
|
|
|
data=json.dumps(email_request),
|
2019-03-07 01:10:59 +00:00
|
|
|
content_type='application_json',
|
|
|
|
headers=dict(Authorization='Bearer {token}'.format(token=access_token))
|
2019-02-21 23:30:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@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()
|
|
|
|
|
|
|
|
|
2019-03-07 01:10:59 +00:00
|
|
|
@when('the email endpoint is called by a not allowed device')
|
2019-02-21 23:30:02 +00:00
|
|
|
@patch('smtplib.SMTP')
|
|
|
|
def send_email_invalid_device(context, email_client):
|
|
|
|
context.client_config['EMAIL_CLIENT'] = email_client
|
2019-05-24 14:07:27 +00:00
|
|
|
context.email_invalid_response = context.client.put(
|
2019-03-08 02:08:19 +00:00
|
|
|
'/v1/device/{uuid}/email'.format(uuid=str(uuid.uuid4())),
|
2019-02-21 23:30:02 +00:00
|
|
|
data=json.dumps(email_request),
|
|
|
|
content_type='application_json'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-03-07 01:10:59 +00:00
|
|
|
@then('401 status code should be returned by the email endpoint')
|
2019-02-21 23:30:02 +00:00
|
|
|
def validate_response_invalid_device(context):
|
|
|
|
response = context.email_invalid_response
|
2019-03-07 01:10:59 +00:00
|
|
|
assert_that(response.status_code, equal_to(HTTPStatus.UNAUTHORIZED))
|
2019-02-21 23:30:02 +00:00
|
|
|
email_client: MagicMock = context.client_config['EMAIL_CLIENT']
|
|
|
|
email_client.send_message.assert_not_called()
|