Skip to content

Commit

Permalink
Changes based on manual test of Flow (attr. list validation; scene_ke…
Browse files Browse the repository at this point in the history
…y for additional_data; data generation fixes)
  • Loading branch information
MartinHeinz committed Mar 24, 2019
1 parent 2b9d017 commit 71547a5
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 19 deletions.
9 changes: 7 additions & 2 deletions app/app/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,13 @@ def trigger_action():
@require_api_token()
def trigger_scene():
name_bi = request.args.get("name_bi", None)
additional_data = request.args.get("additional_data", None)
access_token = token_to_hash(request.headers.get("Authorization", ""))
user = User.get_by_access_token(access_token)

arg_check = check_missing_request_argument(
(name_bi, ACTION_NAME_BI_MISSING_ERROR_MSG))
(name_bi, ACTION_NAME_BI_MISSING_ERROR_MSG),
(additional_data, ADDITIONAL_DATA_MISSING_ERROR_MSG))
if arg_check is not True:
return arg_check

Expand All @@ -424,7 +426,10 @@ def trigger_scene():
return http_json_response(False, 400, **{"error": UNAUTHORIZED_USER_SCENE_ERROR_MSG})

for ac in sc.actions:
payload = create_payload(user.mqtt_creds.username, {"action": ac.name.decode("utf-8")})
payload = create_payload(user.mqtt_creds.username, {
"action": ac.name.decode("utf-8"),
"additional_data": additional_data
})
topic = format_topic(user.mqtt_creds.username, ac.device.mqtt_creds.username)
client.publish(topic, payload)

Expand Down
2 changes: 1 addition & 1 deletion app/app/attribute_authority/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def keygen():
return http_json_response(False, 400, **{"error": INCORRECT_RECEIVER_ID_ERROR_MSG})

attr_list = parse_attr_list(attr_list)
if not attr_list or not is_valid(attr_list, data_owner.id):
if not attr_list:
return http_json_response(False, 400, **{"error": INVALID_ATTR_LIST_ERROR_MSG})

serialized_private_key = create_private_key(data_owner.master_keypair.data_master,
Expand Down
3 changes: 2 additions & 1 deletion app/app/generate_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def create_abe_key_pair():
access_token = random_string()
# noinspection PyArgumentList
aa_user = AttrAuthUser(name=user.name,
id=i,
access_token=token_to_hash(access_token),
access_token_update=random_date(d1, d2),
api_username=user.name)
Expand All @@ -252,7 +253,7 @@ def create_abe_key_pair():
private_key = create_private_key(keypairs[i][1], keypairs[i][0], attr_list)
private_key_obj = PrivateKey(data=private_key,
challenger_id=aa_user.id,
user_id=i+1,
user_id=i+1 if i+1 < len(users) else 0,
device_id=user.owned_devices[0].id,
attributes=[Attribute(value=attr) for attr in attr_list])

Expand Down
5 changes: 4 additions & 1 deletion client/device/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ def process_action(data):
data = json.loads(data)
doc = get_user_data()

additional_data = decrypt_using_fernet_hex(doc["shared_key"], data["additional_data"]).decode()
try:
additional_data = decrypt_using_fernet_hex(doc["shared_key"], data["additional_data"]).decode()
except Exception:
additional_data = decrypt_using_fernet_hex(doc["scene_key"], data["additional_data"]).decode()
if additional_data != "real":
return
action_name = decrypt_using_fernet_hex(doc["action:name"], data["action"])
Expand Down
22 changes: 19 additions & 3 deletions client/user/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def init_global_keys():
table = get_tinydb_table(path, 'global')
table.upsert({
'bi_key': key_to_hex(os.urandom(32)),
}, where('bi_key').exists())
'scene_key': key_to_hex(os.urandom(32)),
}, where('bi_key').exists() & where('scene_key').exists())


@user.command()
Expand Down Expand Up @@ -286,6 +287,7 @@ def _trigger_action(device_id, device_name, name, token, real):
data["additional_data"] = "fake"

data["additional_data"] = encrypt_using_fernet_hex(get_shared_key_by_device_id(path, device_id), data["additional_data"]).decode()
# TODO Authorized user needs shared_key
return requests.get(URL_TRIGGER_ACTION, headers={"Authorization": token}, params=data, verify=VERIFY_CERTS)


Expand Down Expand Up @@ -313,10 +315,17 @@ def schedule_fake_actions(device_id, device_name, start, end, number, action_nam
@user.command()
@click.argument('name')
@click.option('--token', envvar='ACCESS_TOKEN')
def trigger_scene(name, token):
@click.option('--real/--fake', default=True)
def trigger_scene(name, token, real):
data = {
"name_bi": blind_index(get_global_bi_key(), name)
}
if real:
data["additional_data"] = "real"
else:
data["additional_data"] = "fake"

data["additional_data"] = encrypt_using_fernet_hex(key_to_hex(get_global_scene_key()), data["additional_data"]).decode()
r = requests.get(URL_TRIGGER_SCENE, headers={"Authorization": token}, params=data, verify=VERIFY_CERTS)
click.echo(r.content.decode('unicode-escape'))

Expand Down Expand Up @@ -544,6 +553,7 @@ def send_column_keys(user_id, device_id):
payload_keys["device:name"] = fernet_key.encrypt(hex_to_key(doc["device:name"])).decode()
payload_keys["device:status"] = fernet_key.encrypt(hex_to_key(doc["device:status"])).decode()
payload_keys["bi_key"] = fernet_key.encrypt(hex_to_key(doc["bi_key"])).decode()
payload_keys["scene_key"] = fernet_key.encrypt(get_global_scene_key()).decode()

doc = {**doc, **keys}
table.upsert(doc, Query().device_id == device_id)
Expand Down Expand Up @@ -597,7 +607,7 @@ def attr_auth_retrieve_private_keys(token):
@click.argument('abe_pk', type=click.Path(exists=True))
@click.argument('bi_key')
@click.option('--token', envvar='ACCESS_TOKEN')
def setup_authorized_device(device_id, abe_pk, bi_key, token):
def setup_authorized_device(device_id, abe_pk, bi_key, token): # TODO Add shared_key here?
r = requests.post(AA_URL_SK_RETRIEVE, headers={"Authorization": token}, verify=VERIFY_CERTS)
content = json.loads(r.content.decode('unicode-escape'))
abe_sk = next((key for key in content['private_keys'] if str(key["device_id"]) == device_id), None)
Expand Down Expand Up @@ -989,6 +999,12 @@ def get_global_bi_key():
return hex_to_key(doc_global["bi_key"])


def get_global_scene_key():
table = get_tinydb_table(path, 'global')
doc_global = table.all()[0]
return hex_to_key(doc_global["scene_key"])


def get_device_bi_key(device_id):
doc = search_tinydb_doc(path, 'device_keys', Query().device_id == str(device_id))
return hex_to_key(doc["bi_key"])
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def device_col_keys():
},
"device_data:tid": "9692e6525c19e6fa37978626606534015cd120816a28b501bebec142d86002b2",
"device:name": "ae89ebdb00d48b6e2aca3218213888aff3af9915831b9cdde8f82b709fd8802e",
"scene_key": "999d1785bab02131da22f440016c9568a059c73266dbac8964f27fbd0af6bee8",
}
return data

Expand Down
2 changes: 1 addition & 1 deletion tests/test_abe.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_keygen_invalid_attr_list(client, attr_auth_access_token_one):

data = {
"access_token": attr_auth_access_token_one,
"attr_list": "15-GUEST 15",
"attr_list": "15-GUEST #$%^&* 15",
"receiver_id": "2",
"device_id": "1"
}
Expand Down
16 changes: 13 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,30 @@ def test_api_trigger_action(client, app_and_ctx, access_token):


def test_api_trigger_scene(client, app_and_ctx, access_token, access_token_two):

data = {
"access_token": access_token_two,
"name_bi": '0b0a367318926df75879294f1520905ba72d8f1bebe64865645a7e108bfaf3e4'
}
assert_got_error_from_get(client, '/api/scene/trigger', data, 400, ADDITIONAL_DATA_MISSING_ERROR_MSG)

data = {
"access_token": access_token,
"additional_data": 'gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==',
"name_bi": "something"
}
assert_got_error_from_get(client, '/api/scene/trigger', data, 400, INVALID_SCENE_BI_ERROR_MSG)

data = {
"access_token": access_token,
"additional_data": 'gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==',
"name_bi": '0b0a367318926df75879294f1520905ba72d8f1bebe64865645a7e108bfaf3e4', # other user
}
assert_got_error_from_get(client, '/api/scene/trigger', data, 400, UNAUTHORIZED_USER_SCENE_ERROR_MSG)

data = {
"access_token": access_token_two,
"additional_data": 'gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==',
"name_bi": '0b0a367318926df75879294f1520905ba72d8f1bebe64865645a7e108bfaf3e4',
}

Expand All @@ -528,9 +538,9 @@ def test_api_trigger_scene(client, app_and_ctx, access_token, access_token_two):
assert_got_data_from_get(client, '/api/scene/trigger', data)

expected_calls = [
call('u:2/d:45/', '"{"\\"action\\"": "\\"gAAAAABcYAJr_P_8E4S0nWTFU-uyGk8t3MDexB5LzNGHKB6rd_pwKwY41bTMYYqAvuxcrCp3BBYwh7FI4F6fkswMM5JAFMcmqQ==\\"", "\\"user_id\\"": "\\"u:2\\""}"'),
call('u:2/d:34/', '"{"\\"action\\"": "\\"gAAAAABcYAJs8wCzyfEdHGO3TUjK-EeSxD-wFEgCGY8XF_kExmttrzUjM-YFKUaySrc8yLJG8UXe2zLtGr7LPAl5xyW756XscA==\\"", "\\"user_id\\"": "\\"u:2\\""}"'),
call('u:2/d:37/', '"{"\\"action\\"": "\\"gAAAAABcYAJsJHci8zzKE232PYIX-Hw74lYNEt_f7EceuroDqp0pWHGD96_baLE2tlQeFlFRenmpmFwtBZQbLIyBfAPaBXnl-A==\\"", "\\"user_id\\"": "\\"u:2\\""}"')
call('u:2/d:45/', '"{"\\"action\\"": "\\"gAAAAABcYAJr_P_8E4S0nWTFU-uyGk8t3MDexB5LzNGHKB6rd_pwKwY41bTMYYqAvuxcrCp3BBYwh7FI4F6fkswMM5JAFMcmqQ==\\"", "\\"additional_data\\"": "\\"gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==\\"", "\\"user_id\\"": "\\"u:2\\""}"'),
call('u:2/d:34/', '"{"\\"action\\"": "\\"gAAAAABcYAJs8wCzyfEdHGO3TUjK-EeSxD-wFEgCGY8XF_kExmttrzUjM-YFKUaySrc8yLJG8UXe2zLtGr7LPAl5xyW756XscA==\\"", "\\"additional_data\\"": "\\"gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==\\"", "\\"user_id\\"": "\\"u:2\\""}"'),
call('u:2/d:37/', '"{"\\"action\\"": "\\"gAAAAABcYAJsJHci8zzKE232PYIX-Hw74lYNEt_f7EceuroDqp0pWHGD96_baLE2tlQeFlFRenmpmFwtBZQbLIyBfAPaBXnl-A==\\"", "\\"additional_data\\"": "\\"gAAAAABcikpQSsh7iACV6pAFMaldncaSrA9rj3iUh-7ejFnvXw1Uzcodf5Gf7FtZTU39R3L65nd1RzExvF9kMU1t_YwG2FpdMA==\\"", "\\"user_id\\"": "\\"u:2\\""}"')
]

assert all(v in expected_calls for v in publish.mock_calls)
Expand Down
29 changes: 22 additions & 7 deletions tests/test_client/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_send_column_keys(runner, access_token, reset_tiny_db, bi_key, aa_public
})
insert_into_tinydb(cmd.path, "credentials", {"broker_id": "4", "broker_password": 'test_pass'})
insert_into_tinydb(cmd.path, "aa_keys", {"public_key": aa_public_key})
runner.invoke(cmd.init_global_keys)

result = runner.invoke(cmd.send_column_keys, [user_id, device_id])
assert "Data published" in result.output
Expand Down Expand Up @@ -509,17 +510,18 @@ def test_save_column_keys(runner, reset_tiny_db, col_keys):
table.update(set('shared_key', "aefe715635c3f35f7c58da3eb410453712aaf1f8fd635571aa5180236bb21acc"), where('integrity').exists())
table.update(set('id', 1), where('integrity').exists())

data_wrong_user = f'{{"device_data:data": "{plaintext_abe_data}", "device_data:num_data": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "action:name": "gAAAAABcRHibSiR3cHtaSUSk1ipKP_7csl3xTCd4J-JesU8GPlC2iwfblksE3kvuV3U2mAYqiYe3UuYw04JPbYDYaFePY-YTUAzie3OCRzwuMTE6tE9UBJtJ8wUNJSctZnrvSi0rcPzQ", "device:name": "gAAAAABcRHib0mxfmRE3mg4ALX3XPjP7ZuVQ69NiRdebiNCE-40wZuzzNV1krKcnZeRZVWXwYf4xjYLNNygY-kbbgxltBWNJ5rLanpBIqTeoq8uI9up1bZ_vFFCiGPIjHTpYkMnF5XIN", "device:status": "gAAAAABcRHiboBSiAuKLxvSqS1yu4vOR8FlqGBOnzJSQ85e5UShmQ9avtLAXx_w9fKad2xILHWbi_uFywJML8ukoDGB7iiHkLT39iOnrUCAQHFyOdFERixgl-iFHMji-S1YfGKGwxRIU", "device_data:added": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "scene:name": "gAAAAABcRHibVgsVHRls8IGj95TdFKraKbGfyf_TvDzjg0KV_vu-HawiISBzRaxwrFV_QHI5jA73CTM2dF4ePENaMe0QtIJljtqCBUSRhoQideCy0JL4hDAIJUzpGXFK5RMC2fJHUJ17", "scene:description": "gAAAAABcRHib1iH0Bs9sHff-dt7FY9XOUDzARN-mwaq7eI7iLYYwtmBcMkB3T5ChNnoNWhIRLnh_lQLmvCT_itBvjoIHydBVdIcTjzsyHcTMBUdlxPmohokOjunxdMSCY0B48-pYqzsn", "user_id": 2}}'
data_wrong_user = f'{{"device_data:data": "{plaintext_abe_data}", "scene_key": "gAAAAABclmLFdWPbbYJQx0cySvGNwVIMng-Ku-HXVg_030HpHXbVyBSKboJjujjLEEb6rt57eN4C16t0bP5_5PiiUBeSYJ4giYKy2JLY4dDx2Yl7ME0QICHvuffEM3JVQpdsjJZFsWLl", "device_data:num_data": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "action:name": "gAAAAABcRHibSiR3cHtaSUSk1ipKP_7csl3xTCd4J-JesU8GPlC2iwfblksE3kvuV3U2mAYqiYe3UuYw04JPbYDYaFePY-YTUAzie3OCRzwuMTE6tE9UBJtJ8wUNJSctZnrvSi0rcPzQ", "device:name": "gAAAAABcRHib0mxfmRE3mg4ALX3XPjP7ZuVQ69NiRdebiNCE-40wZuzzNV1krKcnZeRZVWXwYf4xjYLNNygY-kbbgxltBWNJ5rLanpBIqTeoq8uI9up1bZ_vFFCiGPIjHTpYkMnF5XIN", "device:status": "gAAAAABcRHiboBSiAuKLxvSqS1yu4vOR8FlqGBOnzJSQ85e5UShmQ9avtLAXx_w9fKad2xILHWbi_uFywJML8ukoDGB7iiHkLT39iOnrUCAQHFyOdFERixgl-iFHMji-S1YfGKGwxRIU", "device_data:added": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "scene:name": "gAAAAABcRHibVgsVHRls8IGj95TdFKraKbGfyf_TvDzjg0KV_vu-HawiISBzRaxwrFV_QHI5jA73CTM2dF4ePENaMe0QtIJljtqCBUSRhoQideCy0JL4hDAIJUzpGXFK5RMC2fJHUJ17", "scene:description": "gAAAAABcRHib1iH0Bs9sHff-dt7FY9XOUDzARN-mwaq7eI7iLYYwtmBcMkB3T5ChNnoNWhIRLnh_lQLmvCT_itBvjoIHydBVdIcTjzsyHcTMBUdlxPmohokOjunxdMSCY0B48-pYqzsn", "user_id": 2}}'
result = runner.invoke(device_cmd.save_column_keys, [data_wrong_user])
assert "This command is only available for device owner." in result.output

data = f'{{"device_data:data": "{plaintext_abe_data}", "device_data:num_data": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "action:name": "gAAAAABcRHibSiR3cHtaSUSk1ipKP_7csl3xTCd4J-JesU8GPlC2iwfblksE3kvuV3U2mAYqiYe3UuYw04JPbYDYaFePY-YTUAzie3OCRzwuMTE6tE9UBJtJ8wUNJSctZnrvSi0rcPzQ", "device:name": "gAAAAABcRHib0mxfmRE3mg4ALX3XPjP7ZuVQ69NiRdebiNCE-40wZuzzNV1krKcnZeRZVWXwYf4xjYLNNygY-kbbgxltBWNJ5rLanpBIqTeoq8uI9up1bZ_vFFCiGPIjHTpYkMnF5XIN", "device:status": "gAAAAABcRHiboBSiAuKLxvSqS1yu4vOR8FlqGBOnzJSQ85e5UShmQ9avtLAXx_w9fKad2xILHWbi_uFywJML8ukoDGB7iiHkLT39iOnrUCAQHFyOdFERixgl-iFHMji-S1YfGKGwxRIU", "device_data:added": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "scene:name": "gAAAAABcRHibVgsVHRls8IGj95TdFKraKbGfyf_TvDzjg0KV_vu-HawiISBzRaxwrFV_QHI5jA73CTM2dF4ePENaMe0QtIJljtqCBUSRhoQideCy0JL4hDAIJUzpGXFK5RMC2fJHUJ17", "scene:description": "gAAAAABcRHib1iH0Bs9sHff-dt7FY9XOUDzARN-mwaq7eI7iLYYwtmBcMkB3T5ChNnoNWhIRLnh_lQLmvCT_itBvjoIHydBVdIcTjzsyHcTMBUdlxPmohokOjunxdMSCY0B48-pYqzsn", "user_id": 1}}'
data = f'{{"device_data:data": "{plaintext_abe_data}", "scene_key": "gAAAAABclmLFdWPbbYJQx0cySvGNwVIMng-Ku-HXVg_030HpHXbVyBSKboJjujjLEEb6rt57eN4C16t0bP5_5PiiUBeSYJ4giYKy2JLY4dDx2Yl7ME0QICHvuffEM3JVQpdsjJZFsWLl", "device_data:num_data": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "action:name": "gAAAAABcRHibSiR3cHtaSUSk1ipKP_7csl3xTCd4J-JesU8GPlC2iwfblksE3kvuV3U2mAYqiYe3UuYw04JPbYDYaFePY-YTUAzie3OCRzwuMTE6tE9UBJtJ8wUNJSctZnrvSi0rcPzQ", "device:name": "gAAAAABcRHib0mxfmRE3mg4ALX3XPjP7ZuVQ69NiRdebiNCE-40wZuzzNV1krKcnZeRZVWXwYf4xjYLNNygY-kbbgxltBWNJ5rLanpBIqTeoq8uI9up1bZ_vFFCiGPIjHTpYkMnF5XIN", "device:status": "gAAAAABcRHiboBSiAuKLxvSqS1yu4vOR8FlqGBOnzJSQ85e5UShmQ9avtLAXx_w9fKad2xILHWbi_uFywJML8ukoDGB7iiHkLT39iOnrUCAQHFyOdFERixgl-iFHMji-S1YfGKGwxRIU", "device_data:added": "gAAAAABcRHibuWXtMvF7XgSN7FR-cHyNl2eDb_HHPCuTjqtdMN2VxxZnSxGCjkoJxRNIGMcpBW-z4n1wynPoCCb1VanmH3EukMPwpf7Vwk9WytkNR9h51ApyGt1QEkaj_JF2A5jKu-vw", "scene:name": "gAAAAABcRHibVgsVHRls8IGj95TdFKraKbGfyf_TvDzjg0KV_vu-HawiISBzRaxwrFV_QHI5jA73CTM2dF4ePENaMe0QtIJljtqCBUSRhoQideCy0JL4hDAIJUzpGXFK5RMC2fJHUJ17", "scene:description": "gAAAAABcRHib1iH0Bs9sHff-dt7FY9XOUDzARN-mwaq7eI7iLYYwtmBcMkB3T5ChNnoNWhIRLnh_lQLmvCT_itBvjoIHydBVdIcTjzsyHcTMBUdlxPmohokOjunxdMSCY0B48-pYqzsn", "user_id": 1}}'
runner.invoke(device_cmd.save_column_keys, [data])

table = get_tinydb_table(device_cmd.path, 'users')
doc = table.get(Query().id == 1)
assert "action:name" in doc
assert len(doc) == 11
assert "scene_key" in doc
assert len(doc) == 12
fernet_key = hex_to_fernet(doc["device:status"])
assert isinstance(fernet_key, Fernet)
cipher = hex_to_ope(doc["device_data:added"])
Expand Down Expand Up @@ -651,12 +653,14 @@ def test_create_scene(runner, access_token, reset_tiny_db):
def test_init_global_keys(runner, reset_tiny_db):
runner.invoke(cmd.init_global_keys)
table = get_tinydb_table(cmd.path, 'global')
doc = table.search(where('bi_key').exists())
doc = table.search(where('bi_key').exists() & where('scene_key').exists())
assert "bi_key" in doc[0]
assert "scene_key" in doc[0]

runner.invoke(cmd.init_global_keys) # Insert again
doc = table.search(where('bi_key').exists())
doc = table.search(where('bi_key').exists() & where('scene_key').exists())
assert "bi_key" in doc[0]
assert "scene_key" in doc[0]


@pytest.mark.parametrize('reset_tiny_db', [cmd.path], indirect=True)
Expand Down Expand Up @@ -811,9 +815,14 @@ def test_schedule_fake_actions(m_add_job, m_start, runner, access_token):
def test_trigger_scene(runner, access_token_two, col_keys, bi_key, reset_tiny_db):
name = "Home"
insert_into_tinydb(cmd.path, 'device_keys', col_keys)
insert_into_tinydb(cmd.path, 'global', {"bi_key": bi_key})
insert_into_tinydb(cmd.path, 'global', {
"bi_key": bi_key,
"scene_key": "999d1785bab02131da22f440016c9568a059c73266dbac8964f27fbd0af6bee8"
})
result = runner.invoke(cmd.trigger_scene, [name, '--token', access_token_two])
assert "\"success\": true" in result.output
result = runner.invoke(cmd.trigger_scene, [name, '--token', access_token_two, "--fake"])
assert "\"success\": true" in result.output


def test_authorize_user(runner, access_token_two):
Expand Down Expand Up @@ -1309,7 +1318,8 @@ def test_process_action(runner, reset_tiny_db, col_keys):
insert_into_tinydb(device_cmd.path, 'users', {
"id": 1,
"action:name": "a70c6a23f6b0ef9163040f4cc02819c22d7e35de6469672d250519077b36fe4d",
"shared_key": "d3e4a9c3aec8386eaa00442e040c21caf483c320f4ecf4a0a485fdcf47bd4251"
"shared_key": "d3e4a9c3aec8386eaa00442e040c21caf483c320f4ecf4a0a485fdcf47bd4251",
"scene_key": "999d1785bab02131da22f440016c9568a059c73266dbac8964f27fbd0af6bee8"
})

result = runner.invoke(device_cmd.process_action, [payload])
Expand All @@ -1320,6 +1330,11 @@ def test_process_action(runner, reset_tiny_db, col_keys):
result = runner.invoke(device_cmd.process_action, [payload])
assert result.output == ""

payload = '{"user_id": "u:1", "action": "gAAAAABcXcF9yNe9emKXALJImsb7v4meic8cR6YnEulQSi8xOxF8d33scDotxPKQBTC80r-QolW2mRroUZOfLuqAqr20Z5333A==", "additional_data": "gAAAAABclm-Ca81Wrd0d4LcL9ZysHvlQzor923iDwDiO8hnMdQJw9M3buS4xlv3lLEVqesESfdxOHtrUkjLgP0V6f4KYpdK6SQ=="}'
# action: b'On' encrypted with col_keys["action:name"], additional_data: "real" encrypted with device_col_keys["scene_key"]
result = runner.invoke(device_cmd.process_action, [payload])
assert "On" in result.output


@pytest.mark.parametrize('reset_tiny_db', [device_cmd.path], indirect=True)
def test_get_next_tid(reset_tiny_db):
Expand Down

0 comments on commit 71547a5

Please sign in to comment.