Exemple #1
0
 def test_delete_category_incorrect_id(self):
     add_category('category-one')
     add_category('category-two')
     token = get_token(self.client)
     response = self.client.delete(
         '/categories/3', headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'fail')
     self.assertIn('Invalid payload.', data['message'])
     self.assertEqual(response.status_code, 404)
Exemple #2
0
 def test_delete_category(self):
     add_category('category-one')
     add_category('category-two')
     token = get_token(self.client)
     response = self.client.delete(
         '/categories/1', headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'success')
     self.assertIn('category-one is already deleted.', data['message'])
     self.assertEqual(response.status_code, 202)
Exemple #3
0
 def test_add_category_no_name(self):
     token = get_token(self.client)
     response = self.client.post(
         '/categories',
         json={},
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'fail')
     self.assertEqual(data['message'], 'Invalid payload.')
     self.assertEqual(response.status_code, 400)
Exemple #4
0
 def test_add_category_with_user(self):
     token = get_token(self.client)
     response = self.client.post(
         '/categories',
         json={'name': 'category-one'},
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['message'], 'category: category-one was added.')
     self.assertEqual(data['status'], 'success')
     self.assertEqual(response.status_code, 201)
Exemple #5
0
 def test_delete_single_article_incorrect_id(self):
     cate = add_category('default')
     article = add_article('default title', 'default body', cate)
     token = get_token(self.client, admin=True)
     response = self.client.delete(
         f'/articles/{article.id + 1}',
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'fail')
     self.assertEqual(data['message'], 'Invalid path params.')
     self.assertEqual(response.status_code, 404)
Exemple #6
0
 def test_delete_single_article(self):
     cate = add_category('default')
     article = add_article('default title', 'default body', cate)
     token = get_token(self.client, admin=True)
     response = self.client.delete(
         f'/articles/{article.id}',
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'success')
     self.assertEqual(data['message'],
                      f'{article.id}: {article.title} is already deleted.')
     self.assertEqual(response.status_code, 202)
Exemple #7
0
 def test_add_article_no_category(self):
     add_category('default')
     token = get_token(self.client)
     response = self.client.post(
         '/articles',
         json={
             'title': 'test title',
             'body': 'test body'
         },
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'success')
     self.assertEqual(data['message'], 'article: test title was added.')
     self.assertEqual(response.status_code, 201)
Exemple #8
0
 def test_add_article_no_body(self):
     add_category('default')
     token = get_token(self.client)
     response = self.client.post(
         '/articles',
         json={
             'title': 'test title',
             'category': 1
         },
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'fail')
     self.assertEqual(data['message'], 'Invalid payload.')
     self.assertEqual(response.status_code, 400)
Exemple #9
0
 def test_udpate_single_article_no_title(self):
     cate1 = add_category('default')
     cate2 = add_category('test')
     article = add_article('title', 'body', cate1)
     token = get_token(self.client)
     response = self.client.put(
         '/articles',
         json={
             'aid': f'{article.id}',
             'body': 'new body',
             'category': f'{cate2.id}'
         },
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'fail')
     self.assertEqual(data['message'], 'Invalid payload.')
     self.assertEqual(response.status_code, 400)
Exemple #10
0
 def test_update_single_article_no_category(self):
     add_category('default')
     cate2 = add_category('test')
     article = add_article('title', 'body', cate2)
     token = get_token(self.client)
     response = self.client.put(
         '/articles',
         json={
             'aid': f'{article.id}',
             'title': 'new title',
             'body': 'new body',
         },
         headers={'Authorization': f'Bearer {token}'})
     data = response.get_json()
     self.assertEqual(data['status'], 'success')
     self.assertEqual(data['message'],
                      f'{article.id}: article is already updated.')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(Article.query.get(article.id).category.id, 1)