Xác thực endpoint bằng Postman
Quy trình kiểm thử thủ công bắt đầu bằng việc sử dụng Postman để gửi các yêu cầu HTTP tới server. Dưới đây là các kịch bản kiểm tra cơ bản cho hai tài nguyên chính là người dùng (users) và nhóm (groups).
Kiểm tra module Users
- Lấy danh sách: Gửi yêu cầu GET để thu thập toàn bộ dữ liệu người dùng hiện có.
- Thêm mới: Sử dụng phương thức POST với payload chứa thông tin cá nhân để tạo bản ghi mới.
- Cập nhật: Gửi yêu cầu PATCH hoặc PUT để chỉnh sửa thông tin của một user cụ thể.
- Xóa dữ liệu: Thực hiện lệnh DELETE trên ID của user cần loại bỏ.
- Kiểm tra bảo mật: Thử truy cập mà không kèm theo thông tin xác thực để đảm bảo API trả về lỗi 401 hoặc 403.
Kiểm tra module Groups
- Truy vấn dữ liệu: Lấy danh sách các nhóm đang tồn tại trong hệ thống.
- Chỉnh sửa nhóm: Cập nhật tên hoặc thuộc tính của một nhóm cụ thể.
- Xóa nhóm: Remove một nhóm khỏi cơ sở dữ liệu qua giao diện API.
Tự động hóa kiểm thử với Requests và Unittest
Để nâng cao hiệu quả, chúng ta có thể xây dựng các script tự động hóa sử dụng thư viện requests kết hợp cùng framework unittest của Python. Tạo file test_api_automation.py trong thư mục dự án với nội dung sau:
import requests
import unittest
class ApiUserVerification(unittest.TestCase):
def setUp(self):
self.api_uri = 'http://localhost:8000/api/users'
self.http_auth = ('admin_user', 'secure_pass_123')
def test_fetch_user_info(self):
response = requests.get(self.api_uri + '/1/', auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['username'], 'admin_user')
self.assertEqual(response_data['email'], 'admin@example.com')
def test_create_new_user(self):
payload = {
'username': 'new_dev_01',
'email': 'dev01@example.com',
'groups': 'http://localhost:8000/api/groups/2/'
}
response = requests.post(self.api_uri + '/', data=payload, auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['username'], 'new_dev_01')
def test_remove_user(self):
response = requests.delete(self.api_uri + '/11/', auth=self.http_auth)
self.assertEqual(response.status_code, 204)
def test_modify_user_info(self):
payload = {'email': 'updated_email@example.com'}
response = requests.patch(self.api_uri + '/2/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['email'], 'updated_email@example.com')
def test_unauthorized_access(self):
response = requests.get(self.api_uri)
response_data = response.json()
self.assertEqual(response_data['detail'], 'Authentication credentials were not provided.')
class ApiGroupVerification(unittest.TestCase):
def setUp(self):
self.api_uri = 'http://localhost:8000/api/groups'
self.http_auth = ('admin_user', 'secure_pass_123')
def test_fetch_group_detail(self):
response = requests.get(self.api_uri + '/7/', auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['name'], 'Engineering')
def test_create_group(self):
payload = {'name': 'Quality Assurance'}
response = requests.post(self.api_uri + '/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['name'], 'Quality Assurance')
def test_update_group(self):
payload = {'name': 'Management'}
response = requests.patch(self.api_uri + '/6/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['name'], 'Management')
def test_delete_group(self):
response = requests.delete(self.api_uri + '/6/', auth=self.http_auth)
self.assertEqual(response.status_code, 204)
if __name__ == '__main__':
unittest.main()
Sử dụng module kiểm thử tích hợp của Django
Django cung cấp sẵn framework kiểm thử mạnh mẽ. Chúng ta có thể viết các test case kế thừa từ django.test.TestCase để quản lý vòng đời dữ liệu tốt hơn. Tuy nhiên, trong kịch bản này, chúng ta vẫn sử dụng requests để gọi HTTP như một client bên ngoài ngay trong test suite.
Tập tin tests.py trong ứng dụng api sẽ chứa các logic sau:
from django.test import TestCase
import requests
class DjangoUserIntegrationTest(TestCase):
def setUp(self):
self.api_uri = 'http://localhost:8000/api/users'
self.http_auth = ('super_admin', 'django_secure_pwd')
def test_retrieve_user(self):
response = requests.get(self.api_uri + '/1/', auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['username'], 'super_admin')
self.assertEqual(response_data['email'], 'super@admin.com')
def test_register_user(self):
payload = {
'username': 'tester_007',
'email': 'tester007@example.com',
'groups': 'http://localhost:8000/api/groups/2/'
}
response = requests.post(self.api_uri + '/', data=payload, auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['username'], 'tester_007')
def test_destroy_user(self):
response = requests.delete(self.api_uri + '/11/', auth=self.http_auth)
self.assertEqual(response.status_code, 204)
def test_patch_user_details(self):
payload = {'email': 'new_contact@example.com'}
response = requests.patch(self.api_uri + '/2/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['email'], 'new_contact@example.com')
def test_duplicate_user_creation(self):
payload = {
'username': 'tester_007',
'email': 'tester007@example.com',
'groups': 'http://localhost:8000/api/groups/2/'
}
response = requests.post(self.api_uri + '/', data=payload, auth=self.http_auth)
response_data = response.json()
# Kiểm tra thông báo lỗi trùng username
self.assertEqual(response_data['username'][0], 'A user with that username already exists.')
def test_missing_auth_header(self):
response = requests.get(self.api_uri)
response_data = response.json()
self.assertEqual(response_data['detail'], 'Authentication credentials were not provided.')
class DjangoGroupIntegrationTest(TestCase):
def setUp(self):
self.api_uri = 'http://localhost:8000/api/groups'
self.http_auth = ('super_admin', 'django_secure_pwd')
def test_get_group_info(self):
response = requests.get(self.api_uri + '/3/', auth=self.http_auth)
response_data = response.json()
self.assertEqual(response_data['name'], 'Product Managers')
def test_create_new_group(self):
payload = {'name': 'Team Leads'}
response = requests.post(self.api_uri + '/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['name'], 'Team Leads')
def test_rename_group(self):
payload = {'name': 'Directors'}
response = requests.patch(self.api_uri + '/6/', auth=self.http_auth, data=payload)
response_data = response.json()
self.assertEqual(response_data['name'], 'Directors')
def test_remove_group(self):
response = requests.delete(self.api_uri + '/6/', auth=self.http_auth)
self.assertEqual(response.status_code, 204)