def test_wordpress(MockXmlrpclib):
    with wrap() as wrapper:
        with open("wordpress.json", "wb") as f:
            json.dump({}, f)

        with open(".dexyapis", "wb") as f:
            json.dump({
                'wordpress' : {
                    'url' : 'http://example.com',
                    'username' : 'foo',
                    'password' : 'bar'
                    }}, f)

        # Create new (unpublished) draft
        doc = mk_wp_doc(wrapper)
        wrapper.run_docs(doc)

        with open("wordpress.json", "rb") as f:
            result = json.load(f)

        assert result['postid'] == 42
        assert result['publish'] == False

        # Update existing draft
        doc = mk_wp_doc(wrapper)
        wrapper.run_docs(doc)
        assert doc.output().json_as_dict().keys() == ['permaLink']

        result['publish'] = True
        with open("wordpress.json", "wb") as f:
            json.dump(result, f)

        # Publish existing draft
        doc = mk_wp_doc(wrapper)
        wrapper.run_docs(doc)
        assert doc.output().as_text() == "http://example.com/blog/42"

        # Now, separately, test an image upload.
        orig = os.path.join(TEST_DATA_DIR, 'color-graph.pdf')
        shutil.copyfile(orig, 'example.pdf')
        doc = Doc("example.pdf|wp",
                wrapper=wrapper)

        with open(".dexyapis", "wb") as f:
            json.dump({
                'wordpress' : {
                    'url' : 'http://example.com',
                    'username' : 'foo',
                    'password' : 'bar'
                    }}, f)

        wrapper.run_docs(doc)
        assert doc.output().as_text() == "http://example.com/example.pdf"

        # test list categories
        with divert_stdout() as stdout:
            WordPressFilter.docmd_list_categories()
            assert stdout.getvalue() == "categoryName\nfoo\nbar\n"
def test_docmd_create_keyfile_if_exists():
    with wrap() as wrapper:
        with open(".dexyapis", "w") as f:
            f.write("{}")
        assert os.path.exists(".dexyapis")
        try:
            WordPressFilter.docmd_create_keyfile()
            assert False, ' should raise exception'
        except dexy.exceptions.UserFeedback as e:
            assert ".dexyapis already exists" in e.message
def test_api_url_without_php_ending_with_trailing_slash():
    with wrap() as wrapper:
        with open(".dexyapis", "wb") as f:
            json.dump({ "wordpress" : {"url" : "http://example.com/api/"} }, f)

        url = WordPressFilter.api_url()
        assert url == "http://example.com/api/xmlrpc.php"
def test_docmd_create_keyfile():
    with wrap() as wrapper:
        assert not os.path.exists(".dexyapis")
        WordPressFilter.docmd_create_keyfile()
        assert os.path.exists(".dexyapis")