import os
import unittest
import urlparse

from bs4 import BeautifulSoup
from flask import url_for, _request_ctx_stack

from cla_public import app
from cla_public.apps import addressfinder_proxy
from cla_public.apps.checker import api
from cla_public.apps.checker.tests import test_integration as util
from cla_public.libs import laalaa, zendesk


class SmokeTests(unittest.TestCase):

    def setUp(self):
        config_file = 'config/docker.py'
        if os.environ.get('CLA_ENV') is None:
            config_file = 'config/common.py'
        self.app = app.create_app(config_file)
        self.app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] = False
        self.ctx = self.app.test_request_context()
        self.ctx.push()

    def tearDown(self):
        if _request_ctx_stack.top and _request_ctx_stack.top.preserved:
            _request_ctx_stack.top.pop()
        self.ctx.pop()
        self.ctx = None

    def test_can_access_addressfinder(self):
        "lookup a postcode with AddressFinder"
        with self.app.test_client() as client:
            addressfinder_proxy.lookup_postcode('sw1a1aa')

    def test_can_access_zendesk(self):
        "connect to Zendesk"
        with self.app.test_client() as client:
            zendesk.tickets()

    def test_can_access_laalaa(self):
        "search for legal advisers on LAALAA"
        with self.app.test_client() as client:
            laalaa.find('sw1a1aa')

    def test_can_access_sendgrid(self):
        "connect to SendGrid"
        with self.app.test_client() as client:
            self.app.mail.connect()

    def test_can_access_backend(self):
        "connect to the backend"
        with self.app.test_client() as client:
            conn = api.get_api_connection()

    def test_can_set_category(self):
        "submit the problem form"
        with self.app.test_client() as client:
            response = client.get(url_for('base.get_started'))
            url = urlparse.urlparse(response.location).path
            form_class = util.get_form(url)
            response = client.get(url)
            csrf_token = BeautifulSoup(response.data).find(id='csrf_token')['value']

            post_data = util.form_data(form_class, {'_law_area': 'Debt'})
            post_data['csrf_token'] = csrf_token
            response = client.post(url, data=post_data)
            self.assertEquals(response.status_code, 302)

@@ -1756,20 +1756,27 @@
 can_
+u
 se
-t_category
+_scope_diagnosis
 (sel
@@ -1792,31 +1792,27 @@
    %22
-submit the problem form
+use scope diagnosis
 %22%0A  
@@ -1995,397 +1995,1282 @@
     
-form_class = util.get_form(url)%0A            response = client.get(url)%0A            csrf_token = BeautifulSoup(response.data).find(id='csrf_token')%5B'value'%5D%0A%0A            post_data = util.form_data(form_class, %7B'_law_area': 'Debt'%7D)%0A            post_data%5B'csrf_token'%5D = csrf_token%0A            response = client.post(url, data=post_data)%0A            self.assertEquals(response.status_code, 302
+response = client.get(url)%0A%0A            # Check the number of scope options%0A            scope_options_item = BeautifulSoup(response.data).find_all(class_='scope-options-list-item')%0A            self.assertEquals(len(scope_options_item), 15)%0A%0A            # Follow the third option's link (Debt) and check the%0A            # number of options on the following screen%0A            response = client.get(scope_options_item%5B2%5D.find('a').get('href'))%0A            scope_options_item = BeautifulSoup(response.data).find_all(class_='scope-options-list-item')%0A            self.assertEquals(len(scope_options_item), 4)%0A%0A            # Follow the first option's link (You own your own home)%0A            # and check the number of options on the following screen%0A            response = client.get(scope_options_item%5B0%5D.find('a').get('href'))%0A            scope_options_item = BeautifulSoup(response.data).find_all(class_='scope-options-list-item')%0A            self.assertEquals(response.status_code, 200)%0A%0A            # Follow the first option's link (Yes) and is redirected to About you page%0A            response = client.get(scope_options_item%5B0%5D.find('a').get('href'))%0A            self.assertEquals(response.status_code, 302)%0A            self.assertTrue(str.find(response.data, '/about') %3E -1
 )%0A

Fix Scope diagnosis smoke test