def test_password2_validation(user_data):
    serializer = serializers.UserSerializer(data=user_data)
    assert not serializer.is_valid()
    assert "password2" in serializer.errors

    user_data["password2"] = user_data["password"] + "wrongwrong"
    serializer = serializers.UserSerializer(data=user_data)
    assert not serializer.is_valid()
    assert "password2" in serializer.errors
Exemplo n.º 2
0
    def post(request):
        """
        @api {post} /users/register/ Request user registration
        @apiName RegisterView
        @apiGroup UserRegistration
        @apiParam {String} first_name User's first_name.
        @apiParam {String} last_name User's last_name.
        @apiParam {String} email User's email.
        @apiParam {String} username User's username.
        @apiParam {String} password User's password.
        @apiSuccess redirect to /users/register-done/
        """
        serializer = serializers.UserSerializer(data=request.data)

        if not serializer.is_valid():
            return Response(serializer.errors)

        user_new = User.objects.create(is_active=False,
                                       **serializer.validated_data)
        user_new.set_password(serializer.validated_data['password'])
        user_new.save()

        email_account_activation.delay(
            user_pk=user_new.pk,
            domain=get_current_site(request).domain,
            uid_encoded=urlsafe_base64_encode(force_bytes(user_new.pk)),
            token=tokens.account_activation_token.make_token(user_new))

        return redirect('user_register_done')
def test_user_serialization(user_data, MockUser):
    user = MockUser.objects.create_user(**user_data)
    serialized_data = serializers.UserSerializer(instance=user).data
    assert serialized_data["username"] == user.username
    assert serialized_data["email"] == user.email
    assert not serialized_data.get("password")
    assert not serialized_data.get("password2")
Exemplo n.º 4
0
    def put(self, request, format=None):
        validator = user_sers.UserUpdateValidator(data=request.data)
        if not validator.is_valid():
            return Response(validator.errors, status=status.HTTP_400_BAD_REQUEST)
        user = request.user
        username = validator.validated_data.get('username')
        data = {'username': username}
        upd_fields = {k: v for k, v in data.items() if v is not None}

        if upd_fields:
            user_models.User.objects.filter(id=user.id).update(**upd_fields)
            user = user_models.User.objects.filter(id=user.id).first()

        serializer = user_sers.UserSerializer(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 5
0
def test_user_serializer_flags(
    flag1_everyone,
    flag1_superusers,
    flag1_add_user,
    flag2_everyone,
    flag2_superusers,
    flag2_add_user,
    user_under_test,
    expect_flag1,
    expect_flag2,
):
    user = {
        "superuser":
        user_model.objects.create_superuser(email="*****@*****.**",
                                            password="******"),
        "hub_admin":
        create_test_user(role_assignment_data={"role_type": Roles.HUB_ADMIN}),
    }[user_under_test]
    Flag = get_waffle_flag_model()

    flag1 = Flag.objects.create(name="flag1",
                                everyone=flag1_everyone,
                                superusers=flag1_superusers)
    if flag1_add_user:
        flag1.users.add(user)
        flag1.save()

    flag2 = Flag.objects.create(name="flag2",
                                everyone=flag2_everyone,
                                superusers=flag2_superusers)
    if flag2_add_user:
        flag2.users.add(user)
        flag2.save()

    request = APIRequestFactory().get("/")
    request.user = user
    data = serializers.UserSerializer(user, context={"request": request}).data
    expected_flag_count = sum([x for x in [expect_flag1, expect_flag2] if x])
    assert len(data["flags"]) == expected_flag_count
    flags = [(flag["name"], flag["id"]) for flag in data["flags"]]
    if expect_flag1:
        assert (flag1.name, flag1.id) in flags
    if not expect_flag1:
        assert (flag1.name, flag1.id) not in flags
    if expect_flag2:
        assert (flag2.name, flag2.id) in flags
    if not expect_flag2:
        assert (flag2.name, flag2.id) not in flags
Exemplo n.º 6
0
    def put(self, request, format=None):
        validator = user_sers.UserUpdatePasswordValidator(data=request.data)
        if not validator.is_valid():
            return Response(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        old_password = validator.validated_data['old_password']
        new_password = validator.validated_data['new_password']
        user = request.user

        if not user.check_password(old_password):
            return Response('Password and old password dissimilar', status=status.HTTP_401_UNAUTHORIZED)

        user.set_password(new_password)
        user.save()
        serializer = user_sers.UserSerializer(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 7
0
    def put(self, request, format=None):
        validator = user_sers.LoginLineValidator(data=request.data)
        if not validator.is_valid():
            return Response(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        line_access_token = validator.validated_data['line_access_token']
        try:
            profiles = user_utils.get_line_profile(line_access_token)
            line_id = profiles['userId']
        except Exception:
            return Response('Incorrect Line Id', status=status.HTTP_401_UNAUTHORIZED)
        user = request.user

        user_models.User.objects.filter(id=user.id).update(line_id=line_id)
        user = user_models.User.objects.filter(id=user.id).first()
        serializer = user_sers.UserSerializer(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 8
0
    def post(self, request, format=None):
        validator = user_sers.RegisterEmailValidator(data=request.data)
        if not validator.is_valid():
            return Response(validator.errors, status=status.HTTP_400_BAD_REQUEST)

        email = validator.validated_data['email']
        password = validator.validated_data['password']
        if user_models.User.objects.filter(email=email).exists():
            return Response('Email already exists', status=status.HTTP_409_CONFLICT)
        else:
            user = user_models.User(email=email, is_active=True)
            user.set_password(password)
            user.save()

        # send_register_confirm_email.delay(email)

        serializer = user_sers.UserSerializer(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 9
0
    def post(self, request, format=None):
        validator = user_sers.ResetPasswordValidator(data=request.data)
        if not validator.is_valid():
            return Response(validator.errors, status=status.HTTP_400_BAD_REQUEST)
        reset_token = validator.validated_data['reset_token']
        password = validator.validated_data['password']
        try:
            reset_token = user_models.ResetToken.objects.get(reset_token=reset_token)
        except Exception:
            return Response('Not found reset token', status=status.HTTP_404_NOT_FOUND)

        user = reset_token.user
        if not user_utils.check_expired_time_reset_token(reset_token):
            reset_token.delete()
            return Response('Token has expired', status=status.HTTP_404_NOT_FOUND)

        user.set_password(password)
        user.save()

        reset_token.delete()
        user.user_type = user_utils.get_user_type(user.id)
        serializer = user_sers.UserSerializer(user)
        return Response(serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 10
0
 def get(self, request, format=None):
     user_utils.create_or_update_login_history(request.user.id)
     user = request.user
     user.user_type = user_utils.get_user_type(user.id)
     serializer = user_sers.UserSerializer(user)
     return Response(serializer.data, status=status.HTTP_200_OK)