def _run_percentage_filled_test(self, remove): self.set_logged() user = self.userdb_new.get_user_by_mail('*****@*****.**') assert isinstance(user, User) # for the IDE saved_user = User(data=user.to_dict( )) # save the original user to be able to restore it after the test # Remove those parts of the user information that are listed in `remove' if 'surname' in remove: user.surname = None if 'display_name' in remove: user.display_name = None if 'given_name' in remove: user.given_name = None if 'language' in remove: user.language = None if 'mail_addresses' in remove: self._empty_user_list(user.mail_addresses) if 'phone_numbers' in remove: self._empty_user_list(user.phone_numbers) if 'nins' in remove: self._empty_user_list(user.nins) self._save_user_to_userdb(user) import pprint logger.debug('SAVED USER TO DB:\n{!s}'.format( pprint.pformat(user.to_dict()))) response = self.testapp.get('/profile/userstatus/') self._save_user_to_userdb(saved_user) logger.debug('RESTORED USER IN DB:\n{!s}'.format( pprint.pformat(saved_user.to_dict()))) return json.loads(response.body)['profile_filled']
def _run_percentage_filled_test(self, remove): self.set_logged() user = self.userdb_new.get_user_by_mail('*****@*****.**') assert isinstance(user, User) # for the IDE saved_user = User(data=user.to_dict()) # save the original user to be able to restore it after the test # Remove those parts of the user information that are listed in `remove' if 'surname' in remove: user.surname = None if 'display_name' in remove: user.display_name = None if 'given_name' in remove: user.given_name = None if 'language' in remove: user.language = None if 'mail_addresses' in remove: self._empty_user_list(user.mail_addresses) if 'phone_numbers' in remove: self._empty_user_list(user.phone_numbers) if 'nins' in remove: self._empty_user_list(user.nins) self._save_user_to_userdb(user) import pprint logger.debug('SAVED USER TO DB:\n{!s}'.format(pprint.pformat(user.to_dict()))) response = self.testapp.get('/profile/userstatus/') self._save_user_to_userdb(saved_user) logger.debug('RESTORED USER IN DB:\n{!s}'.format(pprint.pformat(saved_user.to_dict()))) return json.loads(response.body)['profile_filled']
def __init__(self, userid=None, eppn=None, subject='physical person', data=None): data_in = data data = copy.copy(data_in) # to not modify callers data if data is None: if userid is None: userid = bson.ObjectId() data = dict( _id=userid, eduPersonPrincipalName=eppn, subject=subject, ) _social_network = data.pop('social_network', None) _social_network_id = data.pop('social_network_id', None) _pending_mail_address = data.pop('pending_mail_address', None) if _pending_mail_address: _pending_mail_address = MailAddress(data=_pending_mail_address) self._pending_mail_address = None User.__init__(self, data=data) # now self._data exists so we can call our setters self.social_network = _social_network self.social_network_id = _social_network_id self.pending_mail_address = _pending_mail_address
def setUp(self): super(TestUserDB_mail, self).setUp(None, None) data1 = { u'_id': bson.ObjectId(), u'eduPersonPrincipalName': u'mail-test1', u'mail': u'*****@*****.**', u'mailAliases': [{ u'email': u'*****@*****.**', u'verified': True }], u'passwords': [], } data2 = { u'_id': bson.ObjectId(), u'eduPersonPrincipalName': u'mail-test2', u'mailAliases': [{ u'email': u'*****@*****.**', u'primary': True, u'verified': True }, { u'email': u'*****@*****.**', u'verified': False }], u'passwords': [], } self.user1 = User(data1) self.user2 = User(data2) self.amdb.save(self.user1, check_sync=False) self.amdb.save(self.user2, check_sync=False)
def setUp(self): super(TestUserDB_nin, self).setUp(None, None) data1 = { u'_id': bson.ObjectId(), u'eduPersonPrincipalName': u'nin-test1', u'mail': u'*****@*****.**', u'nins': [ { u'number': u'11111111111', u'primary': True, u'verified': True }, { u'number': u'22222222222', u'primary': False, u'verified': False }, ], u'passwords': [], } data2 = { u'_id': bson.ObjectId(), u'eduPersonPrincipalName': u'nin-test2', u'mail': u'*****@*****.**', u'nins': [{ u'number': u'11111111111', u'primary': False, u'verified': False }, { u'number': u'22222222222', u'primary': True, u'verified': True }, { u'number': u'33333333333', u'primary': False, u'verified': False }], u'passwords': [], } self.user1 = User(data1) self.user2 = User(data2) self.amdb.save(self.user1) self.amdb.save(self.user2)
def setUp(self): phoneuser = deepcopy(MOCKED_USER_STANDARD) phoneuser.update({ 'phone': [{'number': '1111', 'verified': True, }, {'number': '2222', 'verified': True, }, {'number': '0000', 'verified': False, }], }) self.phoneuser = User(data = phoneuser)
def setUp(self): mailuser = deepcopy(MOCKED_USER_STANDARD) mailuser.update({ 'mail': '*****@*****.**', 'mailAliases': [{'email': '*****@*****.**', 'verified': True, }, {'email': '*****@*****.**', 'verified': True, }, {'email': '*****@*****.**', 'verified': False, }], }) self.mailuser = User(data = mailuser)
def setUp(self): ninuser = deepcopy(MOCKED_USER_STANDARD) del ninuser['norEduPersonNIN'] ninuser.update({ 'nins': [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, }, {'number': '0000', 'verified': False, }], }) self.ninuser = User(data = ninuser)
def __init__(self, userid=None, eppn=None, subject='physical person', data=None): data_in = data data = copy.copy(data_in) # to not modify callers data if data is None: if userid is None: userid = bson.ObjectId() data = dict( _id=userid, eduPersonPrincipalName=eppn, subject=subject, ) User.__init__(self, data=data)
def test_remove_all_mail_addresses_in_db(self): """ Remove all e-mail addresses for all users in the db """ for userdoc in self.userdb_new._get_all_docs(): # Remove the addresses one by one until there are none left user = User(data = userdoc) addresses = user.mail_addresses.to_list() for this in addresses: expected = user.mail_addresses.count - 1 new_user = _remove_mail_from_user(this.email, user) msg = 'Removing address {!s} from user {!s} did not result in address count == {!s}'.format( this.email, user, expected) self.assertEqual(new_user.mail_addresses.count, expected, msg) # Remove the addresses individually, recreating the user from userdoc each time expected = len(addresses) - 1 for this in addresses: user = User(data = userdoc) new_user = _remove_mail_from_user(this.email, user) msg = 'Removing address {!s} from user {!s} did not result in address count == {!s}'.format( this.email, user, expected) self.assertEqual(new_user.mail_addresses.count, expected, msg)
def test_add_first_email_address(self): """ Add an email address to a test user that has none """ userdoc = self.mailuser.to_dict() del userdoc['mailAliases'] this = User(data = userdoc) _add_mail_to_user('*****@*****.**', this) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, 'created_by': 'dashboard', }] got = this.mail_addresses.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: del addr['created_ts'] self.assertEqual(got, expected)
def test_add_first_number(self): """ Add an number to a test user that has none """ userdoc = self.ninuser.to_dict() del userdoc['nins'] this = User(data = userdoc) _add_nin_to_user('9999', this) expected = [{'number': '9999', 'verified': True, 'primary': True, 'created_by': 'dashboard', }] got = this.nins.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: del addr['created_ts'] self.assertEqual(got, expected)
def setUp(self): self.redis_instance = RedisTemporaryInstance.get_instance() self.mongo_instance = MongoTemporaryInstance.get_instance() self.etcd_instance = EtcdTemporaryInstance.get_instance() config = deepcopy(TEST_CONFIG) config = self.update_config(config) config['REDIS_PORT'] = str(self.redis_instance.port) config['MONGO_URI'] = 'mongodb://localhost:{}/'.format( self.mongo_instance.port) os.environ.update({'ETCD_PORT': str(self.etcd_instance.port)}) self.app = self.load_app(config) self.browser = self.app.test_client() self.app.central_userdb.save(User(data=NEW_USER_EXAMPLE), check_sync=False) # Helper constants self.content_type_json = 'application/json'
class MongoTestCase(unittest.TestCase): """TestCase with an embedded MongoDB temporary instance. Each test runs on a temporary instance of MongoDB. The instance will be listen in a random port between 40000 and 50000. A test can access the connection using the attribute `conn`. A test can access the port using the attribute `port` """ fixtures = [] MockedUserDB = MockedUserDB user = User(data=MOCKED_USER_STANDARD) mock_users_patches = [] def setUp(self, celery, get_attribute_manager, userdb_use_old_format=False): """ Test case initialization. To not get a circular dependency between eduid-userdb and eduid-am, celery and get_attribute_manager needs to be imported in the place where this module is called. Usage: from eduid_am.celery import celery, get_attribute_manager class MyTest(MongoTestCase): def setUp(self): super(MyTest, self).setUp(celery, get_attribute_manager) ... :param celery: module :param get_attribute_manager: callable :return: """ super(MongoTestCase, self).setUp() self.tmp_db = MongoTemporaryInstance.get_instance() self.conn = self.tmp_db.conn self.port = self.tmp_db.port if celery and get_attribute_manager: self.am_settings = { 'BROKER_TRANSPORT': 'memory', # Don't use AMQP bus when testing 'BROKER_URL': 'memory://', 'CELERY_EAGER_PROPAGATES_EXCEPTIONS': True, 'CELERY_ALWAYS_EAGER': True, 'CELERY_RESULT_BACKEND': "cache", 'CELERY_CACHE_BACKEND': 'memory', # Be sure to tell AttributeManager about the temporary mongodb instance. 'MONGO_URI': self.tmp_db.get_uri(''), } celery.conf.update(self.am_settings) self.am = get_attribute_manager(celery) self.amdb = self.am.userdb else: self.amdb = UserDB(self.tmp_db.get_uri(''), 'eduid_am') self.amdb._drop_whole_collection() mongo_settings = { 'mongo_replicaset': None, 'mongo_uri': self.tmp_db.get_uri(''), } if getattr(self, 'settings', None) is None: self.settings = mongo_settings else: self.settings.update(mongo_settings) for db_name in self.conn.database_names(): self.conn.drop_database(db_name) # Set up test users in the MongoDB. Read the users from MockedUserDB, which might # be overridden by subclasses. _foo_userdb = self.MockedUserDB(self.mock_users_patches) for userdoc in _foo_userdb.all_userdocs(): this = deepcopy( userdoc) # deep-copy to not have side effects between tests user = User(data=this) self.amdb.save(user, check_sync=False, old_format=userdb_use_old_format) def tearDown(self): super(MongoTestCase, self).tearDown() for userdoc in self.amdb._get_all_docs(): assert DashboardUser(data=userdoc) for db_name in self.conn.database_names(): if db_name == 'local': continue db = self.conn[db_name] for col_name in db.collection_names(): if 'system' not in col_name: db.drop_collection(col_name) del db self.conn.drop_database(db_name) self.amdb._drop_whole_collection() self.conn.disconnect() def mongodb_uri(self, dbname): self.assertIsNotNone(dbname) return self.tmp_db.get_uri(dbname=dbname)
def setUp(self, celery, get_attribute_manager, userdb_use_old_format=False): """ Test case initialization. To not get a circular dependency between eduid-userdb and eduid-am, celery and get_attribute_manager needs to be imported in the place where this module is called. Usage: from eduid_am.celery import celery, get_attribute_manager class MyTest(MongoTestCase): def setUp(self): super(MyTest, self).setUp(celery, get_attribute_manager) ... :param celery: module :param get_attribute_manager: callable :return: """ super(MongoTestCase, self).setUp() self.tmp_db = MongoTemporaryInstance.get_instance() self.conn = self.tmp_db.conn self.port = self.tmp_db.port if celery and get_attribute_manager: self.am_settings = { 'BROKER_TRANSPORT': 'memory', # Don't use AMQP bus when testing 'BROKER_URL': 'memory://', 'CELERY_EAGER_PROPAGATES_EXCEPTIONS': True, 'CELERY_ALWAYS_EAGER': True, 'CELERY_RESULT_BACKEND': "cache", 'CELERY_CACHE_BACKEND': 'memory', # Be sure to tell AttributeManager about the temporary mongodb instance. 'MONGO_URI': self.tmp_db.get_uri(''), } celery.conf.update(self.am_settings) self.am = get_attribute_manager(celery) self.amdb = self.am.userdb else: self.amdb = UserDB(self.tmp_db.get_uri(''), 'eduid_am') self.amdb._drop_whole_collection() mongo_settings = { 'mongo_replicaset': None, 'mongo_uri': self.tmp_db.get_uri(''), } if getattr(self, 'settings', None) is None: self.settings = mongo_settings else: self.settings.update(mongo_settings) for db_name in self.conn.database_names(): self.conn.drop_database(db_name) # Set up test users in the MongoDB. Read the users from MockedUserDB, which might # be overridden by subclasses. _foo_userdb = self.MockedUserDB(self.mock_users_patches) for userdoc in _foo_userdb.all_userdocs(): this = deepcopy( userdoc) # deep-copy to not have side effects between tests user = User(data=this) self.amdb.save(user, check_sync=False, old_format=userdb_use_old_format)
class TestRemoveninFromUser(TestCase): def setUp(self): ninuser = deepcopy(MOCKED_USER_STANDARD) del ninuser['norEduPersonNIN'] ninuser.update({ 'nins': [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, }, {'number': '0000', 'verified': False, }], }) self.ninuser = User(data = ninuser) def test_remove_primary_nin(self): """ Remove the primary address and expect the next verified one to be promoted to primary """ this = _remove_nin_from_user('1111', self.ninuser) expected = [{'number': '2222', 'verified': True, 'primary': True, }, {'number': '0000', 'verified': False, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_remove_verified_nin(self): """ Remove a verified non-primary address """ this = _remove_nin_from_user('2222', self.ninuser) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '0000', 'verified': False, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_remove_nonverified_nin(self): """ Remove a non-verified address, although this user would not be found in reality """ this = _remove_nin_from_user('0000', self.ninuser) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_add_new_number(self): """ Add a new number to the test user """ this = self.ninuser _add_nin_to_user('3333', this) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }, {'number': '0000', 'verified': False, 'primary': False, }, {'number': '3333', 'verified': True, 'primary': False, 'created_by': 'dashboard', } ] got = this.nins.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: if addr.get('number') == '3333': del addr['created_ts'] self.assertEqual(got, expected) def test_verify_existing_number(self): """ Verify an existing number on the test user """ this = self.ninuser _add_nin_to_user('0000', this) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }, {'number': '0000', 'verified': True, 'primary': False, }, ] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_add_first_number(self): """ Add an number to a test user that has none """ userdoc = self.ninuser.to_dict() del userdoc['nins'] this = User(data = userdoc) _add_nin_to_user('9999', this) expected = [{'number': '9999', 'verified': True, 'primary': True, 'created_by': 'dashboard', }] got = this.nins.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: del addr['created_ts'] self.assertEqual(got, expected)
class TestRemoveMailFromUser(TestCase): def setUp(self): mailuser = deepcopy(MOCKED_USER_STANDARD) mailuser.update({ 'mail': '*****@*****.**', 'mailAliases': [{'email': '*****@*****.**', 'verified': True, }, {'email': '*****@*****.**', 'verified': True, }, {'email': '*****@*****.**', 'verified': False, }], }) self.mailuser = User(data = mailuser) def test_remove_primary_mail(self): """ Remove the primary address and expect the next verified one to be promoted to primary """ this = _remove_mail_from_user('*****@*****.**', self.mailuser) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, }, {'email': '*****@*****.**', 'verified': False, 'primary': False, }] self.assertEqual(this.mail_addresses.to_list_of_dicts(), expected) def test_remove_verified_mail(self): """ Remove a verified non-primary address """ this = _remove_mail_from_user('*****@*****.**', self.mailuser) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, }, {'email': '*****@*****.**', 'verified': False, 'primary': False, }] self.assertEqual(this.mail_addresses.to_list_of_dicts(), expected) def test_remove_nonverified_mail(self): """ Remove a non-verified address, although this user would not be found in reality """ this = _remove_mail_from_user('*****@*****.**', self.mailuser) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, }, {'email': '*****@*****.**', 'verified': True, 'primary': False, }] self.assertEqual(this.mail_addresses.to_list_of_dicts(), expected) def test_add_new_email_address(self): """ Add a new email address to the test user """ this = self.mailuser _add_mail_to_user('*****@*****.**', this) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, }, {'email': '*****@*****.**', 'verified': True, 'primary': False, }, {'email': '*****@*****.**', 'verified': False, 'primary': False, }, {'email': '*****@*****.**', 'verified': True, 'primary': False, 'created_by': 'dashboard', } ] got = this.mail_addresses.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: if addr.get('email') == '*****@*****.**': del addr['created_ts'] self.assertEqual(got, expected) def test_verify_existing_email_address(self): """ Verify an existing email address on the test user """ this = self.mailuser _add_mail_to_user('*****@*****.**', this) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, }, {'email': '*****@*****.**', 'verified': True, 'primary': False, }, {'email': '*****@*****.**', 'verified': True, 'primary': False, }, ] self.assertEqual(this.mail_addresses.to_list_of_dicts(), expected) def test_add_first_email_address(self): """ Add an email address to a test user that has none """ userdoc = self.mailuser.to_dict() del userdoc['mailAliases'] this = User(data = userdoc) _add_mail_to_user('*****@*****.**', this) expected = [{'email': '*****@*****.**', 'verified': True, 'primary': True, 'created_by': 'dashboard', }] got = this.mail_addresses.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: del addr['created_ts'] self.assertEqual(got, expected)
def save(self, user, check_sync=True, old_format=True): if isinstance(user, DashboardLegacyUser): user = User(data = user._mongo_doc) UserDB.save(self, user, check_sync, old_format)
def to_dict(self, old_userdb_format=False): res = User.to_dict(self, old_userdb_format=old_userdb_format) if self._pending_mail_address is not None: res['pending_mail_address'] = self._pending_mail_address.to_dict() return res
def to_dict(self, old_userdb_format=False): res = User.to_dict(self, old_userdb_format=old_userdb_format) res['terminated'] = self.terminated return res
def get_user(self, userid): if userid not in self.test_users: raise self.UserDoesNotExist return User(deepcopy(self.test_users.get(userid)))
class TestRemoveninFromUser(TestCase): def setUp(self): ninuser = deepcopy(MOCKED_USER_STANDARD) del ninuser['norEduPersonNIN'] ninuser.update({ 'nins': [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, }, {'number': '0000', 'verified': False, }], }) self.ninuser = User(data = ninuser) def test_remove_primary_nin(self): """ Remove the primary address and expect the next verified one to be promoted to primary """ this = _remove_nin_from_user('1111', self.ninuser) expected = [{'number': '2222', 'verified': True, 'primary': True, }, {'number': '0000', 'verified': False, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_remove_verified_nin(self): """ Remove a verified non-primary address """ this = _remove_nin_from_user('2222', self.ninuser) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '0000', 'verified': False, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_remove_nonverified_nin(self): """ Remove a non-verified address, although this user would not be found in reality """ this = _remove_nin_from_user('0000', self.ninuser) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_add_new_number(self): """ Add a new number to the test user """ this = self.ninuser _add_nin_to_user('3333', this) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }, {'number': '0000', 'verified': False, 'primary': False, }, {'number': '3333', 'verified': True, 'primary': False, 'created_by': 'dashboard', } ] got = this.nins.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: if addr.get('number') == '3333': del addr['created_ts'] self.assertEqual(got, expected) def test_verify_existing_number(self): """ Verify an existing number on the test user """ this = self.ninuser now = datetime.datetime.utcnow() _add_nin_to_user('0000', this, created_ts = now) expected = [{'number': '1111', 'verified': True, 'primary': True, }, {'number': '2222', 'verified': True, 'primary': False, }, {'number': '0000', 'verified': True, 'primary': False, 'created_by': 'dashboard', 'created_ts': now, }, ] self.assertEqual(this.nins.to_list_of_dicts(), expected) def test_add_first_number(self): """ Add an number to a test user that has none """ userdoc = self.ninuser.to_dict() del userdoc['nins'] this = User(data = userdoc) _add_nin_to_user('9999', this) expected = [{'number': '9999', 'verified': True, 'primary': True, 'created_by': 'dashboard', }] got = this.nins.to_list_of_dicts() # remove the 'created_ts' from the new entry for addr in got: del addr['created_ts'] self.assertEqual(got, expected)
def all_users(self): for user in self.test_users.values(): yield User(deepcopy(user))