def test_parse_cycle(self): """ Tests that we output something we parsed back into the same thing we output. """ for this in [self.one, self.two, self.three]: this_dict = this.to_list_of_dicts() self.assertEqual(PhoneNumberList(this_dict).to_list_of_dicts(), this.to_list_of_dicts())
def _parse_phone_numbers(self): """ Part of __init__(). Parse all the different formats of mobile/phone attributes in the database. """ if 'mobile' in self._data_in: self._data_in['phone'] = self._data_in.pop('mobile') if 'phone' in self._data_in: _phones = self._data_in.pop('phone') # Clean up for non verified phone elements that where still primary for _this in _phones: if not _this.get('verified', False) and _this.get( 'primary', False): _this['primary'] = False _primary = [x for x in _phones if x.get('primary', False)] if _phones and not _primary: # None of the phone numbers are primary. Promote the first verified # entry found (or none if there are no verified entries). for _this in _phones: if _this.get('verified', False): _this['primary'] = True break self._data_in['phone'] = _phones _phones = self._data_in.pop('phone', []) self._phone_numbers = PhoneNumberList(_phones)
def test_bad_input_two_primary(self): one = copy.deepcopy(_one_dict) two = copy.deepcopy(_two_dict) one['primary'] = True two['primary'] = True with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): PhoneNumberList([one, two])
def test_add_mailaddress(self): third = self.three.find('+46700000003') this = PhoneNumberList([_one_dict, _two_dict, third]) self.assertEqual(this.to_list_of_dicts(), self.three.to_list_of_dicts())
def test_init_bad_data(self): with self.assertRaises(eduid_userdb.element.UserDBValueError): PhoneNumberList('bad input data')
def setUp(self): self.empty = PhoneNumberList([]) self.one = PhoneNumberList([_one_dict]) self.two = PhoneNumberList([_one_dict, _two_dict]) self.three = PhoneNumberList([_one_dict, _two_dict, _three_dict]) self.four = PhoneNumberList([_three_dict, _four_dict])
class TestPhoneNumberList(TestCase): def setUp(self): self.empty = PhoneNumberList([]) self.one = PhoneNumberList([_one_dict]) self.two = PhoneNumberList([_one_dict, _two_dict]) self.three = PhoneNumberList([_one_dict, _two_dict, _three_dict]) self.four = PhoneNumberList([_three_dict, _four_dict]) def test_init_bad_data(self): with self.assertRaises(eduid_userdb.element.UserDBValueError): PhoneNumberList('bad input data') def test_to_list(self): self.assertEqual([], self.empty.to_list(), list) self.assertIsInstance(self.one.to_list(), list) self.assertEqual(1, len(self.one.to_list())) def test_to_list_of_dicts(self): self.assertEqual([], self.empty.to_list_of_dicts(), list) self.assertEqual([_one_dict], self.one.to_list_of_dicts()) def test_find(self): match = self.one.find('+46700000001') self.assertIsInstance(match, PhoneNumber) self.assertEqual(match.number, '+46700000001') self.assertEqual(match.is_verified, True) self.assertEqual(match.verified_ts, None) def test_add(self): second = self.two.find('+46700000002') self.one.add(second) self.assertEqual(self.one.to_list_of_dicts(), self.two.to_list_of_dicts()) def test_add_duplicate(self): dup = self.two.find(self.two.primary.number) with self.assertRaises(eduid_userdb.element.DuplicateElementViolation): self.two.add(dup) def test_add_mailaddress(self): third = self.three.find('+46700000003') this = PhoneNumberList([_one_dict, _two_dict, third]) self.assertEqual(this.to_list_of_dicts(), self.three.to_list_of_dicts()) def test_add_another_primary(self): new = eduid_userdb.phone.phone_from_dict({'number': '+46700000009', 'verified': True, 'primary': True, }) with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): self.one.add(new) def test_add_wrong_type(self): pwdict = {'id': bson.ObjectId(), 'salt': 'foo', } new = eduid_userdb.password.Password(data=pwdict) with self.assertRaises(eduid_userdb.element.UserDBValueError): self.one.add(new) def test_remove(self): now_two = self.three.remove('+46700000003') self.assertEqual(self.two.to_list_of_dicts(), now_two.to_list_of_dicts()) def test_remove_unknown(self): with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): self.one.remove('+46709999999') def test_remove_primary(self): with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): self.two.remove(self.two.primary.number) def test_remove_primary_single(self): now_empty = self.one.remove(self.one.primary.number) self.assertEqual([], now_empty.to_list()) def test_remove_all_mix(self): verified = self.three.verified.to_list() if verified: for mobile in verified: if not mobile.is_primary: self.three.remove(mobile.number) self.three.remove(self.three.primary.number) for mobile in self.three.to_list(): self.three.remove(mobile.number) self.assertEqual([], self.three.to_list()) def test_remove_all_no_verified(self): verified = self.four.verified.to_list() if verified: for mobile in verified: if not mobile.is_primary: self.four.remove(mobile.number) self.four.remove(self.four.primary.number) for mobile in self.four.to_list(): self.four.remove(mobile.number) self.assertEqual([], self.four.to_list()) def test_unverify_all(self): verified = self.three.verified.to_list() if verified: self.three.primary.is_primary = False for mobile in verified: if not mobile.is_primary: mobile.is_verified = False self.assertTrue(all([not x.is_verified for x in self.three.to_list()])) def test_primary(self): match = self.one.primary self.assertEqual(match.number, '+46700000001') def test_empty_primary(self): self.assertEqual(None, self.empty.primary) def test_set_primary_to_same(self): match = self.one.primary self.one.primary = match.number match = self.two.primary self.two.primary = match.number def test_set_unknown_as_primary(self): with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): self.one.primary = '+46709999999' def test_set_unverified_as_primary(self): with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): self.three.primary = '+46700000003' def test_change_primary(self): match = self.two.primary self.assertEqual(match.number, '+46700000001') self.two.primary = '+46700000002' updated = self.two.primary self.assertEqual(updated.number, '+46700000002') def test_bad_input_two_primary(self): one = copy.deepcopy(_one_dict) two = copy.deepcopy(_two_dict) one['primary'] = True two['primary'] = True with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): PhoneNumberList([one, two]) def test_unverified_primary(self): one = copy.deepcopy(_one_dict) one['verified'] = False with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): this = PhoneNumberList([one])
class TestPhoneNumber(TestCase): def setUp(self): self.empty = PhoneNumberList([]) self.one = PhoneNumberList([_one_dict]) self.two = PhoneNumberList([_one_dict, _two_dict]) self.three = PhoneNumberList([_one_dict, _two_dict, _three_dict]) def test_key(self): """ Test that the 'key' property (used by PrimaryElementList) works for the PhoneNumber. """ address = self.two.primary self.assertEqual(address.key, address.number) def test_setting_invalid_mail(self): this = self.one.primary with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.number = None def test_parse_cycle(self): """ Tests that we output something we parsed back into the same thing we output. """ for this in [self.one, self.two, self.three]: this_dict = this.to_list_of_dicts() self.assertEqual(PhoneNumberList(this_dict).to_list_of_dicts(), this.to_list_of_dicts()) def test_bad_is_primary(self): this = self.one.to_list()[0] with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.is_primary = 'foo' def test_unknown_input_data(self): one = copy.deepcopy(_one_dict) one['foo'] = 'bar' with self.assertRaises(eduid_userdb.exceptions.UserHasUnknownData): PhoneNumber(one) def test_unknown_input_data_allowed(self): one = copy.deepcopy(_one_dict) one['foo'] = 'bar' addr = PhoneNumber(one, raise_on_unknown = False) out = addr.to_dict() self.assertIn('foo', out) self.assertEqual(out['foo'], one['foo']) def test_changing_is_verified_on_primary(self): this = self.one.primary with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): this.is_verified = False def test_changing_is_verified(self): this = self.three.find('+46700000003') this.is_verified = False # was False already this.is_verified = True def test_setting_invalid_is_verified(self): this = self.three.find('+46700000003') with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.is_verified = 1 def test_verified_by(self): this = self.three.find('+46700000003') this.verified_by = 'unit test' self.assertEqual(this.verified_by, 'unit test') with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_by = False def test_modify_verified_by(self): this = self.three.find('+46700000003') this.verified_by = 'unit test' with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_by = None with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_by = 'test unit' def test_verified_ts(self): this = self.three.find('+46700000003') this.verified_ts = True self.assertIsInstance(this.verified_ts, datetime.datetime) with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_ts = False def test_modify_verified_ts(self): this = self.three.find('+46700000003') this.verified_ts = datetime.datetime.utcnow() with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_ts = None with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.verified_ts = True def test_created_by(self): this = self.three.find('+46700000003') this.created_by = 'unit test' self.assertEqual(this.created_by, 'unit test') with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_by = False def test_modify_created_by(self): this = self.three.find('+46700000003') with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_by = 1 this.created_by = 'unit test' with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_by = None with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_by = 'test unit' def test_created_ts(self): this = self.three.find('+46700000003') this.created_ts = True self.assertIsInstance(this.created_ts, datetime.datetime) with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_ts = False def test_modify_created_ts(self): this = self.three.find('+46700000003') this.created_ts = datetime.datetime.utcnow() with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_ts = None with self.assertRaises(eduid_userdb.exceptions.UserDBValueError): this.created_ts = True
def test_unverified_primary(self): one = copy.deepcopy(_one_dict) one['verified'] = False with self.assertRaises(eduid_userdb.element.PrimaryElementViolation): this = PhoneNumberList([one])