def test_zone_field_rejects_empty_zone(self): # If the field is present, the zone name has to be valid # and the empty string is not a valid zone name. form = BulkNodeSetZoneForm(user=factory.make_admin(), data={"zone": ""}) self.assertFalse(form.is_valid(), form._errors) self.assertEqual(["This field is required."], form._errors["zone"])
def set_zone(self, request): """@description-title Assign nodes to a zone @description Assigns a given node to a given zone. @param (string) "zone" [required=true] The zone name. @param (string) "nodes" [required=true] The node to add. @success (http-status-code) "204" 204 @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have set the zone. @error-example "no-perms" This method is reserved for admin users. @error (http-status-code) "400" 400 @error (content) "bad-param" The given parameters were not correct. """ data = { "zone": request.data.get("zone"), "system_id": get_optional_list(request.data, "nodes"), } form = BulkNodeSetZoneForm(request.user, data=data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) form.save()
def test_set_zone_does_not_work_if_not_superuser(self): node = factory.make_Node() form = BulkNodeSetZoneForm( user=factory.make_User(), data={ "zone": factory.make_Zone().name, "system_id": [node.system_id], }, ) self.assertFalse(form.is_valid())
def test_rejects_empty_system_ids(self): form = BulkNodeSetZoneForm( user=factory.make_admin(), data={ "system_id": [], "zone": factory.make_Zone().name }, ) self.assertFalse(form.is_valid(), form._errors) self.assertEqual(["No node selected."], form._errors["system_id"])
def test_set_zone_does_not_work_if_not_rbac_pool_admin(self): rbac = self.useFixture(RBACEnabled()) user = factory.make_User() machine = factory.make_Machine() rbac.store.add_pool(machine.pool) rbac.store.allow(user.username, machine.pool, "deploy-machines") rbac.store.allow(user.username, machine.pool, "view") form = BulkNodeSetZoneForm( user=user, data={ "zone": factory.make_Zone().name, "system_id": [machine.system_id], }, ) self.assertFalse(form.is_valid())
def test_rejects_invalid_system_ids(self): node = factory.make_Node() system_ids = [node.system_id, "wrong-system_id"] form = BulkNodeSetZoneForm( user=factory.make_admin(), data={ "system_id": system_ids, "zone": factory.make_Zone().name }, ) self.assertFalse(form.is_valid(), form._errors) self.assertEqual( ["Some of the given system ids are invalid system ids."], form._errors["system_id"], )
def test_set_zone_sets_zone_on_node(self): node = factory.make_Node() zone = factory.make_Zone() form = BulkNodeSetZoneForm( user=factory.make_admin(), data={ "zone": zone.name, "system_id": [node.system_id] }, ) self.assertTrue(form.is_valid(), form._errors) done, not_actionable, not_permitted = form.save() self.assertEqual([1, 0, 0], [done, not_actionable, not_permitted]) node = reload_object(node) self.assertEqual(zone, node.zone)
def test_set_zone_leaves_unselected_nodes_alone(self): unselected_node = factory.make_Node() original_zone = unselected_node.zone form = BulkNodeSetZoneForm( user=factory.make_admin(), data={ "zone": factory.make_Zone().name, "system_id": [factory.make_Node().system_id], }, ) self.assertTrue(form.is_valid(), form._errors) done, not_actionable, not_permitted = form.save() self.assertEqual([1, 0, 0], [done, not_actionable, not_permitted]) unselected_node = reload_object(unselected_node) self.assertEqual(original_zone, unselected_node.zone)
def test_set_zone_works_if_rbac_pool_admin(self): rbac = self.useFixture(RBACEnabled()) user = factory.make_User() machine = factory.make_Machine() zone = factory.make_Zone() rbac.store.add_pool(machine.pool) rbac.store.allow(user.username, machine.pool, "admin-machines") rbac.store.allow(user.username, machine.pool, "view") form = BulkNodeSetZoneForm( user=user, data={ "zone": zone.name, "system_id": [machine.system_id] }, ) self.assertTrue(form.is_valid(), form._errors) done, not_actionable, not_permitted = form.save() self.assertEqual([1, 0, 0], [done, not_actionable, not_permitted]) machine = reload_object(machine) self.assertEqual(zone, machine.zone)