def test_groups_instance(): headers = { 'content-type': "application/json", 'x-mailerlite-apikey': API_KEY_TEST } with pytest.raises(ValueError): Groups(API_KEY_TEST) groups = Groups(headers) res_json = groups.all(as_json=True) # TEST to check if there is new key in the API missing_keys = [] for g in res_json: for k in g.keys(): if not hasattr(Group, k): missing_keys.append(k) missing_keys = set(missing_keys) error_msg = "The following keys are unkown: {}, ".format(missing_keys) error_msg += "please update API" assert not missing_keys, error_msg # Test to check if is group object is valid res = groups.all() assert len(res) > 0 for g in res: assert isinstance(g, Group)
def test_groups_instance(header): with pytest.raises(ValueError): Groups(API_KEY_TEST) try: groups = Groups(header) except ValueError: return res_json = groups.all(as_json=True) # TEST to check if there is new key in the API missing_keys = [] for g in res_json: for k in g.keys(): if not hasattr(Group, k): missing_keys.append(k) missing_keys = set(missing_keys) error_msg = "The following keys are unkown: {}, ".format(missing_keys) error_msg += "please update API" assert not missing_keys, error_msg # Test to check if is group object is valid res = groups.all() assert len(res) > 0 for g in res: assert isinstance(g, Group)
def test_groups_single_subscriber(header): groups = Groups(header) n_groups = groups.all() if not len(n_groups): pytest.skip("No groups found") try: group_1 = n_groups[0] except IndexError: pytest.skip("Group index not found. Maybe deleted by another thread") subs_in_group_1 = groups.subscribers(group_1.id) if not len(subs_in_group_1): pytest.skip("No subscriber found in this group.") try: sub1 = subs_in_group_1[0] except IndexError: pytest.skip("Subscriber not found. Maybe deleted by another thread") tmp_sub = groups.subscriber(group_1.id, sub1.id) assert sub1.email == tmp_sub.email attempt = itertools.count() while True and next(attempt) < 20: try: num = random.randint(1000, 100000) mail = generate_random_email(length=15, seed=num) data = { 'name': 'John', 'email': mail, 'fields': { 'company': 'MailerLite' } } new_sub = groups.add_single_subscriber(group_1.id, data) except OSError: time.sleep(3) else: break if new_sub: assert new_sub.email == mail groups.delete_subscriber(group_1.id, new_sub.id) with pytest.raises(ValueError): data = {'name': 'John', 'fields': {'company': 'MailerLite'}} groups.add_single_subscriber(group_1.id, data)
def test_groups_subscriber(header): groups = Groups(header) n_groups = groups.all() assert len(n_groups) > 0 group_1 = n_groups[0] subs_in_group_1 = groups.subscribers(group_1.id) assert len(subs_in_group_1) > 0 sub1 = subs_in_group_1[0] tmp_sub = groups.subscriber(group_1.id, sub1.id) assert sub1.email == tmp_sub.email while True: try: num = random.randint(1000, 100000) mail = generate_random_email(length=15, seed=num) data = {'name': 'John', 'email': mail, 'fields': {'company': 'MailerLite'} } new_subs = groups.add_subscribers(group_1.id, data) except OSError: time.sleep(3) else: break print(new_subs) if new_subs: assert new_subs[0].email == mail groups.delete_subscriber(group_1.id, new_subs[0].id)
def __init__(self, api_key=None): """Initialize a new mailerlite.api object. Parameters ---------- api_key : str Your mailerlite api_key. """ api_key = api_key or os.environ.get("MAILERLITE_PYTHON_API_KEY", None) if not api_key or not isinstance(api_key, str): raise ValueError("Empty API_KEY. Please enter a valid API_KEY") self._headers = { 'content-type': "application/json", "X-MailerLite-ApiDocs": "true", 'x-mailerlite-apikey': api_key } self.campaigns = Campaigns(headers=self.headers) self.segments = Segments(headers=self.headers) self.subscribers = Subscribers(headers=self.headers) self.groups = Groups(headers=self.headers) self.fields = Fields(headers=self.headers) self.webhooks = Webhooks(headers=self.headers) self.account = Account(headers=self.headers)
def test_groups_crud(header): groups = Groups(header) expected_group_name = "TEST_K_GROUP" expected_group_name_2 = "TEST_GROUP_KKK" e_res = groups.create(expected_group_name) assert e_res.name == expected_group_name res = groups.get(e_res.id) assert res.name == expected_group_name groups.update(e_res.id, expected_group_name_2) res = groups.get(e_res.id) assert res.name == expected_group_name_2 assert groups.delete(e_res.id) with pytest.raises(OSError): groups.get(e_res.id)
def test_groups_crud(): headers = { 'content-type': "application/json", 'x-mailerlite-apikey': API_KEY_TEST } groups = Groups(headers) expected_group_name = "TEST_K_GROUP" expected_group_name_2 = "TEST_GROUP_KKK" e_res = groups.create(expected_group_name) assert e_res.name == expected_group_name res = groups.get(e_res.id) assert res.name == expected_group_name groups.update(e_res.id, expected_group_name_2) res = groups.get(e_res.id) assert res.name == expected_group_name_2 assert groups.delete(e_res.id) with pytest.raises(OSError): groups.get(e_res.id)
def __init__(self, api_key): """Initialize a new mailerlite.api object. Parameters ---------- api_key : str Your mailerlite api_key. """ if not api_key or not isinstance(api_key, str): raise ValueError("Empty API_KEY. Please enter a valid API_KEY") self._headers = { 'content-type': "application/json", 'x-mailerlite-apikey': api_key } self.campaigns = Campaigns(headers=self.headers) self.segments = Segments(headers=self.headers) self.subscribers = Subscribers(headers=self.headers) self.groups = Groups(headers=self.headers) self.fields = Fields(headers=self.headers) self.webhooks = Webhooks(headers=self.headers) self.account = Account(headers=self.headers)