def main(): args = parse_arguments() try: config = import_config(args.config_file) except IOError: print "Unable to locate ~/.python-gitlab.cfg" sys.exit(1) gitlab_key = config.get(section='local', option='private_token') gitlab_url = config.get(section='local', option='url') try: gl = connect_to_gitlab(gitlab_key, gitlab_url) except GitlabAuthenticationError: print "AuthenticationError when connecting to Gitlab Server" sys.exit(1) except GitlabConnectionError: print "Unable to connect to Gitlab server" sys.exit(1) except GitlabError: print "Oops, unknown Gitlab error" sys.exit(1) groups = Fennec.groups(gl) members = Fennec.find_members(groups) namespace_projects = Fennec.find_namespace_projects(gl, groups) forked_projects = Fennec.find_forked_namespace_projects(gl) fennecmessage = FennecMail(groups=groups, members=members, projects=namespace_projects, forked_projects=forked_projects) msg = fennecmessage.render_message() send_mail(msg, mx=args.mx, email_dest=args.email_dest, email_source=args.email_source, reply_to=args.email_dest)
def setUp(self): """ Set up object mock """ self.gl = FakeGitlab self.fennec = Fennec() self.groups = ['group1', 'group2', 'group3']
class TestFennec(unittest.TestCase): def setUp(self): """ Set up object mock """ self.gl = FakeGitlab self.fennec = Fennec() self.groups = ['group1', 'group2', 'group3'] @patch.object(Gitlab, 'Group') def test_group_collection(self, mock_group): # arrange expected_groups = [FakeGroup('i') for i in range(3)] mock_group.return_value = expected_groups # act results = self.fennec.groups(self.gl) # assert self.assertEqual(expected_groups, results) self.assertEqual(isinstance(results[0], Group), True) self.assertEqual(len(results), 3) @patch.object(FakeGroup, 'Member') def test_find_members(self, mock_member): # arrange expected_results = {'group1': ['member1', 'member2', 'member3'], 'group2': ['member1', 'member2', 'member3'], 'group3': ['member1', 'member2', 'member3']} fakegroups = [FakeGroup(group) for group in expected_results.iterkeys()] fakemembers = [FakeGroupMember(member) for member in ['member1', 'member2', 'member3']] mock_member.return_value = fakemembers # act results = self.fennec.find_members(fakegroups) # assert TODO Add more tests self.assertEqual(expected_results, results) @patch.object(Gitlab, 'Project') def test_find_namespace_projects(self, mock_project): # arrange expected_results = {'group1': ['project1', 'project2', 'project3'], 'group2': ['project4', 'project5', 'project6'], 'group3': ['project7', 'project8', 'project9']} fake_projects_group1 = [FakeProject(project) for project in ['project1', 'project2', 'project3']] fake_projects_group2 = [FakeProject(project) for project in ['project4', 'project5', 'project6']] fake_projects_group3 = [FakeProject(project) for project in ['project7', 'project8', 'project9']] for project in fake_projects_group1: setattr(project, 'namespace', FakeGroup('group1')) for project in fake_projects_group2: setattr(project, 'namespace', FakeGroup('group2')) for project in fake_projects_group3: setattr(project, 'namespace', FakeGroup('group3')) fake_groups = [FakeGroup(group) for group in self.groups] fake_projects = fake_projects_group1 + fake_projects_group2 + fake_projects_group3 forked_project = FakeProject('forked_project') setattr(forked_project, 'forked_from_project', dict(path='testpast', name='project2', namespace='not_a_group')) setattr(forked_project, 'namespace', FakeGroup('group3')) fake_projects.append(forked_project) mock_project.return_value = fake_projects # act results = self.fennec.find_namespace_projects(self.gl, fake_groups) # assert self.assertEqual(expected_results, results) @patch.object(Gitlab, 'Project') def test_find_forked_namespace_projects(self, mock_project): # arrange expected_results = {'forked_group': ['project2']} fake_projects_group1 = [FakeProject(project) for project in ['project1', 'project2', 'project3']] fake_projects_group2 = [FakeProject(project) for project in ['project4', 'project5', 'project6']] fake_projects_group3 = [FakeProject(project) for project in ['project7', 'project8', 'project9']] for project in fake_projects_group1: setattr(project, 'namespace', FakeGroup('group1')) for project in fake_projects_group2: setattr(project, 'namespace', FakeGroup('group2')) for project in fake_projects_group3: setattr(project, 'namespace', FakeGroup('group3')) fake_projects = fake_projects_group1 + fake_projects_group2 + fake_projects_group3 forked_project = FakeProject('project2') setattr(forked_project, 'forked_from_project', dict(path='testpath', name='project2', namespace='forked_group')) setattr(forked_project, 'namespace', FakeGroup('forked_group')) fake_projects.append(forked_project) mock_project.return_value = fake_projects # act results = self.fennec.find_forked_namespace_projects(self.gl) # assert self.assertEqual(expected_results, results)
def test_group_collection(self): expected_list = ['group1', 'group2', 'group3', 'group4'] fen = Fennec(self.gl) actually_list = [group.name for group in fen.groups()] self.assertEqual(actually_list, expected_list, "Expected {}, but got {}".format(actually_list, expected_list))