uncomment and fix device pairing tests

pull/191/head
Chris Veilleux 2019-07-06 15:38:27 -05:00
parent 3c535c0c05
commit 919d0a71fc
2 changed files with 68 additions and 48 deletions

View File

@ -1,8 +1,9 @@
#Feature: Pair a device
# Test the whole device pairing workflow
#
# Scenario: A valid login session is returned after the pairing is performed
# Given a device pairing code
# When a device is added to an account using the pairing code
# And device is activated
# Then a login session should be returned
Feature: Pair a device
Test the device pairing workflow
Scenario: Device activation
When a device requests a pairing code
And the device is added to an account using the pairing code
And the device is activated
Then the pairing code request is successful
And the device activation request is successful

View File

@ -1,55 +1,74 @@
import json
import uuid
from http import HTTPStatus
from behave import given, when, then
from behave import when, then
from hamcrest import assert_that, equal_to, has_key
from selene.util.cache import DEVICE_PAIRING_CODE_KEY, DEVICE_PAIRING_TOKEN_KEY
@given('a device pairing code')
ONE_MINUTE = 60
@when('a device requests a pairing code')
def get_device_pairing_code(context):
state = str(uuid.uuid4())
response = context.client.get('/v1/device/code?state={state}'.format(state=state))
context.pairing = json.loads(response.data)
context.state = state
context.state = str(uuid.uuid4())
response = context.client.get(
'/v1/device/code?state={state}'.format(state=context.state))
context.pairing_response = response
@when('a device is added to an account using the pairing code')
@when('the device is added to an account using the pairing code')
def add_device(context):
device = {
'name': 'home',
'wake_word_id': context.wake_word_id,
'text_to_speech_id': context.text_to_speech_id
}
response = context.client.post(
'/api/account/{account_id}/device?code={code}'
.format(account_id=context.account.id, code=context.pairing['code']),
data=json.dumps(device),
content_type='application_json')
context.device_id = response.data.decode('utf-8')
context.device_name = 'home'
"""Imitate the logic in the account API to pair a device"""
pairing_code_key = DEVICE_PAIRING_CODE_KEY.format(
pairing_code=context.pairing_response.json['code']
)
pairing_data = context.cache.get(pairing_code_key)
pairing_data = json.loads(pairing_data)
pairing_data.update(uuid=context.device_id)
context.cache.set_with_expiration(
key=DEVICE_PAIRING_TOKEN_KEY.format(
pairing_token=pairing_data['token']
),
value=json.dumps(pairing_data),
expiration=ONE_MINUTE
)
context.cache.delete(pairing_code_key)
@when('device is activated')
@when('the device is activated')
def activate_device(context):
activate = {
'token': context.pairing['token'],
'state': context.pairing['state'],
'platform': 'picroft',
'coreVersion': '18.8.0',
'enclosureVersion': '1.4.0'
}
response = context.client.post('/v1/device/activate', data=json.dumps(activate), content_type='application_json')
context.activate_device_response = response
login = json.loads(response.data)
context.device_access_token = login['accessToken']
activation_request = dict(
token=context.pairing_response.json['token'],
state=context.pairing_response.json['state'],
platform='picroft',
coreVersion='18.8.0',
enclosureVersion='1.4.0'
)
response = context.client.post(
'/v1/device/activate',
data=json.dumps(activation_request),
content_type='application/json'
)
context.activation_response = response
@then('a login session should be returned')
def validate_response(context):
assert_that(context.pairing['state'], equal_to(context.state))
login = json.loads(context.activate_device_response.data)
assert_that(login, has_key(equal_to('uuid')))
assert_that(login, has_key(equal_to('accessToken')))
assert_that(login, has_key(equal_to('refreshToken')))
assert_that(login, has_key(equal_to('expiration')))
assert_that(login, context.device_id, equal_to(login['uuid']))
@then('the pairing code request is successful')
def check_pairing_code_response(context):
response = context.pairing_response
assert_that(response.status_code, equal_to(HTTPStatus.OK))
assert_that(response.json, has_key('code'))
assert_that(response.json, has_key('token'))
assert_that(response.json['expiration'], equal_to(86400))
assert_that(response.json['state'], equal_to(context.state))
@then('the device activation request is successful')
def validate_activation_response(context):
response = context.activation_response
assert_that(response.status_code, equal_to(HTTPStatus.OK))
assert_that(response.json['uuid'], equal_to(context.device_id))
assert_that(response.json, has_key('accessToken'))
assert_that(response.json, has_key('refreshToken'))
assert_that(response.json['expiration'], equal_to(86400))