Esempio n. 1
0
 def test_get_with_node(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {})
     node = factory.make_Node()
     dhcp_snippet = factory.make_DHCPSnippet(node=node)
     self.assertEqual(self.dehydrate_dhcp_snippet(dhcp_snippet),
                      handler.get({'id': dhcp_snippet.id}))
Esempio n. 2
0
 def test_revert(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {}, None)
     dhcp_snippet = factory.make_DHCPSnippet()
     textfile_ids = [dhcp_snippet.value.id]
     for _ in range(10):
         dhcp_snippet.value = dhcp_snippet.value.update(
             factory.make_string())
         dhcp_snippet.save()
         textfile_ids.append(dhcp_snippet.value.id)
     revert_to = random.randint(-10, -1)
     reverted_ids = textfile_ids[revert_to:]
     remaining_ids = textfile_ids[:revert_to]
     handler.revert({"id": dhcp_snippet.id, "to": revert_to})
     dhcp_snippet = reload_object(dhcp_snippet)
     self.assertEqual(
         VersionedTextFile.objects.get(id=textfile_ids[revert_to - 1]).data,
         dhcp_snippet.value.data,
     )
     for i in reverted_ids:
         self.assertRaises(
             VersionedTextFile.DoesNotExist,
             VersionedTextFile.objects.get,
             id=i,
         )
     for i in remaining_ids:
         self.assertIsNotNone(VersionedTextFile.objects.get(id=i))
Esempio n. 3
0
 def test_get_with_subnet(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {})
     subnet = factory.make_Subnet()
     dhcp_snippet = factory.make_DHCPSnippet(subnet=subnet)
     self.assertEqual(self.dehydrate_dhcp_snippet(dhcp_snippet),
                      handler.get({'id': dhcp_snippet.id}))
Esempio n. 4
0
 def test_update(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet = factory.make_DHCPSnippet()
     node = factory.make_Node()
     handler.update({'id': dhcp_snippet.id, 'node': node.system_id})
     dhcp_snippet = reload_object(dhcp_snippet)
     self.assertEquals(node, dhcp_snippet.node)
Esempio n. 5
0
 def test_delete(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet = factory.make_DHCPSnippet()
     handler.delete({'id': dhcp_snippet.id})
     self.assertRaises(DHCPSnippet.DoesNotExist,
                       DHCPSnippet.objects.get,
                       id=dhcp_snippet.id)
Esempio n. 6
0
 def test_list(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {}, None)
     expected_dhcp_snippets = [
         self.dehydrate_dhcp_snippet(factory.make_DHCPSnippet())
         for _ in range(3)
     ]
     self.assertItemsEqual(expected_dhcp_snippets, handler.list({}))
Esempio n. 7
0
 def test_get_global(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {}, None)
     dhcp_snippet = factory.make_DHCPSnippet()
     self.assertEqual(
         self.dehydrate_dhcp_snippet(dhcp_snippet),
         handler.get({"id": dhcp_snippet.id}),
     )
Esempio n. 8
0
 def test_create(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet_name = factory.make_name('dhcp_snippet_name')
     handler.create({
         'name': dhcp_snippet_name,
         'value': factory.make_string(),
     })
     self.assertIsNotNone(DHCPSnippet.objects.get(name=dhcp_snippet_name))
Esempio n. 9
0
 def test_update(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {}, None)
     dhcp_snippet = factory.make_DHCPSnippet()
     node = factory.make_Node()
     handler.update({"id": dhcp_snippet.id, "node": node.system_id})
     dhcp_snippet = reload_object(dhcp_snippet)
     self.assertEqual(node, dhcp_snippet.node)
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(event.description,
                      "Updated DHCP snippet '%s'." % dhcp_snippet.name)
Esempio n. 10
0
 def test_update(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet = factory.make_DHCPSnippet()
     node = factory.make_Node()
     handler.update({'id': dhcp_snippet.id, 'node': node.system_id})
     dhcp_snippet = reload_object(dhcp_snippet)
     self.assertEquals(node, dhcp_snippet.node)
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(
         event.description, "DHCP snippet '%s'" % dhcp_snippet.name +
         " updated by '%(username)s'.")
Esempio n. 11
0
 def test_create(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {}, None)
     dhcp_snippet_name = factory.make_name("dhcp_snippet_name")
     handler.create({
         "name": dhcp_snippet_name,
         "value": factory.make_string()
     })
     self.assertIsNotNone(DHCPSnippet.objects.get(name=dhcp_snippet_name))
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(event.description,
                      "Created DHCP snippet '%s'." % dhcp_snippet_name)
Esempio n. 12
0
 def test_create(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet_name = factory.make_name('dhcp_snippet_name')
     handler.create({
         'name': dhcp_snippet_name,
         'value': factory.make_string(),
     })
     self.assertIsNotNone(DHCPSnippet.objects.get(name=dhcp_snippet_name))
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(
         event.description, "DHCP snippet '%s'" % dhcp_snippet_name +
         " created by '%(username)s'.")
Esempio n. 13
0
 def test_revert_requires_to(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {}, None)
     dhcp_snippet = factory.make_DHCPSnippet()
     self.assertRaises(
         HandlerValidationError, handler.revert, {"id": dhcp_snippet.id}
     )
Esempio n. 14
0
 def test_revert_requires_to_to_be_an_int(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet = factory.make_DHCPSnippet()
     self.assertRaises(HandlerValidationError, handler.revert, {
         'id': dhcp_snippet.id,
         'to': factory.make_name('to')
     })
Esempio n. 15
0
 def test_revert_errors_on_invalid_id(self):
     user = factory.make_admin()
     handler = DHCPSnippetHandler(user, {})
     dhcp_snippet = factory.make_DHCPSnippet()
     textfile = VersionedTextFile.objects.create(data=factory.make_string())
     self.assertRaises(HandlerValidationError, handler.revert, {
         'id': dhcp_snippet.id,
         'to': textfile.id
     })
Esempio n. 16
0
 def test_revert_is_admin_only(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {}, None)
     self.assertRaises(HandlerPermissionError, handler.delete, {})
Esempio n. 17
0
 def test_create_is_admin_only(self):
     user = factory.make_User()
     handler = DHCPSnippetHandler(user, {})
     self.assertRaises(HandlerPermissionError, handler.create, {})