def setUp(self): self.user_Melissa = User(username = '******', password = '******', email = '*****@*****.**') self.new_blogpost = Blogpost(id=12345, title='Blogpost itself', content="this is a nice blog post", category_id='funny', comments="what a nice comment", user_id = self.user_Melissa)
def setUp(self): """ Runs before each test """ db.create_all() self.blogpost = Blogpost(author="Victor Maina", user_id=1, title="5 Ways to Get Rid of Tough Stains", body="Lorem ipsum...", post_image_path="/path/to/image/", upvotes=3) self.user = User(user_name="johndoe", first_name="John", last_name="Doe") db.session.add_all([self.blogpost, self.user]) db.session.commit()
def find_by_id(self, id) -> Response: blogpost: Blogpost = Blogpost.find_by_id(id) if blogpost is None: raise NotFound(f"No blogpost found with id={id}") resp_dict = blogpost.to_dict() resp_dict.update(user=blogpost.user.to_dict()) return self.make_json_response([resp_dict])
def new(): if current_user.role != "Subscriber": if request.method == 'POST': title = request.form.get("title") content = request.form.get("content") imagelink = request.form.get("coverimage") subtitle = content[:200] #post access access = request.form.get("access") if access == "T": access = True elif access == "F": access = False #save to database new_post = Blogpost(title=title, subtitle=subtitle, author=current_user.name, content=content, access=access, imagelink=imagelink) db.session.add(new_post) db.session.commit() flash("New Post added") return redirect(url_for("main.home")) else: return render_template('add.html') else: abort(404)
def create_blogpost(self, id) -> Response: data: Dict = self.get_required_data_from_request("headline", "body") post: Blogpost = Blogpost(data["headline"], data["body"], id) post.save() return self.make_json_response( dict(message="Blogpost successfully created") )
def delete_blogpost(self, id) -> Response: post: Blogpost = Blogpost.find_by_id(id) if post is None: raise NotFound(f"No blogpost found with id={id}") post.delete() return self.make_json_response( dict(message="Blogpost successfully deleted") )
def find_all(self) -> Response: blogposts: List[Blogpost] = Blogpost.find_all() if blogposts == []: raise NotFound("No blogposts exist") json_list: List[Dict] = [dict( blogpost.to_dict(), user=blogpost.user.to_dict() ) for blogpost in blogposts] return self.make_json_response(json_list)
def test_delete_blogpost_correct() -> None: with CustomTestClient() as c: Blogpost("Headline", "Body", 1).save() token = auth.create_jwt_token(1, "JohnDoe", ["ADMIN"]) c.set_cookie('localhost:5000', 'Authorization', token) res = c.delete('/api/v1/blogposts/1') data = res.get_json() assert data["message"] == "Blogpost successfully deleted" assert res.status_code == 200
def test_post_blogposts_correct() -> None: with CustomTestClient() as c: user = User("jane", "asd123") user.save() user_id = user.id blogpost = dict( headline="This is my headline", body="This is my body") token = auth.create_jwt_token(user_id, user.username, ["ADMIN"]) c.set_cookie('localhost:5000', 'Authorization', token) res = c.post( '/api/v1/blogposts', data=json.dumps(blogpost), content_type="application/json" ) data = res.get_json() assert data["message"] == "Blogpost successfully created" assert res.status_code == 200 blogpost_2 = dict( headline="This is my other headline", body="This is my second body text different from the first!") res_2 = c.post( '/api/v1/blogposts', data=json.dumps(blogpost_2), content_type="application/json" ) data_2 = res_2.get_json() assert data_2["message"] == "Blogpost successfully created" assert res_2.status_code == 200 user = User.find_by_id(user_id) assert user.blogposts is not None assert user.blogposts[0].headline == "This is my headline" assert user.blogposts[0].body == "This is my body" assert user.blogposts[1].headline == "This is my other headline" assert user.blogposts[1].body == \ "This is my second body text different from the first!" blogpost_1 = Blogpost.find_by_id(user.blogposts[0].id) blogpost_2 = Blogpost.find_by_id(user.blogposts[1].id) assert blogpost_1.user is not None assert blogpost_2.user is not None assert blogpost_1.author_id == user_id assert blogpost_2.author_id == user_id
def setUp(self): """ Runs before each test """ db.create_all() self.blogpost = Blogpost(title="How to Start a Blog") self.comment = Comment(full_name="Jane Doe", email = "*****@*****.**", comment="Can't wait to start my own!", blogpost_id = 1) db.session.add_all([self.blogpost, self.comment]) db.session.commit()
def test_get_all_blogpost_correct() -> None: with CustomTestClient() as c: User("jane1", "asd123").save() User("jane2", "asd123").save() User("jane3", "asd123").save() Blogpost("This is my headline", "This is a body text", 1).save() Blogpost("This is my headline 2", "This is a body text 2", 2).save() Blogpost("This is my headline 3", "This is a body text 3", 3).save() res = c.get('/api/v1/blogposts') data = res.get_json() assert data[0]["headline"] == "This is my headline" assert data[0]["body"] == "This is a body text" assert data[0]["user"]["username"] == "jane1" assert data[1]["headline"] == "This is my headline 2" assert data[1]["body"] == "This is a body text 2" assert data[1]["user"]["username"] == "jane2" assert data[2]["headline"] == "This is my headline 3" assert data[2]["body"] == "This is a body text 3" assert data[2]["user"]["username"] == "jane3" assert res.status_code == 200
def setUp(self): ''' Set up method that will run before every Test ''' self.new_post = Blogpost(id=1, title='technology', subtitle='smart phones', author='canssy', date_posted='05/4/2019', content='work made easier', user_id=9)
def setUp(self): ''' Set up method that will run before every Test ''' self.new_post = Blogpost(id=56, title='My post', subtitle='goofy', author='manka', date_posted='1/4/2018', content='I can post all day long', user_id=15)
def addpost(): title = request.form['title'] subtitle = request.form['subtitle'] author = request.form['author'] content = request.form['content'] post = Blogpost(title=title, subtitle=subtitle, author=author, content=content, date_posted=datetime.now()) db.session.add(post) db.session.commit() return redirect(url_for('index'))
def test_blogposts(self): """ Test case to check if blogposts property is created """ db.session.add(self.user) db.session.commit() blogpost = Blogpost(author="Jane Doe", user_id=1) db.session.add(blogpost) db.session.commit() self.assertTrue(len(self.user.blogposts) > 0)
class TestBlogpost(unittest.TestCase): def setUp(self): self.user_Melissa = User(username = '******', password = '******', email = '*****@*****.**') self.new_blogpost = Blogpost(id=12345, title='Blogpost itself', content="this is a nice blog post", category_id='funny', comments="what a nice comment", user_id = self.user_Melissa) def tearDown(self): Blogpost.query.delete() User.query.delete() def test_instance(self): self.assertTrue(isinstance(self.new_blogpost,Blogpost)) def test_check_instance_variables(self): self.assertEquals(self.new_blogpost.id,12345) self.assertEquals(self.new_blogpost.title,'Blogpost itself') self.assertEquals(self.new_blogpost.content,"this is a nice blog post") self.assertEquals(self.new_blogpost.category_id,'funny') self.assertEquals(self.new_blogpost.comments, 'what a nice comment') self.assertEquals(self.new_blogpost.user,self.user_Melissa) def test_save_blogpost(self): self.new_blogpost.save_blogpost() self.assertTrue(len(Blogpost.query.all())>0) def test_get_blogpost_by_id(self): self.new_blogpost.save_blogpost() got_blogposts = Blogpost.get_blogposts(12345) self.assertTrue(len(got_blogposts) == 1)
def addpost(): title = request.form['title'] author = current_user.get_id() #author = request.form['author'] content = request.form['content'] #Adding the information to the database: post = Blogpost(title=title, content=content, date_posted=datetime.now()) db.session.add(post) db.session.commit() return redirect(url_for('index'))
def add(): header = { "title": "Admin page", "subtitle": "Manage your blog!", "image_path": "manage_bg.jpg", "needed": True, "dashboard_flash": True } editor = Editors.query.filter_by(id=current_user.id).one() form = PostForm() form.author.data = editor.name #app.logger.info(editor.name) #if post request to edit form and is validated if form.validate_on_submit(): #get data from modified form title = form.title.data subtitle = form.subtitle.data author = form.author.data tags = form.tags.data content = form.content.data tags_objects = [] separated_tags = tags.split("-") for separate_tag in separated_tags: tag_to_add = Tags.query.filter_by(name=separate_tag).one() tags_objects.append(tag_to_add) #write to DB post = Blogpost(title=title, subtitle=subtitle, author=author, content=content, editor=editor, tags=tags_objects) db.session.add(post) db.session.commit() #flash message flash('post published', 'success') #redirect to index return redirect(url_for('manage')) return render_template('add.html', form=form, header=header)
def test_get_blogpost_correct() -> None: with CustomTestClient() as c: user = User("jane", "asd123") user.save() blogpost: Blogpost = \ Blogpost("This is my headline", "This is a body text", 1) blogpost.save() res = c.get('/api/v1/blogposts/1') data = res.get_json() assert data[0]["headline"] == "This is my headline" assert data[0]["body"] == "This is a body text" assert data[0]["user"]["username"] == user.username assert data[0]["user"]["id"] == user.id assert res.status_code == 200
def new_post(): if current_user.role != 3: abort(404) form = NewPostForm() if form.validate_on_submit(): file = form.main_image.data print(file) if file == None: filename = "post_default.png" print('yeah') else: filename = secure_filename(file.filename) file.save(os.path.join(app.config['POST_IMAGE_SAVE_PATH'], filename)) post = Blogpost( title = form.title.data, content = form.main_text.data, is_in_slider = form.is_in_slider.data, main_image_path = filename ) print(post.main_image_path) db.session.add(post) db.session.commit() return redirect(url_for('posts')) return render_template('new_post.html', form = form)
def create_blogpost(title: str, body: str, uid: int) -> None: blogpost: Blogpost = Blogpost(title, body, uid) blogpost.save()
class TestBlogpost(unittest.TestCase): """ Class for testing Blogpost properties and methods. Inherits from unittest.TestCase """ def setUp(self): """ Runs before each test """ db.create_all() self.blogpost = Blogpost(author="Victor Maina", user_id=1, title="5 Ways to Get Rid of Tough Stains", body="Lorem ipsum...", post_image_path="/path/to/image/", upvotes=3) self.user = User(user_name="johndoe", first_name="John", last_name="Doe") db.session.add_all([self.blogpost, self.user]) db.session.commit() def tearDown(self): """ Runs after each test """ db.session.commit() db.drop_all() def test_blogpost_instance(self): """ Class to check if instance of Blogpost is created and committed to database """ user = User.query.first() blogpost = Blogpost.query.first() self.assertEqual(blogpost.author, "Victor Maina") self.assertEqual(blogpost.user_id, 1) self.assertEqual(blogpost.title, "5 Ways to Get Rid of Tough Stains") self.assertEqual(blogpost.body, "Lorem ipsum...") self.assertEqual(blogpost.post_image_path, "/path/to/image/") self.assertEqual(blogpost.upvotes, 3) def test_comments(self): """ Test to check if comments property is being populated by Comment instances """ comment = Comment(full_name="Jane Doe", comment="Very informative") db.session.add(comment) db.session.commit() self.blogpost.comments.append(comment) self.assertEqual(Blogpost.query.first().comments[0], comment) def test_self_author(self): """ Test case to check is self_author method sets author attribute to user's first and last names """ blogpost = Blogpost.query.first() user = User.query.first() author_name = f"{user.first_name} {user.last_name}" blogpost.self_author() self.assertEqual(blogpost.author, author_name) def test_delete_post(self): """ Test case to check if delete_post method removes blogpost from database """ self.blogpost.delete_post() print(Blogpost.query.all()) self.assertFalse(Blogpost.query.first())
def test_get_blogpost_by_id(self): self.new_blogpost.save_blogpost() got_blogposts = Blogpost.get_blogposts(12345) self.assertTrue(len(got_blogposts) == 1)