def test_get_messages(): sheet = { EMAILS: [["email_date", "", "2019-10-17"], ["email_subject", "", "test subject"], ["email_body", "", "test body\n$test_var"], ["test_var", "", "hi"]], RECIPIENTS: [ ["Email", "Active", "Dryrun", "Test"], ["*****@*****.**", "", "x", "x"], ] } emails = [('subject', 'body', {})] group = 'test' cfg = Config(extra_emails={"test": ["Extra Tester <*****@*****.**>"]}) extra_recipients = cfg.get_extra_recipients_for_group('test') result = shell.messages_from_markdown(sheet, emails, group, extra_recipients) result = list(result) assert len(result) == 2 msg1, msg2 = result assert msg1.subject == 'subject' assert 'body' in msg1.html_body assert msg1.recipient.email == '*****@*****.**' assert msg2.subject == 'subject' assert 'body' in msg2.html_body assert msg2.recipient.email == 'Extra Tester <*****@*****.**>'
def test_get_extra_recipients_for_group_returns_recipient_if_in_group(): assert Config(extra_emails=None).get_extra_recipients_for_group('') == [] assert (Config(extra_emails={ 'a': 'b' }).get_extra_recipients_for_group('') == []) assert (Config(extra_emails={ 'a': 'b' }).get_extra_recipients_for_group('a') == [Recipient('b')])
def test_process(monkeypatch): mock_sheet = {'mock_sheet': {}} output_sheet_called = False def mock_input_source(options, config, creds): assert options.group == 'test' assert config is not None assert creds is None return lambda: [mock_sheet] def mock_output_sheet(options, config, markdown_outputs, message_outputs, sheet): nonlocal output_sheet_called output_sheet_called = True assert options.group == 'test' assert config is not None assert markdown_outputs is not None assert message_outputs is not None assert sheet == mock_sheet monkeypatch.setattr(shell, "decide_input_source", mock_input_source) monkeypatch.setattr(shell, "output_sheet", mock_output_sheet) monkeypatch.setattr(shell, "needs_valid_creds", lambda _: False) monkeypatch.setattr(shell, "load_config", lambda _: Config()) monkeypatch.setattr(shell, "compose_output_gmail_sender", lambda _: lambda _: None) opts = Options(args.get_parsed_args(['--test'])) shell.process(opts) assert output_sheet_called
def test_validate_creds_heuristic_for_when_not_used(monkeypatch): options = Options(args.get_parsed_args(['--stdin', '--skip-send'])) config = Config() monkeypatch.setattr(shell, 'needs_valid_creds', lambda _: False) new_config, creds = shell.validate_creds(options, config) assert new_config is config assert creds is None
def test_decide_input_from_stdin(): """Given the --stdin argument, Then the input source should be stdin.""" options = Options(args.get_parsed_args(['--stdin'])) config = Config() creds = None # pylint: disable=comparison-with-callable assert shell.decide_input_source(options, config, creds) == shell.input_sheet_from_stdin
def test_output_no_message_handlers(output_fixture): config = Config(keys={'a': 1, 'b': 2}) options = Options( args.get_parsed_args(['-d', '2019-10-17', '--all-keys', '--test'])) markdown_outputs = [output_fixture.stub_markdown_output] message_outputs = [] shell.output_sheet(options, config, markdown_outputs, message_outputs, output_fixture.sheet) assert output_fixture.markdown_called assert not output_fixture.message_called
def test_validate_creds(monkeypatch): options = Options(args.get_parsed_args(['--all-keys'])) config = Config() monkeypatch.setattr(config_module, 'find_config_file', lambda _: None) monkeypatch.setattr(Config, 'validate', lambda _: None) monkeypatch.setattr(Config, 'creds', 'sup') monkeypatch.setattr(auth, 'serialize', lambda _: 'sup') monkeypatch.setattr(Config, 'set_serialized_creds', lambda _self, _: _self) monkeypatch.setattr(Config, 'save_to_file', lambda _self, _: None) new_config, creds = shell.validate_creds(options, config) assert new_config is config assert creds == 'sup'
def test_google_sheets_fetcher_called_with_ids(monkeypatch): monkeypatch.setattr(fetcher, "values", lambda sheet_id, _: 'fetched_' + sheet_id) monkeypatch.setattr(api, "sheets", lambda creds: None) config = Config(keys={'a': '1', 'b': '2'}) options = Options(args.get_parsed_args(['--all-keys'])) sheets_fetcher = shell.compose_input_google_sheets(options=options, config=config, creds='sup') sheets = sheets_fetcher() assert set(sheets) == {'fetched_1', 'fetched_2'}
def test_decide_input_from_google(monkeypatch): """Given --stdin is not set, Then the input source should be Google Sheets.""" def mock_sheets_fetcher(): """fake Google Sheets fetcher""" def mock_input_google_sheets(options, config, creds): """internal function that generates a google sheets fetcher""" assert not options.stdin assert config is not None assert creds == 'uh... sure' return mock_sheets_fetcher monkeypatch.setattr(shell, "compose_input_google_sheets", mock_input_google_sheets) opts = Options(args.get_parsed_args([])) cfg = Config() # pylint: disable=comparison-with-callable assert shell.decide_input_source(opts, cfg, 'uh... sure') == mock_sheets_fetcher mock_sheets_fetcher() # 100% coverage
def test_get_keys__empty_dict_without_keys(): assert Config().get_keys(['a']) == set() assert Config().get_all_keys() == set()
def test_gets_selected_sheet_ids(): options = Options(args.get_parsed_args(['-k', 'a', 'b'])) config = Config(keys={'a': '1', 'b': '2', 'c': '3'}) assert shell.google_sheet_ids(options, config) == {'1', '2'}
def test_gets_all_sheet_ids(): options = Options(args.get_parsed_args(['--all-keys'])) config = Config(keys={'a': 1, 'b': 2}) assert shell.google_sheet_ids(options, config) == {1, 2}
def test_load_config(monkeypatch): config = Config() monkeypatch.setattr(config_module, 'find_config_file', lambda _: None) monkeypatch.setattr(config_module, 'load_from_file', lambda _: config) assert shell.load_config('test_dir') is config
def test_get_keys_returns_only_unique_keys(): assert Config(keys={'a': 1, 'b': 1}).get_keys(['a', 'b']) == {1} assert Config(keys={'a': 1, 'b': 1}).get_all_keys() == {1}
def test_creds_calls_create_or_deserialize_creds(monkeypatch): monkeypatch.setattr(config, 'create_or_deserialize_creds', lambda cred, sec: (cred, sec)) config_obj = Config(client_secret={'secret': '1'}, serialized_creds={'cred': '2'}) assert config_obj.creds == ({'cred': '2'}, {'secret': '1'})
def test_get_keys_returns_no_keys_by_default_and_all_keys_if_passed(): conf = Config(keys={'a': 1, 'b': 2}) assert conf.get_keys([]) == set() assert conf.get_keys(['a']) == {1} assert conf.get_all_keys() == {1, 2}
def test_get_extra_values_returns_extra_values_if_any(): assert Config(extra_values=None).get_extra_values() == {} assert Config(extra_values={1: 2}).get_extra_values() == {1: 2}
def test_get_keys_returns_no_keys_by_default_and_all_keys_if_passed(): conf = Config(keys={'a': 1, 'b': 2}) assert list(conf.get_keys([])) == [] assert list(conf.get_keys(['a'])) == [1] assert list(conf.get_all_keys()) == [1, 2]
def test_set_new_serialized_creds(): assert Config('hi').set_serialized_creds('bye').serialized_creds == 'bye'