def testSurveyResponseTab(self): """Tests that survey response tab has the right URL.""" # organization application is now self.org_app.survey_start = timeline_utils.past() self.org_app.survey_end = timeline_utils.future() self.org_app.put() data = request_data.RequestData(None, None, self.kwargs) tabs = soc_tabs.orgTabs(data) survey_response_tab = [ tab for tab in tabs.tabs if tab.tab_id == soc_tabs.ORG_APP_RESPONSE_TAB_ID ][0] self.assertEqual('/gsoc/org/application/submit/%s' % self.org.key.id(), survey_response_tab.url) # organization application is complete self.org_app.survey_start = timeline_utils.past(delta=100) self.org_app.survey_end = timeline_utils.past(delta=50) self.org_app.put() data = request_data.RequestData(None, None, self.kwargs) tabs = soc_tabs.orgTabs(data) survey_response_tab = [ tab for tab in tabs.tabs if tab.tab_id == soc_tabs.ORG_APP_RESPONSE_TAB_ID ][0] self.assertEqual( '/gsoc/org/survey_response/show/%s' % self.org.key.id(), survey_response_tab.url)
def testUserExists(self): """Tests that user entity is returned when it exists.""" user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) data = request_data.RequestData(None, None, None) self.assertEqual(data.ndb_user.key, user.key)
def testForNotActiveProgram(self): """Tests that access if denied if the program is not active.""" data = request_data.RequestData(None, None, None) data._program = self.program data._timeline = request_data.TimelineHelper(self.program.timeline, None) access_checker = access.ProgramActiveAccessChecker() # the program is not visible data._program.status = program_model.STATUS_INVISIBLE data._program.timeline.program_start = timeline_utils.past() data._program.timeline.program_end = timeline_utils.future() with self.assertRaises(exception.UserError) as context: access_checker.checkAccess(data, None) self.assertEqual(context.exception.message, access._MESSAGE_PROGRAM_NOT_ACTIVE) # the program is has already ended data._program.status = program_model.STATUS_VISIBLE data._program.timeline.program_start = timeline_utils.past(delta=100) data._program.timeline.program_end = timeline_utils.past(delta=50) with self.assertRaises(exception.UserError) as context: access_checker.checkAccess(data, None) self.assertEqual(context.exception.message, access._MESSAGE_PROGRAM_NOT_ACTIVE)
def testDeveloperAccessAllowed(self): data = request_data.RequestData(None, None, None) # TODO(nathaniel): Reaching around RequestHandler public API. data._is_developer = True access_checker = access.DeveloperAccessChecker() access_checker.checkAccess(data, None)
def testGetHostName(self): """Tests that a correct host name is returned.""" test_data = request_data.RequestData(None, None, None) test_data._site = site_model.Site(link_id='test', hostname='test_host') try: expected_host = os.environ['HTTP_HOST'] = 'some.testing.host.tld' self.assertEqual(site.getHostname(), expected_host) finally: if self.default_host is None: del os.environ['HTTP_HOST'] else: os.environ['HTTP_HOST'] = self.default_host # test a data object expected_host = 'test_host' self.assertEqual(site.getHostname(data=test_data), expected_host) test_data.site.hostname = '' try: expected_host = os.environ['HTTP_HOST'] = 'some.testing.host.tld' self.assertEqual(site.getHostname(data=test_data), expected_host) finally: if self.default_host is None: del os.environ['HTTP_HOST'] else: os.environ['HTTP_HOST'] = self.default_host
def testNonLoggedInUserAccessDenied(self): """Tests that exception is raised for a non logged-in user.""" data = request_data.RequestData(None, None, {}) data._gae_user = None data.kwargs['user'] = '******' access_checker = access.IsUrlUserAccessChecker() with self.assertRaises(exception.LoginRequired): access_checker.checkAccess(data, None)
def testNonDeveloperAccessDenied(self): data = request_data.RequestData(None, None, None) # TODO(nathaniel): Reaching around RequestHandler public API. data._is_developer = False access_checker = access.DeveloperAccessChecker() with self.assertRaises(exception.UserError): access_checker.checkAccess(data, None)
def testUrlUserWithNoProfileAccessDenied(self): """Tests that access is denied for a user that does not have a profile.""" self.kwargs['user'] = '******' data = request_data.RequestData(None, None, self.kwargs) access_checker = access.NonStudentUrlProfileAccessChecker() with self.assertRaises(exception.UserError) as context: access_checker.checkAccess(data, None) self.assertEqual(context.exception.status, httplib.NOT_FOUND)
def initialize(self, request, args, kwargs): """See Initializer.initialize for specification.""" data = request_data.RequestData(request, args, kwargs) mutator = access_checker.Mutator(data) if data.is_developer: check = access_checker.DeveloperAccessChecker(data) else: check = access_checker.AccessChecker(data) return data, check, mutator
def testNonStudentAccessGranted(self): """Tests that access is granted for users with non-student accounts.""" # seed URL profile who is not a student url_profile = profile_utils.seedNDBProfile(self.program.key()) self.kwargs['user'] = url_profile.profile_id data = request_data.RequestData(None, None, self.kwargs) access_checker = access.NonStudentUrlProfileAccessChecker() access_checker.checkAccess(data, None)
def testForNonExistingProgram(self): """Tests that access is denied if the program does not exist.""" data = request_data.RequestData(None, None, None) data._program = None access_checker = access.ProgramActiveAccessChecker() with self.assertRaises(exception.UserError) as context: access_checker.checkAccess(data, None) self.assertEqual(context.exception.message, access._MESSAGE_PROGRAM_NOT_EXISTING)
def setUp(self): """See unittest.TestCase.setUp for specification.""" sponsor = program_utils.seedSponsor() self.program = program_utils.seedProgram(sponsor_key=sponsor.key()) kwargs = { 'sponsor': sponsor.key().name(), 'program': self.program.program_id } self.data = request_data.RequestData(None, None, kwargs)
def testUserError(self): """Tests that a reasonable response is returned for any user error.""" data = request_data.RequestData(http.HttpRequest(), [], {}) status_code = httplib.EXPECTATION_FAILED message = 'test-user-error-message' user_error = exception.UserError(status_code, message=message) response = error.MELANGE_ERROR_HANDLER.handleUserError( user_error, data) self.assertEqual(status_code, response.status_code) self.assertIn(message, response.content)
def testNoOrgData(self): """Tests that error is raised if there is no org data in kwargs.""" # no data at all data = request_data.RequestData(None, None, {}) with self.assertRaises(exception.UserError) as context: data.url_org self.assertEqual(context.exception.status, httplib.BAD_REQUEST) # program data but no user identifier kwargs = {'sponsor': 'sponsor_id', 'program': 'program_id'} data = request_data.RequestData(None, None, kwargs) with self.assertRaises(exception.UserError) as context: data.url_org self.assertEqual(context.exception.status, httplib.BAD_REQUEST) # user identifier present but no program data data = request_data.RequestData(None, None, {'organization': 'org_id'}) with self.assertRaises(exception.UserError) as context: data.url_org self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
def testServerError(self): """Tests that a reasonable response is returned for any server error.""" data = request_data.RequestData(http.HttpRequest(), [], {}) status_code = httplib.GATEWAY_TIMEOUT message = 'test-server-error-message' server_error = exception.ServerError(status_code, message=message) response = error.MELANGE_ERROR_HANDLER.handleServerError( server_error, data) self.assertEqual(status_code, response.status_code) self.assertIn(message, response.content)
def testOrgDoesNotExist(self): """Tests that error is raised if requested organization does not exist.""" kwargs = { 'sponsor': 'sponsor_id', 'program': 'program_id', 'organization': 'org_id' } data = request_data.RequestData(None, None, kwargs) with self.assertRaises(exception.UserError) as context: data.url_org self.assertEqual(context.exception.status, httplib.NOT_FOUND)
def testPreferencesTab(self): """Tests that Preferences tab is present for accepted organizations only.""" # check that tab is not present for organizations that are not accepted statuses = [ org_model.Status.APPLYING, org_model.Status.PRE_ACCEPTED, org_model.Status.PRE_REJECTED, org_model.Status.REJECTED ] for status in statuses: self.org.status = status self.org.put() data = request_data.RequestData(None, None, self.kwargs) tabs = soc_tabs.orgTabs(data) self.assertNotIn(soc_tabs.ORG_PREFERENCES_TAB_ID, [tab.tab_id for tab in tabs.tabs]) # check that tab is present for an organization that is accepted self.org.status = org_model.Status.ACCEPTED self.org.put() data = request_data.RequestData(None, None, self.kwargs) tabs = soc_tabs.orgTabs(data) self.assertIn(soc_tabs.ORG_PREFERENCES_TAB_ID, [tab.tab_id for tab in tabs.tabs])
def testProfileExists(self): """Tests that profile is returned correctly if exists.""" sponsor = program_utils.seedSponsor() program = program_utils.seedProgram(sponsor_key=sponsor.key()) profile = profile_utils.seedNDBProfile(program.key()) kwargs = { 'sponsor': sponsor.link_id, 'program': program.program_id, 'user': profile.profile_id, } data = request_data.RequestData(None, None, kwargs) url_profile = data.url_ndb_profile self.assertEqual(profile.key, url_profile.key)
def testForNonHostUser(self): """Tests that False is returned for a user who is not a host.""" sponsor = program_utils.seedSponsor() program = program_utils.seedProgram(sponsor_key=sponsor.key()) user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) kwargs = { 'sponsor': sponsor.link_id, 'program': program.program_id, } data = request_data.RequestData(None, None, kwargs) is_host = data.is_host self.assertFalse(is_host)
def setUp(self): """See unittest.TestCase.setUp for specification.""" self.sponsor = program_utils.seedSponsor() self.program = program_utils.seedProgram( sponsor_key=self.sponsor.key()) # seed a user who will be tested for access self.user = profile_utils.seedNDBUser() profile_utils.loginNDB(self.user) kwargs = { 'sponsor': self.sponsor.key().name(), 'program': self.program.program_id, } self.data = request_data.RequestData(None, None, kwargs)
def testNoConnectionData(self): """Tests that error is raised if there is no enough data in the URL.""" # no data at all data = request_data.RequestData(None, None, {}) with self.assertRaises(exception.UserError) as context: data.url_connection self.assertEqual(context.exception.status, httplib.BAD_REQUEST) # program and connection data but no user identifier kwargs = { 'sponsor': 'sponsor_id', 'program': 'program_id', 'id': '1', } data = request_data.RequestData(None, None, kwargs) with self.assertRaises(exception.UserError) as context: data.url_connection self.assertEqual(context.exception.status, httplib.BAD_REQUEST) # profile data but no connection identifier kwargs = { 'sponsor': 'sponsor_id', 'program': 'program_id', 'user': '******', } data = request_data.RequestData(None, None, kwargs) with self.assertRaises(exception.UserError) as context: data.url_connection self.assertEqual(context.exception.status, httplib.BAD_REQUEST) # only connection id kwargs = {'id': '1'} data = request_data.RequestData(None, None, kwargs) with self.assertRaises(exception.UserError) as context: data.url_connection self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
def testForDeveloperWithViewAsUser(self): """Tests for developer who has 'view_as' property set.""" user = profile_utils.seedNDBUser() profile_utils.loginNDB(user, is_admin=True) # set the settings so that 'view_as' is set to an existing user other_user = profile_utils.seedNDBUser() settings = settings_model.UserSettings(parent=user.key, view_as=other_user.key) settings.put() # check that the other user is returned data = request_data.RequestData(None, None, None) self.assertEqual(data.ndb_user.key, other_user.key) # set the settings so that 'view_as' is set to a non-existing user settings.view_as = ndb.Key('User', 'non_existing') settings.put() # check that an error is raised data = request_data.RequestData(None, None, None) with self.assertRaises(exception.UserError) as context: user = data.ndb_user self.assertEqual(context.exception.status, httplib.BAD_REQUEST)
def testForActiveProgram(self): """Tests that access is granted if the program is active.""" data = request_data.RequestData(None, None, None) data._program = self.program data._timeline = request_data.TimelineHelper(self.program.timeline, None) access_checker = access.ProgramActiveAccessChecker() # program is active and visible data._program.status = program_model.STATUS_VISIBLE data._program.timeline.program_start = timeline_utils.past() data._program.timeline.program_end = timeline_utils.future() access_checker.checkAccess(data, None)
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 testIsSecondaryHostName(self): """Tests if a request is from a secondary hostname.""" test_data = request_data.RequestData(None, None, None) test_data._site = site_model.Site(link_id='test', hostname='test_host') try: os.environ['HTTP_HOST'] = 'some.testing.host.tld' self.assertFalse(site.isSecondaryHostname()) self.assertFalse(site.isSecondaryHostname(data=test_data)) test_data.site.hostname = "testing" self.assertTrue(site.isSecondaryHostname(data=test_data)) finally: if self.default_host is None: del os.environ['HTTP_HOST'] else: os.environ['HTTP_HOST'] = self.default_host
def testStudentAccessDenied(self): """Tests that access is denied for a user with a student profile.""" # additionally, seed a profile who is not a student # access should be still denied as the check corresponds to URL profile user = profile_utils.seedNDBUser() profile_utils.loginNDB(user) profile_utils.seedNDBProfile(self.program.key(), user=user) # seed URL profile who is a student url_profile = profile_utils.seedNDBStudent(self.program) self.kwargs['user'] = url_profile.profile_id data = request_data.RequestData(None, None, self.kwargs) access_checker = access.NonStudentUrlProfileAccessChecker() with self.assertRaises(exception.UserError) as context: access_checker.checkAccess(data, None) self.assertEqual(context.exception.message, access._MESSAGE_STUDENTS_DENIED)
def testOrgExists(self): """Tests that organization is returned correctly if exists.""" sponsor = program_utils.seedSponsor() program = seeder_logic.seed(program_model.Program) org_properties = { 'key_name': '%s/%s/test_org' % (sponsor.link_id, program.program_id), 'link_id': 'test_org', } org = seeder_logic.seed(org_model.Organization, org_properties) kwargs = { 'sponsor': sponsor.link_id, 'program': program.program_id, 'organization': org.link_id } data = request_data.RequestData(None, None, kwargs) url_org = data.url_org self.assertEqual(org.key(), url_org.key())
def setUp(self): """See unittest.TestCase.setUp for specification.""" sponsor = program_utils.seedSponsor() program_properties = { 'sponsor': sponsor, 'scope': sponsor, } program = seeder_logic.seed(program_model.Program, properties=program_properties) org_properties = { 'program': program, 'scope': program, } self.organization = seeder_logic.seed(org_model.Organization, properties=org_properties) kwargs = { 'sponsor': sponsor.key().name(), 'program': program.link_id, 'organization': self.organization.link_id, } self.data = request_data.RequestData(None, None, kwargs)
def testForLoggedOutUser(self): """Tests that no exception is raised for a non logged-in user.""" data = request_data.RequestData(http.HttpRequest(), None, {}) data._gae_user = None access.ensureLoggedOut(data)
def testForLoggedInUser(self): """Tests that exception is raised for a logged-in user.""" data = request_data.RequestData(http.HttpRequest(), None, {}) data._gae_user = '******' with self.assertRaises(exception.Redirect): access.ensureLoggedOut(data)