<commit_before>from flask import url_for
from flask.ext import testing
import unittest
from steerclear import app, db
from steerclear.models import User

# name of templates used by the login module
LOGIN_TEMPLATE_NAME = 'login.html'

"""
SteerClearLoginTestCase
-----------------------
TestCase for testing the login module
"""
class SteerClearLoginTestCase(testing.TestCase):

    # do not render templates in responses
    render_templates = False

    """
    create_app
    ----------
    Creates an instance of the steerclear flask app
    and sets up testing context
    """
    def create_app(self):
        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = app.config['TEST_SQLALCHEMY_DATABASE_URI']
        return app

    """
    setUp
    -----
    called before each test function. creates new test database
    """
    def setUp(self):
        self.payload = {
            u'username': u'ryan',
            u'password': u'<PASSWORD>',
        }
        db.create_all()

    """
    tearDown
    --------
    called after each test function. breaks down test fixtures
    """
    def tearDown(self):
        db.session.remove()
        db.drop_all()

    """
    test_get_login_page
    -------------------
    Tests that GET request to login route 
    returns the login page
    """
    def test_get_login_page(self):
        response = self.client.get(url_for('login.login'))
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_failure_incorrect_username
    -------------------------------------
    Tests that login fails if the user supplies the wrong username
    """
    def test_login_failure_incorrect_username(self):
        # test with no Users in db
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

        # test with user that has different username but same password
        user = User(username='kyle', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_failure_incorrect_password
    -------------------------------------
    Tests that login fails if the user supplies the wrong password
    """
    def test_login_failure_incorrect_password(self):
        # test with no Users in db
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

        # test with user that has same username but different password
        user = User(username='ryan', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_success
    ------------------
    Tests that a user can login successfully
    """
    def test_login_success(self):
        user = User(username='ryan', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(
            url_for('login.login'), 
            data=self.payload
        )
        self.assertRedirects(response, url_for('driver_portal.index'))<commit_msg>added logout tests<commit_after>from flask import url_for
from flask.ext import testing
import unittest
from steerclear import app, db
from steerclear.models import User

# name of templates used by the login module
LOGIN_TEMPLATE_NAME = 'login.html'

"""
SteerClearLoginTestCase
-----------------------
TestCase for testing the login module
"""
class SteerClearLoginTestCase(testing.TestCase):

    # do not render templates in responses
    render_templates = False

    """
    create_app
    ----------
    Creates an instance of the steerclear flask app
    and sets up testing context
    """
    def create_app(self):
        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = app.config['TEST_SQLALCHEMY_DATABASE_URI']
        return app

    """
    setUp
    -----
    called before each test function. creates new test database
    """
    def setUp(self):
        self.payload = {
            u'username': u'ryan',
            u'password': u'<PASSWORD>',
        }
        db.create_all()

    """
    tearDown
    --------
    called after each test function. breaks down test fixtures
    """
    def tearDown(self):
        db.session.remove()
        db.drop_all()

    """
    test_get_login_page
    -------------------
    Tests that GET request to login route 
    returns the login page
    """
    def test_get_login_page(self):
        response = self.client.get(url_for('login.login'))
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_failure_incorrect_username
    -------------------------------------
    Tests that login fails if the user supplies the wrong username
    """
    def test_login_failure_incorrect_username(self):
        # test with no Users in db
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

        # test with user that has different username but same password
        user = User(username='kyle', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_failure_incorrect_password
    -------------------------------------
    Tests that login fails if the user supplies the wrong password
    """
    def test_login_failure_incorrect_password(self):
        # test with no Users in db
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

        # test with user that has same username but different password
        user = User(username='ryan', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(url_for('login.login'), data=self.payload)
        self.assertTemplateUsed(LOGIN_TEMPLATE_NAME)
        self.assertTrue(response.status_code, 200)

    """
    test_login_success
    ------------------
    Tests that a user can login successfully
    """
    def test_login_success(self):
        user = User(username='ryan', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        response = self.client.post(
            url_for('login.login'), 
            data=self.payload
        )
        self.assertRedirects(response, url_for('driver_portal.index'))

    """
    test_logout_requires_login
    --------------------------
    Tests that the logout route requires the user to be loged in
    """
    def test_logout_requires_login(self):
        response = self.client.get(url_for('login.logout'))
        self.assertEquals(response.status_code, 401)

    """
    test_logout_success
    -------------------
    Tests that a user can successfully logout
    """
    def test_logout_success(self):
        # login a user
        user = User(username='ryan', password='<PASSWORD>')
        db.session.add(user)
        db.session.commit()
        self.client.post(url_for('login.login'), data=self.payload)

        # logout user
        response = self.client.get(url_for('login.logout'))
        self.assertRedirects(response, url_for('login.login'))

        # make sure we are actually loged out
        response = self.client.get(url_for('login.logout'))
        self.assertEquals(response.status_code, 401)
{'fraction_non_alphanumeric': 0.13510534051935325, 'fraction_numerical': 0.004409603135717786, 'mean_word_length': 2.376757650951199, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3809251', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2258, 'n_words': 525}