Example #1
0
async def _():
    repository = FakeUsersRepository(users=[])
    request = MagicMock()
    request.headers = {}

    result = await JWTAuthBackend(repository).authenticate(request)
    assert not result
Example #2
0
async def _():
    user = User(
        id=1,
        username="******",
        password="******",
        email="*****@*****.**",
        fullname="Marco Acierno",
        name="Marco",
        gender="",
        date_birth=None,
        open_to_newsletter=False,
        open_to_recruiting=False,
        country="",
        date_joined=datetime(2020, 1, 1),
        is_staff=False,
        is_superuser=False,
        is_active=True,
    )
    repository = FakeUsersRepository(users=[user])

    logged_user = await login(
        LoginInputModel(email="*****@*****.**", password="******"),
        users_repository=repository,
    )

    assert logged_user.id == user.id
Example #3
0
async def _():
    user = User(
        id=1,
        email="*****@*****.**",
        name="Name",
        is_staff=False,
        is_active=True,
        date_joined=datetime.utcnow(),
    )

    repository = FakeUsersRepository(users=[user])

    with patch(
            "users.auth.entities.get_jwt_metadata",
            return_value={
                "exp": datetime(1980, 1, 1),
                "iat": datetime(1980, 1, 1)
            },
    ):
        jwt_token = user.generate_token()

    request = MagicMock()
    request.headers = {"Authorization": f"Bearer {jwt_token}"}

    with raises(AuthenticationError):
        await JWTAuthBackend(repository).authenticate(request)
Example #4
0
async def _():
    repository = FakeUsersRepository(users=[])

    with time_machine.travel("2020-10-10 10:10:00Z", tick=False):
        user = await social_login(
            SocialLoginInput(
                email="*****@*****.**",
                social_account=SocialAccount(
                    social_id="1",
                    fullname="Test Account",
                    first_name="Test",
                    last_name="Account",
                ),
            ),
            users_repository=repository,
        )

    assert user
    assert user.id is not None
    assert user.date_joined == datetime(2020,
                                        10,
                                        10,
                                        10,
                                        10,
                                        0,
                                        tzinfo=timezone.utc)
    assert not user.has_usable_password()
    assert user.fullname == "Test Account"
    assert user.name == "Test"
    assert user.email == "*****@*****.**"
Example #5
0
async def _():
    user = User(
        id=1,
        username="******",
        password="******",
        email="*****@*****.**",
        fullname="Marco Acierno",
        name="Marco",
        gender="",
        date_birth=None,
        open_to_newsletter=False,
        open_to_recruiting=False,
        country="",
        date_joined=datetime(2020, 1, 1),
        is_staff=False,
        is_superuser=False,
        is_active=True,
    )
    repository = FakeUsersRepository(users=[user])

    with raises(UserIsNotAdminError):
        await login(
            LoginInputModel(email="*****@*****.**", password="******"),
            reject_non_admins=True,
            users_repository=repository,
        )
Example #6
0
async def _():
    user = User(
        id=1,
        username="******",
        password="******",
        email="*****@*****.**",
        fullname="Marco Acierno",
        name="Marco",
        gender="",
        date_birth=None,
        open_to_newsletter=False,
        open_to_recruiting=False,
        country="",
        date_joined=datetime(2020, 1, 1),
        is_staff=False,
        is_superuser=False,
        is_active=True,
    )
    repository = FakeUsersRepository(users=[user])

    with time_machine.travel("2020-10-10 10:10:00Z", tick=False):
        logged_user = await login(
            LoginInputModel(email="*****@*****.**", password="******"),
            users_repository=repository,
        )

    assert logged_user.id == user.id
    assert logged_user.last_login == datetime(
        2020, 10, 10, 10, 10, 00, tzinfo=timezone.utc
    )
Example #7
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        password="******",
        is_active=False,
        jwt_auth_id=1,
        name="Old name",
        fullname="Old fullname",
        gender="f",
        open_to_recruiting=True,
        open_to_newsletter=True,
        date_birth=date(1900, 1, 1),
        country="US",
    )

    with raises(UserIsNotActiveError):
        await update_profile(
            user.id,
            UpdateProfileInput(
                name="New name",
                full_name="Full name",
                gender="m",
                open_to_recruiting=False,
                open_to_newsletter=False,
                date_birth=date(2020, 10, 1),
                country="IT",
            ),
            users_repository=FakeUsersRepository([user]),
        )
Example #8
0
async def _():
    repository = FakeUsersRepository(
        users=[
            User(
                id=10,
                email="*****@*****.**",
                date_joined=datetime.now(),
                password="******",
                fullname="Hello World",
                name="Hello",
            )
        ]
    )

    user = await social_login(
        SocialLoginInput(
            email="*****@*****.**",
            social_account=SocialAccount(
                social_id="1",
                fullname="Test Account",
                first_name="Test",
                last_name="Account",
            ),
        ),
        users_repository=repository,
    )

    assert user
    assert user.has_usable_password()
    assert user.check_password("my_password")
    assert user.id == 10
    assert user.fullname == "Hello World"
    assert user.name == "Hello"
    assert user.email == "*****@*****.**"
Example #9
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        password="******",
        is_active=True,
        jwt_auth_id=1,
    )

    with time_machine.travel("2021-10-10 15:00:00Z", tick=False):
        token = jwt.encode(
            {
                "user_id": 10,
                "exp": datetime.now(timezone.utc) + timedelta(minutes=30),
                "iat": datetime.now(timezone.utc),
                "iss": "users",
                "aud": "users/not-reset-password",
            },
            str(SECRET_KEY),
        )

    with raises(ResetPasswordTokenInvalidError):
        await reset_password(
            ResetPasswordInput(token=token, new_password="******"),
            repository=FakeUsersRepository([user]),
        )
Example #10
0
async def _():
    repository = FakeUsersRepository(users=[])

    with raises(WrongEmailOrPasswordError):
        await login(
            LoginInputModel(email="*****@*****.**", password="******"),
            users_repository=repository,
        )
Example #11
0
async def _():
    repository = FakeUsersRepository(
        users=[User(email="*****@*****.**", date_joined=datetime.utcnow())])

    with raises(EmailAlreadyUsedError):
        await register(
            RegisterInputModel(email="*****@*****.**", password="******"),
            users_repository=repository,
        )
Example #12
0
async def _():
    repository = FakeUsersRepository(users=[])

    user = await register(
        RegisterInputModel(email="*****@*****.**", password="******"),
        users_repository=repository,
    )

    assert repository.committed
    assert user.id is not None
    assert user.email == "*****@*****.**"
    assert user.check_password("hello_world")
Example #13
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        is_active=False,
    )

    with raises(UserIsNotActiveError):
        await reset_password(
            ResetPasswordInput(token=user.create_reset_password_token(),
                               new_password="******"),
            repository=FakeUsersRepository([user]),
        )
Example #14
0
async def _():
    with raises(UserDoesNotExistError):
        await update_profile(
            5,
            UpdateProfileInput(
                name="New name",
                full_name="Full name",
                gender="m",
                open_to_recruiting=False,
                open_to_newsletter=False,
                date_birth=date(2020, 10, 1),
                country="IT",
            ),
            users_repository=FakeUsersRepository([]),
        )
Example #15
0
async def _():
    user = User(
        id=1,
        email="*****@*****.**",
        name="Name",
        is_staff=False,
        date_joined=datetime.utcnow(),
    )

    repository = FakeUsersRepository(users=[user])

    request = MagicMock()
    request.headers = {"Authorization": f"token abc"}

    result = await JWTAuthBackend(repository).authenticate(request)
    assert not result
Example #16
0
async def _():
    user = User(
        id=1,
        email="*****@*****.**",
        name="Name",
        is_staff=False,
        date_joined=datetime.utcnow(),
    )

    repository = FakeUsersRepository(users=[user])

    request = MagicMock()
    request.headers = {"Authorization": f"Bearer abc"}

    with raises(AuthenticationError):
        await JWTAuthBackend(repository).authenticate(request)
Example #17
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        password="******",
        is_active=True,
        jwt_auth_id=1,
    )

    token = user.create_reset_password_token()
    user.jwt_auth_id = 2

    with raises(ResetPasswordTokenInvalidError):
        await reset_password(
            ResetPasswordInput(token=token, new_password="******"),
            repository=FakeUsersRepository([user]),
        )
Example #18
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        password="******",
        is_active=True,
        jwt_auth_id=1,
    )

    with time_machine.travel("2020-10-10 10:10:10Z", tick=False):
        token = user.create_reset_password_token()

    with time_machine.travel(
            "2020-10-10 15:10:10Z",
            tick=False), raises(ResetPasswordTokenExpiredError):
        await reset_password(
            ResetPasswordInput(token=token, new_password="******"),
            repository=FakeUsersRepository([user]),
        )
Example #19
0
async def _():
    user = User(
        id=10,
        email="*****@*****.**",
        date_joined=datetime.now(timezone.utc),
        password="******",
        is_active=True,
        jwt_auth_id=1,
    )

    await reset_password(
        ResetPasswordInput(token=user.create_reset_password_token(),
                           new_password="******"),
        repository=FakeUsersRepository([user]),
    )

    assert user.new_password == "testnewpassword"

    # we did column + 1 so the value updated in the DB and not python
    assert user.jwt_auth_id.left.name == "jwt_auth_id"
    assert user.jwt_auth_id.right.value == 1
Example #20
0
async def _():
    user = User(
        id=1,
        email="*****@*****.**",
        name="Name",
        is_staff=False,
        date_joined=datetime.utcnow(),
    )

    repository = FakeUsersRepository(users=[user])

    jwt_token = user.generate_token()

    request = MagicMock()
    request.headers = {"Authorization": f"Bearer {jwt_token}"}

    credentials, logged_user = await JWTAuthBackend(repository).authenticate(
        request)

    assert credentials.scopes == [Credential.AUTHENTICATED]
    assert logged_user.id == user.id