def test_put_limit_malformed_limit(self, internal_api): try: internal_api.put_max_devices_limit('foo', '1') except bravado.exception.HTTPError as herr: assert herr.response.status_code == 400 else: pytest.fail("Expected Bad Request (400)")
def test_device_limit_applied(self, management_api, internal_api, tenant_foobar_devices, tenant_foobar): """Verify that max accepted devices limit is indeed applied. Since device limits can only be set on per-tenant basis, use fixtures that setup tenant 'foobar' with devices and a token """ expected = 2 internal_api.put_max_devices_limit('foobar', expected) accepted = 0 try: with orchestrator.run_fake_for_device_id(orchestrator.ANY_DEVICE): for dev, dev_auth in tenant_foobar_devices: auth = 'Bearer ' + tenant_foobar fdev = management_api.find_device_by_identity( dev.identity, Authorization=auth) aid = fdev.auth_sets[0].id management_api.accept_device(fdev.id, aid, Authorization=auth) accepted += 1 except bravado.exception.HTTPError as e: assert e.response.status_code == 422 finally: if accepted > expected: pytest.fail( "expected only {} devices to be accepted".format(expected))
def test_limit_differnt_tenants(self, internal_api): max_devs = 10 internal_api.put_max_devices_limit('foo', max_devs) limit = internal_api.get_max_devices_limit('bar') assert limit.limit == 0
def test_limit(self, internal_api): max_devs = 10 internal_api.put_max_devices_limit('foo', max_devs) limit = internal_api.get_max_devices_limit('foo') assert limit.limit == max_devs
def test_put_limit(self, internal_api): max_devs = 100 internal_api.put_max_devices_limit('foo', max_devs)
def test_check_device_limits( self, clean_db, cli, device_api, management_api, internal_api, test_case ): rsp_q_tadm = asyncio.Queue( maxsize=len(test_case["tenant"]["users"]) + test_case["device_count"] ) rsp_q_wflows = asyncio.Queue( maxsize=len(test_case["tenant"]["users"]) + test_case["device_count"] ) with self.init_service_mocks(wflows_rsp_q=rsp_q_wflows, tadm_rsp_q=rsp_q_tadm): tenant_token = make_fake_tenant_token(test_case["tenant"]["id"]) internal_api.put_max_devices_limit( test_case["tenant"]["id"], test_case["tenant"]["device_limit"] ) for _ in range(test_case["device_count"]): # POST /api/internal/v1/tenantadm/verify rsp_q_tadm.put_nowait( (200, {}, '{"id": "%s", "sub": "user"}' % test_case["tenant"]) ) # POST /api/v1/workflows/update_device_inventory rsp_q_wflows.put_nowait((201, {}, "")) # POST /api/v1/workflows/provision_device rsp_q_wflows.put_nowait((201, {}, "")) dev = Device() da = DevAuthorizer(tenant_token=tenant_token) rsp = device_auth_req(device_api.auth_requests_url, da, dev) assert rsp.status_code == 401 devs = management_api.list_devices( status="pending", Authorization="Bearer " + tenant_token ) for dev in devs: # POST /api/v1/workflows/update_device_status rsp_q_wflows.put_nowait((201, {}, "")) # POST /api/v1/workflows/provision_device rsp_q_wflows.put_nowait((201, {}, "")) management_api.put_device_status( dev["id"], dev["auth_sets"][0]["id"], "accepted", Authorization="Bearer " + tenant_token, ) if test_case["device_count"] >= ( (test_case["tenant"]["device_limit"] * test_case["threshold"] / 100.0) ): # GET /api/management/v1/tenantadm/users usersJSON = json.dumps(test_case["tenant"]["users"]) rsp_q_tadm.put_nowait((200, {}, usersJSON)) usernames = [user["name"] for user in test_case["tenant"]["users"]] # Verify that workflow is started for each user for i in range(len(test_case["tenant"]["users"])): def verify_workflow(handler): assert handler.request.path.endswith("device_limit_email") body_json = json.loads(handler.request.body.decode("utf-8")) assert body_json["to"] in usernames usernames.remove(body_json["to"]) handler.set_status(201) # POST /api/v1/workflows/device_limit_email rsp_q_wflows.put_nowait(verify_workflow) code, stdout, stderr = cli.check_device_limits() assert code == 0 # All pushed mock responses should be consumed at this point. assert ( rsp_q_tadm.empty() ), "TenantAdm mock responses not consumed as expected" assert ( rsp_q_wflows.empty() ), "Workflows mock responses not consumed as expected"