58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from django.urls import reverse
|
|
from rest_framework.test import APITestCase, APIClient
|
|
from rest_framework import status
|
|
from django.contrib.auth import get_user_model
|
|
from django.utils import timezone
|
|
|
|
from helpdesk.models import Ticket
|
|
|
|
UserModel = get_user_model()
|
|
|
|
class TicketViewSetFunctionalTest(APITestCase):
|
|
|
|
def setUp(self):
|
|
# สร้าง Agent User
|
|
self.agent = UserModel.objects.create_user(
|
|
username="agent1",
|
|
email="agent1@example.com",
|
|
password="password123",
|
|
is_staff=True
|
|
)
|
|
|
|
# สร้าง Customer User
|
|
self.customer = UserModel.objects.create_user(
|
|
username="customer1",
|
|
email="customer1@example.com",
|
|
password="password123",
|
|
is_staff=False
|
|
)
|
|
|
|
# Client สำหรับ Agent
|
|
self.client_agent = APIClient()
|
|
self.client_agent.force_authenticate(user=self.agent)
|
|
|
|
# Client สำหรับ Customer
|
|
self.client_customer = APIClient()
|
|
self.client_customer.force_authenticate(user=self.customer)
|
|
|
|
# สร้าง Ticket ตัวอย่าง
|
|
self.ticket = Ticket.objects.create(
|
|
creator=self.customer,
|
|
title="Functional Test Ticket",
|
|
last_message_at=timezone.now()
|
|
)
|
|
|
|
def test_ticket_list_authenticated(self):
|
|
"""GET /tickets/ ต้อง return 200 สำหรับผู้ใช้ล็อกอิน"""
|
|
url = reverse('helpdesk-tickets-list') # ต้องตรงกับ router name
|
|
response = self.client_agent.get(url)
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
self.assertTrue(isinstance(response.data, list))
|
|
|
|
def test_ticket_list_unauthenticated(self):
|
|
"""GET /tickets/ ต้อง return 401 สำหรับผู้ใช้ไม่ล็อกอิน"""
|
|
client = APIClient() # client ไม่ล็อกอิน
|
|
url = reverse('helpdesk-tickets-list')
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|