<commit_before>import json
import unittest

from exam import Exam, fixture
from flask import current_app as app

from zeus.config import db
from zeus.models import User

from .fixtures import Fixtures


class AuthMixin(object):
    @fixture
    def default_user(self):
        user = User(
            email='<EMAIL>',
        )
        db.session.add(user)
        db.session.commit()

        return user

    def login(self, user):
        with self.client.session_transaction() as session:
            session['uid'] = user.id.hex
            session['email'] = user.email

    def login_default(self):
        return self.login(self.default_user)

    def login_default_admin(self):
        return self.login(self.default_admin)


class TestCase(Exam, unittest.TestCase, Fixtures, AuthMixin):
    def setUp(self):
        super(TestCase, self).setUp()
        self.client = app.test_client()

    def from_json(self, response):
        assert response.headers['Content-Type'] == 'application/json'
        return json.loads(response.data)<commit_msg>correct session usage per docs<commit_after>import json
import unittest

from exam import Exam, fixture
from flask import current_app as app

from zeus.config import db
from zeus.models import User

from .fixtures import Fixtures


class AuthMixin(object):
    @fixture
    def default_user(self):
        user = User(
            email='<EMAIL>',
        )
        db.session.add(user)
        db.session.commit()

        return user

    def login(self, user):
        with self.client.session_transaction() as session:
            session['uid'] = user.id.hex

    def login_default(self):
        return self.login(self.default_user)

    def login_default_admin(self):
        return self.login(self.default_admin)


class TestCase(Exam, unittest.TestCase, Fixtures, AuthMixin):
    def setUp(self):
        super(TestCase, self).setUp()
        self.client = app.test_client()
        # initiate a session
        self.client.__enter__()

    def tearDown(self):
        self.client.__exit__(None, None, None)
        super(TestCase, self).tearDown()

    def from_json(self, response):
        assert response.headers['Content-Type'] == 'application/json'
        return json.loads(response.data)
{'fraction_non_alphanumeric': 0.10092961487383798, 'fraction_numerical': 0.0, 'mean_word_length': 2.760399334442596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1324538', 'n_tokens_mistral': 667, 'n_tokens_neox': 650, 'n_words': 166}