def test_post_route_redirects_to_user_list(self): with app.test_client() as client: res = client.post('/users/new', data={'first_name': 'John', 'last_name': 'Doe', 'image_url': 'hello'}) self.assertEqual(res.status_code, 302) self.assertEqual(res.location, 'http://localhost/users')
def test_route_view_correct_post(self): with app.test_client() as client: res = client.get(f'/posts/{self.p1_id}') html = res.get_data(as_text=True) p1 = Post.query.get(self.p1_id) self.assertIn(p1.title, html) self.assertIn(p1.content, html)
def setUpClass(cls): with app.test_request_context(): cls.client = app.test_client() cls.user = User() cls.user.username = '******' cls.user.password = '******' cls.user.save()
def setUp(self): app = self.create_app() self.app = app.test_client() with app.app_context(): self.init_db() self.req = ({'username': '******', 'password': '******'}) self.bucketlist_url = '/api/v1/bucketlists/'
def get(self, *args, **kwargs): with app.test_client() as c: if 'user' in kwargs: user = kwargs.pop('user') with c.session_transaction() as session: session['user'] = user.to_dict() return c.get(*args, **kwargs)
def test_route_deletes_user(self): with app.test_client() as client: res = client.get( f'/users/{self.benny_id}/delete', follow_redirects=True) self.assertEqual(res.status_code, 200) benny = User.query.get(self.benny_id) self.assertIsNone(benny)
def setUp(self): app = self.create_app() self.app = app.test_client() with app.app_context(): self.init_db() self.reg_url = '/api/v1/auth/register' self.login_url = '/api/v1/auth/login' self.bucketlist_url = '/api/v1/bucketlists'
def test_post_route_redirects_to_user_details(self): with app.test_client() as client: res = client.post(f'/users/{self.u_id}/posts/new', data={'title': 'New', 'content': 'New post content!'}) self.assertEqual(res.status_code, 302) self.assertEqual( res.location, f'http://localhost/users/{self.u_id}')
def test_route_deletes_user(self): with app.test_client() as client: res = client.get(f'/tags/{self.sam_id}/delete', follow_redirects=True) self.assertEqual(res.status_code, 200) sam = Tag.query.get(self.sam_id) self.assertIsNone(sam)
def test_route_responds_with_corrent_page(self): with app.test_client() as client: res = client.get(f'/users/{self.u_id}/posts/new') self.assertEqual(res.status_code, 200) html = res.get_data(as_text=True) user = User.query.get(self.u_id) self.assertIn(f'{user.first_name} is making a post', html)
def setUp(self): app = self.create_app() self.app = app.test_client() with app.app_context(): self.init_db() self.user = ({'username': '******', 'password': '******'}) self.bucketlist = ({'name': 'Before the end of the Year'}) self.item_url = '/api/v1/bucketlists/1/items/'
def test_post_route_redirects_to_tag_list(self): with app.test_client() as client: res = client.get(f'/tags/{self.fun_id}/delete') self.assertEqual(res.status_code, 302) self.assertEqual(res.location, 'http://localhost/tags') res = client.get(res.location) html = res.get_data(as_text=True) self.assertNotIn('fun', html)
def test_post_route_rejects_no_name(self): with app.test_client() as client: res = client.post('/tags/new', data={'name': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) tag = Tag.query.filter_by(name="").one_or_none() #tag was never made self.assertIsNone(tag)
def test_post_route_redirects_to_all_tags_page(self): with app.test_client() as client: res = client.post(f'/tags/{self.sam_id}/edit', data={'name': 'John'}) self.assertEqual(res.status_code, 302) self.assertEqual(res.location, 'http://localhost/tags') res = client.get(res.location) html = res.get_data(as_text=True) self.assertIn('John', html)
def test_route_edit_form_contains_post_info(self): with app.test_client() as client: res = client.get(f'/posts/{self.p1_id}/edit') html = res.get_data(as_text=True) p1 = Post.query.get(self.p1_id) self.assertIn(f'value="{p1.title}"', html) self.assertIn(f'value="{p1.content}"', html) self.assertIn(p1.creator.first_name, html)
def test_post_route_redirects_to_user_list(self): with app.test_client() as client: res = client.get(f'/users/{self.sam_id}/delete') self.assertEqual(res.status_code, 302) self.assertEqual(res.location, 'http://localhost/users') res = client.get(res.location) html = res.get_data(as_text=True) self.assertNotIn('Sam Crewe-Sullam', html)
def test_post_route_edits_tag_info(self): with app.test_client() as client: fun_tag = Tag.query.get(self.fun_id) self.assertEqual(fun_tag.name, 'fun') res = client.post(f'/tags/{self.fun_id}/edit', data={'name': 'Benny'}, follow_redirects=True) self.assertEqual(res.status_code, 200) fun_tag = Tag.query.get(self.fun_id) self.assertEqual(fun_tag.name, 'Benny')
def test_post_route_rejects_no_first_name(self): with app.test_client() as client: res = client.post('/users/new', data={'first_name': '', 'last_name': 'Doe', 'image_url': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) user = User.query.filter_by(last_name="doe").one_or_none() # user was never made self.assertIsNone(user)
def test_post_route_redirects_to_user_description_page(self): with app.test_client() as client: res = client.post(f'/users/{self.sam_id}/edit', data={'first_name': 'John', 'last_name': 'Doe', 'image_url': 'hello'}) self.assertEqual(res.status_code, 302) self.assertEqual( res.location, f'http://localhost/users/{self.sam_id}') res = client.get(res.location) html = res.get_data(as_text=True) self.assertIn('John Doe', html)
def test_post_route_edits_user_info(self): with app.test_client() as client: benny = User.query.get(self.benny_id) self.assertEqual(benny.last_name, 'G') res = client.post(f'/users/{self.benny_id}/edit', data={'first_name': 'Benny', 'last_name': 'Gee', 'image_url': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) benny = User.query.get(self.benny_id) self.assertEqual(benny.last_name, 'Gee')
def test_post_route_gives_an_image_if_one_was_not_provided(self): with app.test_client() as client: res = client.post('/users/new', data={'first_name': 'John', 'last_name': 'Doe', 'image_url': ''}, follow_redirects=True) user = User.query.filter_by( first_name='John', last_name='Doe').one_or_none() self.assertNotEqual(user.image_url, '') self.assertEqual(user.image_url, DEFAULT_IMAGE_URL)
def test_post_route_rejects_no_name(self): with app.test_client() as client: res = client.post(f'/tags/{self.sam_id}/edit', data={'name': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) sam = Tag.query.get(self.sam_id) self.assertNotEqual(sam.name, '') # No change was made self.assertEqual(sam.name, 'Sam')
def test_post_route_edits_post_info(self): with app.test_client() as client: post = Post.query.get(self.p1_id) self.assertEqual(post.title, 'SuperCool') res = client.post(f'/posts/{self.p1_id}/edit', data={'title': 'A Different Title', 'content': 'Different Content'}, follow_redirects=True) self.assertEqual(res.status_code, 200) post = Post.query.get(self.p1_id) self.assertEqual(post.title, 'A Different Title') self.assertEqual(post.content, 'Different Content')
def test_post_route_rejects_no_first_name(self): with app.test_client() as client: res = client.post(f'/users/{self.sam_id}/edit', data={'first_name': '', 'last_name': 'Doe', 'image_url': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) sam = User.query.get(self.sam_id) self.assertNotEqual(sam.first_name, '') # No change was made self.assertEqual(sam.first_name, 'Sam')
def test_post_route_rejects_repeats(self): with app.test_client() as client: res = client.post('/tags/new', data={'name': 'Sam'}, follow_redirects=True) self.assertEqual(res.status_code, 200) res = client.post('/tags/new', data={'name': 'Sam'}, follow_redirects=True) self.assertEqual(res.status_code, 200) html = res.get_data(as_text=True) self.assertIn('I think We already got that Tag!', html)
def test_post_route_adds_tag(self): with app.test_client() as client: tags = Tag.query.filter_by(name='Unique Tag').all() #user doesn't exist yet self.assertEqual(tags, []) res = client.post('/tags/new', data={'name': 'Unique Tag'}, follow_redirects=True) self.assertEqual(res.status_code, 200) self.assertIn('Unique Tag', res.get_data(as_text=True)) tag = Tag.query.filter_by(name='Unique Tag').one_or_none() self.assertEqual(tag.name, 'Unique Tag')
def test_post_route_adds_post(self): post = Post.query.filter_by( title='New', content='New post content!').one_or_none() self.assertIsNone(post) with app.test_client() as client: res = client.post(f'/users/{self.u_id}/posts/new', data={'title': 'New', 'content': 'New post content!'}, follow_redirects=True) self.assertEqual(res.status_code, 200) post = Post.query.filter_by( title='New', content='New post content!').first() self.assertEqual(post.user_id, self.u_id)
def test_post_route_gives_an_image_if_one_was_not_provided(self): with app.test_client() as client: toni = User.query.get(self.toni_id) self.assertEqual(toni.image_url, 'https://mysite.com/myimg.jpg') res = client.post(f'/users/{self.toni_id}/edit', data={'first_name': 'Toni', 'last_name': '', 'image_url': ''}, follow_redirects=True) self.assertEqual(res.status_code, 200) toni = User.query.get(self.toni_id) self.assertNotEqual(toni.image_url, 'https://mysite.com/myimg.jpg') self.assertNotEqual(toni.image_url, '') self.assertEqual(toni.image_url, DEFAULT_IMAGE_URL)
def test_post_route_adds_user(self): with app.test_client() as client: users = User.query.filter_by( first_name='John', last_name='Doe').all() # user doesn't exist yet self.assertEqual(users, []) res = client.post('/users/new', data={'first_name': 'John', 'last_name': 'Doe', 'image_url': 'hello'}, follow_redirects=True) self.assertEqual(res.status_code, 200) self.assertIn('John Doe', res.get_data(as_text=True)) user = User.query.filter_by( first_name='John', last_name='Doe').one_or_none() self.assertEqual(user.image_url, 'hello')
def setUpClass(cls): cls.client = app.test_client()
def client(): with app.test_client() as client: yield client
def client_form(): with app.test_client() as client: yield client, get_header(client, content_type='multipart/form-data') db.session.commit()
def setUp(self): self.client = app.test_client()