def create_user(name: str, email: str, password: str) -> Optional[User]: if find_user_by_email(email): return None user = User() user.email = email user.name = name user.hashed_password = hash_text(password) session = db_session.create_session() session.add(user) session.commit() return user
def test_v_register_view_new_user(): # 3 A's of test: Arrange, Act, then Assert # Arrange, get everything setup from pypi_org.views.account_views import register_post form_data = { 'name': 'Rikard', 'email': '*****@*****.**', 'password': '******' * 6, } target = 'pypi_org.services.user_service.find_user_by_email' find_user = unittest.mock.patch(target, return_value=None) target = 'pypi_org.services.user_service.create_user' create_user = unittest.mock.patch(target, return_value=User()) request = flask_app.test_request_context('/account/register', data=form_data) with find_user, create_user, request: # Act, try to do the actual login resp: Response = register_post() # Assert assert resp.location == '/account'
def test_int_account_home_with_login(client: client): target = "pypi_org.services.user_service.find_user_by_id" test_user = User(name='Jan', email="*****@*****.**") with mock.patch(target, return_value=test_user): resp: Response = client.get("/account") assert resp.status_code == 200 assert b"Jan" in resp.data
def test_int_account_home_with_login(client): target = 'pypi_org.services.user_service.find_user_by_id' test_user = User(name='Michael', email='*****@*****.**') with unittest.mock.patch(target, return_value=test_user): resp: Response = client.get('/account') assert resp.status_code == 200 assert b'Michael' in resp.data
def test_int_account_home_with_login(client): target = 'pypi_org.services.user_service.find_user_by_id' test_user = User(name='Rikard', email='*****@*****.**') with unittest.mock.patch(target, return_value=test_user): resp: Response = client.get('/account') assert resp.status_code == 200 assert b'Rikard' in resp.data
def test_account_home_with_login(client): target = 'pypi_org.services.user_service.find_user_by_id' test_user = User(name='Kat', email='*****@*****.**') with unittest.mock.patch(target, return_value=test_user): resp: Response = client.get('/account') assert resp.status_code == 200 assert b'Kat' in resp.data
def test_int_account_home_with_login(client): test_user = User(name='marcos', email='*****@*****.**') target = 'pypi_org.services.user_service.find_user_by_id' with unittest.mock.patch(target, return_value=test_user): resp: Response = client.get('/account') assert resp.status_code == 200 assert b'marcos' in resp.data
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]: print('Importing users ... ', flush=True) for idx, (email, name) in enumerate(user_lookup.items()): session = db_session.create_session() session.expire_on_commit = False user = User() user.email = email user.name = name session.add(user) session.commit() print() sys.stderr.flush() sys.stdout.flush() session = db_session.create_session() return {u.email: u for u in session.query(User)}
def test_int_account_home_with_login(client): target = "pypi_org.services.user_service.find_user_by_id" test_user = User(name='Andy', email='*****@*****.**') with unittest.mock.patch(target, return_value=test_user): # This hits the flask_app at /account. On its own, this will hit the db! resp: Response = client.get('/account') assert resp.status_code == 200 # This is just a random spot check. We know the response data hs something like "welcome Andy" in the page, so we # could look for that. But the response data is in bytes not string assert b'Andy' in resp.data
def test_int_account_home_with_login(client): test_user = User(name='Michael', email='*****@*****.**') p1 = unittest.mock.patch('pypi_org.services.user_service.find_user_by_id', return_value=test_user) p2 = unittest.mock.patch( 'pypi_org.infrastructure.cookie_auth.get_user_id_via_auth_cookie', return_value="value") with p1, p2: resp: Response = client.get('/account') assert resp.status_code == 200 assert b'Michael' in resp.data
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]: print("Importing users ... ", flush=True) with progressbar.ProgressBar(max_value=len(user_lookup)) as bar: for idx, (email, name) in enumerate(user_lookup.items()): session = db_session.create_session() session.expire_on_commit = False user = User() user.email = email user.name = name session.add(user) session.commit() bar.update(idx) print() sys.stderr.flush() sys.stdout.flush() session = db_session.create_session() return {u.email: u for u in session.query(User)}
def create_user(name: str, email: str, password: str, profile_image_url: str = None) -> Optional[User]: if not name or \ not email or \ not password: return None # Could use find_user to avoid committing an existing user, but I'm using the db to handle this. Which is better? user = User() user.name = name user.email = email user.hashed_password = hash_text(password) user.profile_image_url = profile_image_url s = create_session() s.add(user) try: # Commit data to the DB but keep an unexpired version to pass back to caller s.expire_on_commit = False s.commit() except exc.IntegrityError: # Record already exists or cannot be added user = None s.close() return user
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]: print("Importing users ... ", flush=True) with progressbar.ProgressBar(max_value=len(user_lookup)) as bar: for idx, (email, name) in enumerate(user_lookup.items()): session = db_session.create_session() session.expire_on_commit = False user = User() user.email = email user.name = name session.add(user) session.commit() bar.update(idx) print() sys.stderr.flush() sys.stdout.flush() # Why do we create another one down here? Can't we use the one above? And do they close themselves when going out # of scope? session = db_session.create_session() return {u.email: u for u in session.query(User)}
def test_vm_register_validation_for_existing_user(): form_data = { 'name': 'mike', 'email': '*****@*****.**', 'password': '******' * 6, } with flask_app.test_request_context(path='/account/register', data=form_data): vm = RegisterViewModel() target = 'pypi_org.services.user_service.find_user_by_email' with unittest.mock.patch(target, return_value=User()): vm.validate() assert vm.error is not None assert 'already exists' in vm.error
def test_register_validation_view_new_user(): form_data = { 'name': 'Kat', 'email': '*****@*****.**', 'password': '******' * 3 } target_find_user = '******' target_create_user = '******' find_user = unittest.mock.patch(target_find_user, return_value=None) create_user = unittest.mock.patch(target_create_user, return_value=User()) request = flask_app.test_request_context(path='/account/register', data=form_data) with find_user, create_user, request: resp: Response = register_post() assert resp.location == '/account'
def test_v_register_view_new_user(): form_data = { 'name': 'mike', 'email': '*****@*****.**', 'password': '******' * 6, } from pypi_org.views.account_views import register_post target = 'pypi_org.services.user_service.find_user_by_email' find_user = unittest.mock.patch(target, return_value=None) target = 'pypi_org.services.user_service.create_user' create_user = unittest.mock.patch(target, return_value=User()) request = flask_app.test_request_context(path='/account/register', data=form_data) with find_user, create_user, request: resp: Response = register_post() assert resp.location == '/account'
def test_register_validation_for_existing_user(): form_data = { 'name': 'Kat', 'email': '*****@*****.**', 'password': '******' * 3 } with flask_app.test_request_context(path='/account/register', data=form_data): vm = RegisterViewModel() # Avoids database call on register target = 'pypi_org.services.user_service.find_user_by_email' test_user = User(email=form_data.get('email')) with unittest.mock.patch(target, return_value=test_user): vm.validate() assert vm.error is not None assert 'already exists' in vm.error
def test_v_register_view_new_user(): # Arrange from pypi_org.views.account_views import register_post form_data = { 'name': 'marcos', 'email': '*****@*****.**', 'password': '******' * 6, } target = 'pypi_org.services.user_service.find_user_by_email' with unittest.mock.patch(target, return_value=None): target = 'pypi_org.services.user_service.create_user' with unittest.mock.patch(target, return_value=User()): with flask_app.test_request_context(path='/account/register', data=form_data): # Act resp: Response = register_post() # Assert assert resp.location == '/account'
def test_vm_register_validation_for_existing_user(): # Arrange form_data = { 'name': 'marcos', 'email': '*****@*****.**', 'password': '******' * 6, } with flask_app.test_request_context(path='/account/register', data=form_data): vm = RegisterViewModel() # Act target = 'pypi_org.services.user_service.find_user_by_email' test_user = User(email=form_data.get('email')) with unittest.mock.patch(target, return_value=test_user): vm.validate() # Assert assert vm.error is not None assert 'already exists' in vm.error
def test_register_validation_when_existing(): # Arrange form_data = { 'name': 'Jan', 'email': "*****@*****.**", 'password': "******" * 7 } # flask_app with flask_app.test_request_context(path="/account/register", data=form_data): vm = RegisterViewModel() # Act target = "pypi_org.services.user_service.find_user_by_email" testuser = User(email=form_data.get("email")) with mock.patch(target, return_value=testuser): vm.validate() # verify assert "exists" in vm.error
def test_register_view_new_user(): # Arrange from pypi_org.views.account_views import register_post form_data = { 'name': 'Jan', 'email': "*****@*****.**", 'password': "******" * 7 } # flask_app target = "pypi_org.services.user_service.find_user_by_email" with mock.patch(target, return_value=None): target = "pypi_org.services.user_service.create_user" with mock.patch(target, return_value=User()): with flask_app.test_request_context(path="/account/register", data=form_data): # Act resp: Response = register_post() # verify assert resp.location == "/account"
def test_vm_register_validation_for_existing_user(): # 3 A's of test: Arrange, Act, then Assert # Arrange, get everything setup form_data = { 'name': 'Rikard', 'email': '*****@*****.**', 'password': '******' * 6, } with flask_app.test_request_context('/account/register', data=form_data): vm = RegisterViewModel() # Act, try to do the actual login target = 'pypi_org.services.user_service.find_user_by_email' test_user = User(email=form_data.get('email')) with unittest.mock.patch(target, return_value=test_user): vm.validate() # Assert assert vm.error is not None assert 'already exists' in vm.error
def test_register_validation_new_user(): # 3 A's of test: Arrange, Act, then Assert # Arrange from pypi_org.views.account_views import register_post form_data = { 'name': "Eddie", 'email': "*****@*****.**", 'password': '******' * 6, } target = 'pypi_org.services.user_service.find_user_by_email' find_user = unittest.mock.patch(target, return_value=None) target = 'pypi_org.services.user_service.create_user' create_user = unittest.mock.patch(target, return_value=User()) request = flask_app.test_request_context(path='/account/register', data=form_data) with find_user, create_user, request: # Act resp: Response = register_post() # Assert assert resp.location == '/account' # the redirect set the location header to this value
def test_v_register_view_new_user(): # 3 A's of test: Arrange, Act, then Assert # Arrange from pypi_org.views.account_views import register_post form_data = { 'name': 'Michael', 'email': '*****@*****.**', 'password': '******' * 6 } target = 'pypi_org.services.user_service.find_user_by_email' find_user = unittest.mock.patch(target, return_value=None) target = 'pypi_org.services.user_service.create_user' create_user = unittest.mock.patch(target, return_value=User()) request = flask_app.test_request_context(path='/account/register', data=form_data) with find_user, create_user, request: # Act resp: Response = register_post() # Assert assert resp.location == '/account'
def test_register_validation_for_existing_user(): # 3 A's of testing: Arrange, Act, and Assert # Arrange form_data = { 'name': 'John', 'email_address': '*****@*****.**', 'password': '******' } with flask_app.test_request_context(path='/account/register', data=form_data): vm = RegisterViewModel() # Act # To avoid touching the database, we mock the function that would reach out to the database and make a fake one # instead that just returns None (which would be the 'no user to see here!' response) target = 'pypi_org.services.user_service.find_user_by_email' test_user = User(email=form_data.get('email_address')) with unittest.mock.patch(target, return_value=test_user): vm.validate() # Assert assert vm.error is not None assert 'already exists' in vm.error
def test_account_home_no_login(client): target = 'pypi_org.services.user_service.find_user_by_id' with unittest.mock.patch(target, return_value=User(name='Mike')): resp: Response = client.get('/account') assert resp.status_code == 200