[a158f3]: / ForgeBlog / forgeblog / tests / test_app.py  Maximize  Restore  History

Download this file

153 lines (136 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import tempfile
import json
import os
from cgi import FieldStorage
from io import BytesIO
from tg import tmpl_context as c
from ming.odm import ThreadLocalODMSession
from allura import model as M
from allura.lib import helpers as h
from alluratest.controller import setup_basic_test, setup_global_objects
from allura.tests import decorators as td
from forgeblog import model as BM
class TestApp:
def setup_method(self, method):
setup_basic_test()
@td.with_tool('test', 'Blog', 'blog')
def test_uninstall(self):
BM.BlogPost.new(
title='Test title',
text='test post',
)
ThreadLocalODMSession.flush_all()
assert BM.BlogPost.query.get(title='Test title')
# c.app.uninstall(c.project) errors out, but works ok in test_uninstall for repo tools. So instead:
c.project.uninstall_app('blog')
assert not BM.BlogPost.query.get(title='Test title')
@td.with_tool('test', 'Blog', 'blog')
def test_sitemap_xml(self):
assert [] == c.app.sitemap_xml()
BM.BlogPost.new(
title='Blog Title',
state='draft',
text='This is my first blog Post',
)
assert [] == c.app.sitemap_xml()
BM.BlogPost.new(
title='Blog Title',
state='published',
text='This is my first blog Post',
deleted=True
)
assert [] == c.app.sitemap_xml()
BM.BlogPost.new(
title='Blog Title',
state='published',
text='This is my first blog Post',
)
assert 1 == len(c.app.sitemap_xml())
class TestBulkExport:
def setup_method(self, method):
setup_basic_test()
setup_global_objects()
@td.with_tool('test', 'Blog', 'blog')
def test_bulk_export(self):
# Clear out some context vars, to properly simulate how this is run from the export task
# Besides, it's better not to need c context vars
c.app = c.project = None
project = M.Project.query.get(shortname='test')
blog = project.app_instance('blog')
with h.push_context('test', 'blog', neighborhood='Projects'):
post = BM.BlogPost()
post.title = 'Test title'
post.text = 'test post'
post.labels = ['the firstlabel', 'the second label']
post.make_slug()
post.commit()
post.discussion_thread.add_post(text='test comment')
post2 = BM.BlogPost()
post2.title = 'Test2 title'
post2.text = 'test2 post'
post2.make_slug()
post2.commit()
f = tempfile.TemporaryFile('w+')
blog.bulk_export(f)
f.seek(0)
blog = json.loads(f.read())
blog['posts'] = sorted(
blog['posts'], key=lambda x: x['title'], reverse=True)
assert blog['posts'][0]['title'] == 'Test2 title'
assert blog['posts'][0]['text'] == 'test2 post'
assert blog['posts'][1]['title'] == 'Test title'
assert blog['posts'][1]['text'] == 'test post'
assert (blog['posts'][1]['labels'] ==
['the firstlabel', 'the second label'])
assert (blog['posts'][1]['discussion_thread']
['posts'][0]['text'] == 'test comment')
@td.with_tool('test', 'Blog', 'blog')
def test_export_with_attachments(self):
project = M.Project.query.get(shortname='test')
blog = project.app_instance('blog')
with h.push_context('test', 'blog', neighborhood='Projects'):
post = BM.BlogPost.new(
title='Test title',
text='test post',
labels=['the firstlabel', 'the second label'],
delete=None
)
ThreadLocalODMSession.flush_all()
test_file1 = FieldStorage()
test_file1.name = 'file_info'
test_file1.filename = 'test_file'
test_file1.file = BytesIO(b'test file1\n')
p = post.discussion_thread.add_post(text='test comment')
p.add_multiple_attachments(test_file1)
ThreadLocalODMSession.flush_all()
f = tempfile.TemporaryFile('w+')
temp_dir = tempfile.mkdtemp()
blog.bulk_export(f, temp_dir, True)
f.seek(0)
blog = json.loads(f.read())
blog['posts'] = sorted(
blog['posts'], key=lambda x: x['title'], reverse=True)
file_path = 'blog/{}/{}/{}/test_file'.format(
post._id,
post.discussion_thread._id,
list(post.discussion_thread.post_class().query.find())[0].slug
)
assert (blog['posts'][0]['discussion_thread']['posts'][0]
['attachments'][0]['path'] == file_path)
assert os.path.exists(os.path.join(temp_dir, file_path))