from tests.app_tests import BaseTestCase
from app.mod_auth.models import *
from app.mod_auth.views import user_is_logged_in
from flask import url_for

USERNAME = 'username'
PASSWORD = 'password'
INVALID_USERNAME = 'wrong_username'
INVALID_PASSWORD = 'wrong_password'


class TestAuth(BaseTestCase):
    def setUp(self):
        super().setUp()
        user = User(username=USERNAME, password=PASSWORD)
        db.session.add(user)
        db.session.commit()

    def login(self, username, password):
        return self.client.post(url_for('auth.login'), data=dict(
            username=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.client.get(url_for('auth.logout', follow_redirects=True))

    def test_login(self):
        with self.client:
            self.login(INVALID_USERNAME, PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(INVALID_USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())

    def test_logout(self):
        with self.client:
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())
            self.logout()
            self.assertFalse(user_is_logged_in())

from tests.app_tests import BaseTestCase
from app.mod_auth.models import *
from app.mod_auth.views import user_is_logged_in
from flask import url_for

USERNAME = 'username'
PASSWORD = 'password'
INVALID_USERNAME = 'wrong_username'
INVALID_PASSWORD = 'wrong_password'


class TestAuth(BaseTestCase):
    def setUp(self):
        super().setUp()
        User.create(username=USERNAME, password=PASSWORD)

    def login(self, username, password):
        return self.client.post(url_for('auth.login'), data=dict(
            username=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.client.get(url_for('auth.logout', follow_redirects=True))

    def test_login(self):
        with self.client:
            self.login(INVALID_USERNAME, PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(INVALID_USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())

    def test_logout(self):
        with self.client:
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())
            self.logout()
            self.assertFalse(user_is_logged_in())

Use BaseModel.create to create a test user
Use BaseModel.create to create a test user

  from tests.app_tests import BaseTestCase
  from app.mod_auth.models import *
  from app.mod_auth.views import user_is_logged_in
  from flask import url_for
  
  USERNAME = 'username'
  PASSWORD = 'password'
  INVALID_USERNAME = 'wrong_username'
  INVALID_PASSWORD = 'wrong_password'
  
  
  class TestAuth(BaseTestCase):
      def setUp(self):
          super().setUp()
-         user = User(username=USERNAME, password=PASSWORD)
+         User.create(username=USERNAME, password=PASSWORD)
-         db.session.add(user)
-         db.session.commit()
  
      def login(self, username, password):
          return self.client.post(url_for('auth.login'), data=dict(
              username=username,
              password=password
          ), follow_redirects=True)
  
      def logout(self):
          return self.client.get(url_for('auth.logout', follow_redirects=True))
  
      def test_login(self):
          with self.client:
              self.login(INVALID_USERNAME, PASSWORD)
              self.assertFalse(user_is_logged_in())
              self.login(USERNAME, INVALID_PASSWORD)
              self.assertFalse(user_is_logged_in())
              self.login(INVALID_USERNAME, INVALID_PASSWORD)
              self.assertFalse(user_is_logged_in())
              self.login(USERNAME, PASSWORD)
              self.assertTrue(user_is_logged_in())
  
      def test_logout(self):
          with self.client:
              self.login(USERNAME, PASSWORD)
              self.assertTrue(user_is_logged_in())
              self.logout()
              self.assertFalse(user_is_logged_in())
  
Use BaseModel.create to create a test user
## Code Before:
from tests.app_tests import BaseTestCase
from app.mod_auth.models import *
from app.mod_auth.views import user_is_logged_in
from flask import url_for

USERNAME = 'username'
PASSWORD = 'password'
INVALID_USERNAME = 'wrong_username'
INVALID_PASSWORD = 'wrong_password'


class TestAuth(BaseTestCase):
    def setUp(self):
        super().setUp()
        user = User(username=USERNAME, password=PASSWORD)
        db.session.add(user)
        db.session.commit()

    def login(self, username, password):
        return self.client.post(url_for('auth.login'), data=dict(
            username=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.client.get(url_for('auth.logout', follow_redirects=True))

    def test_login(self):
        with self.client:
            self.login(INVALID_USERNAME, PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(INVALID_USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())

    def test_logout(self):
        with self.client:
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())
            self.logout()
            self.assertFalse(user_is_logged_in())

## Instruction:
Use BaseModel.create to create a test user
## Code After:
from tests.app_tests import BaseTestCase
from app.mod_auth.models import *
from app.mod_auth.views import user_is_logged_in
from flask import url_for

USERNAME = 'username'
PASSWORD = 'password'
INVALID_USERNAME = 'wrong_username'
INVALID_PASSWORD = 'wrong_password'


class TestAuth(BaseTestCase):
    def setUp(self):
        super().setUp()
        User.create(username=USERNAME, password=PASSWORD)

    def login(self, username, password):
        return self.client.post(url_for('auth.login'), data=dict(
            username=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.client.get(url_for('auth.logout', follow_redirects=True))

    def test_login(self):
        with self.client:
            self.login(INVALID_USERNAME, PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(INVALID_USERNAME, INVALID_PASSWORD)
            self.assertFalse(user_is_logged_in())
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())

    def test_logout(self):
        with self.client:
            self.login(USERNAME, PASSWORD)
            self.assertTrue(user_is_logged_in())
            self.logout()
            self.assertFalse(user_is_logged_in())
