class TestExportContactsCommand(TestCase): def setUp(self): self.runner = CliRunner() self.api_helper = ApiHelper(self) self.api_helper.patch_api(go_cli.export_contacts, 'ContactsApiClient') def tearDown(self): self.api_helper.tearDown() def invoke_export_contacts(self, args, account="acc-1", token="tok-1"): return self.runner.invoke(cli, [ '--account', account, 'export-contacts', '--token', token, ] + args) def test_help(self): result = self.runner.invoke(cli, ['export-contacts', '--help']) self.assertEqual(result.exit_code, 0) self.assertTrue( "Export contacts from the contacts API." in result.output) def test_export_no_api_details(self): result = self.runner.invoke(cli, ['export-contacts']) self.assertEqual(result.exit_code, 2) self.assertTrue( "Please specify both the account key and the contacts API" " authentication token. See --help." in result.output) def test_export_no_output_specified(self): result = self.invoke_export_contacts([]) self.assertEqual(result.exit_code, 2) self.assertTrue("Please specify either --csv or --json (but not both)." in result.output) def test_export_to_csv(self): response = self.api_helper.add_contacts( "tok-1", contacts=[ {"msisdn": "1234"}, {"msisdn": "5678"}, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts(['--csv', 'contacts.csv']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.csv') as f: self.assertEqual( f.read(), "msisdn\r\n1234\r\n5678\r\n") def test_export_to_json(self): response = self.api_helper.add_contacts( "tok-1", contacts=[ {"msisdn": "1234"}, {"msisdn": "5678"}, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts(['--json', 'contacts.json']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.json') as f: self.assertEqual( f.read(), '{"msisdn": "1234"}\n{"msisdn": "5678"}\n') def test_resume_csv(self): response = self.api_helper.add_contacts( "tok-1", start_cursor="abcd", contacts=[ {"msisdn": "8888"}, {"msisdn": "9999"}, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts([ '--resume', 'abcd', '--csv', 'contacts.csv']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.csv') as f: self.assertEqual( f.read(), '8888\r\n9999\r\n') def test_resume_json(self): response = self.api_helper.add_contacts( "tok-1", start_cursor="abcd", contacts=[ {"msisdn": "8888"}, {"msisdn": "9999"}, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts([ '--resume', 'abcd', '--json', 'contacts.json']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.json') as f: self.assertEqual( f.read(), '{"msisdn": "8888"}\n{"msisdn": "9999"}\n') def test_page_exception(self): def raise_page_exc(*args, **kw): yield {"msisdn": "1234"} raise PagedException("abcd", Exception("Foo")) self.api_helper.patch_api_method( go_cli.export_contacts, 'ContactsApiClient', 'contacts', raise_page_exc) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts([ '--json', 'contacts.json']) self.assertEqual( result.output, "Error: Error downloading contacts. Please re-run with" " --resume=abcd to resume.\n") with open('contacts.json') as f: self.assertEqual( f.read(), '{"msisdn": "1234"}\n')
class TestSendCommand(TestCase): def setUp(self): self.runner = CliRunner() self.api_helper = ApiHelper(self) self.api_helper.patch_api(go_cli.send, 'HttpApiSender') def tearDown(self): self.api_helper.tearDown() def invoke_send(self, args, account="acc-1", conv_key="conv-1", conv_token="tok-1"): return self.runner.invoke(cli, [ '--account', account, 'send', '--conversation', conv_key, '--token', conv_token, ] + args) def test_send_help(self): result = self.runner.invoke(cli, ['send', '--help']) self.assertEqual(result.exit_code, 0) self.assertTrue( "Send messages via an HTTP API (nostream) conversation." in result.output) def test_send_no_conversation_details(self): result = self.runner.invoke(cli, ['send']) self.assertEqual(result.exit_code, 2) self.assertTrue( "Please specify all of the account key, conversation key and" " conversation authentication token. See --help." in result.output) def test_send_no_data(self): result = self.invoke_send([]) self.assertEqual(result.exit_code, 2) self.assertTrue( "Please specify either --csv or --json." in result.output) def test_send_csv(self): response = self.api_helper.add_send("acc-1", "conv-1", "tok-1") with self.runner.isolated_filesystem(): with open('msgs.csv', 'wb') as f: f.write("to_addr,content\n") f.write("+1234,hello\n") result = self.invoke_send(['--csv', 'msgs.csv']) self.assertEqual(result.output, "Sending message to +1234.\n") self.api_helper.check_response(response, 'PUT', { u'content': u'hello', u'to_addr': u'+1234' }) def test_send_json(self): response = self.api_helper.add_send("acc-1", "conv-1", "tok-1") with self.runner.isolated_filesystem(): with open('msgs.json', 'wb') as f: f.write(json.dumps({"to_addr": "+1234", "content": "hello"})) f.write("\n") result = self.invoke_send(['--json', 'msgs.json']) self.assertEqual(result.output, "Sending message to +1234.\n") self.api_helper.check_response(response, 'PUT', { u'content': u'hello', u'to_addr': u'+1234' })
class TestExportContactsCommand(TestCase): def setUp(self): self.runner = CliRunner() self.api_helper = ApiHelper(self) self.api_helper.patch_api(go_cli.export_contacts, 'ContactsApiClient') def tearDown(self): self.api_helper.tearDown() def invoke_export_contacts(self, args, account="acc-1", token="tok-1"): return self.runner.invoke(cli, [ '--account', account, 'export-contacts', '--token', token, ] + args) def test_help(self): result = self.runner.invoke(cli, ['export-contacts', '--help']) self.assertEqual(result.exit_code, 0) self.assertTrue( "Export contacts from the contacts API." in result.output) def test_export_no_api_details(self): result = self.runner.invoke(cli, ['export-contacts']) self.assertEqual(result.exit_code, 2) self.assertTrue( "Please specify both the account key and the contacts API" " authentication token. See --help." in result.output) def test_export_no_output_specified(self): result = self.invoke_export_contacts([]) self.assertEqual(result.exit_code, 2) self.assertTrue("Please specify either --csv or --json (but not both)." in result.output) def test_export_to_csv(self): response = self.api_helper.add_contacts("tok-1", contacts=[ { "msisdn": "1234" }, { "msisdn": "5678" }, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts(['--csv', 'contacts.csv']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.csv') as f: self.assertEqual(f.read(), "msisdn\r\n1234\r\n5678\r\n") def test_export_to_json(self): response = self.api_helper.add_contacts("tok-1", contacts=[ { "msisdn": "1234" }, { "msisdn": "5678" }, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts(['--json', 'contacts.json']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.json') as f: self.assertEqual(f.read(), '{"msisdn": "1234"}\n{"msisdn": "5678"}\n') def test_resume_csv(self): response = self.api_helper.add_contacts("tok-1", start_cursor="abcd", contacts=[ { "msisdn": "8888" }, { "msisdn": "9999" }, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts( ['--resume', 'abcd', '--csv', 'contacts.csv']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.csv') as f: self.assertEqual(f.read(), '8888\r\n9999\r\n') def test_resume_json(self): response = self.api_helper.add_contacts("tok-1", start_cursor="abcd", contacts=[ { "msisdn": "8888" }, { "msisdn": "9999" }, ]) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts( ['--resume', 'abcd', '--json', 'contacts.json']) self.assertEqual(result.output, "") self.api_helper.check_response(response, 'GET') with open('contacts.json') as f: self.assertEqual(f.read(), '{"msisdn": "8888"}\n{"msisdn": "9999"}\n') def test_page_exception(self): def raise_page_exc(*args, **kw): yield {"msisdn": "1234"} raise PagedException("abcd", Exception("Foo")) self.api_helper.patch_api_method(go_cli.export_contacts, 'ContactsApiClient', 'contacts', raise_page_exc) with self.runner.isolated_filesystem(): result = self.invoke_export_contacts(['--json', 'contacts.json']) self.assertEqual( result.output, "Error: Error downloading contacts. Please re-run with" " --resume=abcd to resume.\n") with open('contacts.json') as f: self.assertEqual(f.read(), '{"msisdn": "1234"}\n')