Example #1
0
def login_post(command_bus: CommandBus, query: QueryUserByUserName):
    client_request = request.json
    validate_client_request(client_request, ['username', 'password'])
    login_a_user_command = LoginUser(
        username=UserName(client_request['username']),
        password=Password(client_request['password']),
    )
    command_bus.handle(login_a_user_command)
    user = query.execute(UserName(client_request['username']))

    return to_single_user_response(user), 200
Example #2
0
    def test_two_variables_with_same_values_should_be_equal(self):
        an_id = UserID()
        a_user = User(username=UserName('Maria'),
                      password=Password('uselesspassword'),
                      about='About Maria',
                      ID=an_id)
        another_user = User(username=UserName('Maria'),
                            password=Password('uselesspassword'),
                            about='About Maria',
                            ID=an_id)

        assert a_user == another_user
Example #3
0
    def test_should_create_a_user(self):
        user = User(username=UserName('Maria'),
                    password=Password('uselesspassword'),
                    about='About Maria')

        assert user.username.contents == 'Maria'
        assert user.password.contents == 'uselesspassword'
        assert user.about == 'About Maria'
Example #4
0
    def test_should_raise_an_exception_on_wrong_password(self):
        command = LoginUser(username=UserName('Maria'),
                            password=Password('a_wrong_password'))

        user_login_by_username_and_password = UserLoginByUserNameAndPassword(
            QueryUserByUserName(InMemoryUsersRepository([maria()])),
            FakeClock(a_perfect_day_and_time()))

        with self.assertRaises(InvalidCredentials):
            user_login_by_username_and_password.execute(command)
Example #5
0
    def test_should_raise_an_exception_on_unkown_username(self):
        command = LoginUser(username=UserName('IDontExist'),
                            password=Password('a_password'))

        user_login_by_username_and_password = UserLoginByUserNameAndPassword(
            QueryUserByUserName(InMemoryUsersRepository([maria()])),
            FakeClock(a_perfect_day_and_time()))

        with self.assertRaises(UnknownUser):
            user_login_by_username_and_password.execute(command)
Example #6
0
def registration_post(command_bus: CommandBus):
    client_request = request.json
    validate_client_request(client_request, ['username', 'password', 'about'])
    register_a_user_command = RegisterUser(
        username=UserName(client_request['username']),
        password=Password(client_request['password']),
        about=client_request['about'],
        ID=UserID())
    command_bus.handle(register_a_user_command)

    return to_single_user_response(register_a_user_command), 201
Example #7
0
    def test_should_create_a_copy_with_new_values(self):
        a_user = User(username=UserName('Maria'),
                      password=Password('uselesspassword'),
                      about='About Maria')

        user_with_password_changed = replace(
            a_user, password=Password('thispasswordisuselesstoo'))

        assert a_user.username == user_with_password_changed.username
        assert a_user.about == user_with_password_changed.about
        assert user_with_password_changed.password.contents == 'thispasswordisuselesstoo'
        assert a_user != user_with_password_changed
Example #8
0
    def test_should_login_a_user(self):
        try:
            command = LoginUser(username=UserName('Maria'),
                                password=Password('a_password'))
            user_login_by_username_and_password = UserLoginByUserNameAndPassword(
                QueryUserByUserName(InMemoryUsersRepository([maria()])),
                FakeClock(a_perfect_day_and_time()))

            entity, events = user_login_by_username_and_password.execute(
                command)

            assert entity is None
            assert len(events) == 1
            assert events[0].username == command.username
            assert events[0].timestamp == a_perfect_day_and_time()

        except ValueError as err:
            self.fail("Unexpected exception raised: %s" % err)
Example #9
0
def maria() -> User:
    return User(username=UserName('Maria'),
                password=Password('a_password'),
                about='About Maria',
                ID=UserID('331bfef4-7fe9-47b6-a138-42e81d4a5e14'))
Example #10
0
def create_maria() -> RegisterUser:
    return RegisterUser(ID=UserID(),
                        username=UserName('Maria'),
                        password=Password('a_password'),
                        about='About Maria')
Example #11
0
def bob() -> User:
    return User(username=UserName('Bob'),
                password=Password('another_password'),
                about='About Bob',
                ID=UserID('0d43fe15-8ffa-4845-a818-ddba840501b2'))
Example #12
0
    def test_should_create_a_username(self):
        a_username = UserName('maria')

        assert a_username.contents == 'maria'
Example #13
0
 def test_should_not_allow_undefined_username(self):
     with self.assertRaises(ValueError):
         UserName(None)  # noqa
Example #14
0
 def test_should_not_allow_empty_username(self):
     with self.assertRaises(ValueError):
         UserName('')
Example #15
0
 def test_should_not_allow_username_with_spaces(self):
     with self.assertRaises(ValueError):
         UserName('invalid username')