level12 / keg-app-cookiecutter

0 stars 0 forks source link

Add authenticated test app to test_views.py #44

Closed rsyring closed 5 years ago

rsyring commented 6 years ago
class ViewBase(object):
    uses_auth = False

    @classmethod
    def testapp_login(cls, user=None):
        if not user:
            user = ents.User.testing_create()
        ta = webtest.TestApp(flask.current_app)
        resp = ta.get('/login')
        resp.form['email'] = user.email
        resp.form['password'] = user._plaintext_pass
        resp = resp.form.submit()
        assert ('success', 'Login successful.') in resp.flashes
        return ta, user

    @classmethod
    def setup_class(cls):
        # anonymous user
        cls.ta = webtest.TestApp(flask.current_app)
        if cls.uses_auth:
            cls.authta, cls.auth_user = cls.testapp_login()

class TestPublic(ViewBase):

    def test_home(self):
        resp = self.ta.get('/')
        assert resp.status_code == 302
        assert '/login' in resp.headers['Location']

    @mock.patch('tang.views.public.log', autospec=True, spec_set=True)
    def test_ping_db(self, m_log):
        resp = self.ta.get('/ping-db')
        assert 'tang ok' in resp.text
        m_log.info.assert_called_once_with('ping-db ok')

class TestPrivateViews(ViewBase):
    uses_auth = True

    def test_something(self):
       pass
guruofgentoo commented 6 years ago

FWIW, there is a helper in KegAuth for this, on the branch I've been working on.

guruofgentoo commented 5 years ago

Keg-Auth is well-integrated now with the cookiecutter app, so we can close this.

Addressed in #62