import unittest import turbogears from turbogears import testutil from turbogears.database import bind_metadata, metadata, session from transifex.controllers import Root import cherrypy from transifex.model import Repository, Branch, Module, User, Group cherrypy.root = Root() class TestModule(unittest.TestCase): """Set up the database accordingly for this set of tests.""" def setUp(self): """Set up the database for this set of tests.""" # Testing elements self.testrepo = u'foorepo2' self.testbranch = u'foobranch2' self.testmodule = u'foomodule2' bind_metadata() metadata.create_all() # Create a module session.begin() r = Repository(name=self.testrepo, description="%s desc" % self.testrepo, disabled=False) b = Branch(name=self.testbranch, description="%s desc" % self.testbranch, disabled=False) m = Module(name=self.testmodule, description="%s desc" % self.testmodule, disabled=False) u = User(user_name=u'admin', display_name=u'Adminy Administrator', email_address=u'admin@example.com', password=u'admin') g=Group(group_name=u'l10n-admin', display_name=u'Administrators') g.users.append(u) u = User(user_name=u'guest', display_name=u'Guesty Guest', email_address=u'guest@example.com', password=u'guest') g=Group(group_name=u'cvsl10n', display_name=u'Translators') g.users.append(u) m.repository = r m.branches.append(b) session.commit() turbogears.startup.startTurboGears() def tearDown(self): """Clean up the database.""" metadata.drop_all() turbogears.startup.stopTurboGears() class TestModuleVisitor(TestModule): """Test how module pages look to a visitor (non authenticated user).""" def test_modulelist(self): """Test the content of the Module list page.""" mods = Module.query().filter_by(disabled=False) testutil.create_request(u"/module/") page = cherrypy.response.body[0] assert "Module list" in page for m in mods: #FIXME: Do we really have to use encode here? assert m.name.encode() in page assert m.repository.name.encode() in page assert m.branches[0].name.encode() in page assert "Add a new module" not in page, \ "Assert that link for adding modules is not shown" def test_moduleinfo(self): """Test the content of the Module info page.""" m = Module.query().filter_by(name=self.testmodule).one() testutil.create_request(u"/module/%s" % self.testmodule) page = cherrypy.response.body[0] assert "Submit to %s" % m.name.encode() in page assert m.branches[0].name.encode() in page assert m.repository.name.encode() in page assert "Authenticate as a translator" in page def test_module_randomname(self): """Test the content of the Module page for an inexistent module.""" m = Module.query().filter_by(name=self.testmodule).one() testutil.create_request(u"/module/foobar") page = cherrypy.response.body[0] assert "There is no active module with this name." in page def test_module_add(self): """Test the content of the Module Add page.""" testutil.create_request("/module/add") response = cherrypy.response assert response.status == '403 Forbidden' assert "You must provide your credentials" in response.body[0] def test_modulepreview_nologin(self): """Test that module preview needs login.""" m = Module.query().filter_by(name=self.testmodule).one() testutil.create_request("/module/%s/preview" % self.testmodule) response = cherrypy.response assert response.status == '403 Forbidden' assert "You must provide your credentials" in response.body[0] class TestModuleAdmin(TestModule): def test_module_add_logged(self): """Test access to the Module Add page as Admin """ user = testutil.BrowsingSession() user.goto('/login?user_name=admin&password=admin&login=Login') user.goto("/module/add") assert 'Add a new module' in user.response def test_moduleinfo_logged_as_admin(self): """Test access to the Module Submit page as Admin """ m = Module.query().filter_by(name=self.testmodule).one() user = testutil.BrowsingSession() user.goto('/login?user_name=admin&password=admin&login=Login') user.goto(u"/module/%s" % self.testmodule) assert 'Submit translations' in user.response class TestModuleTranslator(TestModule): """Test how module pages look to a translator.""" def test_moduleinfo_logged(self): """Test access to the Module Submit page as Guest """ m = Module.query().filter_by(name=self.testmodule).one() user = testutil.BrowsingSession() user.goto('/login?user_name=guest&password=guest&login=Login') user.goto(u"/module/%s" % self.testmodule) assert 'Submit translations' in user.response