Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_charset(self):
"""Check encoding charset"""
# With an invalid encoding both names wouldn't be inserted;
# In MySQL, chars 'ı' and 'i' are considered the same with a
# collation distinct to _unicode_ci
org1 = Organization(name='ıCompany'.encode('utf-8'))
org2 = Organization(name='iCompany')
self.session.add(org1)
self.session.add(org2)
self.session.commit()
self.session.commit()
self.session.rollback()
with self.assertRaisesRegex(IntegrityError, NULL_CHECK_ERROR):
uid = UniqueIdentity(uuid='John Smith')
self.session.add(uid)
rol2 = Enrollment(uidentity=uid)
self.session.add(rol2)
self.session.commit()
self.session.rollback()
with self.assertRaisesRegex(IntegrityError, NULL_CHECK_ERROR):
org = Organization(name='Example')
self.session.add(org)
rol3 = Enrollment(organization=org)
self.session.add(rol3)
self.session.commit()
self.session.rollback()
def test_to_dict(self):
"""Test output of to_dict() method"""
org = Organization(name='Example')
self.session.add(org)
dom = Domain(domain='example.com',
is_top_domain=True,
organization=org)
self.session.add(dom)
self.session.commit()
# Tests
d = dom.to_dict()
self.assertIsInstance(d, dict)
self.assertEqual(d['domain'], 'example.com')
self.assertEqual(d['top_domain'], True)
self.assertEqual(d['organization'], 'Example')
def test_domain_none(self):
"""Check whether domains with None name cannot be added"""
with self.db.connect() as session:
org = Organization(name='Example')
session.add(org)
with self.assertRaisesRegex(ValueError, DOMAIN_NAME_NONE_ERROR):
api.add_domain(session, org, None)
def test_add_organizations(self):
"""Check whether it adds a set of organizations"""
api.add_organization(self.db, 'Example')
api.add_organization(self.db, 'Bitergia')
api.add_organization(self.db, 'LibreSoft')
with self.db.connect() as session:
org = session.query(Organization).\
filter(Organization.name == 'Example').first()
self.assertEqual(org.name, 'Example')
org = session.query(Organization).\
filter(Organization.name == 'Bitergia').first()
self.assertEqual(org.name, 'Bitergia')
org = session.query(Organization).\
filter(Organization.name == 'LibreSoft').first()
self.assertEqual(org.name, 'LibreSoft')
def test_delete_identity(self):
"""Check whether it deletes an identity"""
with self.db.connect() as session:
example_org = Organization(name='Example')
bitergia_org = Organization(name='Bitergia')
session.add(example_org)
session.add(bitergia_org)
jsmith = UniqueIdentity(uuid='AAAA')
jsmith.profile = Profile(name='John Smith', email='jsmith@example.net')
jsmith.identities.append(Identity(id='0001', name='John Smith', source='scm'))
jsmith.identities.append(Identity(id='0002', email='jsmith@example.net', source='mls'))
session.add(jsmith)
enrol = Enrollment(uidentity=jsmith, organization=example_org)
session.add(enrol)
enrol = Enrollment(uidentity=jsmith, organization=bitergia_org)
session.add(enrol)
with self.db.connect() as session:
id0 = uid.identities[0]
self.assertIsInstance(id0, Identity)
self.assertEqual(id0.id, None)
self.assertEqual(id0.name, 'John Doe')
self.assertEqual(id0.email, None)
self.assertEqual(id0.username, None)
self.assertEqual(id0.uuid, 'John Doe')
self.assertEqual(id0.source, 'unknown')
enrollments = uid.enrollments
self.assertEqual(len(enrollments), 2)
rol0 = enrollments[0]
self.assertIsInstance(rol0, Enrollment)
self.assertIsInstance(rol0.organization, Organization)
self.assertEqual(rol0.organization.name, 'Bitergia')
self.assertEqual(rol0.start, MIN_PERIOD_DATE)
self.assertEqual(rol0.end, MAX_PERIOD_DATE)
rol1 = enrollments[1]
self.assertIsInstance(rol1, Enrollment)
self.assertIsInstance(rol1.organization, Organization)
self.assertEqual(rol1.organization.name, 'Example')
self.assertEqual(rol1.start, MIN_PERIOD_DATE)
self.assertEqual(rol1.end, datetime.datetime(2010, 1, 1, 0, 0, 0))
# John Smith unique identity
uid = uids[1]
self.assertIsInstance(uid, UniqueIdentity)
self.assertEqual(uid.uuid, 'John Smith')
self.assertEqual(len(uid.identities), 4)
def __parse_enrollments(self, user):
"""Parse user enrollments"""
enrollments = []
for company in user['companies']:
name = company['company_name']
org = self._organizations.get(name, None)
if not org:
org = Organization(name=name)
self._organizations[name] = org
start_date = MIN_PERIOD_DATE
end_date = MAX_PERIOD_DATE
if company['end_date']:
end_date = str_to_datetime(company['end_date'])
rol = Enrollment(start=start_date, end=end_date,
organization=org)
enrollments.append(rol)
return enrollments
]
}
:param json: JSON object to parse
:raises InvalidFormatError: raised when the format of the JSON is
not valid.
"""
try:
for company in json['companies']:
name = self.__encode(company['company_name'])
org = self._organizations.get(name, None)
if not org:
org = Organization(name=name)
self._organizations[name] = org
for domain in company['domains']:
if not domain:
continue
dom = Domain(domain=domain)
org.domains.append(dom)
except KeyError as e:
msg = "invalid json format. Attribute %s not found" % e.args
raise InvalidFormatError(cause=msg)
# Ignore organization
if not active and not inactive:
continue
if not active:
active = MIN_PERIOD_DATE
if not inactive:
inactive = MAX_PERIOD_DATE
except InvalidDateError as e:
raise InvalidFormatError(cause=str(e))
org = self._organizations.get(name, None)
if not org:
org = Organization(name=name)
# Store metadata valid for identities parsing
org.active = active
org.inactive = inactive
self._organizations[name] = org
except KeyError as e:
msg = "invalid json format. Attribute %s not found" % e.args
raise InvalidFormatError(cause=msg)