def test_edit_link(self): ases = AS.objects.iterator() as_a = next(ases) as_b = next(ases) link = Link.objects.create_from_ases(Link.PROVIDER, as_a, as_b) form_data = dict( type=Link.PROVIDER, active=True, mtu=2345, from_host=as_a.hosts.first().pk, to_host=as_b.hosts.first().pk, from_public_ip='192.0.2.1', from_public_port=50000, to_public_ip='192.0.2.2', to_public_port=50001, ) form = LinkAdminForm(instance=link, data=form_data) self.assertTrue(form.is_valid(), form.errors.as_data()) link = form.save() self.assertIsNotNone(link) self.assertEqual(link.type, Link.PROVIDER) self.assertEqual(link.active, True) self.assertEqual(link.mtu, 2345) self.assertEqual(link.interfaceA.public_ip, '192.0.2.1') self.assertEqual(link.interfaceA.public_port, 50000) self.assertEqual(link.interfaceB.public_ip, '192.0.2.2') self.assertEqual(link.interfaceB.public_port, 50001) # Re-creating form with same data should result in no changes: edit_form = LinkAdminForm(instance=link, data=form_data) self.assertTrue(edit_form.is_valid(), edit_form.errors) self.assertFalse(edit_form.has_changed(), edit_form.changed_data)
def test_create_link(self): ases = AS.objects.iterator() as_a = next(ases) as_b = next(ases) form_data = dict( type=Link.PROVIDER, active=True, mtu=2345, from_host=as_a.hosts.first().pk, from_public_port=50000, to_host=as_b.hosts.first().pk, to_public_port=50000, ) form = LinkAdminForm(data=form_data) self.assertTrue(form.is_valid(), form.errors.as_data()) link = form.save() self.assertIsNotNone(link) # Re-creating form with same data should result in no changes: edit_form = LinkAdminForm(instance=link, data=form_data) self.assertTrue(edit_form.is_valid(), edit_form.errors) self.assertFalse(edit_form.has_changed(), edit_form.changed_data)
def test_create_link_error(self): # Create an illegal loop-back link as_a = AS.objects.first() form_data = dict( type=Link.PROVIDER, active=True, mtu=2345, from_host=as_a.hosts.first().pk, from_public_port=50000, to_host=as_a.hosts.first().pk, to_public_port=50000, ) form = LinkAdminForm(data=form_data) self.assertFalse(form.is_valid()) self.assertIn('Loop AS-link', str(form.errors['__all__'][0]))