def testConnectionExists(self): """Tests that access is denied if connection already exists.""" # seed a connection between the profile and organization connection_utils.seed_new_connection(self.profile.key, self.organization.key) access_checker = (connection_view.NoConnectionExistsAccessChecker( urls.UrlNames)) with self.assertRaises(exception.Redirect): access_checker.checkAccess(self.data, None)
def testConnectionExists(self): """Tests that access is denied if connection already exists.""" # seed a connection between the profile and organization connection_utils.seed_new_connection( self.profile.key, self.organization.key) access_checker = ( connection_view.NoConnectionExistsAccessChecker(urls.UrlNames)) with self.assertRaises(exception.Redirect): access_checker.checkAccess(self.data, None)
def testConnectionDoesNotExist(self): """Tests that False is returned if connection does not exist.""" # seed a connection between the org and another profile other_profile = profile_utils.seedNDBProfile(self.program.key()) connection_utils.seed_new_connection(other_profile.key, self.org.key) # seed a connection between the profile and another org other_org = org_utils.seedOrganization(self.program.key()) connection_utils.seed_new_connection(self.profile.key, other_org.key) self.assertFalse( connection_logic.connectionExists(self.profile.key, self.org.key))
def setUp(self): """See unittest.TestCase.setUp for specification.""" program = program_utils.seedProgram() profile = profile_utils.seedNDBProfile(program.key()) org = org_utils.seedOrganization(program.key()) self.connection = connection_utils.seed_new_connection( profile.key, org.key)
def setUp(self): """See unittest.TestCase.setUp for specification.""" self.program = program_utils.seedProgram() self.profile = profile_utils.seedNDBProfile(self.program.key()) org = org_utils.seedOrganization(self.program.key()) self.connection = connection_utils.seed_new_connection( self.profile.key, org.key)
def setUp(self): """See unittest.TestCase.setUp for specification.""" self.init() # seed another profile for a connected user other_profile = profile_utils.seedNDBProfile(self.program.key()) self.connection = connection_utils.seed_new_connection( other_profile.key, self.org.key, seen_by_org=False)
def testConnectionExists(self): """Tests that connection is returned if it exists.""" # seed a connection connection = connection_utils.seed_new_connection( self.profile.key, self.org.key) self.assertEqual( connection_logic.connectionForProfileAndOrganization( self.profile.key, self.org.key), connection)
def setUp(self): """See unittest.TestCase.setUp for specification.""" program = program_utils.seedProgram() # seed a few organizations self.first_org = org_utils.seedOrganization(program.key()) self.second_org = org_utils.seedOrganization(program.key()) self.third_org = org_utils.seedOrganization(program.key()) # seed a few profiles first_profile = profile_utils.seedNDBProfile(program.key()) second_profile = profile_utils.seedNDBProfile(program.key()) self.first_connection = connection_utils.seed_new_connection( first_profile.key, self.first_org.key) self.second_connection = connection_utils.seed_new_connection( second_profile.key, self.first_org.key) self.third_connection = connection_utils.seed_new_connection( first_profile.key, self.second_org.key)
def testListData(self): """Tests that correct list data is loaded.""" user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile = profile_utils.seedNDBProfile(self.program.key(), user=user) first_org = org_utils.seedSOCOrganization( self.program.key(), status=org_model.Status.ACCEPTED) connection_utils.seed_new_connection(profile.key, first_org.key) other_org = org_utils.seedSOCOrganization( self.program.key(), status=org_model.Status.ACCEPTED) connection_utils.seed_new_connection(profile.key, other_org.key) list_data = self.getListData(_getListForUserUrl(self.program), 0) # check that all two connections are listed self.assertEqual(len(list_data), 2)
def setUp(self): """See unittest.TestCase.setUp for specification.""" self.init() user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile = profile_utils.seedNDBProfile(self.program.key(), user=user) self.connection = connection_utils.seed_new_connection( profile.key, self.org.key)
def testConnectionExists(self): """Tests that exception is raised when connection already exists.""" user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile = profile_utils.seedNDBProfile(self.program.key(), user=user) connection = connection_utils.seed_new_connection(profile.key, self.org.key) # check that user is redirected when a connection exists response = self.get(_getStartAsUserUrl(self.org)) self.assertResponseRedirect(response, _getManageAsUserUrl(connection)) # check that bad request is raised when a connection already exists # on POST request after access checker concludes with mock.patch.object( soc_connection_view.START_CONNECTION_AS_USER, 'access_checker', new=access.ALL_ALLOWED_ACCESS_CHECKER): response = self.post(_getStartAsUserUrl(self.org)) self.assertResponseBadRequest(response)
def testConnectionExists(self): """Tests that connection is returned correctly if exists.""" sponsor = program_utils.seedSponsor() program = program_utils.seedProgram(sponsor_key=sponsor.key()) org = org_utils.seedOrganization(program.key()) profile = profile_utils.seedNDBProfile(program.key()) connection = connection_utils.seed_new_connection(profile.key, org.key) kwargs = { 'sponsor': sponsor.link_id, 'program': program.program_id, 'user': profile.profile_id, 'id': str(connection.key.id()) } data = request_data.RequestData(None, None, kwargs) url_connection = data.url_connection self.assertEqual(connection.key, url_connection.key)
def testConnectionExists(self): """Tests that exception is raised when connection already exists.""" user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile = profile_utils.seedNDBProfile(self.program.key(), user=user) connection = connection_utils.seed_new_connection( profile.key, self.org.key) # check that user is redirected when a connection exists response = self.get(_getStartAsUserUrl(self.org)) self.assertResponseRedirect(response, _getManageAsUserUrl(connection)) # check that bad request is raised when a connection already exists # on POST request after access checker concludes with mock.patch.object(soc_connection_view.START_CONNECTION_AS_USER, 'access_checker', new=access.ALL_ALLOWED_ACCESS_CHECKER): response = self.post(_getStartAsUserUrl(self.org)) self.assertResponseBadRequest(response)
def testCorrectMessagesReturned(self): """Tests that correct messages are returned.""" # seed a couple of messages for the connection message1 = connection_utils.seed_new_connection_message( self.connection.key, author=self.profile.key) message2 = connection_utils.seed_new_connection_message(self.connection.key) # seed another organization and a connection other_org = org_utils.seedOrganization(self.program.key()) other_connection = connection_utils.seed_new_connection( self.profile.key, other_org.key) # create a few messages for the other connection for _ in range(4): connection_utils.seed_new_connection_message( other_connection.key, author=self.profile.key) # check that only correct messages are returned messages = connection_logic.getConnectionMessages(self.connection.key) expected_keys = set([message1.key, message2.key]) actual_keys = set([message.key for message in messages]) self.assertEqual(actual_keys, expected_keys)
def testCorrectMessagesReturned(self): """Tests that correct messages are returned.""" # seed a couple of messages for the connection message1 = connection_utils.seed_new_connection_message( self.connection.key, author=self.profile.key) message2 = connection_utils.seed_new_connection_message( self.connection.key) # seed another organization and a connection other_org = org_utils.seedOrganization(self.program.key()) other_connection = connection_utils.seed_new_connection( self.profile.key, other_org.key) # create a few messages for the other connection for _ in range(4): connection_utils.seed_new_connection_message( other_connection.key, author=self.profile.key) # check that only correct messages are returned messages = connection_logic.getConnectionMessages(self.connection.key) expected_keys = set([message1.key, message2.key]) actual_keys = set([message.key for message in messages]) self.assertEqual(actual_keys, expected_keys)
def testListData(self): """Tests that all connections for orgs administrated by user are listed.""" # seed another organization which is administrated by the user second_org = org_utils.seedSOCOrganization(self.program.key()) user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile = profile_utils.seedNDBProfile( self.program.key(), user=user, admin_for=[self.org.key, second_org.key]) # seed a few connections for first organization for _ in range(_NUMBER_OF_CONNECTIONS_FOR_MAIN_ORG): other_profile = profile_utils.seedNDBProfile(self.program.key()) connection_utils.seed_new_connection(other_profile.key, self.org.key) for _ in range(_NUMBER_OF_CONNECTIONS_FOR_SECOND_ORG): other_profile = profile_utils.seedNDBProfile(self.program.key()) connection_utils.seed_new_connection(other_profile.key, second_org.key) # seed another organization which is not administrated by the user third_org = org_utils.seedSOCOrganization(self.program.key()) # seed a few connections for the other organization for _ in range(_NUMBER_OF_CONNECTIONS_FOR_THIRD_ORG): other_profile = profile_utils.seedNDBProfile(self.program.key()) connection_utils.seed_new_connection(other_profile.key, third_org.key) list_data = self.getListData( _getListConnectionsForOrgAdminUrl(profile), 0) # check that all connections are listed: the ones created above for the main # org and the second plus two for the organization admin itself self.assertEqual( len(list_data), _NUMBER_OF_CONNECTIONS_FOR_MAIN_ORG + _NUMBER_OF_CONNECTIONS_FOR_SECOND_ORG + 2)
def testConnectionExists(self): """Tests that True is returned if connection does exist.""" # seed a connection connection_utils.seed_new_connection(self.profile.key, self.org.key) self.assertTrue( connection_logic.connectionExists(self.profile.key, self.org.key))
def seedNDBProfile(program_key, model=ndb_profile_model.Profile, user=None, mentor_for=None, admin_for=None, **kwargs): """Seeds a new profile. Args: program_key: Program key for which the profile is seeded. model: Model class of which a new profile should be seeded. user: User entity corresponding to the profile. mentor_for: List of organizations keys for which the profile should be registered as a mentor. admin_for: List of organizations keys for which the profile should be registered as organization administrator. Returns: A newly seeded Profile entity. """ user = user or seedNDBUser() mentor_for = mentor_for or [] admin_for = admin_for or [] residential_address = address_model.Address(street=TEST_STREET, city=TEST_CITY, province=TEST_PROVINCE, country=TEST_COUNTRY, postal_code=TEST_POSTAL_CODE) properties = {'email': '*****@*****.**' % user.user_id} contact_properties = dict((k, v) for k, v in kwargs.iteritems() if k in contact_model.Contact._properties) properties.update(**contact_properties) contact = contact_model.Contact(**properties) properties = { 'program': ndb.Key.from_old_key(program_key), 'status': ndb_profile_model.Status.ACTIVE, 'public_name': TEST_PUBLIC_NAME, 'first_name': TEST_FIRST_NAME, 'last_name': TEST_LAST_NAME, 'birth_date': datetime.date(1990, 1, 1), 'residential_address': residential_address, 'tee_style': ndb_profile_model.TeeStyle.MALE, 'tee_size': ndb_profile_model.TeeSize.M, 'mentor_for': list(set(mentor_for + admin_for)), 'admin_for': admin_for, 'contact': contact, } properties.update(**kwargs) profile = model(id='%s/%s' % (program_key.name(), user.key.id()), parent=user.key, **properties) profile.put() org_keys = list(set(mentor_for + admin_for)) for org_key in org_keys: if org_key in admin_for: org_role = connection_model.ORG_ADMIN_ROLE else: org_role = connection_model.MENTOR_ROLE connection_properties = { 'user_role': connection_model.ROLE, 'org_role': org_role } # TODO(daniel): remove when all organizations are converted if not isinstance(org_key, ndb.Key): org_key = ndb.Key.from_old_key(org_key) connection_utils.seed_new_connection(profile.key, org_key, **connection_properties) return profile
def seedNDBProfile(program_key, model=ndb_profile_model.Profile, user=None, mentor_for=None, admin_for=None, **kwargs): """Seeds a new profile. Args: program_key: Program key for which the profile is seeded. model: Model class of which a new profile should be seeded. user: User entity corresponding to the profile. mentor_for: List of organizations keys for which the profile should be registered as a mentor. admin_for: List of organizations keys for which the profile should be registered as organization administrator. Returns: A newly seeded Profile entity. """ user = user or seedNDBUser() mentor_for = mentor_for or [] admin_for = admin_for or [] residential_address = address_model.Address( street=TEST_STREET, city=TEST_CITY, province=TEST_PROVINCE, country=TEST_COUNTRY, postal_code=TEST_POSTAL_CODE) properties = {'email': '*****@*****.**' % user.user_id} contact_properties = dict( (k, v) for k, v in kwargs.iteritems() if k in contact_model.Contact._properties) properties.update(**contact_properties) contact = contact_model.Contact(**properties) properties = { 'program': ndb.Key.from_old_key(program_key), 'status': ndb_profile_model.Status.ACTIVE, 'public_name': TEST_PUBLIC_NAME, 'first_name': TEST_FIRST_NAME, 'last_name': TEST_LAST_NAME, 'birth_date': datetime.date(1990, 1, 1), 'residential_address': residential_address, 'tee_style': ndb_profile_model.TeeStyle.MALE, 'tee_size': ndb_profile_model.TeeSize.M, 'mentor_for': list(set(mentor_for + admin_for)), 'admin_for': admin_for, 'contact': contact, } properties.update(**kwargs) profile = model(id='%s/%s' % (program_key.name(), user.key.id()), parent=user.key, **properties) profile.put() org_keys = list(set(mentor_for + admin_for)) for org_key in org_keys: if org_key in admin_for: org_role = connection_model.ORG_ADMIN_ROLE else: org_role = connection_model.MENTOR_ROLE connection_properties = { 'user_role': connection_model.ROLE, 'org_role': org_role } # TODO(daniel): remove when all organizations are converted if not isinstance(org_key, ndb.Key): org_key = ndb.Key.from_old_key(org_key) connection_utils.seed_new_connection( profile.key, org_key, **connection_properties) return profile