"""Tests what happens if the config file is invalid."""

import os
import tempfile
from ppp_core import app
from webtest import TestApp
from unittest import TestCase
from ppp_core.exceptions import InvalidConfig

class InvalidConfFileTestCase(TestCase):
    def testNoConfFile(self):
        self.app = TestApp(app)
        obj = {'language': 'en', 'tree': {}}
        self.assertRaises(InvalidConfig, self.app.post_json,
                          '/', obj, status='*')

    def testEmptyConfFile(self):
        self.app = TestApp(app)
        self.config_file = tempfile.NamedTemporaryFile('w+')
        os.environ['PPP_CORE_CONFIG'] = self.config_file.name
        obj = {'language': 'en', 'tree': {}}
        self.assertRaises(InvalidConfig, self.app.post_json,
                          '/', obj, status='*')
        del os.environ['PPP_CORE_CONFIG']
        del self.config_file

    def testModuleWithNoName(self):
        self.app = TestApp(app)
        self.config_file = tempfile.NamedTemporaryFile('w+')
        self.config_file.write('''{"debug": true, "modules": [{
            "url": "http://foo/bar/"}]}''')
        self.config_file.seek(0)
        os.environ['PPP_CORE_CONFIG'] = self.config_file.name
        obj = {'language': 'en', 'tree': {}}
        self.assertRaises(InvalidConfig, self.app.post_json,
                          '/', obj, status='*')
        del os.environ['PPP_CORE_CONFIG']
        del self.config_file

    def testModuleWithNoUrl(self):
        self.app = TestApp(app)
        self.config_file = tempfile.NamedTemporaryFile('w+')
        self.config_file.write('''{"debug": true, "modules": [{
            "name": "foo"}]}''')
        self.config_file.seek(0)
        os.environ['PPP_CORE_CONFIG'] = self.config_file.name
        obj = {'language': 'en', 'tree': {}}
        self.assertRaises(InvalidConfig, self.app.post_json,
                          '/', obj, status='*')
        del os.environ['PPP_CORE_CONFIG']
        del self.config_file

@@ -212,23 +212,18 @@
 %0A%0Aclass 
-Invalid
+No
 ConfFile
@@ -463,40 +463,122 @@
 ')%0A%0A
-    def testEmptyConfFile(self):
+class InvalidConfFileTestCase(TestCase):%0A    def setUp(self):%0A        super(InvalidConfFileTestCase, self).setUp()
 %0A   
@@ -737,358 +737,411 @@
     
-    obj = %7B'language': 'en', 'tree': %7B%7D%7D%0A        self.assertRaises(InvalidConfig, self.app.post_json,%0A                          '/', obj, status='*')%0A        del os.environ%5B'PPP_CORE_CONFIG'%5D%0A        del self.config_file%0A%0A    def testModuleWithNoName(self):%0A        self.app = TestApp(app)%0A        self.config_file = tempfile.NamedTemporaryFile('w+')
+def tearDown(self):%0A        del os.environ%5B'PPP_CORE_CONFIG'%5D%0A        self.config_file.close()%0A        del self.config_file%0A        super(InvalidConfFileTestCase, self).tearDown()%0A    def testEmptyConfFile(self):%0A        obj = %7B'language': 'en', 'tree': %7B%7D%7D%0A        self.assertRaises(InvalidConfig, self.app.post_json,%0A                          '/', obj, status='*')%0A%0A    def testModuleWithNoName(self):
 %0A   
@@ -1282,70 +1282,8 @@
 (0)%0A
-        os.environ%5B'PPP_CORE_CONFIG'%5D = self.config_file.name%0A
     
@@ -1436,207 +1436,43 @@
 *')%0A
-        del os.environ%5B'PPP_CORE_CONFIG'%5D%0A        del self.config_file%0A%0A    def testModuleWithNoUrl(self):%0A        self.app = TestApp(app)%0A        self.config_file = tempfile.NamedTemporaryFile('w+')
+%0A    def testModuleWithNoUrl(self):
 %0A   
@@ -1602,70 +1602,8 @@
 (0)%0A
-        os.environ%5B'PPP_CORE_CONFIG'%5D = self.config_file.name%0A
     
@@ -1756,75 +1756,4 @@
 *')%0A
-        del os.environ%5B'PPP_CORE_CONFIG'%5D%0A        del self.config_file%0A

Remove code duplication in tests.