def test_version(self):
        host_info = self.mock_servers['mynewhost']
        with timed('csr', 10):
            data = {
                'fqdn': host_info['fqdn'],
                'nodename': host_info['nodename'],
                'version': '1.0',
                'capabilities': ['manage_targets'],
                'address': 'mynewhost',
                'csr': generate_csr(host_info['fqdn']),
            }

        with patch(settings, VERSION='2.0'):
            # Try with a mis-matched version
            token = RegistrationToken.objects.create(
                profile=ServerProfile.objects.get())
            with timed('register fail', 10):
                response = Client().post("/agent/register/%s/" % token.secret,
                                         data=json.dumps(data),
                                         content_type="application/json")
            self.assertEqual(response.status_code, 400)

            # Try with a matching version
            token = RegistrationToken.objects.create(
                profile=ServerProfile.objects.get())
            settings.VERSION = '1.1'
            with timed('register pass', 10):
                response = Client().post("/agent/register/%s/" % token.secret,
                                         data=json.dumps(data),
                                         content_type="application/json")
            self.assertEqual(response.status_code, 201)
            content = json.loads(response.content)

            # reregistration should fail with unknown serial
            data = {'address': 'mynewhost', 'fqdn': 'mynewhost.newcompany.com'}
            headers = {
                'HTTP_X_SSL_CLIENT_NAME': host_info['fqdn'],
                'HTTP_X_SSL_CLIENT_SERIAL': ''
            }
            response = Client().post('/agent/reregister/',
                                     data=json.dumps(data),
                                     content_type='application/json',
                                     **headers)
            self.assertEqual(response.status_code, 403)

            # reregistration should update host's domain name
            headers['HTTP_X_SSL_CLIENT_SERIAL'] = Crypto().get_serial(
                content['certificate'])
            response = Client().post('/agent/reregister/',
                                     data=json.dumps(data),
                                     content_type='application/json',
                                     **headers)
            self.assertEqual(response.status_code, 200)
            host = ManagedHost.objects.get(id=content['host_id'])
            self.assertEqual(host.fqdn, data['fqdn'])
Exemplo n.º 2
0
    def test_version(self):
        host_info = self.mock_servers["mynewhost"]
        with timed("csr", 10):
            data = {
                "fqdn": host_info["fqdn"],
                "nodename": host_info["nodename"],
                "version": "1.0",
                "capabilities": ["manage_targets"],
                "address": "mynewhost",
                "csr": generate_csr(host_info["fqdn"]),
            }

        with patch(settings, VERSION="2.0"):
            # Try with a mis-matched version
            token = RegistrationToken.objects.create(
                profile=ServerProfile.objects.get())
            with timed("register fail", 10):
                response = Client().post("/agent/register/%s/" % token.secret,
                                         data=json.dumps(data),
                                         content_type="application/json")
            self.assertEqual(response.status_code, 400)

            # Try with a matching version
            token = RegistrationToken.objects.create(
                profile=ServerProfile.objects.get())
            settings.VERSION = "1.1"
            with timed("register pass", 10):
                response = Client().post("/agent/register/%s/" % token.secret,
                                         data=json.dumps(data),
                                         content_type="application/json")
            self.assertEqual(response.status_code, 201)
            content = json.loads(response.content)

            # reregistration should fail with unknown serial
            data = {"address": "mynewhost", "fqdn": "mynewhost.newcompany.com"}
            headers = {
                "HTTP_X_SSL_CLIENT_NAME": host_info["fqdn"],
                "HTTP_X_SSL_CLIENT_SERIAL": ""
            }
            response = Client().post("/agent/reregister/",
                                     data=json.dumps(data),
                                     content_type="application/json",
                                     **headers)
            self.assertEqual(response.status_code, 403)

            # reregistration should update host's domain name
            headers["HTTP_X_SSL_CLIENT_SERIAL"] = Crypto().get_serial(
                content["certificate"])
            response = Client().post("/agent/reregister/",
                                     data=json.dumps(data),
                                     content_type="application/json",
                                     **headers)
            self.assertEqual(response.status_code, 200)
            host = ManagedHost.objects.get(id=content["host_id"])
            self.assertEqual(host.fqdn, data["fqdn"])
Exemplo n.º 3
0
def test_negative() -> None:
    response, execution_time = timed(client.get)("/bulk/items")
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "detail": [{
            "loc": ["body"],
            "msg": "field required",
            "type": "value_error.missing"
        }]
    }
    assert 0.1 > execution_time

    response, execution_time = timed(client.get)("/bulk/items",
                                                 json="not a list")
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "detail": [{
            "loc": ["body"],
            "msg": "value is not a valid list",
            "type": "type_error.list"
        }]
    }
    assert 0.1 > execution_time

    response, execution_time = timed(client.get)("/bulk/items", json=[])
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "No items found"}
    assert 0.1 > execution_time

    response, execution_time = timed(client.get)("/bulk/items", json=[None])
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "detail": [{
            "loc": ["body", 0],
            "msg": "none is not an allowed value",
            "type": "type_error.none.not_allowed"
        }]
    }
    assert 0.1 > execution_time

    response, execution_time = timed(client.get)("/bulk/items", json=[{}])
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "detail": [{
            "loc": ["body", 0],
            "msg": "str type expected",
            "type": "type_error.str"
        }]
    }
    assert 0.1 > execution_time
Exemplo n.º 4
0
def test_negative() -> None:
    response, execution_time = timed(client.get)("/items")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Not Found"}
    assert 0.1 > execution_time

    assert sync(redis.get)("invalid_key") is None
    response, execution_time = timed(client.get)("/items/invalid_key")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Item not found"}
    assert 0.1 > execution_time
def positive_test(items: Dict[Key, Value]) -> None:
    response, execution_time = timed(client.put)("/bulk/items", json=items)
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "stored": [f"/items/{key}" for key in items.keys()]
    }
    assert 0.1 > execution_time
def positive_test(key: Key, value: Value) -> None:
    response, execution_time = timed(client.put)(f"/items/{key}?value={value}")
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.headers.get("location") == f"/items/{key}"
    assert response.json() == {"stored": f"/items/{key}"}
    assert sync(redis.get)(key) == value
    assert 0.1 > execution_time
def positive_test(key: Key, *, value: Value = "v") -> None:
    # Add key to the storage
    sync(redis.set)(key, value)
    assert sync(redis.get)(key) == value

    response, execution_time = timed(client.delete)(f"/items/{key}")
    assert response.status_code == 204
    assert 0.1 > execution_time
def test_negative() -> None:
    response, execution_time = timed(client.put)("/items")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Not Found"}
    assert 0.1 > execution_time

    response, execution_time = timed(client.put)("/items/test_key")
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "detail": [{
            "loc": ["query", "value"],
            "msg": "field required",
            "type": "value_error.missing"
        }]
    }
    assert 0.1 > execution_time
Exemplo n.º 9
0
def test_positive() -> None:
    positive_test(keys=["test_key"])
    positive_test(keys=["key1", "key2", "key3"])
    positive_test(keys=["test_key"], value="new_value")

    response, execution_time = timed(client.get)("/bulk/items",
                                                 json=["invalid_key"])
    assert response.status_code == 200
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"invalid_key": None}
    assert 0.1 > execution_time

    response, execution_time = timed(client.get)(
        "/bulk/items", json=["invalid_key1", "test_key"])
    assert response.status_code == 200
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"invalid_key1": None, "test_key": "new_value"}
    assert 0.1 > execution_time
Exemplo n.º 10
0
def positive_test(keys: List[Key], *, value: Value = "v") -> None:
    # Add keys to the storage
    sync(redis.multi_set)([(key, value) for key in keys])
    assert len(sync(redis.multi_get)(keys)) == len(keys)

    response, execution_time = timed(client.delete)("/bulk/items", json=keys)
    assert response.status_code == 200
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"deleted_count": len(set(keys))}
    assert 0.1 > execution_time
Exemplo n.º 11
0
def positive_test(key: Key, value: Value) -> None:
    # Add key to the storage
    sync(redis.set)(key, value)
    assert sync(redis.get)(key) == value

    response, execution_time = timed(client.get)(f"/items/{key}")
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"value": value}
    assert 0.1 > execution_time
def test_negative() -> None:
    response, execution_time = timed(client.delete)("/items")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Not Found"}
    assert 0.1 > execution_time

    response, execution_time = timed(client.delete)("/items/invalid_key")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Item not found"}
    assert 0.1 > execution_time

    # Consequent operation
    response, execution_time = timed(client.delete)("/items/test_key")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Item not found"}
    assert 0.1 > execution_time