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 create(self, request, *args, **kwargs): """ Attempts to create a student confirmation token and email it to the user. """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = request.user if user.is_verified_student(): raise ValidationError(detail="Already confirmed a student username") student_username = serializer.validated_data.get("student_username") token = Registrations.generate_student_confirmation_token( student_username, serializer.validated_data.get("course"), serializer.validated_data.get("member"), ) send_email.delay( to_email=f"{student_username}@{constants.STUDENT_EMAIL_DOMAIN}", context={"token": token, "full_name": user.get_full_name()}, subject="Bekreft student kontoen din på Abakus.no", plain_template="users/email/student_confirmation.txt", html_template="users/email/student_confirmation.html", from_email=None, ) return Response(status=status.HTTP_204_NO_CONTENT)
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_with_valid_token_and_capitalized_student_username(self): AbakusGroup.objects.get(name="Users").add_user( self.user_without_student_confirmation) self.client.force_authenticate(self.user_without_student_confirmation) response = self.client.get( _get_student_confirmation_token_request_url( Registrations.generate_student_confirmation_token( "TestStudentUsername", constants.DATA, True))) self.assertEqual(response.status_code, 200) self.assertEqual(response.json().get("studentUsername"), "teststudentusername") self.assertEqual(response.json().get("course"), constants.DATA) self.assertEqual(response.json().get("member"), True)
def test_with_valid_token_and_capitalized_student_username(self): AbakusGroup.objects.get(name='Users').add_user( self.user_without_student_confirmation) self.client.force_authenticate(self.user_without_student_confirmation) response = self.client.get( _get_student_confirmation_token_request_url( Registrations.generate_student_confirmation_token( 'TestStudentUsername', constants.DATA, True))) self.assertEqual(response.status_code, 200) self.assertEqual(response.data.get('student_username'), 'teststudentusername') self.assertEqual(response.data.get('course'), constants.DATA) self.assertEqual(response.data.get('member'), True)
def create_token(self, student_username="******", course=constants.DATA, member=True): return Registrations.generate_student_confirmation_token( student_username, course, member)