Ejemplo n.º 1
0
class TestEmmaMembersGroups(unittest.TestCase):
    def setUp(self):
        self.em = Em(account_id, public_key, private_key)

    def test100_create_groups(self):
        result = self.em.create_groups({"groups": [{"group_name": "Test Group Start1"}, {"group_name": "Test Group2"}]})
        self.assertEqual(len(result), 2, "Wrong number of groups added")
        self.assertEqual(result[0]["group_name"], "Test Group Start1", "Incorrect group_name")
        self.assertEqual(result[1]["group_name"], "Test Group2", "Incorrect group_name")

        TestEmmaMembersGroups.member_group_id1 = result[0]["member_group_id"]
        TestEmmaMembersGroups.member_group_id2 = result[1]["member_group_id"]

    def test150_list_groups(self):
        result = self.em.list_groups()
        self.assertTrue(isinstance(result, list), "No groups listed")

    def test200_update_group(self):
        time.sleep(3)
        result = self.em.update_group(TestEmmaMembersGroups.member_group_id1, {"group_name": "Test Group1"})
        self.assertTrue(result, "Group not updated")
        time.sleep(3)
        result = self.em.get_group_detail(TestEmmaMembersGroups.member_group_id1)
        self.assertTrue(isinstance(result, dict), "Group detail returned None")
        self.assertEqual(result["group_name"], "Test Group1", "Updated group has incorrect name")

    def test300_single_member(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        email = random_email(8)
        # Need to use a random email address because this problem:
        # When adding a member using import_single_member(), and then doing a delete, Emma doesn't appear to
        # completely remove the member. If I again attempt to add the member with same email address, I get this result:
        # result: {u'status': u'o', u'added': False, u'member_id': 241661075}
        # However, when I check my account info online, do not even see this member.

        print ("email: %s" % email)
        member = {"fields": {"first_name": "Firstname", "last_name": "Lastname"}, "email": email}
        result = self.em.import_single_member(member)
        print ("import_single_member: %s" % result)
        TestEmmaMembersGroups.member_id1 = result["member_id"]
        self.assertEqual(result["added"], True, "Member not added")
        print ("TestEmmaMembersGroups.member_id1: %s" % TestEmmaMembersGroups.member_id1)

        result = self.em.update_member(
            TestEmmaMembersGroups.member_id1, {"fields": {"first_name": "John", "last_name": "Smith"}}
        )
        self.assertTrue(result, "Member not updated")
        time.sleep(3)

        result = self.em.get_member_detail(TestEmmaMembersGroups.member_id1)
        print ("get_member_detail: %s" % result)
        self.assertTrue(isinstance(result, dict), "Member detail not returned")
        print ("result['member_id']: %s" % result["member_id"])
        self.assertEqual(result["member_id"], TestEmmaMembersGroups.member_id1, "Member_id is incorrect")

        # Note: Emma converts email addresses to lowercase
        result = self.em.get_member_detail_by_email(email)
        print ("get_member_detail_by_email(%s): %s" % (email, result))
        self.assertTrue(isinstance(result, dict), "Member detail by email not returned")
        self.assertEqual(result["email"], email, "Member email is incorrect")

        result = self.em.update_members_status({"member_ids": [TestEmmaMembersGroups.member_id1], "status_to": "o"})
        self.assertTrue(result, "Member status not changed")

        result = self.em.get_member_optout_detail(TestEmmaMembersGroups.member_id1)

    def test400_delete_member(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        if TestEmmaMembersGroups.member_id1 is not None:
            result = self.em.delete_member(TestEmmaMembersGroups.member_id1)
            print ("delete_member: %s" % result)
            self.assertTrue(result, "Member not deleted")

    def test500_add_members(self):
        members = {"members": [{"email": "*****@*****.**"}, {"email": "*****@*****.**"}]}
        result = self.em.import_member_list(members)
        self.assertTrue(isinstance(result, dict), "Members not added")

    def test600_list_members(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        time.sleep(5)
        result = self.em.list_members()
        print ("list_members: %s" % result)
        self.assertNotEqual(len(result), 0, "Members not found")
        TestEmmaMembersGroups.members_list = result

    def test700_add_members_to_group(self):

        if len(TestEmmaMembersGroups.members_list) > 0:
            member_ids_list = []
            for mem in TestEmmaMembersGroups.members_list:
                member_ids_list.append(mem["member_id"])

            result = self.em.add_members_to_group(
                TestEmmaMembersGroups.member_group_id1, {"member_ids": member_ids_list}
            )
            self.assertTrue(isinstance(result, list), "No members added to group")

    def test750_list_group_members(self):
        result = self.em.list_group_members(TestEmmaMembersGroups.member_group_id1)
        self.assertTrue(isinstance(result, list), "No members found in group")

    def test800_list_member_groups(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        if len(TestEmmaMembersGroups.members_list) > 0:
            mem = TestEmmaMembersGroups.members_list[0]
            member_id = mem["member_id"]
            print ("member_id: %s" % member_id)
            result = self.em.list_member_groups(member_id)
            self.assertTrue(isinstance(result, list), "no groups found for member")

    def test900_add__member_to_groups(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        if len(TestEmmaMembersGroups.members_list) > 0:
            mem = TestEmmaMembersGroups.members_list[0]
            member_id = mem["member_id"]
            print ("member_id: %s" % member_id)
            result = self.em.add_member_to_groups(member_id, {"group_ids": [TestEmmaMembersGroups.member_group_id2]})
            self.assertTrue(isinstance(result, list), "member not added to any group")

    def test910_delete_members(self):
        if len(TestEmmaMembersGroups.members_list) > 0:
            member_ids_list = []
            for mem in TestEmmaMembersGroups.members_list:
                member_ids_list.append(mem["member_id"])

            result = self.em.delete_members({"member_ids": member_ids_list})
            self.assertTrue(result, "Members not deleted")

    def test915_delete_all_members(self):
        members = {"members": [{"email": "*****@*****.**"}, {"email": "*****@*****.**"}]}
        result = self.em.import_member_list(members)
        self.assertTrue(isinstance(result, dict), "New members not added")

        result = self.em.remove_all_members()
        self.assertTrue(result, "Not all nembers deleted")

    def test920_delete_group(self):
        result = self.em.delete_group(TestEmmaMembersGroups.member_group_id1)
        self.assertTrue(result, "Group not deleted")
        result = self.em.delete_group(TestEmmaMembersGroups.member_group_id2)
        self.assertTrue(result, "Group not deleted")

    def test930_removing_members_from_groups(self):
        print "--------------- %s" % sys._getframe().f_code.co_name
        group_list = self.em.create_groups(
            {"groups": [{"group_name": "Test Group1"}, {"group_name": "Test Group2"}, {"group_name": "Test Group3"}]}
        )
        self.assertEqual(len(group_list), 3, "Three groups not created")
        gl = []
        for group in group_list:
            gl.append(group["member_group_id"])
        time.sleep(3)

        # Add 4 new members to all 3 groups that we just created
        members = {
            "members": [
                {"email": "*****@*****.**"},
                {"email": "*****@*****.**"},
                {"email": "*****@*****.**"},
                {"email": "*****@*****.**"},
            ],
            "group_ids": gl,
        }
        result = self.em.import_member_list(members)

        time.sleep(3)
        member_list = self.em.list_members()
        self.assertEqual(len(member_list), 4, "Four members not created")
        ml = []
        for mem in member_list:
            ml.append(mem["member_id"])

        result = self.em.remove_members_from_groups({"group_ids": [gl[0], gl[1]], "member_ids": [ml[0]]})
        self.assertTrue(result, "1. No members removed from groups")

        result = self.em.remove_member_from_groups(ml[1], {"group_ids": [gl[1], gl[2]]})
        self.assertTrue(isinstance(result, list), "2. Member not removed from any group")

        result = self.em.remove_member_from_all_groups(ml[2])
        self.assertTrue(result, "3. Members not removed from any groups")

        result = self.em.remove_members_from_group(gl[0], {"member_ids": [ml[1], ml[3]]})
        self.assertTrue(isinstance(result, list), "4. No members removed from any group")

        result = self.em.remove_all_members_from_group(gl[2])
        self.assertEqual(result, 2, "5. Number of members removed from group not correct")

        result = self.em.remove_all_members_from_all_groups(str(gl[1]))
        self.assertTrue(result, "6. Not all members removed from all groups")

        # Tear down 3 groups
        result = self.em.delete_group(gl[0])
        self.assertTrue(result, "Group0 not deleted")
        result = self.em.delete_group(gl[1])
        self.assertTrue(result, "Group1 not deleted")
        result = self.em.delete_group(gl[2])
        self.assertTrue(result, "Group2 not deleted")

        # Remove any left over members
        result = self.em.remove_all_members()

    def test940_copy_group_to_group(self):
        group_list = self.em.create_groups({"groups": [{"group_name": "Test Group1"}, {"group_name": "Test Group2"}]})
        self.assertEqual(len(group_list), 2, "Two groups were not created")
        gl = []
        for group in group_list:
            gl.append(group["member_group_id"])
        time.sleep(3)

        # Add 2 new members to first group
        members = {"members": [{"email": "*****@*****.**"}, {"email": "*****@*****.**"}], "group_ids": [gl[0]]}
        result = self.em.import_member_list(members)
        time.sleep(3)

        result = self.em.list_group_members(gl[0])
        self.assertTrue(isinstance(result, list), "No members found in group0")

        # Copy members of group gl[0] to group gl[1]; Note: member_status_id is required
        result = self.em.copy_group_to_group(gl[0], gl[1], {"member_status_id": ["a"]})
        self.assertTrue(result, "Active members from group0 did copy to group1")

        result = self.em.list_group_members(gl[1])
        self.assertTrue(isinstance(result, list), "No members found in group1")

        # Tear down 3 groups
        result = self.em.delete_group(gl[0])
        self.assertTrue(result, "Group0 not deleted")
        result = self.em.delete_group(gl[1])
        self.assertTrue(result, "Group1 not deleted")

        # Remove any left over members
        result = self.em.remove_all_members()

    def test950_copy_to_group(self):
        group_list = self.em.create_groups({"groups": [{"group_name": "Test Group1"}]})
        self.assertEqual(len(group_list), 1, "Test group not created")
        time.sleep(3)

        # Add 2 new members to general audience
        members = {"members": [{"email": "*****@*****.**"}, {"email": "*****@*****.**"}]}
        result = self.em.import_member_list(members)
        time.sleep(3)

        # Copy 2 members to group
        g = group_list[0]
        result = self.em.copy_to_group(g["member_group_id"])
        self.assertTrue(result, "No members were copied to Test Group1")

        result = self.em.delete_group(g["member_group_id"])
        self.assertTrue(result, "Test Group1 not deleted")

        # Remove any left over members
        result = self.em.remove_all_members()