Ilo/apps/accounts/tests_public.py
2026-05-02 20:01:30 +03:30

78 lines
3.2 KiB
Python

from django.contrib.auth import get_user_model
from django.test import TestCase
from rest_framework.test import APIClient
from apps.accounts.models import Role, TeacherProfile
from apps.courses.models import Course, CourseStatus
from apps.payments.models import Gateway, Order, OrderStatus
User = get_user_model()
def _teacher(phone, name="T", approved=True):
u = User.objects.create_user(phone_number=phone, full_name=name, role=Role.TEACHER)
TeacherProfile.objects.create(user=u, is_approved=approved, bio=f"bio of {name}", headline=f"{name} headline")
return u
def _student(phone="09120000099"):
return User.objects.create_user(phone_number=phone, full_name="Student", role=Role.STUDENT)
def _course(instructor, title="C", published=True, price_toman=None):
c = Course.objects.create(
instructor=instructor, title=title, tagline="T", description="D",
level="beginner", language="fa", price_toman=price_toman,
)
if published:
c.publish()
return c
class InstructorPublicEndpointsTests(TestCase):
def setUp(self):
self.client = APIClient()
self.t1 = _teacher("09120000001", name="Arash")
self.t2 = _teacher("09120000002", name="Neda")
# t1: 2 courses, t2: 0 courses
_course(self.t1, title="C1")
_course(self.t1, title="C2")
def test_list_instructors_returns_all_teachers_with_counts(self):
r = self.client.get("/api/instructors/")
self.assertEqual(r.status_code, 200)
body = r.json()
ids = {row["id"]: row for row in body}
self.assertIn(self.t1.id, ids)
self.assertIn(self.t2.id, ids)
self.assertEqual(ids[self.t1.id]["courses_count"], 2)
self.assertEqual(ids[self.t1.id]["students_count"], 0)
self.assertEqual(ids[self.t2.id]["courses_count"], 0)
def test_instructor_detail_returns_bio_courses_stats(self):
r = self.client.get(f"/api/instructors/{self.t1.id}/")
self.assertEqual(r.status_code, 200)
body = r.json()
self.assertEqual(body["id"], self.t1.id)
self.assertEqual(body["full_name"], "Arash")
self.assertEqual(body["headline"], "Arash headline")
self.assertEqual(body["bio"], "bio of Arash")
self.assertEqual(body["courses_count"], 2)
self.assertEqual(len(body["courses"]), 2)
def test_instructor_detail_404_for_non_teacher(self):
s = _student()
r = self.client.get(f"/api/instructors/{s.id}/")
self.assertEqual(r.status_code, 404)
def test_students_count_increments_with_paid_orders(self):
course = _course(self.t1, title="Sold", price_toman=1000)
s1 = _student("09120000091")
s2 = _student("09120000092")
Order.objects.create(student=s1, course=course, amount_toman=1000, gateway=Gateway.CONSOLE, status=OrderStatus.PAID)
Order.objects.create(student=s2, course=course, amount_toman=1000, gateway=Gateway.CONSOLE, status=OrderStatus.PAID)
# Same student buys again — should still count as 1 unique student
Order.objects.create(student=s1, course=course, amount_toman=1000, gateway=Gateway.CONSOLE, status=OrderStatus.PAID)
r = self.client.get(f"/api/instructors/{self.t1.id}/")
self.assertEqual(r.json()["students_count"], 2)