<commit_before>#!/usr/bin/python

import json
import re
import urllib
import appcomposer
import appcomposer.application

from appcomposer.appstorage import api
from appcomposer.appstorage.api import get_app_by_name


class TestTranslateAppMerge:
    def __init__(self):
        self.flask_app = None
        self.tapp = None
        self.firstApp = None
        self.secondApp = None

    def login(self, username, password):
        return self.flask_app.post('/login', data=dict(
            login=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.flask_app.get('/logout', follow_redirects=True)

    def _cleanup(self):
        """
        Does cleanup tasks in case the tests failed before.
        Can be invoked *before* and *after* the tests.
        """
        self.flask_app.get("/")  # This is required to create a context. Otherwise session etc don't exist.

        if self.firstApp is not None:
            app = api.get_app(self.firstApp.unique_id)
            if app is not None:
                api.delete_app(app)

        if self.secondApp is not None:
            app = api.get_app(self.secondApp.unique_id)
            if app is not None:
                api.delete_app(app)

    def setUp(self):
        appcomposer.app.config['DEBUG'] = True
        appcomposer.app.config['TESTING'] = True
        appcomposer.app.config['CSRF_ENABLED'] = False
        appcomposer.app.config["SECRET_KEY"] = 'secret'
        self.flask_app = appcomposer.app.test_client()

        self.flask_app.__enter__()

        # In case the test failed before, start from a clean state.
        self._cleanup()

        self.flask_app.get("/")
        rv = self.login("testuser", "password")

        # Create the PARENT app
        url = "appcomposer/tests_data/relativeExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp", "appurl": url}, follow_redirects=True)

        # Create the CHILDREN app
        url = "appcomposer/tests_data/relativeExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp2", "appurl": url}, follow_redirects=True)

        # We need to be in the flask client context to get app by name.
        self.flask_app.get("/")
        self.firstApp = get_app_by_name("UTApp")
        self.secondApp = get_app_by_name("UTApp2")

        # *** Force the creation of a proposal into the firstApp (the parent app).
        # Set Autoaccept to False
        url = u"/composers/translate/config/autoaccept/" + self.firstApp.unique_id
        rv = self.flask_app.post(url, data={"value": 0})
        assert rv.status_code == 200
        # Edit the child app and propose changes.
        url = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % (self.secondApp.unique_id)
        rv = self.flask_app.get(url)
        posturl = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % self.secondApp.unique_id
        postdata = {"appid": self.secondApp.unique_id,
                    "srclang": "all_ALL",
                    "targetlang": "all_ALL",
                    "srcgroup": "ALL",
                    "targetgroup": "ALL",
                    "_message_blue": "Blue",
                    "_message_hello_world": "Hello Test World",
                    "save": "",
                    "proposeToOwner": "true"}
        rv = self.flask_app.post(posturl, data=postdata)
        assert rv.status_code == 200
        # Access the parent's selectlang to see the proposal button.
        url = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % (self.firstApp.unique_id)
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data.decode("utf8")  # This bypasses an apparent utf8 FlaskClient bug.
        assert """class="badge" style>1</span>"""



    def tearDown(self):
        self._cleanup()
        self.flask_app.__exit__(None, None, None)

    def test_proposal_screen_basic(self):
        """
        Check that we can access the proposal screen and see a proposal.
        """
        # Access the merge screen itself.
        url = u"/composers/translate/proposed_list?appid=%s" % self.firstApp.unique_id
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data.decode("utf8")

        # Ensure that the translation appears on the list.
        assert "testuser's all_ALL_ALL" in data

        # Ensure there is a single translation.
        assert "translation (1)" in data

    def test_ajax_get_proposal(self):
        # Access the merge screen itself.
        url = u"/composers/translate/proposed_list?appid=%s" % self.firstApp.unique_id
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data.decode("utf8")

        # Extract the proposal ID.
        matches = re.findall("""option value="([0-9]+)">""", data)
        proposal_id = int(matches[0])


        # Get the proposal info through AJAX.
        url = u"/composers/translate/get_proposal?proposal_id=%d" % proposal_id
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data

        # Check that the proposal description is the valid JSON we expect,
        # and that the contents of the proposal are what we expect.
        proposal_data = json.loads(data)
        proposal = proposal_data["proposal"]
        assert proposal["from"] == "testuser"<commit_msg>Yet another fix to the tests<commit_after>#!/usr/bin/python

import json
import re
import unittest
import urllib
import appcomposer
import appcomposer.application

from appcomposer.appstorage import api
from appcomposer.appstorage.api import get_app_by_name


class TestTranslateAppMerge(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestTranslateAppMerge, self).__init__(*args, **kwargs)

        self.flask_app = None
        self.tapp = None
        self.firstApp = None
        self.secondApp = None

    def login(self, username, password):
        return self.flask_app.post('/login', data=dict(
            login=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.flask_app.get('/logout', follow_redirects=True)

    def _cleanup(self):
        """
        Does cleanup tasks in case the tests failed before.
        Can be invoked *before* and *after* the tests.
        """
        self.flask_app.get("/")  # This is required to create a context. Otherwise session etc don't exist.

        if self.firstApp is not None:
            app = api.get_app(self.firstApp.unique_id)
            if app is not None:
                api.delete_app(app)

        if self.secondApp is not None:
            app = api.get_app(self.secondApp.unique_id)
            if app is not None:
                api.delete_app(app)

    def setUp(self):
        appcomposer.app.config['DEBUG'] = True
        appcomposer.app.config['TESTING'] = True
        appcomposer.app.config['CSRF_ENABLED'] = False
        appcomposer.app.config["SECRET_KEY"] = 'secret'
        self.flask_app = appcomposer.app.test_client()

        self.flask_app.__enter__()

        # In case the test failed before, start from a clean state.
        self._cleanup()

        self.flask_app.get("/")
        rv = self.login("testuser", "password")

        # Create the PARENT app
        url = "appcomposer/tests_data/relativeExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp", "appurl": url}, follow_redirects=True)

        # Create the CHILDREN app
        url = "appcomposer/tests_data/relativeExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp2", "appurl": url}, follow_redirects=True)

        # We need to be in the flask client context to get app by name.
        self.flask_app.get("/")
        self.firstApp = get_app_by_name("UTApp")
        self.secondApp = get_app_by_name("UTApp2")

        # *** Force the creation of a proposal into the firstApp (the parent app).
        # Set Autoaccept to False
        url = u"/composers/translate/config/autoaccept/" + self.firstApp.unique_id
        rv = self.flask_app.post(url, data={"value": 0})
        assert rv.status_code == 200
        # Edit the child app and propose changes.
        url = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % (self.secondApp.unique_id)
        rv = self.flask_app.get(url)
        posturl = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % self.secondApp.unique_id
        postdata = {"appid": self.secondApp.unique_id,
                    "srclang": "all_ALL",
                    "targetlang": "all_ALL",
                    "srcgroup": "ALL",
                    "targetgroup": "ALL",
                    "_message_blue": "Blue",
                    "_message_hello_world": "Hello Test World",
                    "save": "",
                    "proposeToOwner": "true"}
        rv = self.flask_app.post(posturl, data=postdata)
        self.assertEquals(302, rv.status_code)

        # Access the parent's selectlang to see the proposal button.
        url = u"/composers/translate/edit?appid=%s&srclang=all_ALL&editSelectedSourceButton=&targetlang=all_ALL&srcgroup=ALL&targetgroup=ALL" % (self.firstApp.unique_id)
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data.decode("utf8")  # This bypasses an apparent utf8 FlaskClient bug.
        assert """class="badge" style>1</span>"""



    def tearDown(self):
        self._cleanup()
        self.flask_app.__exit__(None, None, None)

    def test_proposal_screen_basic(self):
        """
        Check that we can access the proposal screen and see a proposal.
        """
        # Access the merge screen itself.
        url = u"/composers/translate/proposed_list?appid=%s" % self.firstApp.unique_id
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data.decode("utf8")

        # Ensure that the translation appears on the list.
        assert "testuser's all_ALL_ALL" in data

        # Ensure there is a single translation.
        assert "translation (1)" in data

    def test_ajax_get_proposal(self):
        # Access the merge screen itself.
        url = u"/composers/translate/proposed_list?appid=%s" % self.firstApp.unique_id
        rv = self.flask_app.get(url)
        print "STATUS CODE IS: ", rv.status_code
        assert rv.status_code == 200
        data = rv.data.decode("utf8")

        # Extract the proposal ID.
        matches = re.findall("""option value="([0-9]+)">""", data)
        proposal_id = int(matches[0])


        # Get the proposal info through AJAX.
        url = u"/composers/translate/get_proposal?proposal_id=%d" % proposal_id
        rv = self.flask_app.get(url)
        assert rv.status_code == 200
        data = rv.data

        # Check that the proposal description is the valid JSON we expect,
        # and that the contents of the proposal are what we expect.
        proposal_data = json.loads(data)
        proposal = proposal_data["proposal"]
        assert proposal["from"] == "testuser"
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.005843430437397955, 'mean_word_length': 2.5022569966897383, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1360430', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3485, 'n_words': 825}