Пример #1
0
def test_create():
    """Verify that you can create users."""
    mock_request = Mock()
    users = Users(fixture, mock_request)

    users.create("joe", pwd="abcd", sgrp=SGRP_ADMIN)
    mock_request.assert_called_with(
        "post",
        "/axis-cgi/pwdgrp.cgi",
        data={
            "action": "add",
            "user": "******",
            "pwd": "abcd",
            "grp": "users",
            "sgrp": "viewer:operator:admin",
        },
    )

    users.create("joe", pwd="abcd", sgrp=SGRP_ADMIN, comment="comment")
    mock_request.assert_called_with(
        "post",
        "/axis-cgi/pwdgrp.cgi",
        data={
            "action": "add",
            "user": "******",
            "pwd": "abcd",
            "grp": "users",
            "sgrp": "viewer:operator:admin",
            "comment": "comment",
        },
    )
Пример #2
0
    def test_can_send_message_without_body(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send('/topic/test', headers={'my-header': 'my-value'})

        send_mock.assert_called_with(
            {
                'destination': '/topic/test',
                'my-header': 'my-value',
            }, '')
Пример #3
0
def test_delete():
    """Verify that you can delete users."""
    mock_request = Mock()
    users = Users(fixture, mock_request)
    users.delete("joe")

    mock_request.assert_called_with("post",
                                    "/axis-cgi/pwdgrp.cgi",
                                    data={
                                        "action": "remove",
                                        "user": "******"
                                    })
Пример #4
0
    def test_can_send_message_without_body(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send("/topic/test", headers={"my-header": "my-value"})

        send_mock.assert_called_with(
            {
                "destination": "/topic/test",
                "my-header": "my-value",
                "content-length": 0,
            },
            b"",
        )
Пример #5
0
    def test_can_send_message_with_body_without_content_lenght(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send('/topic/test',
                        headers={'my-header': 'my-value'},
                        body='my body utf-8 ç',
                        send_content_length=False)

        send_mock.assert_called_with(
            {
                'destination': '/topic/test',
                'my-header': 'my-value'
            }, b'my body utf-8 \xc3\xa7')
Пример #6
0
    def test_can_send_message_with_body_binary(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send('/topic/test',
                        headers={'my-header': 'my-value'},
                        body=b'\xc3\xa7')

        send_mock.assert_called_with(
            {
                'destination': '/topic/test',
                'my-header': 'my-value',
                'content-length': 2
            }, b'\xc3\xa7')
Пример #7
0
    def test_can_send_message_with_body_binary(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send("/topic/test",
                        headers={"my-header": "my-value"},
                        body=b"\xc3\xa7")

        send_mock.assert_called_with(
            {
                "destination": "/topic/test",
                "my-header": "my-value",
                "content-length": 2,
            },
            b"\xc3\xa7",
        )
Пример #8
0
    def test_can_send_message_with_body_without_content_length(self):
        send_mock = Mock()
        self.stomp._protocol.send = send_mock

        self.stomp.send(
            "/topic/test",
            headers={"my-header": "my-value"},
            body="my body utf-8 ç",
            send_content_length=False,
        )

        send_mock.assert_called_with(
            {
                "destination": "/topic/test",
                "my-header": "my-value"
            },
            b"my body utf-8 \xc3\xa7",
        )
Пример #9
0
def test_ports():
    """Test that different types of ports work."""
    mock_request = Mock()
    mock_request.return_value = fixture_ports
    params = Params(mock_request)
    ports = Ports(params, mock_request)
    ports.update()

    mock_request.assert_called_once

    assert ports["0"].id == "0"
    assert ports["0"].configurable == "no"
    assert ports["0"].direction == "input"
    assert not ports["0"].name

    ports["0"].action(action=ACTION_LOW)
    mock_request.assert_called_once

    assert ports["1"].id == "1"
    assert ports["1"].configurable == "no"
    assert ports["1"].direction == "input"
    assert ports["1"].name == "PIR sensor"
    assert ports["1"].input_trig == "closed"

    assert ports["2"].id == "2"
    assert ports["2"].configurable == "no"
    assert ports["2"].direction == "input"
    assert ports["2"].name == ""
    assert ports["2"].output_active == "closed"

    assert ports["3"].id == "3"
    assert ports["3"].configurable == "no"
    assert ports["3"].direction == "output"
    assert ports["3"].name == "Tampering"
    assert ports["3"].output_active == "open"

    ports["3"].close()
    mock_request.assert_called_with("get", "/axis-cgi/io/port.cgi?action=4%3A%2F")

    ports["3"].open()
    mock_request.assert_called_with("get", "/axis-cgi/io/port.cgi?action=4%3A%5C")
Пример #10
0
class TestExample(asynctest.TestCase):
    def setUp(self):
        self.env_vars = {"EXAMPLE_VAR": "VALUE", "OTHER_VAR": ANY}
        self.p_getenv = Mock(side_effect=self.retrieve_env_var)
        start_patch(self, Example, os, new=Mock(getenv=self.p_getenv))
        self.p_stderr = Mock()
        start_patch(self, Example, sys, new=Mock(stderr=self.p_stderr))

        self.post_return = {"elements": [100, 2, 15, 28], "other_thing": "This is a thing (other)"}
        post = Mock(return_value=Mock(status_code=200,
                                      json=Mock(return_value=self.post_return)))
        self.post = post
        self.requests = start_patch(self, Example, requests, new=Mock(name="requests", post=post))

        self.open = start_patch(self,
                                target='builtins.open',
                                new=mock_open(read_data=json.dumps([{"field": 1}, {"field": 2}])))

        self.timer = Mock()
        self.timer_constructor = start_patch(self, Example, Timer, return_value=self.timer)

        self.example = Example()

    def test_gets_environment_variable_in_constructor(self):
        self.p_getenv.assert_called_with("EXAMPLE_VAR", "default to this")

    def test_sends_request(self):
        data = {"field": 1, "field2": 200}
        self.example.make_post_request("www.google.com", data)

        self.requests.post.assert_called_with("www.google.com", data=data)

    def test_writes_error_on_non_200(self):
        self.requests.post = Mock(return_value=Mock(status_code=404))
        self.example.make_post_request("www.google.com", {})

        self.p_stderr.write.assert_called_with("There was an error making the request: 404\n")

    def test_writes_error_on_exception(self):
        self.requests.post.side_effect = Exception("BadTimesMan")
        self.example.make_post_request("www.google.com", {})

        self.p_stderr.write.assert_called_with("There was an exception: Exception('BadTimesMan',)\n")

    def test_gets_json_response_from_request(self):
        self.assertEqual(self.post_return, self.example.make_post_request("www.google.com", {}))

    def test_timer_created(self):
        self.example.execute_on_timer(20, self.placeholder)
        self.timer_constructor.assert_called_with(20, self.example.execute_on_timer, 20, self.placeholder)

    def test_timer_started(self):
        self.example.execute_on_timer(20, self.placeholder)
        self.timer.start.assert_called()

    def retrieve_env_var(self, var_name, default):
        if var_name in self.env_vars:
            return self.env_vars.get(var_name)
        return default

    def placeholder(self):
        pass