'Make sure \'zappa certify\':
* Writes a warning with the --no-cleanup flag.
* Errors out when a deployment hasn\'t taken place.
* Writes errors when certificate settings haven\'t been specified.
* Calls Zappa correctly for creates vs. updates.'

def test_certify_sanity_checks(self):

    old_stdout = sys.stderr
    if (sys.version_info[0] < 3):
        sys.stdout = OldStringIO()
    try:
        zappa_cli = ZappaCLI()
        zappa_cli.domain = 'test.example.com'
        try:
            zappa_cli.certify(no_cleanup=True)
        except AttributeError:
            pass
        log_output = sys.stdout.getvalue()
        self.assertIn('You    are    calling    certify    with', log_output)
        self.assertIn('--no-cleanup', log_output)
        class ZappaMock(object, ):
            def __init__(self):
                self.function_versions = []
                self.domain_names = {}
                self.calls = []
            def get_lambda_function_versions(self, function_name):
                return self.function_versions
            def get_domain_name(self, domain):
                return self.domain_names.get(domain)
            def create_domain_name(self, *args, **kw):
                self.calls.append(('create_domain_name', args, kw))
            def update_route53_records(self, *args, **kw):
                self.calls.append(('update_route53_records', args, kw))
            def update_domain_name(self, *args, **kw):
                self.calls.append(('update_domain_name', args, kw))
        zappa_cli.zappa = ZappaMock()
        self.assertRaises(ClickException, zappa_cli.certify)
        zappa_cli.zappa.function_versions = ['$LATEST']
        zappa_cli.api_stage = 'stage'
        zappa_cli.zappa_settings = {'stage': {}}
        zappa_cli.api_stage = 'stage'
        zappa_cli.domain = 'test.example.com'
        try:
            zappa_cli.certify()
        except ClickException as e:
            log_output = str(e)
            self.assertIn("Can't    certify    a    domain    without", log_output)
            self.assertIn('domain', log_output)
        zappa_cli.zappa_settings['stage']['domain'] = 'test.example.com'
        try:
            zappa_cli.certify()
            self.fail('Expected    a    ClickException')
        except ClickException as e:
            log_output = str(e)
            self.assertIn("Can't    certify    a    domain    without", log_output)
            self.assertIn('lets_encrypt_key', log_output)
        zappa_cli.zappa_settings['stage']['certificate'] = 'foo'
        try:
            zappa_cli.certify()
            self.fail('Expected    a    ClickException')
        except ClickException as e:
            log_output = str(e)
            self.assertIn("Can't    certify    a    domain    without", log_output)
            self.assertIn('certificate_key', log_output)
            self.assertIn('certificate_chain', log_output)
        zappa_cli.zappa_settings['stage']['certificate_key'] = 'key'
        try:
            zappa_cli.certify()
            self.fail('Expected    a    ClickException')
        except ClickException as e:
            log_output = str(e)
            self.assertIn("Can't    certify    a    domain    without", log_output)
            self.assertIn('certificate_key', log_output)
            self.assertIn('certificate_chain', log_output)
        zappa_cli.zappa_settings['stage']['certificate_chain'] = 'chain'
        del zappa_cli.zappa_settings['stage']['certificate_key']
        try:
            zappa_cli.certify()
            self.fail('Expected    a    ClickException')
        except ClickException as e:
            log_output = str(e)
            self.assertIn("Can't    certify    a    domain    without", log_output)
            self.assertIn('certificate_key', log_output)
            self.assertIn('certificate_chain', log_output)
        cert_file = tempfile.NamedTemporaryFile()
        cert_file.write('Hello    world')
        cert_file.flush()
        zappa_cli.zappa_settings['stage'].update({'certificate': cert_file.name, 'certificate_key': cert_file.name, 'certificate_chain': cert_file.name})
        sys.stdout.truncate(0)
        zappa_cli.certify(no_cleanup=True)
        self.assertEquals(len(zappa_cli.zappa.calls), 2)
        self.assertTrue((zappa_cli.zappa.calls[0][0] == 'create_domain_name'))
        self.assertTrue((zappa_cli.zappa.calls[1][0] == 'update_route53_records'))
        log_output = sys.stdout.getvalue()
        self.assertIn('Created    a    new    domain    name', log_output)
        zappa_cli.zappa.calls = []
        zappa_cli.zappa.domain_names['test.example.com'] = '*.example.com'
        sys.stdout.truncate(0)
        zappa_cli.certify(no_cleanup=True)
        self.assertEquals(len(zappa_cli.zappa.calls), 1)
        self.assertTrue((zappa_cli.zappa.calls[0][0] == 'update_domain_name'))
        log_output = sys.stdout.getvalue()
        self.assertNotIn('Created    a    new    domain    name', log_output)
        zappa_cli.zappa_settings['stage'].update({'route53_enabled': False})
        zappa_cli.zappa.calls = []
        zappa_cli.zappa.domain_names['test.example.com'] = ''
        sys.stdout.truncate(0)
        zappa_cli.certify(no_cleanup=True)
        self.assertEquals(len(zappa_cli.zappa.calls), 1)
        self.assertTrue((zappa_cli.zappa.calls[0][0] == 'create_domain_name'))
        log_output = sys.stdout.getvalue()
        self.assertIn('Created    a    new    domain    name', log_output)
    finally:
        sys.stdout = old_stdout
