def test_endpoint_mutable_model_update_entire_object(cbcsdk_mock):
    """Testing EndpointStandardDevice._update_entire_object()."""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    model = Device(api, model_unique_id=12345)
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    # if model_unique_id has not been modified, _update_entire_object method makes a POST API call to update
    update_entire = model._update_entire_object()

    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    updated_obj_resp = cbcsdk_mock.StubResponse({"success": True, "deviceId": 12345},
                                                text="text in response", scode=200)
    refresh = model._refresh_if_needed(updated_obj_resp)
    assert refresh == update_entire

    # if model_unique_id is None or has been modified, _update_entire_object method makes a PATCH API Call
    model._set("deviceId", 54321)
    assert model.__class__.primary_key in model._dirty_attributes.keys()
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    update_entire = model._update_entire_object()

    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/54321", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    new_obj_response = cbcsdk_mock.StubResponse({"success": True, "deviceId": 54321},
                                                text="text in response", scode=200)
    refresh = model._refresh_if_needed(new_obj_response)
    assert refresh == update_entire

    # clear _dirty_attributes for further tests
    model.reset()
def test_delete_mbm(cbcsdk_mock):
    """Test delete method of MutableBaseModel"""
    api = cbcsdk_mock.api
    # object without a _model_unique_id can't be deleted
    emptyMutableBase = MutableBaseModel(api)
    assert emptyMutableBase._model_unique_id is None
    assert emptyMutableBase.delete() is None

    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345",
                             ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    mutableBaseModelDevice = EndpointStandardDevice(api, 12345)
    assert mutableBaseModelDevice._model_unique_id == 12345
    cbcsdk_mock.mock_request("DELETE",
                             "/integrationServices/v3/device/12345",
                             body=None)
    assert mutableBaseModelDevice.delete() is None

    # receiving a status code outside of (200,204) should raise a ServerError
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/54321",
                             ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP_1)
    newDevice = EndpointStandardDevice(api, 54321)
    delete_resp = cbcsdk_mock.StubResponse(
        contents={"success": False},
        scode=403,
        text="Failed to delete for some reason")
    cbcsdk_mock.mock_request("DELETE", "/integrationServices/v3/device/54321",
                             delete_resp)
    with pytest.raises(ServerError):
        newDevice.delete()

    newDevice.reset()
    mutableBaseModelDevice.reset()
def test_device_search_with_where(cbcsdk_mock):
    """Testing Device _search() with hostname and ip where clauses"""
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/98765", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device", ENDPOINT_STANDARD_DEVICE_GET_HOST_IP_RESP)
    api = cbcsdk_mock.api
    endpoint_standard_device_select_host_ip = (api.select(Device).where('hostName:Win7x64')
                                               .and_('ipAddress:192.10.34.165'))
    assert isinstance(endpoint_standard_device_select_host_ip, Query)
    query = endpoint_standard_device_select_host_ip._search(start=0, rows=100)
    results = [result for result in query]
    assert len(results) == 1
    found_dev = results[0]
    assert found_dev['deviceId'] == 98765
    assert found_dev['name'] == 'Win7x64'
    dev = Device(api, found_dev['deviceId'], found_dev)
    assert isinstance(dev, Device)
    dev.validate()
Ejemplo n.º 4
0
def test_refresh_if_needed_mbm(cbcsdk_mock):
    """Test _refresh_if_needed method of MutableBaseModel"""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    mutableBaseModelDevice = EndpointStandardDevice(api, 12345)

    # 200 status code
    refresh_resp_200 = cbcsdk_mock.StubResponse({"success": True, "deviceInfo": {"deviceId": 12345}}, 200)
    model_id = mutableBaseModelDevice._refresh_if_needed(refresh_resp_200)
    assert model_id == 12345

    # 404 status code
    refresh_resp_400 = cbcsdk_mock.StubResponse({}, 404, "Object not found text")
    with pytest.raises(ServerError):
        model_id = mutableBaseModelDevice._refresh_if_needed(refresh_resp_400)
        assert model_id == 12345

    # 200 status code with "result": "error"
    refresh_resp = cbcsdk_mock.StubResponse({"success": False, "message": "Couldn't update the record :("}, 200)
    with pytest.raises(ServerError):
        model_id = mutableBaseModelDevice._refresh_if_needed(refresh_resp)
        assert model_id == 12345

    # 200 status code with a response that isn't a dictionary
    refresh_unknown_resp = cbcsdk_mock.StubResponse("not_a_dictionary", 200)
    with pytest.raises(ServerError):
        model_id = mutableBaseModelDevice._refresh_if_needed(refresh_unknown_resp)

    # refresh at end of tests to clear dirty_attributes
    mutableBaseModelDevice.reset()
Ejemplo n.º 5
0
def test_reset_mbm(cbcsdk_mock):
    """Test reset method of MutableBaseModel"""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    device = EndpointStandardDevice(api, 12345)
    assert device._dirty_attributes == {}
    device._set("lastName", "restNewName")
    assert "lastName" in device._dirty_attributes
    assert device.is_dirty()

    device.reset()
    assert device.is_dirty() is False
    assert device._dirty_attributes == {}

    # refresh at end of tests to clear dirty_attributes
    device.reset()
Ejemplo n.º 6
0
def test_is_dirty_mbm(cbcsdk_mock):
    """Test is_dirty method of MutableBaseModel"""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    mutableBaseModelDevice = EndpointStandardDevice(api, 12345)
    assert mutableBaseModelDevice.is_dirty() is False

    mutableBaseModelDevice._set("deviceId", 99999)
    assert mutableBaseModelDevice.is_dirty()

    # refresh at end of tests to clear dirty_attributes
    mutableBaseModelDevice.reset()
Ejemplo n.º 7
0
def test_update_object_mbm(cbcsdk_mock):
    """Test _update_object method of MutableBaseModel"""
    # if primary_key hasn't been modified, we use the _change_object_http_method
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    mutableBaseModelDevice = EndpointStandardDevice(api, 12345)
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    print(mutableBaseModelDevice._info)
    mutableBaseModelDevice._set("name", "newFakeName")
    mutableBaseModelDevice._set("testId", 1)

    assert mutableBaseModelDevice._update_object() == 12345
    print(mutableBaseModelDevice._info)

    assert mutableBaseModelDevice.deviceId == 12345
    assert mutableBaseModelDevice._info['name'] == 'newFakeName'
    assert mutableBaseModelDevice._info['testId'] == 1

    # refresh at end of tests to clear dirty_attributes
    mutableBaseModelDevice.reset()
def test_endpoint_mutable_model_update_object(cbcsdk_mock):
    """Testing EndpointStandardMutableModel._query_implementation()."""
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    model = Device(cbcsdk_mock.api, model_unique_id=12345)
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    assert isinstance(model, EndpointStandardMutableModel)
    assert model._change_object_http_method == "PATCH"
    # when _change_object_http_method is PATCH, the _update_object method makes a PATCH API call
    update = model._update_object()
    patch = model._patch_object()
    assert update == patch

    model._change_object_http_method = "POST"
    # when _change_object_http_method is != "PATCH", the _update_object method makes a POST API call
    update = model._update_object()
    update_entire = model._update_entire_object()
    assert update == update_entire
Ejemplo n.º 9
0
def test_save_mbm(cbcsdk_mock):
    """Test save method of MutableBaseModel"""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    mutableBaseModelDevice = EndpointStandardDevice(api, 12345)

    assert mutableBaseModelDevice.save() is None
    assert mutableBaseModelDevice._info["firstName"] is None

    # now make dirty
    mutableBaseModelDevice._set("firstName", "newName")
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    # fails on PATCH unimplemented
    assert mutableBaseModelDevice.save() == mutableBaseModelDevice
    assert mutableBaseModelDevice._info["firstName"] == "newName"

    # refresh at end of tests to clear dirty_attributes
    mutableBaseModelDevice.reset()
def test_endpoint_mutable_model_patch_object(cbcsdk_mock):
    """Testing EndpointStandardMutableModel._patch_object()."""
    api = cbcsdk_mock.api
    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/12345", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    model = Device(api, model_unique_id=12345)
    model._set("deviceId", 56789)
    assert model.deviceId == 56789
    assert model.__class__.primary_key in model._dirty_attributes.keys()
    cbcsdk_mock.mock_request("PATCH", "/integrationServices/v3/device", ENDPOINT_STANDARD_DEVICE_PATCH_RESP)
    patch_object_return = model._patch_object()

    cbcsdk_mock.mock_request("GET", "/integrationServices/v3/device/56789", ENDPOINT_STANDARD_DEVICE_GET_SPECIFIC_RESP)
    new_obj_response = cbcsdk_mock.StubResponse({"success": True, "deviceId": 56789},
                                                text="text in response", scode=200)
    patched_device = model._refresh_if_needed(new_obj_response)

    assert patch_object_return == patched_device