def setUp(self):
     super(AccountRequestServiceTest, self).setUp()
     self.email_service = Mock(EmailService)
     self.crowd_client = Mock(CrowdClient)
     crowd_client_factory = CrowdClientFactory()
     crowd_client_factory.get_client = Mock(return_value=self.crowd_client)
     self.account_request_service = AccountRequestService(
         email_service=self.email_service,
         crowd_client_factory=crowd_client_factory)
     self.clean_database()
    def test_GIVEN_user_and_password_WHEN_password_set_THEN_password_call_made_to_crowd_and_forgotten_password_blanked(self):
        user = self.login()
        crowd_client = Mock(CrowdClient)
        crowd_client_factory = CrowdClientFactory()
        crowd_client_factory.get_client = Mock(return_value=crowd_client)
        user_service = UserService(crowd_client_factory=crowd_client_factory)
        user_service.set_forgot_password(user.id)
        password = "******"

        user_service.reset_password(user.id, password, password)

        assert_that(crowd_client.update_users_password.called, is_(True), "Crowd called to update user")
        user = user_service.get_user_by_id(user.id)
        assert_that(user.forgotten_password_uuid, is_(None), "uuid")
        assert_that(user.forgotten_password_expiry_date, is_(None), "expiry date")
    def test_GIVEN_user_and_password_WHEN_password_set_and_crowd_client_raises_THEN_forgotten_password_not_blanked_error(self):
        user = self.login()
        crowd_client = Mock(CrowdClient)
        crowd_client.update_users_password = Mock(side_effect=ClientException())
        crowd_client_factory = CrowdClientFactory()
        crowd_client_factory.get_client = Mock(return_value=crowd_client)
        user_service = UserService(crowd_client_factory=crowd_client_factory)
        user_service.set_forgot_password(user.id)
        password = "******"

        with self.assertRaises(ServiceException, msg="Service exception not raise"):
            user_service.reset_password(user.id, password, password)

        user = user_service.get_user_by_id(user.id)
        assert_that(user.forgotten_password_uuid, is_not(None), "uuid")
        assert_that(user.forgotten_password_expiry_date, is_not(None), "expiry date")
 def setUp(self):
     self.client = CrowdClientFactory().get_client()
     if not self.client.use_crowd:
         raise self.skipTest("Can not talk to crowd")
class CrowdClientTests(TestController):

    client = None
    username = None

    def create_user(self):
        self.username = '******'
        self.password = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))

        self.tearDown()

        u = self.client.create_user(self.username,
                                    'Test',
                                    'User',
                                    '*****@*****.**',
                                    self.password)

    def tearDown(self):
        if self.client.use_crowd and self.username:
            try:
                self.client.delete_user(self.username)
            except ClientException:
                pass

    def setUp(self):
        self.client = CrowdClientFactory().get_client()
        if not self.client.use_crowd:
            raise self.skipTest("Can not talk to crowd")

    def test_authentication_with_valid_credentials(self):
        self.create_user()

        self.client.check_authenticated(self.username, self.password)

    def test_change_password_with_valid_credentials(self):
        self.create_user()
        new_password = "******"

        self.client.update_users_password(self.username, new_password)

        self.client.check_authenticated(self.username, new_password)

    def test_user_session_control_with_valid_credentials(self):
        self.create_user()

        response = self.client.create_user_session(self.username, self.password, remote_addr="80.252.78.170")
        obj = self.client.verify_user_session(response['token'])

        self.assertNotEqual(obj, None)

        # Delete the session
        self.client.delete_session(response['token'])

        self.assertRaises(SessionNotFoundException, lambda: self.client.verify_user_session(response['token']))

    def test_user_session_request_with_invalid_credentials(self):
        self.create_user()

        self.assertRaises(
            AuthenticationFailedException,
            lambda:self.client.create_user_session("crowd-admin", "sddfghfh", "127.0.0.1"))

    def test_duff_token(self):

        self.assertRaises(SessionNotFoundException, lambda: self.client.verify_user_session("abc123"))

    def test_get_user_info_returns_populated_object(self):
        self.create_user()

        user = self.client.get_user_info(self.username)

        assert_that(self.username, is_(self.username), "username")