selene-backend/api/public/tests/features/steps/device_pairing.py

56 lines
1.9 KiB
Python
Raw Normal View History

import json
import uuid
from behave import given, when, then
from hamcrest import assert_that, equal_to, has_key
@given('a device pairing code')
def get_device_pairing_code(context):
state = str(uuid.uuid4())
2019-03-08 02:08:19 +00:00
response = context.client.get('/v1/device/code?state={state}'.format(state=state))
context.pairing = json.loads(response.data)
context.state = state
@when('a 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
}
2019-02-20 23:22:22 +00:00
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'
@when('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'
}
2019-03-08 02:08:19 +00:00
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']
@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)
2019-02-20 22:13:04 +00:00
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']))