1
1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from unittest import skipIf
8
from unittest2 import skipIf # NOQA
10
from django.conf import settings
5
11
from django.core.management import call_command
6
12
from django.core.urlresolvers import reverse
7
13
from django.http import HttpResponse
8
14
from django.test import TestCase
9
from django.template import RequestContext, TemplateDoesNotExist
15
from django.template import RequestContext
16
from django.template.loader import render_to_string
15
22
from cStringIO import StringIO
23
from pyquery import PyQuery
17
25
from . import Preflight
18
26
from .models import (
294
307
mock_settings.PREFLIGHT_HIDDEN_SETTINGS = ''
295
308
settings = gather_settings()
296
309
self.assertEqual([], settings)
312
@skipIf(not settings.USE_GARGOYLE, 'skipping for Django 1.1')
313
class GargoyleTestCase(TestCase):
314
from gargoyle import gargoyle # NOQA
315
from gargoyle.builtins import IPAddressConditionSet # NOQA
316
from gargoyle.models import ( # NOQA
325
super(GargoyleTestCase, self).setUp()
326
self.gargoyle.register(self.IPAddressConditionSet())
328
def get_switches(self):
330
self.Switch(key='DISABLED', status=self.DISABLED,
331
description='switch 1'),
332
self.Switch(key='SELECTIVE_1', status=self.SELECTIVE),
333
self.Switch(key='SELECTIVE_2', status=self.SELECTIVE),
334
self.Switch(key='GLOBAL', status=self.GLOBAL),
335
self.Switch(key='INHERIT', status=self.INHERIT),
337
selective = switches[2]
338
selective.add_condition(
340
condition_set='gargoyle.builtins.IPAddressConditionSet',
341
field_name='ip_address',
342
condition='127.0.0.1',
346
@patch.dict(sys.modules, **{'gargoyle.models': None})
347
def test_gather_switches_no_gargoyle(self):
348
self.assertEqual(gather_gargoyle(), None)
350
def assert_switches_dict(self, actual):
352
dict(name='DISABLED',
353
status=self.DISABLED,
354
description='switch 1',
355
status_text=self.Switch.STATUS_LABELS[self.DISABLED],
357
dict(name='SELECTIVE_1',
358
status=self.SELECTIVE,
360
status_text=self.Switch.STATUS_LABELS[self.GLOBAL],
362
dict(name='SELECTIVE_2', status=self.SELECTIVE,
364
status_text=self.Switch.STATUS_LABELS[self.SELECTIVE],
365
conditions=['IP Address(ip_address=127.0.0.1)']),
366
dict(name='GLOBAL', status=self.GLOBAL,
368
status_text=self.Switch.STATUS_LABELS[self.GLOBAL],
370
dict(name='INHERIT', status=self.INHERIT,
372
status_text=self.Switch.STATUS_LABELS[self.INHERIT],
375
self.assertEqual(actual, expected)
377
@patch('gargoyle.models.Switch.objects.all')
378
def test_gather_switches(self, mock_all):
379
mock_all.return_value = self.get_switches()
380
self.assert_switches_dict(gather_gargoyle())
382
@patch('gargoyle.models.Switch.objects.all')
383
def test_gargoyle_template(self, mock_all):
384
switches = self.get_switches()
385
mock_all.return_value = switches
386
the_switches = gather_switches()
388
"switches": the_switches,
389
"switches_json": json.dumps(the_switches),
390
"preflight_base_template": BASE_TEMPLATE,
391
"preflight_table_class": TABLE_CLASS,
393
response = render_to_string('preflight/overview.html', context)
394
dom = PyQuery(response)
395
table = dom.find('#switches-table tbody')
396
self.assertEqual(table.find('tr')[0][0].text, 'gargoyle')
398
for row, switch in zip(table.find('tr.switch'), switches):
399
self.assertEqual(row[0].text, switch.key)
400
self.assertEqual(row[1].text, str(switch.description))
401
self.assertEqual(row[3].text, switch.get_status_label())
403
data = json.loads(dom.find('#switches-json').text())
404
self.assertTrue('gargoyle' in data)
405
json_switches = data['gargoyle']
406
self.assert_switches_dict(json_switches)