|
| 1 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +import sys |
| 16 | +import time |
| 17 | + |
| 18 | +from google.cloud import pubsub |
| 19 | + |
| 20 | +# Add manager for bootstrapping device registry / device for testing |
| 21 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'manager')) # noqa |
| 22 | +import manager |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +import cloudiot_http_example |
| 27 | + |
| 28 | + |
| 29 | +cloud_region = 'us-central1' |
| 30 | +device_id_template = 'test-device-{}' |
| 31 | +es_cert_path = 'resources/ec_public.pem' |
| 32 | +rsa_cert_path = 'resources/rsa_cert.pem' |
| 33 | +rsa_private_path = 'resources/rsa_private.pem' |
| 34 | +topic_id = 'test-device-events-{}'.format(int(time.time())) |
| 35 | + |
| 36 | +project_id = os.environ['GCLOUD_PROJECT'] |
| 37 | +service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] |
| 38 | + |
| 39 | +pubsub_topic = 'projects/{}/topics/{}'.format(project_id, topic_id) |
| 40 | +registry_id = 'test-registry-{}'.format(int(time.time())) |
| 41 | + |
| 42 | +_BASE_URL = 'https://cloudiot-device.googleapis.com/v1beta1' |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope='module') |
| 46 | +def test_topic()
F438
: |
| 47 | + topic = manager.create_iot_topic(project_id, topic_id) |
| 48 | + |
| 49 | + yield topic |
| 50 | + |
| 51 | + pubsub_client = pubsub.PublisherClient() |
| 52 | + topic_path = pubsub_client.topic_path(project_id, topic_id) |
| 53 | + pubsub_client.delete_topic(topic_path) |
| 54 | + |
| 55 | + |
| 56 | +def test_event(test_topic, capsys): |
| 57 | + device_id = device_id_template.format('RSA256') |
| 58 | + manager.open_registry( |
| 59 | + service_account_json, project_id, cloud_region, pubsub_topic, |
| 60 | + registry_id) |
| 61 | + |
| 62 | + manager.create_rs256_device( |
| 63 | + service_account_json, project_id, cloud_region, registry_id, |
| 64 | + device_id, rsa_cert_path) |
| 65 | + |
| 66 | + manager.get_device( |
| 67 | + service_account_json, project_id, cloud_region, registry_id, |
| 68 | + device_id) |
| 69 | + |
| 70 | + jwt_token = cloudiot_http_example.create_jwt( |
| 71 | + project_id, 'resources/rsa_private.pem', 'RS256') |
| 72 | + |
| 73 | + print(cloudiot_http_example.publish_message( |
| 74 | + 'hello', 'event', _BASE_URL, project_id, cloud_region, |
| 75 | + registry_id, device_id, jwt_token)) |
| 76 | + |
| 77 | + manager.get_state( |
| 78 | + service_account_json, project_id, cloud_region, registry_id, |
| 79 | + device_id) |
| 80 | + |
| 81 | + manager.delete_device( |
| 82 | + service_account_json, project_id, cloud_region, registry_id, |
| 83 | + device_id) |
| 84 | + |
| 85 | + manager.delete_registry( |
| 86 | + service_account_json, project_id, cloud_region, registry_id) |
| 87 | + |
| 88 | + out, _ = capsys.readouterr() |
| 89 | + assert 'format : RSA_X509_PEM' in out |
| 90 | + assert 'State: {' in out |
| 91 | + assert '200' in out |
| 92 | + |
| 93 | + |
| 94 | +def test_state(test_topic, capsys): |
| 95 | + device_id = device_id_template.format('RSA256') |
| 96 | + manager.open_registry( |
| 97 | + service_account_json, project_id, cloud_region, pubsub_topic, |
| 98 | + registry_id) |
| 99 | + |
| 100 | + manager.create_rs256_device( |
| 101 | + service_account_json, project_id, cloud_region, registry_id, |
| 102 | + device_id, rsa_cert_path) |
| 103 | + |
| 104 | + manager.get_device( |
| 105 | + service_account_json, project_id, cloud_region, registry_id, |
| 106 | + device_id) |
| 107 | + |
| 108 | + jwt_token = cloudiot_http_example.create_jwt( |
| 109 | + project_id, 'resources/rsa_private.pem', 'RS256') |
| 110 | + |
| 111 | + print(cloudiot_http_example.publish_message( |
| 112 | + 'hello', 'state', _BASE_URL, project_id, cloud_region, |
| 113 | + registry_id, device_id, jwt_token)) |
| 114 | + |
| 115 | + manager.get_state( |
| 116 | + service_account_json, project_id, cloud_region, registry_id, |
| 117 | + device_id) |
| 118 | + |
| 119 | + manager.delete_device( |
| 120 | + service_account_json, project_id, cloud_region, registry_id, |
| 121 | + device_id) |
| 122 | + |
| 123 | + manager.delete_registry( |
| 124 | + service_account_json, project_id, cloud_region, registry_id) |
| 125 | + |
| 126 | + out, _ = capsys.readouterr() |
| 127 | + assert 'format : RSA_X509_PEM' in out |
| 128 | + assert 'State: {' in out |
| 129 | + assert 'aGVsbG8=' in out |
| 130 | + assert '200' in out |
| 131 | + |
| 132 | + |
| 133 | +def test_config(test_topic, capsys): |
| 134 | + device_id = device_id_template.format('RSA256') |
| 135 | + manager.open_registry( |
| 136 | + service_account_json, project_id, cloud_region, pubsub_topic, |
| 137 | + registry_id) |
| 138 | + |
| 139 | + manager.create_rs256_device( |
| 140 | + service_account_json, project_id, cloud_region, registry_id, |
| 141 | + device_id, rsa_cert_path) |
| 142 | + |
| 143 | + manager.get_device( |
| 144 | + service_account_json, project_id, cloud_region, registry_id, |
| 145 | + device_id) |
| 146 | + |
| 147 | + jwt_token = cloudiot_http_example.create_jwt( |
| 148 | + project_id, 'resources/rsa_private.pem', 'RS256') |
| 149 | + |
| 150 | + print(cloudiot_http_example.get_config( |
| 151 | + '0', 'state', _BASE_URL, project_id, cloud_region, |
| 152 | + registry_id, device_id, jwt_token).text) |
| 153 | + |
| 154 | + manager.get_state( |
| 155 | + service_account_json, project_id, cloud_region, registry_id, |
| 156 | + device_id) |
| 157 | + |
| 158 | + manager.delete_device( |
| 159 | + service_account_json, project_id, cloud_region, registry_id, |
| 160 | + device_id) |
| 161 | + |
| 162 | + manager.delete_registry( |
| 163 | + service_account_json, project_id, cloud_region, registry_id) |
| 164 | + |
| 165 | + out, _ = capsys.readouterr() |
| 166 | + assert 'format : RSA_X509_PEM' in out |
| 167 | + assert 'State: {' in out |
| 168 | + assert '"version": "1"' in out |
0 commit comments