hg-review / bundled / flask / examples / minitwit /

# -*- coding: utf-8 -*-
    MiniTwit Tests

    Tests the MiniTwit application.

    :copyright: (c) 2010 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
import os
import minitwit
import unittest
import tempfile

class MiniTwitTestCase(unittest.TestCase):

    def setUp(self):
        """Before each test, set up a blank database"""
        self.db_fd,['DATABASE'] = tempfile.mkstemp() =

    def tearDown(self):
        """Get rid of the database again after each test."""

    # helper functions

    def register(self, username, password, password2=None, email=None):
        """Helper function to register a user"""
        if password2 is None:
            password2 = password
        if email is None:
            email = username + ''
        return'/register', data={
            'username':     username,
            'password':     password,
            'password2':    password2,
            'email':        email,
        }, follow_redirects=True)

    def login(self, username, password):
        """Helper function to login"""
        return'/login', data={
            'username': username,
            'password': password
        }, follow_redirects=True)

    def register_and_login(self, username, password):
        """Registers and logs in in one go"""
        self.register(username, password)
        return self.login(username, password)

    def logout(self):
        """Helper function to logout"""
        return'/logout', follow_redirects=True)

    def add_message(self, text):
        """Records a message"""
        rv ='/add_message', data={'text': text},
        if text:
            assert 'Your message was recorded' in
        return rv

    # testing functions

    def test_register(self):
        """Make sure registering works"""
        rv = self.register('user1', 'default')
        assert 'You were successfully registered ' \
               'and can login now' in
        rv = self.register('user1', 'default')
        assert 'The username is already taken' in
        rv = self.register('', 'default')
        assert 'You have to enter a username' in
        rv = self.register('meh', '')
        assert 'You have to enter a password' in
        rv = self.register('meh', 'x', 'y')
        assert 'The two passwords do not match' in
        rv = self.register('meh', 'foo', email='broken')
        assert 'You have to enter a valid email address' in

    def test_login_logout(self):
        """Make sure logging in and logging out works"""
        rv = self.register_and_login('user1', 'default')
        assert 'You were logged in' in
        rv = self.logout()
        assert 'You were logged out' in
        rv = self.login('user1', 'wrongpassword')
        assert 'Invalid password' in
        rv = self.login('user2', 'wrongpassword')
        assert 'Invalid username' in

    def test_message_recording(self):
        """Check if adding messages works"""
        self.register_and_login('foo', 'default')
        self.add_message('test message 1')
        self.add_message('<test message 2>')
        rv ='/')
        assert 'test message 1' in
        assert '&lt;test message 2&gt;' in

    def test_timelines(self):
        """Make sure that timelines work"""
        self.register_and_login('foo', 'default')
        self.add_message('the message by foo')
        self.register_and_login('bar', 'default')
        self.add_message('the message by bar')
        rv ='/public')
        assert 'the message by foo' in
        assert 'the message by bar' in

        # bar's timeline should just show bar's message
        rv ='/')
        assert 'the message by foo' not in
        assert 'the message by bar' in

        # now let's follow foo
        rv ='/foo/follow', follow_redirects=True)
        assert 'You are now following &#34;foo&#34;' in

        # we should now see foo's message
        rv ='/')
        assert 'the message by foo' in
        assert 'the message by bar' in

        # but on the user's page we only want the user's message
        rv ='/bar')
        assert 'the message by foo' not in
        assert 'the message by bar' in
        rv ='/foo')
        assert 'the message by foo' in
        assert 'the message by bar' not in

        # now unfollow and check if that worked
        rv ='/foo/unfollow', follow_redirects=True)
        assert 'You are no longer following &#34;foo&#34;' in
        rv ='/')
        assert 'the message by foo' not in
        assert 'the message by bar' in

if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.