def test_translate_local_sync_creation_selectlang(self):
        """
        Ensure that we can create an app normally through a synchronous POST request to SELECTLANG.
        Note that this test relies on the accessibility of a local i18n.xml file.
        """
        url = "appcomposer/tests_data/googleExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp", "appurl": url}, follow_redirects=True)

        # Check whether it seems to be the page we expect.
        assert rv.status_code == 200  # Page found code.
        assert rv.data.count("option") > 100  # Lots of them, because of the languages list.
        assert "submit" in rv.data

        # Check that we did indeed create the app properly.
        with self.flask_app:
            self.flask_app.get("/")
            app = api.get_app_by_name("UTApp")

            assert app is not None
            appdata = app.data

            data = json.loads(appdata)

            assert "spec" in data
            assert url == data["spec"]

            full_app_data = load_appdata_from_db(app)
            bm = BundleManager.create_from_existing_app(full_app_data)

            assert bm.get_gadget_spec() == url
            assert len(bm._bundles) > 3

            defaultBundle = bm.get_bundle("all_ALL_ALL")
            assert defaultBundle is not None
            assert len(defaultBundle.get_msgs()) > 6
    def test_load_appdata_from_db(self):
        """
        Test the method to load the appdata as if it was the old legacy JSON object from the database.
        :return:
        """
        data = {
            "spec": "http://justatest.com",
            "random_setting": 1,
            "bundles": {
                "all_ALL_ALL": {
                    "lang": "all",
                    "country": "ALL",
                    "target": "ALL",
                    "messages" : {
                        "key.one": "One"
                    }
                }
            }
        }
        self.tapp.data = json.dumps(data)
        db.session.add(self.tapp)
        db.session.commit()

        appdata = load_appdata_from_db(self.tapp)

        # The bundles should now be empty, because load_appdata_from_db should not rely on the data's bundles, but
        # on the db information (and for now there is none). The settings should be respected.
        self.assertIsNotNone(appdata)
        self.assertIn("random_setting", appdata)
        self.assertIn("bundles", appdata)
        self.assertTrue(len(appdata["bundles"]) == 0)


        # To further test info retrieval, we need to add info to the db.
        testBundle = Bundle("all", "ALL", "ALL")
        testBundle._msgs["key.three"] = "Three"
        testBundle._msgs["key.four"] = "Four"
        bm = BundleManager()
        bm._bundles = {}
        bm._bundles["all_ALL_ALL"] = testBundle
        save_bundles_to_db(self.tapp, bm)


        appdata = load_appdata_from_db(self.tapp)
        self.assertTrue(appdata["bundles"]["all_ALL_ALL"]["messages"]["key.three"] == "Three")
        self.assertTrue(appdata["bundles"]["all_ALL_ALL"]["messages"]["key.four"] == "Four")
    def test_translate_default_autoaccept(self):
        with self.flask_app:
            rv = self.login("testuser", "password")
            app = api.create_app("UTApp", "translate", "http://justatest.com", '{"spec":"http://justatest.com", "bundles":{}}')

            # Test that autoaccept is True (it's the default).
            full_app_data = load_appdata_from_db(app)
            bm = BundleManager.create_from_existing_app(full_app_data)
            assert bm.get_autoaccept() == True
    def test_translate_create_app_with_empty_default(self):
        """
        [REGRESSION TEST: If the DEFAULT XML is an invalid fail or empty, it was not being handled nicely]
        Ensure that we can create an app normally through a synchronous POST request to SELECTLANG.
        """
        url = "appcomposer/tests_data/relativeExampleEmptyDefault/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp", "appurl": url}, follow_redirects=True)

        # Check whether it seems to be the page we expect.
        assert rv.status_code == 200  # Page found code.
        assert rv.data.count("option") > 100  # Lots of them, because of the languages list.
        assert "submit" in rv.data

        # Check that we did indeed create the app properly.
        with self.flask_app:
            self.flask_app.get("/")
            app = api.get_app_by_name("UTApp")

            assert app is not None
            appdata = app.data

            data = json.loads(appdata)

            assert "spec" in data
            assert url == data["spec"]

            full_app_data = load_appdata_from_db(app)
            bm = BundleManager.create_from_existing_app(full_app_data)

            assert bm.get_gadget_spec() == url

            # The bundles should be 3 (DEFAULT - copied from English, English, German).
            print "BUNDLES: %r" % bm._bundles
            assert len(bm._bundles) == 3

            defaultBundle = bm.get_bundle("all_ALL_ALL")
            assert defaultBundle is not None
            assert len(defaultBundle.get_msgs()) > 6

            gerBundle = bm.get_bundle("de_ALL_ALL")
            assert gerBundle is not None
            assert len(gerBundle.get_msgs()) > 6

            enBundle = bm.get_bundle("en_ALL_ALL")
            assert enBundle is not None
            assert len(enBundle.get_msgs()) > 6

            # Ensure that the language that got copied as DEFAULT is really english
            assert enBundle.get_msgs()["hello_world"] == "Hello World."
            assert defaultBundle.get_msgs()["hello_world"] == "Hello World."
    def test_translate_create_with_multi_bundle(self):
        """
        Ensure that we can create an app where the xml contains two different bundles for the same language.
        """
        url = "appcomposer/tests_data/relativeMultibundleExample/i18n.xml"
        rv = self.flask_app.post("/composers/translate/selectlang", data={"appname": "UTApp", "appurl": url}, follow_redirects=True)

        # Check whether it seems to be the page we expect.
        assert rv.status_code == 200  # Page found code.
        assert rv.data.count("option") > 100  # Lots of them, because of the languages list.
        assert "submit" in rv.data

        # Check that we did indeed create the app properly.
        with self.flask_app:
            self.flask_app.get("/")
            app = api.get_app_by_name("UTApp")

            assert app is not None
            appdata = app.data

            data = json.loads(appdata)

            assert "spec" in data
            assert url == data["spec"]

            full_app_data = load_appdata_from_db(app)
            bm = BundleManager.create_from_existing_app(full_app_data)

            assert bm.get_gadget_spec() == url
            assert len(bm._bundles) == 2

            defaultBundle = bm.get_bundle("all_ALL_ALL")
            assert defaultBundle is not None
            assert len(defaultBundle.get_msgs()) > 6

            gerBundle = bm.get_bundle("de_ALL_ALL")
            assert gerBundle is not None
            assert len(gerBundle.get_msgs()) > 6
            msgs = gerBundle.get_msgs()

            # Ensure the translations in the first file are present
            assert msgs["hello_world"] == "Hallo Welt."

            # Ensure the translations in the second file are present as well
            assert msgs["gray"] == "Grau"