import unittest from wanikaniburned import app, db, User from flask.ext.testing import TestCase class BaseTestCase(TestCase): def create_app(self): app.config.update( TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:', SECRET_KEY="testkey", WTF_CSRF_ENABLED=False, PRESERVE_CONTEXT_ON_EXCEPTION=False, HASH_ROUNDS=1 ) return app def setUp(self): db.create_all() def tearDown(self): db.session.remove() db.drop_all() class TestLogin(BaseTestCase): def login(self): user = User('test', False) return self.client.post('/', data={'api_key': user.api_key}) def test_user_login(self): response = self.login() self.assertRedirects(response, '/quiz') def test_user_logout(self): response = self.login() self.assertRedirects(response, '/quiz') response = self.client.get('/logout') self.assertRedirects(response, '/') def test_homepage_without_login(self): response = self.client.get('/') self.assert200(response) self.assertTemplateUsed("welcome.html") def test_quiz_inaccessible_without_login(self): response = self.client.get('/quiz') self.assertRedirects(response, '/') def test_user_items_inaccessible_without_login(self): response = self.client.get('/user_items') self.assertRedirects(response, '/') def test_homepage_with_existing_login(self): response = self.login() # Login self.assertRedirects(response, '/quiz') response = self.client.get('/') # Try to go back to the homepage self.assertRedirects(response, '/quiz') def test_user_items_with_existing_login(self): response = self.login() # Login self.assertRedirects(response, '/quiz') response = self.client.get('/user_items') self.assert200(response) if __name__ == '__main__': unittest.main()