|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django_email_learning.models import OrganizationUser |
| 3 | +from django.test import Client |
| 4 | +from django_email_learning.models import ( |
| 5 | + ImapConnection, |
| 6 | + Quiz, |
| 7 | + Lesson, |
| 8 | + Course, |
| 9 | + BlockedEmail, |
| 10 | + Learner, |
| 11 | + Enrollment, |
| 12 | + CourseContent, |
| 13 | +) |
| 14 | +import pytest |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture() |
| 18 | +def users(db): |
| 19 | + superadmin = User( |
| 20 | + id=1, |
| 21 | + username="superadmin", |
| 22 | + |
| 23 | + password="adminpass", |
| 24 | + is_superuser=True, |
| 25 | + ) |
| 26 | + editor_user = User( |
| 27 | + id=2, username="editor", email="[email protected]", password="editorpass" |
| 28 | + ) |
| 29 | + platform_admin = User( |
| 30 | + id=3, |
| 31 | + username="platformadmin", |
| 32 | + |
| 33 | + password="platformadminpass", |
| 34 | + ) |
| 35 | + viewer_user = User( |
| 36 | + id=4, username="viewer", email="[email protected]", password="viewerpass" |
| 37 | + ) |
| 38 | + User.objects.bulk_create([superadmin, editor_user, platform_admin, viewer_user]) |
| 39 | + editor = OrganizationUser(user=editor_user, organization_id=1, role="editor") |
| 40 | + admin = OrganizationUser(user=platform_admin, organization_id=1, role="admin") |
| 41 | + viewer = OrganizationUser(user=viewer_user, organization_id=1, role="viewer") |
| 42 | + OrganizationUser.objects.bulk_create([editor, admin, viewer]) |
| 43 | + return { |
| 44 | + "superadmin": superadmin, |
| 45 | + "editor_user": editor_user, |
| 46 | + "platform_admin": platform_admin, |
| 47 | + "viewer_user": viewer_user, |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope="session") |
| 52 | +def anonymous_client(): |
| 53 | + return Client() |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture() |
| 57 | +def superadmin_client(users): |
| 58 | + client = Client() |
| 59 | + client.force_login(users["superadmin"]) |
| 60 | + return client |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture() |
| 64 | +def editor_client(users): |
| 65 | + client = Client() |
| 66 | + client.force_login(users["editor_user"]) |
| 67 | + return client |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture() |
| 71 | +def platform_admin_client(users): |
| 72 | + client = Client() |
| 73 | + client.force_login(users["platform_admin"]) |
| 74 | + return client |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture() |
| 78 | +def viewer_client(users): |
| 79 | + client = Client() |
| 80 | + client.force_login(users["viewer_user"]) |
| 81 | + return client |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture() |
| 85 | +def client( |
| 86 | + request, |
| 87 | + superadmin_client, |
| 88 | + editor_client, |
| 89 | + platform_admin_client, |
| 90 | + viewer_client, |
| 91 | + anonymous_client, |
| 92 | +): |
| 93 | + def _get_client(role_name): |
| 94 | + role_map = { |
| 95 | + "anonymous": anonymous_client, |
| 96 | + "superadmin": superadmin_client, |
| 97 | + "editor": editor_client, |
| 98 | + "platform_admin": platform_admin_client, |
| 99 | + "viewer": viewer_client, |
| 100 | + } |
| 101 | + return role_map.get(role_name) |
| 102 | + |
| 103 | + return _get_client(request.param) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.fixture() |
| 107 | +def imap_connection(db) -> ImapConnection: |
| 108 | + connection = ImapConnection( |
| 109 | + server="IMAP.example.com", |
| 110 | + port=993, |
| 111 | + |
| 112 | + password="my_secret_password", |
| 113 | + organization_id=1, |
| 114 | + ) |
| 115 | + connection.save() |
| 116 | + return connection |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture() |
| 120 | +def quiz(db) -> Quiz: |
| 121 | + quiz = Quiz(title="Sample Quiz", required_score=70) |
| 122 | + quiz.save() |
| 123 | + return quiz |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture() |
| 127 | +def lesson(db) -> Lesson: |
| 128 | + lesson = Lesson(title="Sample Lesson", content="Lesson Content", is_published=True) |
| 129 | + lesson.save() |
| 130 | + return lesson |
| 131 | + |
| 132 | + |
| 133 | +@pytest.fixture() |
| 134 | +def course(db, imap_connection) -> Course: |
| 135 | + course = Course( |
| 136 | + title="Sample Course", |
| 137 | + slug="sample-course", |
| 138 | + imap_connection=imap_connection, |
| 139 | + organization_id=1, |
| 140 | + ) |
| 141 | + course.save() |
| 142 | + return course |
| 143 | + |
| 144 | + |
| 145 | +@pytest.fixture() |
| 146 | +def blocked_email(db) -> BlockedEmail: |
| 147 | + blocked_email = BlockedEmail( email="[email protected]") |
| 148 | + blocked_email.save() |
| 149 | + return blocked_email |
| 150 | + |
| 151 | + |
| 152 | +@pytest.fixture() |
| 153 | +def learner(db) -> Learner: |
| 154 | + learner = Learner( email="[email protected]") |
| 155 | + learner.save() |
| 156 | + return learner |
| 157 | + |
| 158 | + |
| 159 | +@pytest.fixture() |
| 160 | +def enrollment(db, learner, course) -> Enrollment: |
| 161 | + enrollment = Enrollment.objects.create(learner=learner, course=course) |
| 162 | + return enrollment |
| 163 | + |
| 164 | + |
| 165 | +@pytest.fixture |
| 166 | +def course_lesson_content(db, course, lesson) -> CourseContent: |
| 167 | + content = CourseContent.objects.create( |
| 168 | + course=course, priority=1, type="lesson", lesson=lesson, waiting_period=10 |
| 169 | + ) |
| 170 | + return content |
| 171 | + |
| 172 | + |
| 173 | +@pytest.fixture |
| 174 | +def course_quiz_content(db, course, quiz) -> CourseContent: |
| 175 | + content = CourseContent.objects.create( |
| 176 | + course=course, priority=2, type="quiz", quiz=quiz, waiting_period=5 |
| 177 | + ) |
| 178 | + return content |
0 commit comments