def testGetConfig_NoSuchProject(self): """We reject a request to get a config for a non-existent project.""" request = projects_pb2.GetConfigRequest(project_name='unknown-proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') with self.assertRaises(exceptions.NoSuchProjectException): self.CallWrapped(self.projects_svcr.GetConfig, mc, request)
def testGetConfig_Normal(self): """We can get a project config.""" request = projects_pb2.GetConfigRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped(self.projects_svcr.GetConfig, mc, request) self.assertEqual('proj', response.project_name)
def testGetConfig_PermissionDenied(self): """We reject a request to get a config for a non-viewable project.""" self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY request = projects_pb2.GetConfigRequest(project_name='proj') # User is a member of the members-only project. mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped(self.projects_svcr.GetConfig, mc, request) self.assertEqual('proj', response.project_name) # User is not a member of the members-only project. mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') with self.assertRaises(permissions.PermissionException): self.CallWrapped(self.projects_svcr.GetConfig, mc, request)
def testAssertBaseChecks_Anon(self): """We allow anonymous access, with a XSRF token generated by our app.""" self.auth.user_id = 0 metadata = {monorail_servicer.XSRF_TOKEN_HEADER: xsrf.GenerateToken(0, xsrf.XHR_SERVLET_PATH)} mc = monorailcontext.MonorailContext(self.services, auth=self.auth) self.svcr.AssertBaseChecks(mc, self.request, metadata)
def testAssertWhitelistedOrXSRF_XSRFToken_Header(self, mock_get_client_id): """Our API is limited to our client by checking an XSRF token.""" # Disable special whitelisting of the default client_id while testing. mock_get_client_id.return_value = None # pylint: disable=attribute-defined-outside-init self.request.project_name = 'proj' self.project.committer_ids.append(222) metadata = {'x-xsrf-token': self.token} mc = monorailcontext.MonorailContext(self.services, auth=self.auth) # The token set in setUp() works with self.auth. self.svcr.AssertWhitelistedOrXSRF(mc, metadata) # Passing no token is OK in local_mode try: orig_local_mode = settings.local_mode settings.local_mode = True metadata = {'x-xsrf-token': ''} self.svcr.AssertWhitelistedOrXSRF(mc, metadata) finally: settings.local_mode = orig_local_mode # We detect a missing token. metadata = {'x-xsrf-token': ''} with self.assertRaises(xsrf.TokenIncorrect): self.svcr.AssertWhitelistedOrXSRF(mc, metadata) # We detect a malformed, inappropriate, or expired token. metadata = {'x-xsrf-token': 'bad token'} with self.assertRaises(xsrf.TokenIncorrect): self.svcr.AssertWhitelistedOrXSRF(mc, metadata)
def testListProjectTemplates_PermissionDenied(self): self.project.access = project_pb2.ProjectAccess.MEMBERS_ONLY mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') request = projects_pb2.GetConfigRequest(project_name='proj') with self.assertRaises(permissions.PermissionException): self.CallWrapped(self.projects_svcr.ListProjectTemplates, mc, request)
def testAssertBaseChecks_ProjectNonmember(self): """We allow non-members.""" # pylint: disable=attribute-defined-outside-init self.request.project_name = 'proj' metadata = {} mc = monorailcontext.MonorailContext(self.services, auth=self.auth) self.svcr.AssertBaseChecks(mc, self.request, metadata)
def CallGetStarCount(self): request = projects_pb2.GetProjectStarCountRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.GetProjectStarCount, mc, request) return response.star_count
def testAddIssuesToHotlists(self): # Create two hotlists hotlist_1 = self.services.features.CreateHotlist( self.cnxn, 'Hotlist-1', 'Summary', 'Description', owner_ids=[111], editor_ids=[]) hotlist_2 = self.services.features.CreateHotlist( self.cnxn, 'Hotlist-2', 'Summary', 'Description', owner_ids=[111], editor_ids=[]) # Add Issue 1 to both hotlists request = features_pb2.AddIssuesToHotlistsRequest( note='Foo', hotlist_refs=[ common_pb2.HotlistRef( name='Hotlist-1', owner=common_pb2.UserRef(user_id=111)), common_pb2.HotlistRef( name='Hotlist-2', owner=common_pb2.UserRef(user_id=111))], issue_refs=[ common_pb2.IssueRef(project_name='proj', local_id=1)]) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') self.CallWrapped(self.features_svcr.AddIssuesToHotlists, mc, request) self.assertEqual( [self.issue_1.issue_id], [item.issue_id for item in hotlist_1.items]) self.assertEqual( [self.issue_1.issue_id], [item.issue_id for item in hotlist_2.items]) self.assertEqual('Foo', hotlist_1.items[0].note) self.assertEqual('Foo', hotlist_2.items[0].note)
def testPredictComponent_Normal(self): """Test normal case when predicted component exists.""" component_id = self.services.config.CreateComponentDef( cnxn=None, project_id=self.project.project_id, path='Ruta>Baga', docstring='', deprecated=False, admin_ids=[], cc_ids=[], created=None, creator_id=None, label_ids=[]) self._top_words = { 'foo': 0, 'bar': 1, 'baz': 2} self._components_by_index = { '0': '123', '1': str(component_id), '2': '789'} self._ml_engine.expected_features = [3, 0, 1, 0, 0] self._ml_engine.scores = [5, 10, 3] request = features_pb2.PredictComponentRequest( project_name='proj', text='foo baz foo foo') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') result = self.CallWrapped(self.features_svcr.PredictComponent, mc, request) self.assertEqual( common_pb2.ComponentRef( path='Ruta>Baga'), result.component_ref)
def testCreateHotlist_Normal(self): request = features_pb2.CreateHotlistRequest( name='Fake-Hotlist', summary='Summary', description='Description', editor_refs=[ common_pb2.UserRef(user_id=222), common_pb2.UserRef(display_name='*****@*****.**')], issue_refs=[ common_pb2.IssueRef(project_name='proj', local_id=1), common_pb2.IssueRef(project_name='proj', local_id=2)], is_private=True) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') self.CallWrapped(self.features_svcr.CreateHotlist, mc, request) # Check that the hotlist was successfuly added. hotlist_id = self.services.features.LookupHotlistIDs( self.cnxn, ['Fake-Hotlist'], [111]).get(('fake-hotlist', 111)) hotlist = self.services.features.GetHotlist(self.cnxn, hotlist_id) self.assertEqual('Summary', hotlist.summary) self.assertEqual('Description', hotlist.description) self.assertEqual([111], hotlist.owner_ids) self.assertEqual([222, 333], hotlist.editor_ids) self.assertEqual( [self.issue_1.issue_id, self.issue_2.issue_id], [item.issue_id for item in hotlist.items]) self.assertTrue(hotlist.is_private)
def testCheckHotlistName_InvalidName(self): request = features_pb2.CheckHotlistNameRequest(name='**Invalid**') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') result = self.CallWrapped(self.features_svcr.CheckHotlistName, mc, request) self.assertNotEqual('', result.error)
def testListHotlistIssues(self): hotlist_id = self.services.features.CreateHotlist( self.cnxn, 'Fake-Hotlist', 'Summary', 'Description', owner_ids=[111], editor_ids=[]).hotlist_id self.services.features.UpdateHotlistItems( self.cnxn, hotlist_id, [], [(self.issue_1.issue_id, 222, 12345, 'Note'), (self.issue_2.issue_id, 111, 12346, 'Note')]) self.issue_2.labels = ['Restrict-View-CoreTeam'] owner_ref = common_pb2.UserRef(user_id=111) hotlist_ref = common_pb2.HotlistRef(name='Fake-Hotlist', owner=owner_ref) request = features_pb2.ListHotlistIssuesRequest(hotlist_ref=hotlist_ref) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.LookupLoggedInUserPerms(self.project) response = self.CallWrapped( self.features_svcr.ListHotlistIssues, mc, request) self.assertEqual(1, len(response.items)) self.assertEqual(10, response.items[0].rank) self.assertEqual(12345, response.items[0].added_timestamp) self.assertEqual('Note', response.items[0].note) self.assertEqual( common_pb2.UserRef( user_id=222, display_name='*****@*****.**'), response.items[0].adder_ref) self.assertEqual(1, response.items[0].issue.local_id) self.assertEqual('proj', response.items[0].issue.project_name) self.assertEqual('sum', response.items[0].issue.summary) self.assertEqual('New', response.items[0].issue.status_ref.status)
def testProcessIssueReply_Success(self): self.services.user.TestAddUser('*****@*****.**', 111) mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.perms = permissions.COMMITTER_ACTIVE_PERMISSIONSET mock_uia = commitlogcommands.UpdateIssueAction(self.issue.local_id) self.mox.StubOutWithMock(commitlogcommands, 'UpdateIssueAction') commitlogcommands.UpdateIssueAction( self.issue.local_id).AndReturn(mock_uia) self.mox.StubOutWithMock(mock_uia, 'Parse') mock_uia.Parse(self.cnxn, self.project.project_name, 111, ['awesome!'], self.services, strip_quoted_lines=True) self.mox.StubOutWithMock(mock_uia, 'Run') mock_uia.Run(mc, self.services) self.mox.ReplayAll() ret = self.inbound.ProcessIssueReply(mc, self.project, self.issue.local_id, self.project_addr, 'awesome!') self.mox.VerifyAll() self.assertIsNone(ret)
def testGetUsersProjects(self): self.user = self.services.user.TestAddUser('*****@*****.**', 333) self.services.project_star.SetStar(self.cnxn, self.project.project_id, 222, True) self.project.committer_ids.extend([222]) self.AddUserProjects(222) self.AddUserProjects(333) request = users_pb2.GetUsersProjectsRequest(user_refs=[ common_pb2.UserRef(display_name='*****@*****.**'), common_pb2.UserRef(display_name='*****@*****.**') ]) mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.LookupLoggedInUserPerms(self.project) response = self.CallWrapped(self.users_svcr.GetUsersProjects, mc, request) self.assertEqual([ user_objects_pb2.UserProjects( user_ref=common_pb2.UserRef(display_name='*****@*****.**'), owner_of=['members-only-222', 'owner-live-222'], member_of=['committer-live-222', 'proj'], contributor_to=['contributor-live-222'], starred_projects=['proj']), user_objects_pb2.UserProjects( user_ref=common_pb2.UserRef(display_name='*****@*****.**'), owner_of=['owner-live-333'], member_of=['committer-live-333'], contributor_to=['contributor-live-333']) ], list(response.users_projects))
def testUnlinkAccounts_NotFound(self): """Reject attempt to unlink a user that does not exist or unspecified.""" mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester='*****@*****.**') request = users_pb2.UnlinkAccountsRequest( parent=common_pb2.UserRef(display_name='*****@*****.**'), child=common_pb2.UserRef(display_name='*****@*****.**')) with self.assertRaises(exceptions.NoSuchUserException): self.CallWrapped(self.users_svcr.UnlinkAccounts, mc, request) request = users_pb2.UnlinkAccountsRequest( parent=common_pb2.UserRef(display_name='*****@*****.**'), child=common_pb2.UserRef(display_name='*****@*****.**')) with self.assertRaises(exceptions.NoSuchUserException): self.CallWrapped(self.users_svcr.UnlinkAccounts, mc, request) request = users_pb2.UnlinkAccountsRequest(parent=common_pb2.UserRef( display_name='*****@*****.**')) with self.assertRaises(exceptions.InputException): self.CallWrapped(self.users_svcr.UnlinkAccounts, mc, request) request = users_pb2.UnlinkAccountsRequest(child=common_pb2.UserRef( display_name='*****@*****.**')) with self.assertRaises(exceptions.InputException): self.CallWrapped(self.users_svcr.UnlinkAccounts, mc, request)
def testListProjects_Normal(self): """We can get a list of all projects on the site.""" request = projects_pb2.ListProjectsRequest() mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped(self.projects_svcr.ListProjects, mc, request) self.assertEqual(2, len(response.projects))
def testGetUserSavedQueries_Other_Allowed(self): """See other people's queries if you're an admin.""" self.services.features.UpdateUserSavedQueries(self.cnxn, 111, [ tracker_pb2.SavedQuery(query_id=101, name='test', query='owner:me'), tracker_pb2.SavedQuery(query_id=202, name='hello', query='world', executes_in_project_ids=[987]) ]) self.user_2.is_site_admin = True request = users_pb2.GetSavedQueriesRequest() request.user_ref.display_name = '*****@*****.**' mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped(self.users_svcr.GetSavedQueries, mc, request) self.assertEqual(2, len(response.saved_queries)) self.assertEqual('test', response.saved_queries[0].name) self.assertEqual('owner:me', response.saved_queries[0].query) self.assertEqual('hello', response.saved_queries[1].name) self.assertEqual('world', response.saved_queries[1].query) self.assertEqual(['proj'], response.saved_queries[1].project_names)
def testListFields_TwiceIndirectPermission(self): """Test that only direct memberships are considered.""" self.AddField('Foo Field', needs_perm='FooPerm') self.project.contributor_ids.extend([777]) self.project.contributor_ids.extend([999]) self.project.extra_perms = [ project_pb2.Project.ExtraPerms( member_id=777, perms=['FooPerm', 'BarPerm'])] request = projects_pb2.ListFieldsRequest( project_name='proj', include_user_choices=True) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.LookupLoggedInUserPerms(self.project) response = self.CallWrapped( self.projects_svcr.ListFields, mc, request) self.assertEqual(1, len(response.field_defs)) field = response.field_defs[0] self.assertEqual('Foo Field', field.field_ref.field_name) self.assertEqual( [666, 777, 999], sorted([user_ref.user_id for user_ref in field.user_choices])) self.assertEqual( ['*****@*****.**', '*****@*****.**', '*****@*****.**'], sorted([user_ref.display_name for user_ref in field.user_choices]))
def testListFields_MultipleFields(self): self.AddField('Bar Field', needs_perm=permissions.VIEW) self.AddField('Foo Field', needs_perm=permissions.EDIT_ISSUE) request = projects_pb2.ListFieldsRequest( project_name='proj', include_user_choices=True) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.ListFields, mc, request) self.assertEqual(2, len(response.field_defs)) field_defs = sorted( response.field_defs, key=lambda field: field.field_ref.field_name) self.assertEqual( ['Bar Field', 'Foo Field'], [field.field_ref.field_name for field in field_defs]) self.assertEqual( [[111, 222, 333], [111, 222]], [sorted(user_ref.user_id for user_ref in field.user_choices) for field in field_defs]) self.assertEqual( [['*****@*****.**', '*****@*****.**', '*****@*****.**'], ['*****@*****.**', '*****@*****.**']], [sorted(user_ref.display_name for user_ref in field.user_choices) for field in field_defs])
def testListFields_IndirectPermission(self): """Test that the permissions of effective ids are also considered.""" self.AddField('Foo Field', needs_perm='FooPerm') self.project.contributor_ids.extend([999]) self.project.extra_perms = [ project_pb2.Project.ExtraPerms( member_id=999, perms=['FooPerm', 'BarPerm'])] request = projects_pb2.ListFieldsRequest( project_name='proj', include_user_choices=True) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.LookupLoggedInUserPerms(self.project) response = self.CallWrapped( self.projects_svcr.ListFields, mc, request) self.assertEqual(1, len(response.field_defs)) field = response.field_defs[0] self.assertEqual('Foo Field', field.field_ref.field_name) # Users 111 and 444 are members of group 999, which has the needed # permission. self.assertEqual( [111, 444, 999], sorted([user_ref.user_id for user_ref in field.user_choices])) self.assertEqual( ['*****@*****.**', '*****@*****.**', '*****@*****.**'], sorted([user_ref.display_name for user_ref in field.user_choices]))
def testListFields_CustomPermission(self): self.AddField('Foo Field', needs_perm='FooPerm') self.project.extra_perms = [ project_pb2.Project.ExtraPerms( member_id=111, perms=['UnrelatedPerm']), project_pb2.Project.ExtraPerms( member_id=222, perms=['FooPerm'])] request = projects_pb2.ListFieldsRequest( project_name='proj', include_user_choices=True) mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.ListFields, mc, request) self.assertEqual(1, len(response.field_defs)) field = response.field_defs[0] self.assertEqual('Foo Field', field.field_ref.field_name) self.assertEqual( [222], sorted([user_ref.user_id for user_ref in field.user_choices])) self.assertEqual( ['*****@*****.**'], sorted([user_ref.display_name for user_ref in field.user_choices]))
def testListComponents(self): self.services.config.CreateComponentDef( self.cnxn, self.project.project_id, 'Foo', 'Foo Component', True, [], [], True, 111, []) self.services.config.CreateComponentDef( self.cnxn, self.project.project_id, 'Bar', 'Bar Component', False, [], [], True, 111, []) self.services.config.CreateComponentDef( self.cnxn, self.project.project_id, 'Bar>Baz', 'Baz Component', False, [], [], True, 111, []) request = projects_pb2.ListComponentsRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.ListComponents, mc, request) self.assertEqual( [project_objects_pb2.ComponentDef( path='Foo', docstring='Foo Component', deprecated=True), project_objects_pb2.ComponentDef( path='Bar', docstring='Bar Component', deprecated=False), project_objects_pb2.ComponentDef( path='Bar>Baz', docstring='Baz Component', deprecated=False)], list(response.component_defs))
def assertVisibleMembers(self, expected_user_ids, expected_group_ids, requester=None): request = projects_pb2.GetVisibleMembersRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester=requester) mc.LookupLoggedInUserPerms(self.project) response = self.CallWrapped( self.projects_svcr.GetVisibleMembers, mc, request) self.assertEqual( expected_user_ids, [user_ref.user_id for user_ref in response.user_refs]) # Assert that we get the full email address. self.assertEqual( [self.services.user.LookupUserEmail(self.cnxn, user_id) for user_id in expected_user_ids], [user_ref.display_name for user_ref in response.user_refs]) self.assertEqual( expected_group_ids, [group_ref.user_id for group_ref in response.group_refs]) # Assert that we get the full email address. self.assertEqual( [self.services.user.LookupUserEmail(self.cnxn, user_id) for user_id in expected_group_ids], [group_ref.display_name for group_ref in response.group_refs]) return response
def testListFields_NoFields(self): request = projects_pb2.ListFieldsRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.ListFields, mc, request) self.assertEqual(0, len(response.field_defs))
def testGetCustomPermissions_NoCustomPermissions(self): self.project.extra_perms = [] request = projects_pb2.GetConfigRequest(project_name='proj') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') response = self.CallWrapped( self.projects_svcr.GetCustomPermissions, mc, request) self.assertEqual([], response.permissions)
def testCheckProjectName_NotAllowed(self): """Users that can't create a project shouldn't get any information.""" request = projects_pb2.CheckProjectNameRequest(project_name='Foo') mc = monorailcontext.MonorailContext( self.services, cnxn=self.cnxn, requester='*****@*****.**') mc.LookupLoggedInUserPerms(self.project) with self.assertRaises(permissions.PermissionException): self.CallWrapped(self.projects_svcr.CheckProjectName, mc, request)
def testSetUserPrefs_Anon(self): """Anon cannot set prefs.""" request = users_pb2.SetUserPrefsRequest() mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester=None) with self.assertRaises(exceptions.InputException): self.CallWrapped(self.users_svcr.SetUserPrefs, mc, request)
def testAssertBaseChecks_ProjectMember(self): """We allow signed-in project members.""" # pylint: disable=attribute-defined-outside-init self.request.project_name = 'proj' self.project.committer_ids.append(222) metadata = {} mc = monorailcontext.MonorailContext(self.services, auth=self.auth) self.svcr.AssertBaseChecks(mc, self.request, metadata)
def CallStar(self, requester='*****@*****.**', starred=True): request = users_pb2.StarUserRequest( user_ref=common_pb2.UserRef(user_id=222), starred=starred) mc = monorailcontext.MonorailContext(self.services, cnxn=self.cnxn, requester=requester) response = self.CallWrapped(self.users_svcr.StarUser, mc, request) return response.star_count