Files
@ 790aeeddcab5
Branch filter:
Location: kallithea/kallithea/controllers/admin/gists.py
790aeeddcab5
10.0 KiB
text/x-python
tests: fix assertion rewriting in some tests with pytest-3.0.0+
Since pytest 3.0.0, asserts present in modules that are not directly seen by
pytest as 'test modules', are no longer rewritten to have improved
reporting, unless they are explicitly marked as up-for-rewriting.
Rationale from pytest upstream:
However since we do not want to test different bytecode then you will
run in production this hook only re-writes test modules themselves as
well as any modules which are part of plugins. Any other imported module
will not be re-written and normal assertion behaviour will happen.
This is e.g. the case for asserts in files like
kallithea/tests/api/api_base.py and kallithea/tests/models/common.py.
See
http://doc.pytest.org/en/latest/changelog.html#id13
http://doc.pytest.org/en/latest/writing_plugins.html#assertion-rewriting
This commit registers all modules below kallithea.tests for assert
rewriting, but note that asserts in kallithea/tests/__init__.py itself are
not rewritten as kallithea.tests is already imported when the register
statement is encountered.
Moving the register statement to kallithea/__init__.py would fix that, but
even then asserts in kallithea/tests/__init__.py seem not to be rewritten
(let alone the issue that we do not want a pytest dependency enforced in
kallithea/__init__.py which is also used in production).
Moving the code from kallithea/tests/__init__.py to a proper module, as
suggested by Mads Kiilerich, solves that problem.
Since pytest 3.0.0, asserts present in modules that are not directly seen by
pytest as 'test modules', are no longer rewritten to have improved
reporting, unless they are explicitly marked as up-for-rewriting.
Rationale from pytest upstream:
However since we do not want to test different bytecode then you will
run in production this hook only re-writes test modules themselves as
well as any modules which are part of plugins. Any other imported module
will not be re-written and normal assertion behaviour will happen.
This is e.g. the case for asserts in files like
kallithea/tests/api/api_base.py and kallithea/tests/models/common.py.
See
http://doc.pytest.org/en/latest/changelog.html#id13
http://doc.pytest.org/en/latest/writing_plugins.html#assertion-rewriting
This commit registers all modules below kallithea.tests for assert
rewriting, but note that asserts in kallithea/tests/__init__.py itself are
not rewritten as kallithea.tests is already imported when the register
statement is encountered.
Moving the register statement to kallithea/__init__.py would fix that, but
even then asserts in kallithea/tests/__init__.py seem not to be rewritten
(let alone the issue that we do not want a pytest dependency enforced in
kallithea/__init__.py which is also used in production).
Moving the code from kallithea/tests/__init__.py to a proper module, as
suggested by Mads Kiilerich, solves that problem.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | # -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.controllers.admin.gists
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gist controller for Kallithea
This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: May 9, 2013
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""
import time
import logging
import traceback
import formencode.htmlfill
from pylons import request, response, tmpl_context as c, url
from pylons.i18n.translation import _
from webob.exc import HTTPFound, HTTPNotFound, HTTPForbidden
from kallithea.model.forms import GistForm
from kallithea.model.gist import GistModel
from kallithea.model.meta import Session
from kallithea.model.db import Gist, User
from kallithea.lib import helpers as h
from kallithea.lib.base import BaseController, render
from kallithea.lib.auth import LoginRequired, NotAnonymous
from kallithea.lib.utils import jsonify
from kallithea.lib.utils2 import safe_int, safe_unicode, time_to_datetime
from kallithea.lib.helpers import Page
from sqlalchemy.sql.expression import or_
from kallithea.lib.vcs.exceptions import VCSError, NodeNotChangedError
log = logging.getLogger(__name__)
class GistsController(BaseController):
"""REST Controller styled on the Atom Publishing Protocol"""
def __load_defaults(self, extra_values=None):
c.lifetime_values = [
(str(-1), _('Forever')),
(str(5), _('5 minutes')),
(str(60), _('1 hour')),
(str(60 * 24), _('1 day')),
(str(60 * 24 * 30), _('1 month')),
]
if extra_values:
c.lifetime_values.append(extra_values)
c.lifetime_options = [(c.lifetime_values, _("Lifetime"))]
@LoginRequired()
def index(self):
not_default_user = not c.authuser.is_default_user
c.show_private = request.GET.get('private') and not_default_user
c.show_public = request.GET.get('public') and not_default_user
gists = Gist().query() \
.filter(or_(Gist.gist_expires == -1, Gist.gist_expires >= time.time())) \
.order_by(Gist.created_on.desc())
# MY private
if c.show_private and not c.show_public:
gists = gists.filter(Gist.gist_type == Gist.GIST_PRIVATE) \
.filter(Gist.gist_owner == c.authuser.user_id)
# MY public
elif c.show_public and not c.show_private:
gists = gists.filter(Gist.gist_type == Gist.GIST_PUBLIC) \
.filter(Gist.gist_owner == c.authuser.user_id)
# MY public+private
elif c.show_private and c.show_public:
gists = gists.filter(or_(Gist.gist_type == Gist.GIST_PUBLIC,
Gist.gist_type == Gist.GIST_PRIVATE)) \
.filter(Gist.gist_owner == c.authuser.user_id)
# default show ALL public gists
if not c.show_public and not c.show_private:
gists = gists.filter(Gist.gist_type == Gist.GIST_PUBLIC)
c.gists = gists
p = safe_int(request.GET.get('page'), 1)
c.gists_pager = Page(c.gists, page=p, items_per_page=10)
return render('admin/gists/index.html')
@LoginRequired()
@NotAnonymous()
def create(self):
self.__load_defaults()
gist_form = GistForm([x[0] for x in c.lifetime_values])()
try:
form_result = gist_form.to_python(dict(request.POST))
#TODO: multiple files support, from the form
filename = form_result['filename'] or Gist.DEFAULT_FILENAME
nodes = {
filename: {
'content': form_result['content'],
'lexer': form_result['mimetype'] # None is autodetect
}
}
_public = form_result['public']
gist_type = Gist.GIST_PUBLIC if _public else Gist.GIST_PRIVATE
gist = GistModel().create(
description=form_result['description'],
owner=c.authuser.user_id,
gist_mapping=nodes,
gist_type=gist_type,
lifetime=form_result['lifetime']
)
Session().commit()
new_gist_id = gist.gist_access_id
except formencode.Invalid as errors:
defaults = errors.value
return formencode.htmlfill.render(
render('admin/gists/new.html'),
defaults=defaults,
errors=errors.error_dict or {},
prefix_error=False,
encoding="UTF-8",
force_defaults=False)
except Exception as e:
log.error(traceback.format_exc())
h.flash(_('Error occurred during gist creation'), category='error')
raise HTTPFound(location=url('new_gist'))
raise HTTPFound(location=url('gist', gist_id=new_gist_id))
@LoginRequired()
@NotAnonymous()
def new(self, format='html'):
self.__load_defaults()
return render('admin/gists/new.html')
@LoginRequired()
@NotAnonymous()
def delete(self, gist_id):
gist = GistModel().get_gist(gist_id)
owner = gist.gist_owner == c.authuser.user_id
if h.HasPermissionAny('hg.admin')() or owner:
GistModel().delete(gist)
Session().commit()
h.flash(_('Deleted gist %s') % gist.gist_access_id, category='success')
else:
raise HTTPForbidden()
raise HTTPFound(location=url('gists'))
@LoginRequired()
def show(self, gist_id, revision='tip', format='html', f_path=None):
c.gist = Gist.get_or_404(gist_id)
#check if this gist is not expired
if c.gist.gist_expires != -1:
if time.time() > c.gist.gist_expires:
log.error('Gist expired at %s',
time_to_datetime(c.gist.gist_expires))
raise HTTPNotFound()
try:
c.file_changeset, c.files = GistModel().get_gist_files(gist_id,
revision=revision)
except VCSError:
log.error(traceback.format_exc())
raise HTTPNotFound()
if format == 'raw':
content = '\n\n'.join([f.content for f in c.files if (f_path is None or safe_unicode(f.path) == f_path)])
response.content_type = 'text/plain'
return content
return render('admin/gists/show.html')
@LoginRequired()
@NotAnonymous()
def edit(self, gist_id, format='html'):
c.gist = Gist.get_or_404(gist_id)
#check if this gist is not expired
if c.gist.gist_expires != -1:
if time.time() > c.gist.gist_expires:
log.error('Gist expired at %s',
time_to_datetime(c.gist.gist_expires))
raise HTTPNotFound()
try:
c.file_changeset, c.files = GistModel().get_gist_files(gist_id)
except VCSError:
log.error(traceback.format_exc())
raise HTTPNotFound()
self.__load_defaults(extra_values=('0', _('Unmodified')))
rendered = render('admin/gists/edit.html')
if request.POST:
rpost = request.POST
nodes = {}
for org_filename, filename, mimetype, content in zip(
rpost.getall('org_files'),
rpost.getall('files'),
rpost.getall('mimetypes'),
rpost.getall('contents')):
nodes[org_filename] = {
'org_filename': org_filename,
'filename': filename,
'content': content,
'lexer': mimetype,
}
try:
GistModel().update(
gist=c.gist,
description=rpost['description'],
owner=c.gist.owner,
gist_mapping=nodes,
gist_type=c.gist.gist_type,
lifetime=rpost['lifetime']
)
Session().commit()
h.flash(_('Successfully updated gist content'), category='success')
except NodeNotChangedError:
# raised if nothing was changed in repo itself. We anyway then
# store only DB stuff for gist
Session().commit()
h.flash(_('Successfully updated gist data'), category='success')
except Exception:
log.error(traceback.format_exc())
h.flash(_('Error occurred during update of gist %s') % gist_id,
category='error')
raise HTTPFound(location=url('gist', gist_id=gist_id))
return rendered
@LoginRequired()
@NotAnonymous()
@jsonify
def check_revision(self, gist_id):
c.gist = Gist.get_or_404(gist_id)
last_rev = c.gist.scm_instance.get_changeset()
success = True
revision = request.POST.get('revision')
##TODO: maybe move this to model ?
if revision != last_rev.raw_id:
log.error('Last revision %s is different than submitted %s',
revision, last_rev)
# our gist has newer version than we
success = False
return {'success': success}
|