def test_send_mail_creates_message_mailchimp_disquss(self, mailchimp, Message, mail): with patch.dict(self.flask_app.config, {'MAILCHIMP_API_KEY': 'k-3', 'MAILCHIMP_LIST_ID': 1, 'DISQUS_SECRET_KEY': 'key'}): user = UserFactory.create() user_id = user.id brand = 'PYBOSSA' subject = '[%s]: Your account has been deleted' % brand body = """Hi,\n Your account and personal data has been deleted from %s.""" % brand body += '\nDisqus does not provide an API method to delete your account. You will have to do it by hand yourself in the disqus.com site.' admin_addr = '*****@*****.**' recipients = [user.email_addr] + current_app.config.get('ADMINS', []) mail_dict = dict(recipients=recipients, subject=subject, body=body, bcc=[admin_addr]) user_old = user.dictize() mailchimp.side_effect = [FakeResponse(text=json.dumps(dict(status=204)), json=lambda : '', status_code=204)] delete_account(user.id, admin_addr) Message.assert_called_once_with(**mail_dict) mail.send.assert_called_once_with(Message()) user_new = user_repo.get(user_id) assert user_new.name != user_old['name'] assert user_new.fullname != user_old['fullname'] assert user_new.email_addr != user_old['email_addr'] assert not user_new.info assert not user_new.user_pref
def test_send_mail_creates_message_mailchimp_ok(self, mailchimp, Message, mail): with patch.dict(self.flask_app.config, { 'MAILCHIMP_API_KEY': 'k-3', 'MAILCHIMP_LIST_ID': 1 }): user = UserFactory.create() user_id = user.id brand = 'PYBOSSA' subject = '[%s]: Your account has been deleted' % brand body = """Hi,\nYour account and personal data has been deleted from %s.""" % brand recipients = [user.email_addr, '*****@*****.**'] mail_dict = dict(recipients=recipients, subject=subject, body=body) mailchimp.side_effect = [ FakeResponse(text=json.dumps(dict(status=204)), json=lambda: '', status_code=204) ] delete_account(user.id) Message.assert_called_once_with(**mail_dict) mail.send.assert_called_once_with(Message()) user = user_repo.get(user_id) assert user is None
def test_send_mail_creates_message_mailchimp_disquss( self, mailchimp, Message, mail): with patch.dict( self.flask_app.config, { 'MAILCHIMP_API_KEY': 'k-3', 'MAILCHIMP_LIST_ID': 1, 'DISQUS_SECRET_KEY': 'key' }): user = UserFactory.create() user_id = user.id brand = 'PYBOSSA' subject = '[%s]: Your account has been deleted' % brand body = """Hi,\n Your account and personal data has been deleted from the %s.""" % brand body += '\nDisqus does not provide an API method to delete your account. You will have to do it by hand yourself in the disqus.com site.' recipients = [user.email_addr, '*****@*****.**'] mail_dict = dict(recipients=recipients, subject=subject, body=body) mailchimp.side_effect = [ FakeResponse(text=json.dumps(dict(status=204)), json=lambda: '', status_code=204) ] delete_account(user.id) Message.assert_called_once_with(**mail_dict) mail.send.assert_called_once_with(Message()) user = user_repo.get(user_id) assert user is None
def test_webhooks(self, mock): """Test WEBHOOK works.""" mock.return_value = FakeResponse(text=json.dumps(dict(foo='bar')), status_code=200) err_msg = "The webhook should return True from patched method" assert webhook('url', self.webhook_payload), err_msg err_msg = "The post method should be called" assert mock.called, err_msg
def test_get_tasks_raises_exception_for_invalid_manifest(self, requests): headers = {'Content-Type': 'application/json'} invalid_manifest = {'foo': 'bar'} response = FakeResponse(text=json.dumps(invalid_manifest), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = response assert_raises(BulkImportException, self.importer.tasks)
def test_count_tasks_returns_0_if_no_rows_other_than_header(self, request): empty_file = FakeResponse(text='CSV,with,no,content\n', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = empty_file number_of_tasks = self.importer.count_tasks(googledocs_url=self.url) assert number_of_tasks is 0, number_of_tasks
def test_count_tasks_returns_1_for_CSV_with_one_valid_row(self, request): valid_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = valid_file number_of_tasks = self.importer.count_tasks(googledocs_url=self.url) assert number_of_tasks is 1, number_of_tasks
def test_count_tasks_returns_number_of_tasks_in_project(self, request): data = [dict(DeviceID=23), dict(DeviceID=24)] response = FakeResponse(text=json.dumps(data), status_code=200, headers={'content-type': 'application/json'}, encoding='utf-8') request.return_value = response number_of_tasks = self.importer.count_tasks() assert number_of_tasks is 2, number_of_tasks
def test_ask_user_to_subscribe_returns_false_if_user_subscribed(self, req_mock): user = UserFactory.build(newsletter_prompted=False) app = MagicMock() nw = Newsletter() nw.init_app(app) req_mock.side_effect = [FakeResponse(text=json.dumps(dict(foo='bar')), json=lambda: dict(status=200), status_code=200)] assert nw.ask_user_to_subscribe(user) is False
def test_validated_manifest_returned_as_json(self, requests): headers = {'Content-Type': 'application/json'} wrapper = {'okay': 1, 'received': json.dumps(self.create_manifest())} valid_manifest = FakeResponse(text=json.dumps(wrapper), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = valid_manifest returned_manifest = self.importer._get_validated_manifest(None) assert_equal(type(returned_manifest), dict)
def test_exception_when_404_response_for_manifest(self, requests): headers = {'Content-Type': 'application/json'} manifest = self.create_manifest() response = FakeResponse(text=json.dumps(manifest), status_code=404, headers=headers, encoding='utf-8') requests.get.return_value = response assert_raises(BulkImportException, self.importer._get_validated_manifest, None, '2.1')
def test_validated_manifest_returned_as_json(self, requests): headers = {'Content-Type': 'application/json'} manifest = self.create_manifest() response = FakeResponse(text=json.dumps(manifest), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = response returned_manifest = self.importer._get_validated_manifest(None, '2.1') assert_equal(type(returned_manifest), OrderedDict)
def test_tasks_returns_tasks_in_project(self, request): data = [dict(DeviceID=23), dict(DeviceID=24)] response = FakeResponse(text=json.dumps(data), status_code=200, headers={'content-type': 'application/json'}, encoding='utf-8') request.return_value = response task = next(self.importer.tasks()) assert task == {'info': {'DeviceID': 23}}, task
def test_task_count_returns_1_for_valid_manifest(self, requests): headers = {'Content-Type': 'application/json'} manifest = self.create_manifest() response = FakeResponse(text=json.dumps(manifest), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = response count = self.importer.count_tasks() assert_equal(count, 1)
def test_task_count_returns_1_for_valid_manifest(self, requests): headers = {'Content-Type': 'application/json'} wrapper = {'okay': 1, 'received': json.dumps(self.create_manifest())} valid_manifest = FakeResponse(text=json.dumps(wrapper), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = valid_manifest count = self.importer.count_tasks() assert_equal(count, 1)
def test_tasks_works_with_encodings_other_than_utf8(self, request): csv_file = FakeResponse(text=u'Foo\nM\xc3\xbcnchen', status_code=200, headers={'content-type': 'text/plain'}, encoding='ISO-8859-1') request.return_value = csv_file tasks = self.importer.tasks(googledocs_url=self.url) task = tasks.next() assert csv_file.encoding == 'utf-8'
def test_tasks_return_tasks_with_only_info_fields(self, request): csv_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = csv_file tasks = self.importer.tasks(googledocs_url=self.url) task = tasks.next() assert task == {"info": {u'Bar': u'2', u'Foo': u'1', u'Baz': u'3'}}, task
def test_count_tasks_raises_exception_if_dup_header(self, request): empty_file = FakeResponse(text='Foo,Bar,Foo\n1,2,3', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = empty_file msg = "The file you uploaded has two headers with the same name." try: self.importer.count_tasks() except BulkImportException as e: assert e.message == msg, e
def test_tasks_return_tasks_with_non_info_fields_too(self, request): csv_file = FakeResponse(text='Foo,Bar,priority_0\n1,2,3', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = csv_file tasks = self.importer.tasks() task = next(tasks) assert task == {'info': {'Foo': 1, 'Bar': 2}, 'priority_0': 3}, task
def test_tasks_return_tasks_with_only_info_fields(self, request): csv_file = FakeResponse(text='Foo,Bar,Baz\n1,2,3', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = csv_file tasks = self.importer.tasks() task = next(tasks) assert task == {"info": {'Bar': 2, 'Foo': 1, 'Baz': 3}}, task
def test_tasks_raises_exception_if_file_forbidden(self, request): forbidden_request = FakeResponse(text='Forbidden', status_code=403, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = forbidden_request msg = "Oops! It looks like you don't have permission to access that file" assert_raises(BulkImportException, self.importer.tasks, googledocs_url=self.url) try: self.importer.tasks(googledocs_url=self.url) except BulkImportException as e: assert e[0] == msg, e
def test_count_tasks_returns_0_if_no_rows_other_than_header(self, request): empty_file = FakeResponse(text='CSV,with,no,content\n', status_code=200, headers={'content-type': 'text/plain'}, encoding='utf-8') request.return_value = empty_file try: number_of_tasks = self.importer.count_tasks() except BulkImportException as e: msg = "The file you uploaded is a malformed CSV." assert msg == str(e), str(e)
def test_tasks_raises_exception_if_not_CSV_file(self, request): html_request = FakeResponse(text='Not a CSV', status_code=200, headers={'content-type': 'text/html'}, encoding='utf-8') request.return_value = html_request msg = "Oops! That file doesn't look like the right file." assert_raises(BulkImportException, self.importer.tasks, googledocs_url=self.url) try: self.importer.tasks(googledocs_url=self.url) except BulkImportException as e: assert e[0] == msg, e
def test_webhooks_rerun(self, mock): """Test WEBHOOK rerun works.""" mock.return_value = FakeResponse(text=json.dumps(dict(foo='bar')), status_code=200) assert webhook('url', self.webhook_payload, rerun=True), err_msg err_msg = "The post method should be called" assert mock.called, err_msg headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} mock.assert_called_with('url', params=dict(rerun=True), data=json.dumps(self.webhook_payload), headers=headers)
def test_is_user_subscribed_true(self, req_mock): """Test is_user_subscribed returns True.""" with patch.dict(self.flask_app.config, {'MAILCHIMP_API_KEY': 'k-3', 'MAILCHIMP_LIST_ID': 1}): email = '*****@*****.**' nw = Newsletter() nw.init_app(self.flask_app) req_mock.side_effect = [FakeResponse(text=json.dumps(dict(status='200')), json=lambda : dict(status=200), status_code=200)] res, err = nw.is_user_subscribed(email) assert res is True, (res, err)
def test_tasks_raises_exception_if_not_json(self, request): html_request = FakeResponse(text='Not an application/json', status_code=200, headers={'content-type': 'text/html'}, encoding='utf-8') request.return_value = html_request msg = "Oops! That project and form do not look like the right one." assert_raises(BulkImportException, self.importer.tasks) try: self.importer.tasks() except BulkImportException as e: assert e[0] == msg, e
def test_tasks_raises_exception_if_file_forbidden(self, request): forbidden_request = FakeResponse(text='Forbidden', status_code=403, headers={'content-type': 'text/json'}, encoding='utf-8') request.return_value = forbidden_request msg = "Oops! It looks like you don't have permission to access the " \ "EpiCollect Plus project" assert_raises(BulkImportException, self.importer.tasks) try: self.importer.tasks() except BulkImportException as e: assert e.message == msg, e
def test_count_tasks_raises_exception_if_file_forbidden(self, request): forbidden_request = FakeResponse(text='Forbidden', status_code=403, headers={'content-type': 'text/csv'}, encoding='utf-8') request.return_value = forbidden_request msg = "Oops! It looks like you don't have permission to access that file" assert_raises(BulkImportException, self.importer.count_tasks) try: self.importer.count_tasks() except BulkImportException as e: assert msg in str(e), msg
def test_get_tasks_raises_exception_for_invalid_manifest(self, requests): headers = {'Content-Type': 'application/json'} wrapper = {'okay': 0, 'received': 'Something else'} invalid_manifest = FakeResponse(text=json.dumps(wrapper), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = invalid_manifest msg = "Oops! That doesn't look like a valid IIIF manifest." assert_raises(BulkImportException, self.importer.count_tasks) try: self.importer.tasks() except BulkImportException as e: assert e[0] == msg, e
def test_exeption_if_no_collection_iri_for_parent(self, requests): """Test exception if no collection iri when child tasks generated.""" manifest = self.create_manifest() headers = {'Content-Type': 'application/json'} response = FakeResponse(text=json.dumps(manifest), status_code=200, headers=headers, encoding='utf-8') requests.get.return_value = response parent = ProjectFactory() task = TaskFactory(project=parent, n_answers=1) TaskRunFactory.create(task=task) importer = BulkTaskIIIFEnhancedImporter(manifest_uri=self.manifest_uri, parent_id=parent.id) assert_raises(BulkImportException, importer.tasks)