import urllib

from django.test.client import Client
from django.core.urlresolvers import reverse

from go.base.tests.utils import VumiGoDjangoTestCase
from go.base import utils as base_utils
from go.base.utils import get_router_view_definition
from go.vumitools.router.models import ROUTER_ARCHIVED


class RouterViewsTestCase(VumiGoDjangoTestCase):
    use_riak = True

    def setUp(self):
        super(RouterViewsTestCase, self).setUp()
        self.setup_api()
        self.user = self.mk_django_user()
        self.user_api = base_utils.vumi_api_for_user(self.user)

        self.client = Client()
        self.client.login(username=self.user.username, password='password')

    def get_view_url(self, view, router_key):
        view_def = get_router_view_definition('keyword', None)
        return view_def.get_view_url(view, router_key=router_key)

    def mk_router(self, **kw):
        params = {
            'router_type': u'keyword',
            'name': u'kwr',
            'description': u'keyword router',
            'config': {},
        }
        params.update(kw)
        return self.user_api.new_router(**params)

    def test_index(self):
        router = self.mk_router()
        archived_router = self.mk_router(
            name=u'archived', archive_status=ROUTER_ARCHIVED)
        response = self.client.get(reverse('routers:index'))
        self.assertContains(response, urllib.quote(router.key))
        self.assertNotContains(response, urllib.quote(archived_router.key))

    def test_get_new_router(self):
        response = self.client.get(reverse('routers:new_router'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Router name')
        self.assertContains(response, 'kind of router')
        self.assertContains(response, 'keyword')
        self.assertNotContains(response, 'bulk_message')

    def test_post_new_router(self):
        router_data = {
            'name': 'new router',
            'router_type': 'keyword',
        }
        response = self.client.post(reverse('routers:new_router'), router_data)
        [router] = self.user_api.active_routers()
        self.assertRedirects(response, self.get_view_url('edit', router.key))
        self.assertEqual(router.name, 'new router')
        self.assertEqual(router.router_type, 'keyword')

    def test_show_router_missing(self):
        response = self.client.get(self.get_view_url('show', u'missingrouter'))
        self.assertEqual(response.status_code, 404)

    def test_show_router(self):
        router = self.mk_router()
        response = self.client.get(self.get_view_url('show', router.key))
        self.assertContains(response, router.name)
        self.assertContains(response, router.description)

    def test_archive_router(self):
        router = self.mk_router()
        self.assertFalse(router.archived())
        response = self.client.post(self.get_view_url('archive', router.key))
        self.assertRedirects(response, reverse('routers:index'))
        router = self.user_api.get_router(router.key)
        self.assertTrue(router.archived())

@@ -12,46 +12,8 @@
 ib%0A%0A
-from django.test.client import Client%0A
 from
@@ -53,16 +53,16 @@
 reverse%0A
+
 %0Afrom go
@@ -111,48 +111,8 @@
 ase%0A
-from go.base import utils as base_utils%0A
 from
@@ -399,207 +399,51 @@
 elf.
-u
 se
-r = self.mk_django_user()%0A        self.user_api = base_utils.vumi_api_for_user(self.user)%0A%0A        self.client = Client()%0A        self.client.login(username=self.user.username, password='password'
+tup_user_api()%0A        self.setup_client(
 )%0A%0A 

Fix router tests.