Skip to content

Commit

Permalink
tests and remove user import
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 14, 2023
1 parent 22de360 commit 5bd781a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ jobs:
# run: pipenv run python manage.py makemigrations --check --dry-run

# TODO: assert code coverage target.
# - name: Test Code Units
# working-directory: ./backend
# run: pipenv run pytest
- name: Test Code Units
working-directory: ./backend
run: pipenv run pytest

build-and-deploy:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
codeforlife = {ref = "v0.6.6", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
codeforlife = {ref = "v0.6.7", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
django = "==3.2.20"
djangorestframework = "==3.13.1"
# https://pypi.org/user/codeforlife/
Expand Down
4 changes: 2 additions & 2 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@


class BaseAuthForm(forms.Form):
def __init__(self, request: HttpRequest = None, *args, **kwargs):
self.request = request # Should this be optional?
def __init__(self, request: HttpRequest, *args, **kwargs):
self.request = request
self.user: t.Optional[AbstractBaseUser] = None

def clean(self, **kwargs):
Expand Down
9 changes: 0 additions & 9 deletions backend/api/tests/views/session.py

This file was deleted.

12 changes: 12 additions & 0 deletions backend/api/tests/views/test_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest.mock import Mock, patch

from codeforlife.tests import CronTestCase
from django.urls import reverse


class TestClearExpiredView(CronTestCase):
@patch("django.core.management.call_command")
def test_clear_expired_view(self, call_command: Mock):
self.client.get(reverse("clear-expired-sessions"))

call_command.assert_called_once_with("clearsessions")
20 changes: 14 additions & 6 deletions backend/api/views/session.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
from enum import Enum
from functools import cached_property

# from codeforlife.user.models import User
from codeforlife.mixins import CronMixin
from codeforlife.user.models import User
from common.models import UserSession
from django.contrib.auth.views import LoginView as _LoginView
from django.contrib.sessions.models import Session, SessionManager
Expand All @@ -19,22 +20,29 @@
)


# TODO: use User.Type from cfl package
class UserType(str, Enum):
TEACHER = "teacher"
DEP_STUDENT = "dependent-student"
INDEP_STUDENT = "independent-student"


# TODO: add 2FA logic
class LoginView(_LoginView):
@cached_property
def user_type(self):
return User.Type(self.kwargs["user_type"])
return UserType(self.kwargs["user_type"])

def get_form_class(self):
if self.user_type == User.Type.INDEP_STUDENT:
if self.user_type == UserType.INDEP_STUDENT:
return CredentialsForm

elif self.user_type == User.Type.DEP_STUDENT:
elif self.user_type == UserType.DEP_STUDENT:
if "user_id" in self.request.POST:
return DependentStudentUserIdCredentialsForm
return DependentStudentUsernameCredentialsForm

else: # user_type == User.Type.TEACHER
else: # user_type == UserType.TEACHER
if False: # TODO: add 2fa logic.
return OneTimePasswordForm
return CredentialsForm
Expand All @@ -45,7 +53,7 @@ def form_valid(self, form: BaseAuthForm):
# TODO: use google analytics
user = form.get_user()
user_session = {"user": user}
if self.user_type == User.Type.DEP_STUDENT:
if self.user_type == UserType.DEP_STUDENT:
user_session["class_field"] = user.new_student.class_field
user_session["login_type"] = (
"direct" if "user_id" in self.request.POST else "classform"
Expand Down

0 comments on commit 5bd781a

Please sign in to comment.