import unittest
from selenium import webdriver
from selenium.webdriver.firefox import firefox_binary
from selenium.common.exceptions import WebDriverException
from multiprocessing import Process
import socket
import shutil
import os
import gnupg
import urllib2
import sys


# Set environment variable so config.py uses a test environment
os.environ['SECUREDROP_ENV'] = 'test'
import config

import source
import journalist
from tests import common
import urllib2

import signal
import traceback
from datetime import datetime
import time
import mock

os.environ['SECUREDROP_ENV'] = 'test'


class FunctionalTest():

    def _unused_port(self):
        s = socket.socket()
        s.bind(("localhost", 0))
        port = s.getsockname()[1]
        s.close()
        return port

    def _create_webdriver(self):
        log_file = open('tests/log/firefox.log', 'a')
        log_file.write(
            '\n\n[%s] Running Functional Tests\n' % str(
                datetime.now()))
        log_file.flush()
        firefox = firefox_binary.FirefoxBinary(log_file=log_file)
        return webdriver.Firefox(firefox_binary=firefox)

    def setUp(self):
        # Patch the two-factor verification to avoid intermittent errors
        patcher = mock.patch('journalist.Journalist.verify_token')
        self.addCleanup(patcher.stop)
        self.mock_journalist_verify_token = patcher.start()
        self.mock_journalist_verify_token.return_value = True

        signal.signal(signal.SIGUSR1, lambda _, s: traceback.print_stack(s))

        common.create_directories()
        self.gpg = common.init_gpg()
        common.init_db()

        source_port = self._unused_port()
        journalist_port = self._unused_port()

        self.source_location = "http://localhost:%d" % source_port
        self.journalist_location = "http://localhost:%d" % journalist_port

        def start_source_server():
            source.app.run(
                port=source_port,
                debug=True,
                use_reloader=False,
                threaded=True)

        def start_journalist_server():
            journalist.app.run(
                port=journalist_port,
                debug=True,
                use_reloader=False,
                threaded=True)

        self.source_process = Process(target=start_source_server)
        self.journalist_process = Process(target=start_journalist_server)

        self.source_process.start()
        self.journalist_process.start()

        self.driver = self._create_webdriver()

        # Poll the DOM briefly to wait for elements. It appears .click() does
        # not always do a good job waiting for the page to load, or perhaps
        # Firefox takes too long to render it (#399)
        self.driver.implicitly_wait(5)

        self.secret_message = 'blah blah blah'

    def tearDown(self):
        common.clean_root()
        self.driver.quit()
        self.source_process.terminate()
        self.journalist_process.terminate()

    def wait_for(self, function_with_assertion, timeout=5):
        """Polling wait for an arbitrary assertion."""
        # Thanks to
        # http://chimera.labs.oreilly.com/books/1234000000754/ch20.html#_a_common_selenium_problem_race_conditions
        start_time = time.time()
        while time.time() - start_time < timeout:
            try:
                return function_with_assertion()
            except (AssertionError, WebDriverException):
                time.sleep(0.1)
        # one more try, which will raise any errors if they are outstanding
        return function_with_assertion()

@@ -547,47 +547,8 @@
 ck%0A%0A
-os.environ%5B'SECUREDROP_ENV'%5D = 'test'%0A%0A
 %0Acla

Remove old test setting of env var