1

I've been developing flask apps but never got around using unittest. I am trying to write a test like below:

import os
import unittest

from nextg_project_source import app, db, mail

TEST_DB = 'test.db'
app.config['BASEDIR'] = os.path.abspath(os.path.dirname(__file__))

class BasicTests(unittest.TestCase):
    def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_METHODS'] = []
        app.config['DEBUG'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+ os.path.join(app.config['BASEDIR'], TEST_DB)
        self.app = app.test_client()
        db.drop_all()
        db.create_all()

        mail.init_app(app)
        self.assertEqual(app.debug, False)

    def tearDown(self):
        pass

    def login(self, email, password):
        return self.app.post('/login', data=dict(email=email, password=password,
                                                 follow_redirects=True))

    def vendor_register(self, email, password, confirm, op):
        return self.app.post(
            '/myregistration',
            data=dict(email=email,
                      password= password,
                      confirm=confirm,
                      op='3'),
                      follow_redirects=True)

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


    def test_main_page(self):
        response = self.app.get('/', follow_redirects=True)
        self.assertEqual(response.status_code, 200)

    def test_valid_user_registration(self):
         response = self.vendor_register('valid@gmail.com',
                                         'password',
                                         'password',
                                         '3')
         self.assertEqual(response.status_code, 200)
         self.assertIn(b'Thanks for signing up', response.data)
    def test_user_login(self):
        response= self.login(‘valid@gmail.com', 'password')
        self.assertEqual(response.status_code, 200)

if __name__ == "__main__":
    unittest.main()

In my flask app, I am using Flask-Security and I have customised the registration form where submit request is POST'ed to "/myregistration" with an additional field "op" . I have couple of questions

  1. I am using MySql as my backend but using sqlite here. Hope thats ok?
  2. Since I use CSRF_TOKEN in my form, I need to disable it here. I don't understand how server can process the request without this while testing? Does app.config['TESTING'] = True prevent it from this?
  3. I see that test_user_login passes even if I give wrong username and password. I feel I am missing something... this is supposed to connect to the sqlite DB and check in there right? is it doing that ?
  4. Even if the username and password is wrong, I would get 200 which is why I see it passes, how do I error out if its wrong?
  5. For test_valid_user_registration gives me this :

    Traceback (most recent call last): File "tests_basic.py", line 55, in test_valid_user_registration self.assertEqual(response.status_code, 200) AssertionError: 400 != 200

  6. When I say assertIn(b'Thanks for signing up'), is this looking for flash message I use when I redirect to new page like /thanks?

Thanks

vkk07
  • 69
  • 1
  • 10

0 Answers0