def test_validate_registration_token(self): registration_token = Registrations.generate_registration_token('*****@*****.**') token_email = Registrations.validate_registration_token(registration_token) self.assertEqual(token_email, '*****@*****.**') registration_token = Registrations.generate_registration_token('*****@*****.**') token_email = Registrations.validate_registration_token(registration_token) self.assertNotEqual(token_email, '*****@*****.**')
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_with_valid_token(self): response = self.client.get( _get_registration_token_url( Registrations.generate_registration_token("*****@*****.**") ) ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json().get("email"), "*****@*****.**")
def test_with_valid_token_and_capitalized_email(self): response = self.client.get( _get_registration_token_url( Registrations.generate_registration_token("*****@*****.**") ) ) self.assertEqual(response.status_code, 200) self.assertEqual(response.json().get("email"), "*****@*****.**")
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 registration token. The request errors out if the token has expired or is invalid. Request URL: GET /api/v1/users/registration/?token=<token> """ if not request.GET.get('token', False): raise ValidationError(detail='Registration token is required.') token_email = Registrations.validate_registration_token(request.GET.get('token', False)) if token_email is None: raise ValidationError(detail='Token expired or invalid.') return Response({'email': token_email}, status=status.HTTP_200_OK)
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 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)
def create(self, request, *args, **kwargs): """ Attempts to create a registration token and email it to the user. """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data.get('email') token = Registrations.generate_registration_token(email) send_email.delay( to_email=email, context={ "token": token, }, subject='Velkommen til Abakus.no', plain_template='users/email/registration.txt', html_template='users/email/registration.html', from_email=None ) return Response(status=status.HTTP_202_ACCEPTED)
def create(self, request, *args, **kwargs): """ Attempts to register a new user based on the registration token. """ if not request.GET.get('token', False): raise ValidationError(detail='Registration token is required.') token_email = Registrations.validate_registration_token( request.GET.get('token', False)) if token_email is None: raise ValidationError(detail='Token expired or invalid.') serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = User.objects.create_user(email=token_email, **serializer.validated_data) user_group = AbakusGroup.objects.get(name=constants.USER_GROUP) user_group.add_user(user) payload = get_jwt_token(user) return Response(payload, status=status.HTTP_201_CREATED)
def test_with_valid_token_and_capitalized_email(self): response = self.client.get( _get_registration_token_url( Registrations.generate_registration_token('*****@*****.**'))) self.assertEqual(response.status_code, 200) self.assertEqual(response.data.get('email'), '*****@*****.**')
def create_token(self, student_username="******", course=constants.DATA, member=True): return Registrations.generate_student_confirmation_token( student_username, course, member)
def test_with_valid_token(self): response = self.client.get( _get_registration_token_url( Registrations.generate_registration_token('*****@*****.**'))) self.assertEqual(response.status_code, 200) self.assertEqual(response.data.get('email'), '*****@*****.**')
def create_token(self, email=None): token_email = email or self.new_email return Registrations.generate_registration_token(token_email)