def test_c7_user_deletes_all_posts(self): data = dict(email='*****@*****.**', password='******') self.client.post(api_v1.url_for(Login), data=str(data), content_type='application/json') rv = self.client.delete(api_v1.url_for(SingleUserAllPosts, user_id=1)) self.assertEqual(204, rv.status_code)
def test_a9_logout(self): self.login('*****@*****.**', 'password.Pa55word') rv = self.client.post(api_v1.url_for(Logout)) self.assert200(rv) self.assertIn(b'logged out successfully', rv.data) rv = self.client.post(api_v1.url_for(Logout)) self.assert400(rv) self.assertIn(b'no user in session', rv.data)
def test_c6_user_deletes_single_post(self): data = dict(email='*****@*****.**', password='******') self.client.post(api_v1.url_for(Login), data=str(data), content_type='application/json') self.create_post('My post title Number 2', 'Something interesting for people to read.') rv = self.client.delete( api_v1.url_for(SingleUserSinglePost, user_id=1, post_id=1)) self.assertEqual(204, rv.status_code)
def test_c5_user_patches_post(self): data = dict(email='*****@*****.**', password='******') self.client.post(api_v1.url_for(Login), data=str(data), content_type='application/json') data = dict(title='My Second Post Title') rv = self.client.patch(api_v1.url_for(SingleUserSinglePost, user_id=1, post_id=1), data=str(data), content_type='application/json') # self.assert200(rv) self.assertIn(b'My Second Post Title', rv.data)
def test_c4_user_creates_post(self): # user logs in data = dict(email='*****@*****.**', password='******') self.client.post(api_v1.url_for(Login), data=str(data), content_type='application/json') rv = self.create_post('My post title.', 'Something interesting for people to read.') self.assertEqual(201, rv.status_code)
def register(self, email: str = None, password_tuple: tuple = (None, None), security_tuple: tuple = (None, None)): data = dict(email=email) data['password'], data['confirm_password'] = password_tuple data['security_question'], data['security_answer'] = security_tuple return self.client.post(api_v1.url_for(Register), data=str(data), content_type='application/json')
def test_no_data(self): rv = self.client.post(api_v1.url_for(Register), content_type='application/json') self.assertEqual(400, rv.status_code) self.assertIn(b'no data was found in the request', rv.data)
def test_not_json(self): rv = self.client.post(api_v1.url_for(Register)) self.assertEqual(415, rv.status_code)
def test_b2_view_single_post_fail(self): rv = self.client.get(api_v1.url_for(SinglePost, post_id=1)) self.assert400(rv) self.assertIn(b'Post not found!', rv.data)
def create_post(self, title, body): data = dict(title=title, body=body) return self.client.post(api_v1.url_for(SingleUserAllPosts, user_id=1), data=str(data), content_type='application/json')
def test_c3_view_user_posts_empty(self): rv = self.client.get(api_v1.url_for(SingleUserAllPosts, user_id=1)) self.assert200(rv) self.assertIn(b'"posts": []', rv.data)
def test_c2_view_single_user(self): rv = self.client.get(api_v1.url_for(SingleUser, user_id=1)) self.assert200(rv) self.assertIn(b'{"user":'******'"email":', rv.data) self.assertIn(b'"url":', rv.data)
def test_c1_view_all_users(self): rv = self.client.get(api_v1.url_for(AllUsers)) self.assert200(rv) self.assertIn(b'{"users": [', rv.data)
def test_d3_view_single_user_fail(self): rv = self.client.get(api_v1.url_for(SingleUser, user_id=1)) self.assert400(rv) self.assertIn(b'User not found!', rv.data)
def test_c4_user_views_post(self): rv = self.client.get(api_v1.url_for(SingleUserSinglePost, user_id=1, post_id=1), follow_redirects=True) self.assertIn(b'My post title.', rv.data)
def login(self, email: str = None, password: str = None): data = dict(email=email, password=password) return self.client.post(api_v1.url_for(Login), data=str(data), content_type='application/json')
def test_b1_view_all_posts(self): rv = self.client.get(api_v1.url_for(AllPosts)) self.assert200(rv) self.assertIn(b'[]', rv.data)