def test_get_all_with_one_user_returns_that_user(self): repo = UserRepo(TEST_SESSION_FACTORY) u = User(username="******", email="good@somewhere") repo.add_user(u) users = repo.get_all_users() self.assertEqual(1, len(users)) self.assertEqual(u, users[0])
def test_cannot_add_unamed_catalog(self): repo = CatalogRepo(TEST_SESSION_FACTORY) user_repo = UserRepo(TEST_SESSION_FACTORY) owner = User(username="******", email="bob@bob") user_repo.add_user(owner) catalog = Catalog(owner_id=owner.id) with self.assertRaises(SQLAlchemyError): repo.add(catalog)
def test_can_get_catalog_by_id_when_catalog_with_owner_and_name_added(self): repo = CatalogRepo(TEST_SESSION_FACTORY) user_repo = UserRepo(TEST_SESSION_FACTORY) owner = User(username="******", email="bob@bob") user_repo.add_user(owner) catalog = Catalog(owner_id=owner.id, name="Bob's catalog.") repo.add(catalog) actual_catalog = repo.get_by_id(catalog.id) self.assertEqual(owner.id, actual_catalog.owner_id) self.assertEqual(catalog.name, actual_catalog.name) self.assertIsNone(actual_catalog.description) self.assertIsNotNone(actual_catalog.created_at_utc)
def test_get_all_returns_all_when_catalog_contains_one_entry(self): repo = CatalogRepo(TEST_SESSION_FACTORY) user_repo = UserRepo(TEST_SESSION_FACTORY) owner = User(username="******", email="bob@bob") user_repo.add_user(owner) catalog = Catalog(owner_id=owner.id, name="Bob's catalog.") repo.add(catalog) catalogs = repo.get_all() self.assertEqual(1, len(catalogs)) self.assertEqual(owner.id, catalogs[0].owner_id) self.assertEqual(catalog.name, catalogs[0].name) self.assertIsNone(catalogs[0].description) self.assertIsNotNone(catalogs[0].created_at_utc)
def test_get_all_returns_all_when_catalog_contains_more_than_one_entry(self): repo = CatalogRepo(TEST_SESSION_FACTORY) user_repo = UserRepo(TEST_SESSION_FACTORY) owner = User(username="******", email="bob@bob") user_repo.add_user(owner) expected_catalogs = [] expected_catalogs.append(Catalog(owner_id=owner.id, name="A")) expected_catalogs.append(Catalog(owner_id=owner.id, name="B")) expected_catalogs.append(Catalog(owner_id=owner.id, name="C")) for catalog in expected_catalogs: repo.add(catalog) actual_catalogs = repo.get_all() self.assertEqual(sorted(expected_catalogs), sorted(actual_catalogs))
def test_get_all_with_more_than_one_user_returns_all_users(self): repo = UserRepo(TEST_SESSION_FACTORY) u1 = User(username="******", email="good@somewhere") u2 = User(username="******", email="agood@somewhere") u3 = User(username="******", email="bgood@somewhere") repo.add_user(u1) repo.add_user(u2) repo.add_user(u3) users = repo.get_all_users() expected = [u1, u2, u3] self.assertEqual(len(expected), len(users)) self.assertListEqual(expected, users)
def test_update_user_with_known_user_performs_update(self): repo = UserRepo(TEST_SESSION_FACTORY) u = User(username="******", email="good@somewhere", admin=False, provider=AuthProvider.google, active=False) repo.add_user(u) u_pre_change_actual = repo.get_by_username("goodusername", AuthProvider.google) self.assertEqual(u, u_pre_change_actual) u_activated = User(username="******", email="good@somewhere", provider=AuthProvider.google, admin=False, active=True) repo.update_user(u_activated) u_actual = repo.get_by_username("goodusername", AuthProvider.google) self.assertEqual(u_activated, u_actual)
def test_can_add_two_catalogs_with_same_name_and_different_owner(self): repo = CatalogRepo(TEST_SESSION_FACTORY) user_repo = UserRepo(TEST_SESSION_FACTORY) bob = User(username="******", email="bob@bob") user_repo.add_user(bob) tony = User(username="******", email="tony@tony") user_repo.add_user(tony) expected_catalogs = [] catalog = Catalog(owner_id=bob.id, name="A") expected_catalogs.append(catalog) repo.add(catalog) catalog = Catalog(owner_id=tony.id, name="A") repo.add(catalog) expected_catalogs.append(catalog) self.assertEqual(sorted(expected_catalogs), sorted(repo.get_all()))
def setUp(self): """Reset the database before each test is run""" CategoryRepoTest._HELPER.reset_database() # add a test owners user_repo = UserRepo(TEST_SESSION_FACTORY) self._test_owner = User(username="******", email="bob@bob") self._test_owner2 = User(username="******", email="tony@tony") user_repo.add_user(self._test_owner) user_repo.add_user(self._test_owner2) # add a test catalogs catalogRepo = CatalogRepo(TEST_SESSION_FACTORY) self._test_catalog = Catalog(owner_id=self._test_owner.id, name="catalog") self._test_catalog2 = Catalog(owner_id=self._test_owner2.id, name="catalog two") catalogRepo.add(self._test_catalog) catalogRepo.add(self._test_catalog2)
def test_get_user_by_email_returns_correct_user_when_user_exists(self): # pylint: disable=line-too-long repo = UserRepo(TEST_SESSION_FACTORY) u = User(username="******", email="good@somewhere") repo.add_user(u) actual_u = repo.get_by_email("good@somewhere") self.assertEqual(u, actual_u)
def test_get_user_by_email_returns_none_when_no_matching_user(self): repo = UserRepo(TEST_SESSION_FACTORY) u = User(username="******", email="good@somewhere") repo.add_user(u) self.assertIsNone(repo.get_by_email("bad@nowhere"))
def test_get_user_by_email_returns_none_when_no_users(self): repo = UserRepo(TEST_SESSION_FACTORY) self.assertIsNone(repo.get_by_email("bad@nowhere"))
def test_get_user_by_username_returns_none_when_no_users(self): repo = UserRepo(TEST_SESSION_FACTORY) self.assertIsNone(repo.get_by_username("badusername"))
def test_exists_by_username_returns_true_when_user_exists(self): repo = UserRepo(TEST_SESSION_FACTORY) repo.add_user(User(username="******", email="good@somewhere")) self.assertTrue(repo.exists_by_username("goodusername"))
def test_exists_by_username_returns_false_when_no_user_exists(self): repo = UserRepo(TEST_SESSION_FACTORY) self.assertFalse(repo.exists_by_username("badusername"))
def test_update_user_with_unknown_user_raises_lookuperror(self): repo = UserRepo(TEST_SESSION_FACTORY) u = User(username="******", email="good@somewhere") with self.assertRaises(LookupError): repo.update_user(u)
"""Script to grant administation rights to a user.""" import argparse from sqlalchemy.exc import SQLAlchemyError from catalog_webapp.db.default_engine import SESSION_FACTORY from catalog_webapp.repository.user import UserRepo from catalog_webapp.model.auth_provider import AuthProvider _USER_REPO = UserRepo(SESSION_FACTORY) def read_priviliges_to_apply(): """Retrieves the email address, auth provider, active and admin privileges from the command line arguments.""" parser = argparse.ArgumentParser(description="Grant user privileges.") parser.add_argument("email", type=str, help="the email address of the user") parser.add_argument("provider", type=str, default="local", help="the auth provier of the user. " "Defaults to 'local'") parser.add_argument("--active", action="store_true", help="activate the user. Defaults to deactivate.") parser.add_argument("--admin", action="store_true", help="grant the user administration privileges. " "Defaults to not admin.") args = parser.parse_args()
def test_get_all_with_no_users_returns_an_empty_list(self): repo = UserRepo(TEST_SESSION_FACTORY) users = repo.get_all_users() self.assertEqual([], users)