From 804d6fc6b9268577dbfd85f169f1a3f58e5cd6bc Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Fri, 24 Apr 2015 19:05:14 +0200 Subject: adding base unittests --- pyaggr3g470r/controllers/abstract.py | 17 ++++++++++------- pyaggr3g470r/models/__init__.py | 3 ++- pyaggr3g470r/models/article.py | 10 +++++----- 3 files changed, 17 insertions(+), 13 deletions(-) (limited to 'pyaggr3g470r') diff --git a/pyaggr3g470r/controllers/abstract.py b/pyaggr3g470r/controllers/abstract.py index 3ea4fbff..f67f932c 100644 --- a/pyaggr3g470r/controllers/abstract.py +++ b/pyaggr3g470r/controllers/abstract.py @@ -19,9 +19,12 @@ class AbstractController(object): allowing for a kind of "super user" mode. """ self.user_id = user_id - if self.user_id is not None \ - and self.user_id != g.user.id and not g.user.is_admin(): - self.user_id = g.user.id + try: + if self.user_id is not None \ + and self.user_id != g.user.id and not g.user.is_admin(): + self.user_id = g.user.id + except RuntimeError: # passing on out of context errors + pass def _to_filters(self, **filters): """ @@ -67,13 +70,13 @@ class AbstractController(object): def get(self, **filters): """Will return one single objects corresponding to filters""" obj = self._get(**filters).first() - if not obj: - raise NotFound({'message': 'No %r (%r)' - % (self._db_cls.__class__.__name__, filters)}) - if not self._has_right_on(obj): + if obj and not self._has_right_on(obj): raise Forbidden({'message': 'No authorized to access %r (%r)' % (self._db_cls.__class__.__name__, filters)}) + if not obj: + raise NotFound({'message': 'No %r (%r)' + % (self._db_cls.__class__.__name__, filters)}) return obj def create(self, **attrs): diff --git a/pyaggr3g470r/models/__init__.py b/pyaggr3g470r/models/__init__.py index 42903f4e..ba52b0de 100644 --- a/pyaggr3g470r/models/__init__.py +++ b/pyaggr3g470r/models/__init__.py @@ -98,4 +98,5 @@ def db_create(db): user1.roles.extend([role_admin, role_user]) db.session.add(user1) - db.session.commit() \ No newline at end of file + db.session.commit() + return role_admin, role_user diff --git a/pyaggr3g470r/models/article.py b/pyaggr3g470r/models/article.py index 58cd0384..3151071a 100644 --- a/pyaggr3g470r/models/article.py +++ b/pyaggr3g470r/models/article.py @@ -66,14 +66,14 @@ class Article(db.Model): .order_by(asc("Article.date")).first() def __repr__(self): - return json.dumps({ - "title": self.title, - "link": self.link, - "content": self.content - }) + return json.dumps({"title": self.title, + "link": self.link, + "content": self.content + }) def dump(self): return {"id": self.id, + "user_id": self.user_id, "title": self.title, "link": self.link, "content": self.content, -- cgit