<commit_before>from flask import session

import appcomposer
import appcomposer.application


class TestLogin:
    def login(self, username, password, redirect=True):
        return self.flask_app.post('/login', data=dict(
            login=username,
            password=password
        ), follow_redirects=redirect)

    def logout(self):
        return self.flask_app.get('/logout', follow_redirects=True)

    def setUp(self):
        appcomposer.app.config['DEBUG'] = True
        appcomposer.app.config['TESTING'] = True
        appcomposer.app.config['CSRF_ENABLED'] = False
        appcomposer.app.config["SECRET_KEY"] = 'secret'

    def tearDown(self):
        pass

    def test_login_works(self):
        self.flask_app = appcomposer.app.test_client()
        with self.flask_app:
            rv = self.login("testuser", "password")
            assert rv.status_code == 200
            assert "logged_in" in session
            assert session["login"] == "testuser"
            assert session["name"] == "Test User"

    def test_login_fails_when_wrong_pass(self):
        self.flask_app = appcomposer.app.test_client()
        with self.flask_app:
            rv = self.login("testuser", "3413124141242142342442")
            assert "logged_in" not in session
            assert "login" not in session
            assert "name" not in session

    def test_login_fails_when_wrong_user(self):
        self.flask_app = appcomposer.app.test_client()
        with self.flask_app:
            rv = self.login("testuser23414", "21412412424")
            assert "logged_in" not in session
            assert "login" not in session
            assert "name" not in session

    def test_login_redir(self):
        self.flask_app = appcomposer.app.test_client()
        with self.flask_app:
            rv = self.login("testuser", "password", False)
            assert rv.status_code == 302
            assert rv.location == "http://localhost/user/"

    def test_login_not_wrong_redir(self):
        self.flask_app = appcomposer.app.test_client()
        with self.flask_app:
            rv = self.login("testuser", "<PASSWORD>", False)
            assert rv.status_code != 302
            assert rv.location != "http://localhost/user/"<commit_msg>[tests]: Login tests now use flask's test client __enter__ and __exit__ instead of with, which slightly improves readability<commit_after>from flask import session

import appcomposer
import appcomposer.application


class TestLogin:
    def __init__(self):
        self.flask_app = None

    def login(self, username, password, redirect=True):
        return self.flask_app.post('/login', data=dict(
            login=username,
            password=password
        ), follow_redirects=redirect)

    def logout(self):
        return self.flask_app.get('/logout', follow_redirects=True)

    def setUp(self):
        appcomposer.app.config['DEBUG'] = True
        appcomposer.app.config['TESTING'] = True
        appcomposer.app.config['CSRF_ENABLED'] = False
        appcomposer.app.config["SECRET_KEY"] = 'secret'
        self.flask_app = appcomposer.app.test_client()
        self.flask_app.__enter__()

    def tearDown(self):
        self.flask_app.__exit__(None, None, None)

    def test_login_works(self):
        rv = self.login("testuser", "password")
        assert rv.status_code == 200
        assert "logged_in" in session
        assert session["login"] == "testuser"
        assert session["name"] == "Test User"

    def test_login_fails_when_wrong_pass(self):
        rv = self.login("testuser", "<PASSWORD>")
        assert "logged_in" not in session
        assert "login" not in session
        assert "name" not in session

    def test_login_fails_when_wrong_user(self):
        rv = self.login("testuser23414", "214124<PASSWORD>")
        assert "logged_in" not in session
        assert "login" not in session
        assert "name" not in session

    def test_login_redir(self):
        rv = self.login("testuser", "password", False)
        assert rv.status_code == 302
        assert rv.location == "http://localhost/user/"

    def test_login_not_wrong_redir(self):
        rv = self.login("testuser", "<PASSWORD>", False)
        assert rv.status_code != 302
        assert rv.location != "http://localhost/user/"
{'fraction_non_alphanumeric': 0.10888785481619358, 'fraction_numerical': 0.015588645881805491, 'mean_word_length': 2.6339814032121724, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5262728', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1266, 'n_words': 289}