def article(seeded_database, admin_user, category): article = Article(title="Test article", content="Test content", user=admin_user, category=category) article.commit() return article
def make_article(self, data, **kwargs): # check if category exists category = Category.find_by_id(data['category_id']) if not category: raise ValidationError('Category with given uuid does not exist') del data['category_id'] article = Article(**data) article.category = category return article
def get(self): # Get optional page number try: filter_params = { 'per_page': request.args.get('per_page', default=10, type=int), 'page': request.args.get('page', default=1, type=int), 'from': request.args.get('from', default=None, type=toDate), 'to': request.args.get('to', default=None, type=toDate), 'sort': request.args.get('sort', default=OrderType.DESCENDING, type=OrderType), 'query': request.args.get('query', default='', type=str) } except ValueError: error_schema = ErrorResponseSchema() error_response = error_schema.dump( ErrorResponse(details={'page': 'Page should be number'}, error='validation errors')) log.info(f'Invalid page query argument: {error_response}') return error_response, 400 # Find all articles on that page articles = Article.find_all_articles(filter_params) # Map articles to schema return self.paginated_articles_schema.dump(articles), 200
def put(self, uuid): # Map input data try: update_info = self.update_article_schema.load(api.payload) except ValidationError as err: error_response = self.error_schema.dump( ErrorResponse(details=err.messages, error='validation errors')) log.info( f'Validation errors during article update: {error_response}') return error_response, 400 # Try to find article article = Article.find_by_uuid(uuid) if not article: error_response = self.error_schema.dump( ErrorResponse(details={ 'uuid': f'Article with {uuid} uuid does not exist' }, error='Article not found')) log.info(f'Article not found: {error_response}') return error_response, 404 # Update article for key, value in update_info.items(): setattr(article, key, value) article.commit() return self.article_schema.dump(article), 200
def get(self, uuid): try: uuid = UUID(uuid) except ValueError: return self.incorrect_uuid(uuid), 404 article = Article.find_by_uuid(uuid) if not article: error_response = self.error_schema.dump( ErrorResponse(details={ 'uuid': f'Article with {uuid} uuid does not exist' }, error='Article not found')) log.info(f'Article not found: {error_response}') return error_response, 404 return self.article_schema.dump(article), 200
def test_admin_creates_article(app, seeded_database, admin_access_token_header, category, admin_user): # Given new_article_info = { 'title': 'Test Title', 'content': 'Test Content', 'category_id': str(category.id) } # When response = app.test_client().post( '/api/article', data=dumps(new_article_info), headers=admin_access_token_header, content_type='application/json' ) response_body = loads(response.data) created_article = Article.find_by_uuid(response_body['uuid']) # Then assert response.status_code == 201 assert created_article.uuid == UUID(response_body['uuid']) assert UUID(response_body['category']['uuid']) == category.uuid assert UUID(response_body['user']['uuid']) == admin_user.uuid
def make_article(self, data, **kwargs): return Article(**data)