~ubuntuone-pqm-team/django-preflight/trunk

« back to all changes in this revision

Viewing changes to preflight/tests.py

  • Committer: Tarmac
  • Author(s): Simon Davy
  • Date: 2013-06-03 11:50:52 UTC
  • mfrom: (24.1.2 preflight)
  • Revision ID: tarmac-20130603115052-cv451q6nz0e77cm6
[r=mfoord] Add support for gargoyle flags in preflight

Show diffs side-by-side

added added

removed removed

Lines of Context:
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).
3
3
import sys
 
4
import json
 
5
try:
 
6
    from unittest import skipIf
 
7
except ImportError:
 
8
    from unittest2 import skipIf  # NOQA
4
9
 
 
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
10
17
from mock import (
11
18
    Mock,
12
19
    patch,
13
20
)
14
21
 
15
22
from cStringIO import StringIO
 
23
from pyquery import PyQuery
16
24
 
17
25
from . import Preflight
18
26
from .models import (
21
29
    REGISTRY,
22
30
    authenticate,
23
31
    gather_checks,
 
32
    gather_switches,
 
33
    gather_gargoyle,
24
34
    gather_settings,
25
35
    gather_versions,
26
36
)
 
37
from preflight.conf import BASE_TEMPLATE, TABLE_CLASS
27
38
 
28
39
 
29
40
class CheckTestCase(TestCase):
142
153
    def authenticate(self, request):
143
154
        return False
144
155
 
 
156
 
145
157
def clear_registry():
146
158
    while REGISTRY:
147
159
        REGISTRY.pop()
182
194
    def versions(self):
183
195
        return [{'name': 'spam', 'version': 'ni'}]
184
196
 
 
197
 
185
198
class ExtraVersionAsList(Preflight):
186
199
 
187
200
    versions = [{'name': 'eggs', 'version': 'peng'}]
294
307
        mock_settings.PREFLIGHT_HIDDEN_SETTINGS = ''
295
308
        settings = gather_settings()
296
309
        self.assertEqual([], settings)
 
310
 
 
311
 
 
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
 
317
        Switch,
 
318
        DISABLED,
 
319
        SELECTIVE,
 
320
        GLOBAL,
 
321
        INHERIT
 
322
    )
 
323
 
 
324
    def setUp(self):
 
325
        super(GargoyleTestCase, self).setUp()
 
326
        self.gargoyle.register(self.IPAddressConditionSet())
 
327
 
 
328
    def get_switches(self):
 
329
        switches = [
 
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),
 
336
        ]
 
337
        selective = switches[2]
 
338
        selective.add_condition(
 
339
            self.gargoyle,
 
340
            condition_set='gargoyle.builtins.IPAddressConditionSet',
 
341
            field_name='ip_address',
 
342
            condition='127.0.0.1',
 
343
        )
 
344
        return switches
 
345
 
 
346
    @patch.dict(sys.modules, **{'gargoyle.models': None})
 
347
    def test_gather_switches_no_gargoyle(self):
 
348
        self.assertEqual(gather_gargoyle(), None)
 
349
 
 
350
    def assert_switches_dict(self, actual):
 
351
        expected = [
 
352
            dict(name='DISABLED',
 
353
                 status=self.DISABLED,
 
354
                 description='switch 1',
 
355
                 status_text=self.Switch.STATUS_LABELS[self.DISABLED],
 
356
                 conditions=[]),
 
357
            dict(name='SELECTIVE_1',
 
358
                 status=self.SELECTIVE,
 
359
                 description=None,
 
360
                 status_text=self.Switch.STATUS_LABELS[self.GLOBAL],
 
361
                 conditions=[]),
 
362
            dict(name='SELECTIVE_2', status=self.SELECTIVE,
 
363
                 description=None,
 
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,
 
367
                 description=None,
 
368
                 status_text=self.Switch.STATUS_LABELS[self.GLOBAL],
 
369
                 conditions=[]),
 
370
            dict(name='INHERIT', status=self.INHERIT,
 
371
                 description=None,
 
372
                 status_text=self.Switch.STATUS_LABELS[self.INHERIT],
 
373
                 conditions=[]),
 
374
        ]
 
375
        self.assertEqual(actual, expected)
 
376
 
 
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())
 
381
 
 
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()
 
387
        context = {
 
388
            "switches": the_switches,
 
389
            "switches_json": json.dumps(the_switches),
 
390
            "preflight_base_template": BASE_TEMPLATE,
 
391
            "preflight_table_class": TABLE_CLASS,
 
392
        }
 
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')
 
397
 
 
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())
 
402
 
 
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)