def test_check_amount_of_members_gsuite_dominates(self, mocked_logger): g_suite_members = [ { "email": "*****@*****.**" }, { "email": "*****@*****.**" }, ] user = G(OnlineUser) group = G(Group, name="dotkom") ow4_gsuite_sync = settings.OW4_GSUITE_SYNC ow4_gsuite_sync["ENABLED"] = False ow4_gsuite_sync["GROUPS"] = {"appkom": "appkom", "dotkom": "dotkom"} with override_settings(OW4_GSUITE_SYNC=ow4_gsuite_sync): group.user_set.add(user) self.assertFalse( check_amount_of_members_ow4_g_suite(g_suite_members, group.user_set.all(), quiet=False)) mocked_logger.assert_called_with( "There are more users in G Suite ({g_suite_count}) than on OW4 ({ow4_count}). " "Need to trim inactive users from G Suite.".format( g_suite_count=len(g_suite_members), ow4_count=group.user_set.count()))
def test_check_amount_of_members_gsuite_dominates(self, mocked_logger): g_suite_members = [{ 'email': '*****@*****.**' }, { 'email': '*****@*****.**' }] user = G(OnlineUser) group = G(Group, name='dotkom') ow4_gsuite_sync = settings.OW4_GSUITE_SYNC ow4_gsuite_sync['ENABLED'] = False ow4_gsuite_sync['GROUPS'] = {'appkom': 'appkom', 'dotkom': 'dotkom'} with override_settings(OW4_GSUITE_SYNC=ow4_gsuite_sync): group.user_set.add(user) self.assertFalse( check_amount_of_members_ow4_g_suite(g_suite_members, group.user_set.all(), quiet=False)) mocked_logger.assert_called_with( 'There are more users in G Suite ({g_suite_count}) than on OW4 ({ow4_count}). ' 'Need to trim inactive users from G Suite.'.format( g_suite_count=len(g_suite_members), ow4_count=group.user_set.count()))
def test_check_amount_of_members_equal(self): g_suite_members = [{"email": "*****@*****.**"}] user = G(OnlineUser) group = G(Group, name="dotkom") ow4_gsuite_sync = settings.OW4_GSUITE_SYNC ow4_gsuite_sync["ENABLED"] = False ow4_gsuite_sync["GROUPS"] = {"appkom": "appkom", "dotkom": "dotkom"} with override_settings(OW4_GSUITE_SYNC=ow4_gsuite_sync): group.user_set.add(user) self.assertTrue( check_amount_of_members_ow4_g_suite(g_suite_members, group.user_set.all(), quiet=False))
def test_check_amount_of_members_equal(self): g_suite_members = [{'email': '*****@*****.**'}] user = G(OnlineUser) group = G(Group, name='dotkom') ow4_gsuite_sync = settings.OW4_GSUITE_SYNC ow4_gsuite_sync['ENABLED'] = False ow4_gsuite_sync['GROUPS'] = {'appkom': 'appkom', 'dotkom': 'dotkom'} with override_settings(OW4_GSUITE_SYNC=ow4_gsuite_sync): group.user_set.add(user) self.assertTrue( check_amount_of_members_ow4_g_suite(g_suite_members, group.user_set.all(), quiet=False))
def test_check_amount_of_members_ow4_dominates(self, mocked_logger): g_suite_members = [{"email": "*****@*****.**"}] user = G(OnlineUser) user2 = G(OnlineUser) group = G(Group, name="dotkom") ow4_gsuite_sync = settings.OW4_GSUITE_SYNC ow4_gsuite_sync["ENABLED"] = False ow4_gsuite_sync["GROUPS"] = {"appkom": "appkom", "dotkom": "dotkom"} with override_settings(OW4_GSUITE_SYNC=ow4_gsuite_sync): group.user_set.add(user) group.user_set.add(user2) self.assertFalse( check_amount_of_members_ow4_g_suite(g_suite_members, group.user_set.all(), quiet=False)) mocked_logger.assert_called_with( f"There are more users on OW4 ({group.user_set.count()}) than in G Suite ({len(g_suite_members)}). " "Need to update G Suite with new members.")
def handle(self, *args, **kwargs): groups_to_sync = settings.OW4_GSUITE_SYNC.get("GROUPS") domain = settings.OW4_GSUITE_SYNC.get("DOMAIN") logger.info( "Starting sync of OW4 Groups' members to G Suite (domain: %s) with %s" % (domain, settings.OW4_GSUITE_SYNC.get("DELEGATED_ACCOUNT")) ) logger.debug("Groups to be synced: %s" % groups_to_sync) for group in groups_to_sync: g_suite_users = get_g_suite_users_for_group(domain, group) ow4_users = get_ow4_users_for_group(group) logger.debug("Users in OW4: %s" % ow4_users) logger.debug("Users in G Suite: %s" % g_suite_users) should_update = False account_count_eq = check_amount_of_members_ow4_g_suite( g_suite_users, ow4_users ) if not account_count_eq: logger.debug( "Updating G Suite members since the number of members are inconsistent" ) should_update = True else: logger.debug( "Number of group members are equal, double checking email addresses" ) should_update = not check_emails_match_each_other( g_suite_users, ow4_users ) if should_update: logger.info("Syncing %s@%s with OW4 ..." % (group, domain)) update_g_suite_group(domain, group, g_suite_users, ow4_users) logger.info("%s@%s is up to date with OW4." % (group, domain)) logger.info("Done syncing OW4 with G Suite.")