def test_validate_student_confirmation_token(self): student_confirmation_token = Registrations.generate_student_confirmation_token( 'teststudentusername', constants.DATA, True) token = Registrations.validate_student_confirmation_token( student_confirmation_token) self.assertEqual(token['student_username'], 'teststudentusername') self.assertEqual(token['course'], constants.DATA) self.assertEqual(token['member'], True) student_confirmation_token = Registrations.generate_student_confirmation_token( 'teststudentusername', constants.DATA, True) token = Registrations.validate_student_confirmation_token( student_confirmation_token) self.assertNotEqual(token['student_username'], 'wrongtestusername') self.assertNotEqual(token['course'], constants.KOMTEK) self.assertNotEqual(token['member'], False)
def test_validate_student_confirmation_token(self): student_confirmation_token = Registrations.generate_student_confirmation_token( "teststudentusername", constants.DATA, True ) token = Registrations.validate_student_confirmation_token( student_confirmation_token ) self.assertEqual(token["student_username"], "teststudentusername") self.assertEqual(token["course"], constants.DATA) self.assertEqual(token["member"], True) student_confirmation_token = Registrations.generate_student_confirmation_token( "teststudentusername", constants.DATA, True ) token = Registrations.validate_student_confirmation_token( student_confirmation_token ) self.assertNotEqual(token["student_username"], "wrongtestusername") self.assertNotEqual(token["course"], constants.KOMTEK) self.assertNotEqual(token["member"], False)
def list(self, request): """ Validates a student confirmation token. The request errors out if the token has expired or is invalid. Request URL: GET /api/v1/users/student-confirmation/?token=<token> """ token = request.GET.get("token") if not token: raise ValidationError(detail="Student confirmation token is required.") student_confirmation = Registrations.validate_student_confirmation_token(token) if student_confirmation is None: raise ValidationError(detail="Token expired or invalid.") return Response(student_confirmation, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): """ Attempts to confirm the student based on the student confirmation token. """ token = request.GET.get('token') if not token: raise ValidationError( detail='Student confirmation token is required.') student_confirmation = Registrations.validate_student_confirmation_token( token) if student_confirmation is None: raise ValidationError(detail='Token expired or invalid.') user = request.user if user.is_verified_student(): raise ValidationError( detail='Already confirmed a student username', ) user.student_username = student_confirmation['student_username'] course = student_confirmation['course'].lower() if course == constants.DATA: course_group = AbakusGroup.objects.get(name=constants.DATA_LONG) course_group.add_user(user) grade_group = AbakusGroup.objects.get( name=constants.FIRST_GRADE_DATA) grade_group.add_user(user) else: course_group = AbakusGroup.objects.get(name=constants.KOMTEK_LONG) course_group.add_user(user) grade_group = AbakusGroup.objects.get( name=constants.FIRST_GRADE_KOMTEK) grade_group.add_user(user) if student_confirmation['member']: member_group = AbakusGroup.objects.get(name=constants.MEMBER_GROUP) member_group.add_user(user) user.save() return Response(MeSerializer(user).data, status=status.HTTP_200_OK)