async def run(self): self.load_env("api") self.db_register() if self.option("target", False) == False: self.set_option("target", "dev") if self.option("target") == "env": # self.load_env("seed") logger.info(f"seeding {self.option('target')}") raise NotImplementedError() # seed based on env vars elif self.option("target") == "dev": logger.info(f"seeding {self.option('target')}") logger.info("creating superuser") super = factory("SuperUserFactory").create(email="*****@*****.**") logger.info("creating zones") zone = factory("ZoneFactory").create(domain="example.com", ip="127.0.1.1") dns_server_name = uuid.uuid4() zone2 = factory("ZoneFactory").create( domain="potato.com", ip="127.0.1.1", dns_server_name=dns_server_name) dns_server_name2 = uuid.uuid4() zone3 = factory("ZoneFactory").create( domain="differentzone.com", ip="127.0.1.1", dns_server_name=dns_server_name2, ) logger.info("creating api_tokens") for i in range(65): factory("ApiTokenFactory").create( dns_server_name=dns_server_name) logger.info("creating dns_requests") dns_request = factory("DnsRequestFactory").create(zone_id=zone.id) dns_request = factory("DnsRequestFactory").create(zone_id=zone2.id) for i in range(35): dns_request = factory("DnsRequestFactory").create( dns_server_name=dns_server_name) for i in range(35): dns_request = factory("DnsRequestFactory").create( dns_server_name=dns_server_name2) else: print("invalid target set for seeder") self.exit(1)
def test_superuser_can_update_user(client, session): auth = factory("SuperUserFactory", session=session).create() target = factory("UserFactory", session=session).create(email="*****@*****.**") token = create_bearer_token(data={"sub": auth.id, "scopes": SUPER_SCOPES}) bearer = "Bearer " + str(token) data = {"email": "*****@*****.**"} response = client.put( f"/api/v1/user/{str(target.id)}", json=data, headers={ "Content-Type": "application/json", "Authorization": bearer }, ) assert response.status_code == 200 assert response.json()["user"]["email"] == data["email"]
def test_email_must_not_already_exist_or_failure(client, session): auth = factory("SuperUserFactory", session=session).create() existing = factory("UserFactory", session=session).create(email="*****@*****.**") target = factory("UserFactory", session=session).create(email="*****@*****.**") token = create_bearer_token(data={"sub": auth.id, "scopes": SUPER_SCOPES}) bearer = "Bearer " + str(token) data = {"email": existing.email} response = client.put( f"/api/v1/user/{str(target.id)}", json=data, headers={ "Content-Type": "application/json", "Authorization": bearer }, ) assert response.status_code == 422
def test_username_is_not_valid_email_format_causes_failure(client, session): auth = factory("SuperUserFactory", session=session).create() token = create_bearer_token(data={"sub": auth.id, "scopes": SUPER_SCOPES}) bearer = "Bearer " + str(token) data = { "email": "testtest", "password": "******", "password_confirm": "Password123", } response = client.post( "/api/v1/user", json=data, headers={ "Content-Type": "application/json", "Authorization": bearer }, ) assert response.status_code == 422
def test_regular_user_cannot_create_user(client, session): auth = factory("UserFactory", session=session).create() token = create_bearer_token(data={"sub": auth.id, "scopes": NORMAL_SCOPES}) bearer = "Bearer " + str(token) data = { "email": "*****@*****.**", "password": "******", "password_confirm": "Password123", } response = client.post( "/api/v1/user", json=data, headers={ "Content-Type": "application/json", "Authorization": bearer }, ) assert response.status_code == 403
def seed_from_env(self): from bountydns.core.entities.user import UserRepo for i in range(9): i = str(i) user_data = {} email_key = f"SEED_USER_{i}_EMAIL" email = environ.get(email_key, None) password_key = f"SEED_USER_{i}_PASSWORD" password = environ.get(password_key, None) superuser_key = f"SEED_USER_{i}_SUPERUSER" is_superuser = int(environ.get(superuser_key, 0)) if email and password: hashed_password = hash_password(password) repo = UserRepo(db=self.session()) if not repo.exists(email=email): logger.info(f"seeding user {email}") user = factory("UserFactory").create( email=email, hashed_password=hashed_password, is_superuser=is_superuser, ) else: logger.info(f"seeded user {email} already exists")
def seed_from_env(self): from bountydns.core.user import UserRepo from bountydns.core.zone import ZoneRepo from bountydns.core.dns_server import DnsServerRepo from bountydns.db.session import _scoped_session session = _scoped_session for i in range(9): i = str(i) user_data = {} email_key = f"SEED_USER_{i}_EMAIL" email = environ.get(email_key, None) password_key = f"SEED_USER_{i}_PASSWORD" password = environ.get(password_key, None) superuser_key = f"SEED_USER_{i}_SUPERUSER" is_superuser = int(environ.get(superuser_key, 0)) if email and password: email = email.lower() hashed_password = hash_password(password) repo = UserRepo(db=session) if not repo.exists(email=email): logger.info(f"seeding user {email}") user = factory("UserFactory", session=session).create( email=email, hashed_password=hashed_password, is_superuser=is_superuser, ) else: logger.info(f"seeded user {email} already exists") for i in range(9): i = str(i) name_key = f"SEED_DNS_SERVER_{i}_NAME" name = environ.get(name_key, None) if name: repo = DnsServerRepo(db=session) if not repo.exists(name=name): logger.info(f"seeding domain {name}") domain = factory("DnsServerFactory", session=session).create(name=name) for i in range(9): i = str(i) ip_key = f"SEED_ZONE_{i}_IP" domain_key = f"SEED_ZONE_{i}_DOMAIN" dns_server_name_key = f"SEED_ZONE_{i}_DNS_SERVER_NAME" ip = environ.get(ip_key, None) domain = environ.get(domain_key, None) if domain: domain = domain.lower() dns_server_name = environ.get(dns_server_name_key, None) if ip and domain: if dns_server_name: dns_server_repo = DnsServerRepo(db=session) if dns_server_repo.exists(name=dns_server_name): dns_server = dns_server_repo.results() else: logger.info( f"seeding dns server as zone dependency: {name}") dns_server = factory( "DnsServerFactory", session=session).create(name=dns_server_name) factory("ZoneFactory", session=session).create(ip=ip, domain=domain, dns_server=dns_server) else: repo = ZoneRepo(db=session) if not repo.exists(ip=ip, domain=domain): logger.info( f"seeding zone without dns server: {ip}, {domain}") factory("GlobalZoneFactory", session=session).create(ip=ip, domain=domain)
async def run(self): env = self.option("env") self.load_env(f"api.{env}") self.db_register() if self.option("target", False) == False: self.set_option("target", "dev") if self.option("target") == "env": # self.load_env("seed") logger.info(f"seeding {self.option('target')}") raise NotImplementedError() # seed based on env vars elif self.option("target") == "dev": logger.info(f"seeding {self.option('target')}") logger.info("creating superuser") super = factory("SuperUserFactory").create(email="*****@*****.**") logger.info("creating normal user") norm = factory("UserFactory").create(email="*****@*****.**") logger.info("creating zones") zone = factory("ZoneFactory").create(domain="othersite.com", ip="127.0.1.1") dns_server = factory("DnsServerFactory").create() zone2 = factory("ZoneFactory").create( domain="friends4life.com", ip="127.0.1.1", dns_server=dns_server ) zone3 = factory("ZoneFactory").create( domain="differentzone.com", ip="127.0.1.1" ) logger.info("creating api_tokens") for i in range(65): factory("ApiTokenFactory").create(dns_server=dns_server) logger.info("creating dns_requests") for i in range(35): factory("DnsRequestFactory").create(dns_server=dns_server, zone=zone2) for i in range(35): factory("DnsRequestFactory").create(dns_server=dns_server, zone=zone3) logger.info("creating dns_records") for i in range(3): factory("DnsRecordFactory").create(zone=zone) for i in range(3): factory("DnsRecordFactory").create(zone=zone2) for i in range(3): factory("DnsRecordFactory").create(zone=zone3) else: print("invalid target set for seeder") self.exit(1)