Ejemplo n.º 1
0
    def run(self, T, B=5):
        gamma = 0.98
        # Load a pretrained model
        if self.saved_model:
            # Load pretrained model
            self.q.load_state_dict(torch.load(saved_model))

        env = self.env
        score = 0.0
        best_episode_score = 0
        total_frames = 0
        state = get_state(env.reset())  # Start first game
        for episode in range(1, T):
            # anneal 100% to 1% over training
            epsilon = self.epsilon_decay(total_frames)
            episode_score = 0
            done = False
            while not done:
                # Select action using current policy (depends on annealing epsilon)
                with torch.no_grad():
                    s = torch.Tensor(state).unsqueeze(0)  #.to(device)
                    action = self.q.sample_action(s, epsilon)
                    q = self.q(s).squeeze().numpy()
                # Apply the action in the environment
                obs, reward, done, _ = env.step(action)
                # Add data to the local buffer
                self.local_buffer.put((state, action, reward, gamma, q),
                                      done=done)
                state = get_state(obs)

                score += reward
                episode_score += reward

                # Send a batch of size B to the experience replay memory every B frames
                if self.local_buffer.nstep_size() >= B:
                    batch = self.local_buffer.sample(B)
                    p = self.compute_priorities(batch)
                    # async: add the batch to the replay buffer
                    self.shared_memory.put.remote(batch, p)

                # Async call to get the newest parameters
                if total_frames % self.update_parameters_interval == 0:
                    #print("Actor ", self.id, "Getting latest parameters from server.", total_frames)
                    updated_parameters = ray.get(
                        self.shared_state.get.remote())['q']
                    self.q.load_state_dict(
                        updated_parameters)  # update the network parameters

                # Reset environment for the next game
                if done:
                    best_episode_score = max(best_episode_score, episode_score)
                    state = get_state(env.reset())
                    out = " Actor_id: {}, n_episode : {}, Total Frames : {}, Average Score : {:.1f}, Episode Score : {:.1f}, Best Score : {:.1f}, eps : {:.1f}%".format(
                        self.id, episode, total_frames, score / episode,
                        episode_score, best_episode_score, epsilon * 100)
                    print(out)
                total_frames += 1
Ejemplo n.º 2
0
def test_get_userinfo_from_token(app,
                                 example_keycloak_token,
                                 example_keycloak_userinfo,
                                 example_keycloak_realm_info):
    """Test the "id_token" extraction mechanism from Keycloak's response."""
    mock_keycloak(app.config,
                  example_keycloak_token,
                  dict(),
                  example_keycloak_realm_info)

    token = example_keycloak_token["id_token"]
    options = {"verify_signature": False}
    expected_result = jwt.decode(token, verify=False, options=options)

    with app.test_client() as c:
        # ensure that remote apps have been loaded (before first request)
        c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))
        remote = app.extensions["oauthlib.client"].remote_apps["keycloak"]

        # the OAuthClient has to get its token from this call
        c.get(
            url_for(
                "invenio_oauthclient.authorized", remote_app="keycloak",
                code="test", state=get_state("keycloak")
            )
        )

        user_info = get_user_info(remote, example_keycloak_token,
                                  fallback_to_endpoint=False,
                                  options={"verify_exp": False})

        assert user_info is not None
        assert user_info == expected_result
Ejemplo n.º 3
0
    def _init():
        ioc = app_rest.extensions['oauthlib.client']

        # setup the user account via cern_openid
        with app_rest.test_client() as c:
            # Ensure remote apps have been loaded (due to before first request)
            resp = c.get(
                url_for('invenio_oauthclient.rest_login',
                        remote_app='cern_openid'))
            assert resp.status_code == 302

            example_response, example_token, example_account_info = \
                example_cern_openid_rest

            mock_response(app_rest.extensions['oauthlib.client'],
                          'cern_openid', example_token)
            mock_remote_get(ioc, 'cern_openid', example_response)

            resp = c.get(
                url_for('invenio_oauthclient.rest_authorized',
                        remote_app='cern_openid',
                        code='test',
                        state=get_state('cern_openid')))
            assert resp.status_code == 302
            expected_url_args = {
                "message": "Successfully authorized.",
                "code": 200,
            }
            check_response_redirect_url_args(resp, expected_url_args)

            assert len(g.identity.provides) == 3
Ejemplo n.º 4
0
def test_account_setup(app, example_cern, models_fixture):
    """Test account setup after login."""
    with app.test_client() as c:
        ioc = app.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(url_for('invenio_oauthclient.login', remote_app='cern'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = example_cern

        mock_response(app.extensions['oauthlib.client'], 'cern', example_token)
        mock_remote_get(ioc, 'cern', example_response)

        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='cern',
                    code='test',
                    state=get_state('cern')))
        assert resp.status_code == 302
        assert resp.location == ('http://localhost/account/settings/'
                                 'linkedaccounts/')
        assert len(g.identity.provides) == 7

    datastore = app.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    assert user

    with app.test_request_context():
        resp = disconnect_handler(ioc.remote_apps['cern'])
        assert resp.status_code >= 300

        login_user(user)
        assert len(g.identity.provides) == 7
        disconnect_handler(ioc.remote_apps['cern'])
Ejemplo n.º 5
0
def test_get_userinfo_from_endpoint(app,
                                    example_keycloak_token,
                                    example_keycloak_userinfo,
                                    example_keycloak_realm_info):
    """Test the "/userinfo" mechanism when the "id_token" mechanism fails."""
    mock_keycloak(app.config,
                  example_keycloak_token,
                  example_keycloak_userinfo.data,
                  example_keycloak_realm_info)

    with app.test_client() as c:
        # ensure that remote apps have been loaded (before first request)
        c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))

        remote = app.extensions["oauthlib.client"].remote_apps["keycloak"]

        # the OAuthClient has to get its token from this call
        c.get(
            url_for(
                "invenio_oauthclient.authorized", remote_app="keycloak",
                code="test", state=get_state("keycloak")
            )
        )

        # force the endpoint mechanism by not providing a token
        user_info = get_user_info(remote, None)

        assert user_info is not None
        assert user_info == example_keycloak_userinfo.data
def test_account_setup(app, example_cern, models_fixture):
    """Test account setup after login."""
    with app.test_client() as c:
        ioc = app.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(url_for('invenio_oauthclient.login', remote_app='cern'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = example_cern

        mock_response(app.extensions['oauthlib.client'], 'cern',
                      example_token)
        mock_remote_get(ioc, 'cern', example_response)

        resp = c.get(url_for(
            'invenio_oauthclient.authorized',
            remote_app='cern', code='test',
            state=get_state('cern')))
        assert resp.status_code == 302
        assert resp.location == ('http://localhost/account/settings/'
                                 'linkedaccounts/')
        assert len(g.identity.provides) == 7

    datastore = app.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    assert user

    with app.test_request_context():
        resp = disconnect_handler(ioc.remote_apps['cern'])
        assert resp.status_code >= 300

        login_user(user)
        assert len(g.identity.provides) == 7
        disconnect_handler(ioc.remote_apps['cern'])
def test_account_setup(app_rest, example_cern_openid_rest, models_fixture):
    """Test account setup after login."""
    with app_rest.test_client() as c:
        ioc = app_rest.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(
            url_for('invenio_oauthclient.rest_login',
                    remote_app='cern_openid'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = \
            example_cern_openid_rest

        mock_response(app_rest.extensions['oauthlib.client'], 'cern_openid',
                      example_token)
        mock_remote_get(ioc, 'cern_openid', example_response)

        resp = c.get(
            url_for('invenio_oauthclient.rest_authorized',
                    remote_app='cern_openid',
                    code='test',
                    state=get_state('cern_openid')))
        assert resp.status_code == 302
        expected_url_args = {
            "message": "Successfully authorized.",
            "code": 200,
        }
        check_response_redirect_url_args(resp, expected_url_args)

        assert len(g.identity.provides) == 3

    datastore = app_rest.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    user.password = hash_password("1234")
    assert user

    with app_rest.test_request_context():
        resp = disconnect_rest_handler(ioc.remote_apps['cern_openid'])
        assert resp.status_code >= 300

        # simulate login (account_info fetch)
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern_openid']

        login_user(user)
        assert len(g.identity.provides) == 3

        logout_user()
        assert len(g.identity.provides) == 1
        assert "cern_resource" not in session
        assert OAUTHCLIENT_CERN_OPENID_SESSION_KEY not in session

        # Login again to test the disconnect handler
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern_openid']
        login_user(user)
        assert len(g.identity.provides) == 3

        disconnect_rest_handler(ioc.remote_apps['cern_openid'])
def test_authorized_reject(app):
    """Test a rejected request."""
    with app.test_client() as c:
        c.get(url_for('invenio_oauthclient.login', remote_app='cern'))
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='cern', error='access_denied',
                    error_description='User denied access',
                    state=get_state('cern')))
        assert resp.status_code in (301, 302)
        assert resp.location == 'http://localhost/'
        # Check message flash
        assert session['_flashes'][0][0] == 'info'
def test_authorized_reject(app):
    """Test a rejected request."""
    with app.test_client() as c:
        c.get(url_for('invenio_oauthclient.login', remote_app='cern_openid'))
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='cern_openid', error='access_denied',
                    error_description='User denied access',
                    state=get_state('cern_openid')))
        assert resp.status_code in (301, 302)
        assert resp.location == 'http://localhost/'
        # Check message flash
        assert session['_flashes'][0][0] == 'info'
Ejemplo n.º 10
0
def test_account_setup(app, example_cern, models_fixture):
    """Test account setup after login."""
    with app.test_client() as c:
        ioc = app.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(url_for('invenio_oauthclient.login', remote_app='cern'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = example_cern

        mock_response(app.extensions['oauthlib.client'], 'cern', example_token)
        mock_remote_get(ioc, 'cern', example_response)

        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='cern',
                    code='test',
                    state=get_state('cern')))
        assert resp.status_code == 302
        assert resp.location == ('http://localhost/account/settings/'
                                 'linkedaccounts/')
        assert len(g.identity.provides) == 7

    datastore = app.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    user.password = hash_password("1234")
    assert user

    with app.test_request_context():
        resp = disconnect_handler(ioc.remote_apps['cern'])
        assert resp.status_code >= 300

        # simulate login (account_info fetch)
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern']

        login_user(user)
        assert len(g.identity.provides) == 7

        logout_user()
        assert len(g.identity.provides) == 1
        assert "cern_resource" not in session
        assert OAUTHCLIENT_CERN_SESSION_KEY not in session

        # Login again to test the disconnect handler
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern']
        login_user(user)
        assert len(g.identity.provides) == 7

        disconnect_handler(ioc.remote_apps['cern'])
def test_authorized_reject(app_rest):
    """Test a rejected request."""
    with app_rest.test_client() as c:
        c.get(url_for('invenio_oauthclient.rest_login', remote_app='cern'))
        resp = c.get(
            url_for('invenio_oauthclient.rest_authorized',
                    remote_app='cern', error='access_denied',
                    error_description='User denied access',
                    state=get_state('cern')))
        assert resp.status_code in (301, 302)
        expected_url_args = {
            "message": "You rejected the authentication request.",
            "code": 400,
        }
        check_response_redirect_url_args(resp, expected_url_args)
def test_authorized_reject(app, example_keycloak_token):
    """Test a rejected request."""
    with app.test_client() as c:
        c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))

        resp = c.get(
            url_for("invenio_oauthclient.authorized",
                    remote_app="keycloak",
                    error="access_denied",
                    error_description="User denied access",
                    state=get_state("keycloak")))

        assert resp.status_code in (301, 302)
        assert resp.location == "http://localhost/"

        # check message flash
        assert session["_flashes"][0][0] == "info"
def test_account_setup(app_rest, example_cern_openid_rest, models_fixture):
    """Test account setup after login."""
    with app_rest.test_client() as c:
        ioc = app_rest.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(
            url_for('invenio_oauthclient.rest_login',
                    remote_app='cern_openid'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = \
            example_cern_openid_rest

        mock_response(app_rest.extensions['oauthlib.client'], 'cern_openid',
                      example_token)
        mock_remote_get(ioc, 'cern_openid', example_response)

        resp = c.get(
            url_for('invenio_oauthclient.rest_authorized',
                    remote_app='cern_openid',
                    code='test',
                    state=get_state('cern_openid')))
        assert resp.status_code == 302
        expected_url_args = {
            "message": "Successfully authorized.",
            "code": 200,
        }
        check_response_redirect_url_args(resp, expected_url_args)

        assert len(g.identity.provides) == 3

    datastore = app_rest.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    assert user

    with app_rest.test_request_context():
        resp = disconnect_rest_handler(ioc.remote_apps['cern_openid'])
        assert resp.status_code >= 300

        login_user(user)
        assert len(g.identity.provides) == 3
        disconnect_rest_handler(ioc.remote_apps['cern_openid'])
Ejemplo n.º 14
0
import cv2

import agent as agent
import helpers

# Valid actions are 0: NoOp, 1: Fire, 2: Left, 3: Right
ACTIONS = [0, 1, 2, 3]

env = gym.make('Breakout-v0')
env.reset()
#env = wrappers.Monitor(env, 'Breakout-v0', force=True)
a = agent.Agent(env.observation_space.shape, env.action_space.n, 0.3)
for i_episode in range(100):
    observation = env.reset()
    observation, reward, done = helpers.get_state(env, 0)
    episode_reward = 0
    while (done != True):
        #env.render()
        #action = env.action_space.sample()
        with autograd.record():
            # Select action
            action1, max_ind1, qval1 = a.action_nd(observation)

            # Do action and observe new state and reward
            observation, reward, done = helpers.get_state(env, max_ind1)
            episode_reward += reward
            if done:
                print("Episode finished after {} episodes".format(i_episode +
                                                                  1))
                #print('Gradient b1: {}'.format(a.b1))
Ejemplo n.º 15
0
def test_account_setup(app_rest, example_cern, models_fixture):
    """Test account setup after login."""
    with app_rest.test_client() as c:
        ioc = app_rest.extensions['oauthlib.client']

        # Ensure remote apps have been loaded (due to before first request)
        resp = c.get(
            url_for('invenio_oauthclient.rest_login', remote_app='cern'))
        assert resp.status_code == 302

        example_response, example_token, example_account_info = example_cern

        mock_response(app_rest.extensions['oauthlib.client'], 'cern',
                      example_token)
        mock_remote_get(ioc, 'cern', example_response)

        resp = c.get(
            url_for('invenio_oauthclient.rest_authorized',
                    remote_app='cern',
                    code='test',
                    state=get_state('cern')))
        assert resp.status_code == 302
        expected_url_args = {
            "message": "Successfully authorized.",
            "code": 200,
        }
        check_response_redirect_url_args(resp, expected_url_args)
        assert len(g.identity.provides) == 7

    datastore = app_rest.extensions['invenio-accounts'].datastore
    user = datastore.find_user(email='*****@*****.**')
    user.password = hash_password("1234")
    assert user

    with app_rest.test_request_context():
        resp = disconnect_rest_handler(ioc.remote_apps['cern'])
        assert resp.status_code >= 300

        # simulate login (account_info fetch)
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern']

        login_user(user)
        assert isinstance(g.identity, Identity)
        assert g.identity.provides == set([
            UserNeed(4),
            UserNeed('*****@*****.**'),
            RoleNeed('*****@*****.**'),
            RoleNeed('*****@*****.**'),
            RoleNeed('*****@*****.**'),
            RoleNeed('*****@*****.**'),
            RoleNeed('*****@*****.**'),
        ])

        logout_user()
        assert isinstance(g.identity, AnonymousIdentity)
        # NOTE: Wrong role, g.identity.provides = {Need(['id', 4])} read more
        # https://github.com/inveniosoftware/invenio-access/blob/e28e76d5361a29202b94d498f1968454c24c5c80/tests/test_loaders.py#L47
        assert len(g.identity.provides) == 1

        assert "cern_resource" not in session
        assert OAUTHCLIENT_CERN_SESSION_KEY not in session

        # Login again to test the disconnect handler
        g.oauth_logged_in_with_remote = ioc.remote_apps['cern']
        login_user(user)
        assert isinstance(g.identity, Identity)
        assert len(g.identity.provides) == 7

        disconnect_rest_handler(ioc.remote_apps['cern'])
Ejemplo n.º 16
0
    def run(self, num_episodes):
        self.q_target.load_state_dict(
            self.q.state_dict())  # Load policy weights into target network
        self.optimizer = optim.Adam(self.q.parameters(), lr=self.learning_rate)

        if self.saved_model:
            self.q.load_state_dict(
                torch.load(saved_model))  # Load pretrained model

        self.beginLogging()
        #watcher = tw.Watcher()
        env = self.env
        best_episode_score = float('-Inf')
        score = 0.0
        total_frames = 0
        state = get_state(env.reset())  # Start first game
        for episode in tqdm(
                range(self.start_episode, self.start_episode + num_episodes)):
            # anneal 100% to 1% over training
            epsilon = self.epsilon_decay(total_frames)
            episode_score = 0
            done = False
            while not done:
                action = self.q.sample_action(
                    torch.Tensor(state).unsqueeze(0).to(device), epsilon)

                obs, reward, done, info = env.step(action)

                next_state = get_state(obs)

                done_mask = 0.0 if done else 1.0
                self.memory.put((state, action, reward, next_state, done_mask))
                state = next_state

                score += reward
                episode_score += reward

                if total_frames > self.training_frame_start:
                    self.train()

                # Copy policy weights to target
                if total_frames % self.update_target_interval == 0:
                    self.q_target.load_state_dict(self.q.state_dict())
                # Save policy weights
                if total_frames % self.save_interval == 0:
                    torch.save(
                        self.q.state_dict(),
                        os.path.join(self.save_location,
                                     'policy_%s.pt' % episode))
                # Reset environment for the next game
                if done:
                    state = get_state(env.reset())
                total_frames += 1

            best_episode_score = max(best_episode_score, episode_score)
            # Print updates every episode
            out = "n_episode : {}, Total Frames : {}, Average Score : {:.1f}, Episode Score : {:.1f}, Best Score : {:.1f}, n_buffer : {}, eps : {:.1f}%".format(
                episode, total_frames, score / episode, episode_score,
                best_episode_score, len(self.memory), epsilon * 100)
            print(out)
            self.log(out)

            # Microsoft Tensorwatch Watcher for Visualizing Training
            #watcher.observe(
            #    episode = episode,
            #    episode_score = episode_score,
            #    total_score = score,
            #    buffer_size = self.memory.size(),
            #    epsilon = epsilon,
            #    frames = total_frames,
            #)

        # save final model weights
        torch.save(self.q.state_dict(),
                   os.path.join(self.save_location, 'policy_final.pt'))
def test_authorized_already_authenticated(app, models_fixture, example_orcid,
                                          orcid_bio):
    """Test authorized callback with sign-up."""
    datastore = app.extensions['invenio-accounts'].datastore
    login_manager = app.login_manager

    example_data, example_account_info = example_orcid
    existing_email = '*****@*****.**'
    user = datastore.find_user(email=existing_email)

    @login_manager.user_loader
    def load_user(user_id):
        return user

    @app.route('/foo_login')
    def login():
        login_user(user)
        return 'Logged In'

    with app.test_client() as client:

        # make a fake login (using my login function)
        client.get('/foo_login', follow_redirects=True)

        # Ensure remote apps have been loaded (due to before first
        # request)
        client.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        # Mock access token request
        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # Mock request to ORCID to get user bio.
        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            'https://pub.orcid.org/v1.2/{0}/orcid-bio'.format(
                example_data['orcid']),
            body=orcid_bio,
            content_type='application/orcid+json; qs=2;charset=UTF-8',
        )

        # User then goes to 'Linked accounts' and clicks 'Connect'
        resp = client.get(
            url_for('invenio_oauthclient.login',
                    remote_app='orcid',
                    next='/someurl/'))
        assert resp.status_code == 302

        # User authorized the requests and is redirected back
        resp = client.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid',
                    code='test',
                    state=get_state('orcid')))
        httpretty.disable()

        # Assert database state (Sign-up complete)
        u = User.query.filter_by(email=existing_email).one()
        UserIdentity.query.filter_by(method='orcid',
                                     id_user=u.id,
                                     id=example_data['orcid']).one()
        # FIXME see contrib/orcid.py line 167
        # assert u.given_names == 'Josiah'
        # assert u.family_name == 'Carberry'

        # Disconnect link
        resp = client.get(
            url_for('invenio_oauthclient.disconnect', remote_app='orcid'))
        assert resp.status_code == 302

        # User exists
        u = User.query.filter_by(email=existing_email).one()
        # UserIdentity removed.
        assert 0 == UserIdentity.query.filter_by(
            method='orcid', id_user=u.id, id=example_data['orcid']).count()
Ejemplo n.º 18
0
def test_authorized_already_authenticated(app,
                                          models_fixture,
                                          example_keycloak_token,
                                          example_keycloak_userinfo,
                                          example_keycloak_realm_info):
    """Test authorized callback with sign-in."""
    datastore = app.extensions["invenio-accounts"].datastore
    login_manager = app.login_manager

    example_keycloak = example_keycloak_userinfo.data
    existing_mail = "*****@*****.**"
    user = datastore.find_user(email=existing_mail)

    @login_manager.user_loader
    def load_user(user_id):
        return user

    @app.route("/logmein")
    def login():
        login_user(user)
        return "Logged in"

    with app.test_client() as c:
        c.get("/logmein", follow_redirects=True)

        # ensure that remote apps have been loaded (before first request)
        c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))

        # mock a running keycloak instance
        mock_keycloak(app.config,
                      example_keycloak_token,
                      example_keycloak,
                      example_keycloak_realm_info)

        # user goes to 'linked accounts' and clicks 'connect' with Keycloak
        resp = c.get(
            url_for("invenio_oauthclient.login", remote_app="keycloak",
                    next="/someurl/")
        )

        assert resp.status_code == 302

        # the user logged in to Keycloak and authorized the request
        resp = c.get(
            url_for(
                "invenio_oauthclient.authorized", remote_app="keycloak",
                code="test", state=get_state("keycloak")
            )
        )

        # check if the Keycloak account has been linked to the user
        u = User.query.filter_by(email=existing_mail).one()
        UserIdentity.query.filter_by(
            method="keycloak",
            id_user=u.id,
            id=example_keycloak["sub"]
        ).one()

        # let the user hit the 'disconnect' button
        resp = c.get(
            url_for("invenio_oauthclient.disconnect", remote_app="keycloak")
        )
        assert resp.status_code == 302

        # check that the user still exists,
        # but the Keycloak account has been unlinked
        u = User.query.filter_by(email=existing_mail).one()
        count = UserIdentity.query.filter_by(
            method="keycloak",
            id_user=u.id,
            id=example_keycloak["sub"]
        ).count()
        assert count == 0
def test_authorized_signup(userprofiles_app, example_orcid, orcid_bio):
    """Test authorized callback with sign-up."""
    app = userprofiles_app
    example_data, example_account_info = example_orcid
    example_email = '*****@*****.**'

    with app.test_client() as c:
        # Ensure remote apps have been loaded (due to before first
        # request)
        c.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # User authorized the requests and is redirect back
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid', code='test',
                    state=get_state('orcid')))
        assert resp.status_code == 302
        assert resp.location == (
            'http://localhost' +
            url_for('invenio_oauthclient.signup', remote_app='orcid')
        )

        # User load sign-up page.
        resp = c.get(url_for('invenio_oauthclient.signup', remote_app='orcid'))
        assert resp.status_code == 200

        account_info = session[token_session_key('orcid') + '_account_info']
        data = {
            'email': example_email,
            'password': '******',
            'profile.username': '******',
            'profile.full_name': account_info['user']['profile']['full_name'],
        }

        # Mock request to ORCID to get user bio.
        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            'http://orcid.org/{0}/orcid-bio'.format(example_data['orcid']),
            body=orcid_bio,
            content_type='application/orcid+json; qs=2;charset=UTF-8',
        )

        # User fills form to register
        resp = c.post(
            url_for('invenio_oauthclient.signup', remote_app='orcid'),
            data=data,
        )
        assert resp.status_code == 302
        httpretty.disable()

        # Assert database state (Sign-up complete)
        user = User.query.filter_by(email=example_email).one()
        UserIdentity.query.filter_by(
            method='orcid', id_user=user.id,
            id=example_data['orcid']
        ).one()
        # FIXME see contrib/orcid.py line 167
        assert user.profile.full_name == 'Josiah Carberry'
        #  assert user.given_names == 'Josiah'
        #  assert user.family_name == 'Carberry'
        # check that the user's email is not yet validated
        assert user.active
        # check that the validation email has been sent
        #  assert hasattr(locmem, 'outbox') and len(locmem.outbox) == 1

        # Disconnect link
        resp = c.get(
            url_for('invenio_oauthclient.disconnect', remote_app='orcid'))
        assert resp.status_code == 302

        # User exists
        user = User.query.filter_by(email=example_email).one()
        # UserIdentity removed.
        assert 0 == UserIdentity.query.filter_by(
            method='orcid', id_user=user.id,
            id=example_data['orcid']
        ).count()
        assert RemoteAccount.query.filter_by(user_id=user.id).count() == 0
        assert RemoteToken.query.count() == 0
def test_authorized_already_authenticated(models_fixture, example_orcid,
                                          orcid_bio):
    """Test authorized callback with sign-up."""
    app = models_fixture

    datastore = app.extensions['invenio-accounts'].datastore
    login_manager = app.login_manager

    example_data, example_account_info = example_orcid
    existing_email = '*****@*****.**'
    user = datastore.find_user(email=existing_email)

    @login_manager.user_loader
    def load_user(user_id):
        return user

    @app.route('/foo_login')
    def login():
        login_user(user)
        return 'Logged In'

    with app.test_client() as client:

        # make a fake login (using my login function)
        client.get('/foo_login', follow_redirects=True)

        # Ensure remote apps have been loaded (due to before first
        # request)
        client.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        # Mock access token request
        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # Mock request to ORCID to get user bio.
        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            'https://pub.orcid.org/v1.2/{0}/orcid-bio'.format(
                example_data['orcid']),
            body=orcid_bio,
            content_type='application/orcid+json; qs=2;charset=UTF-8',
        )

        # User then goes to 'Linked accounts' and clicks 'Connect'
        resp = client.get(
            url_for('invenio_oauthclient.login', remote_app='orcid',
                    next='/someurl/')
        )
        assert resp.status_code == 302

        # User authorized the requests and is redirected back
        resp = client.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid', code='test',
                    state=get_state('orcid')))
        httpretty.disable()

        # Assert database state (Sign-up complete)
        u = User.query.filter_by(email=existing_email).one()
        UserIdentity.query.filter_by(
            method='orcid', id_user=u.id,
            id=example_data['orcid']
        ).one()
        # FIXME see contrib/orcid.py line 167
        # assert u.given_names == 'Josiah'
        # assert u.family_name == 'Carberry'

        # Disconnect link
        resp = client.get(
            url_for('invenio_oauthclient.disconnect', remote_app='orcid'))
        assert resp.status_code == 302

        # User exists
        u = User.query.filter_by(email=existing_email).one()
        # UserIdentity removed.
        assert 0 == UserIdentity.query.filter_by(
            method='orcid', id_user=u.id,
            id=example_data['orcid']
        ).count()
def test_authorized_signup(app_with_userprofiles, example_orcid, orcid_bio):
    """Test authorized callback with sign-up."""
    app = app_with_userprofiles
    example_data, example_account_info = example_orcid
    example_email = '*****@*****.**'

    with app.test_client() as c:
        # Ensure remote apps have been loaded (due to before first
        # request)
        c.get(url_for('invenio_oauthclient.login', remote_app='orcid'))

        mock_response(app.extensions['oauthlib.client'], 'orcid', example_data)

        # User authorized the requests and is redirect back
        resp = c.get(
            url_for('invenio_oauthclient.authorized',
                    remote_app='orcid',
                    code='test',
                    state=get_state('orcid')))
        assert resp.status_code == 302
        assert resp.location == (
            'http://localhost' +
            url_for('invenio_oauthclient.signup', remote_app='orcid'))

        # User load sign-up page.
        resp = c.get(url_for('invenio_oauthclient.signup', remote_app='orcid'))
        assert resp.status_code == 200

        account_info = session[token_session_key('orcid') + '_account_info']
        data = {
            'email': example_email,
            'password': '******',
            'profile.username': '******',
            'profile.full_name': account_info['user']['profile']['full_name'],
        }

        # Mock request to ORCID to get user bio.
        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            'http://orcid.org/{0}/orcid-bio'.format(example_data['orcid']),
            body=orcid_bio,
            content_type='application/orcid+json; qs=2;charset=UTF-8',
        )

        # User fills form to register
        resp = c.post(
            url_for('invenio_oauthclient.signup', remote_app='orcid'),
            data=data,
        )
        assert resp.status_code == 302
        httpretty.disable()

        # Assert database state (Sign-up complete)
        user = User.query.filter_by(email=example_email).one()
        UserIdentity.query.filter_by(method='orcid',
                                     id_user=user.id,
                                     id=example_data['orcid']).one()
        # FIXME see contrib/orcid.py line 167
        assert user.profile.full_name == 'Josiah Carberry'
        #  assert user.given_names == 'Josiah'
        #  assert user.family_name == 'Carberry'
        # check that the user's email is not yet validated
        assert user.active
        # check that the validation email has been sent
        #  assert hasattr(locmem, 'outbox') and len(locmem.outbox) == 1

        # Disconnect link
        # should not work, because it's the user's only means of login
        resp = c.get(
            url_for('invenio_oauthclient.disconnect', remote_app='orcid'))
        assert resp.status_code == 400

        user = User.query.filter_by(email=example_email).one()
        assert 1 == UserIdentity.query.filter_by(
            method='orcid', id_user=user.id, id=example_data['orcid']).count()

        # set a password for the user
        user.password = hash_password("1234")
        db.session.commit()

        # Disconnect again
        resp = c.get(
            url_for('invenio_oauthclient.disconnect', remote_app='orcid'))
        assert resp.status_code == 302

        # User exists
        user = User.query.filter_by(email=example_email).one()
        # UserIdentity removed.
        assert 0 == UserIdentity.query.filter_by(
            method='orcid', id_user=user.id, id=example_data['orcid']).count()
        assert RemoteAccount.query.filter_by(user_id=user.id).count() == 0
        assert RemoteToken.query.count() == 0
Ejemplo n.º 22
0
def test_authorized_signup_valid_user(app_with_userprofiles,
                                      example_keycloak_token,
                                      example_keycloak_userinfo,
                                      example_keycloak_realm_info):
    """Test authorized callback with sign-up."""
    app = app_with_userprofiles
    example_keycloak = example_keycloak_userinfo.data

    with app.test_client() as c:
        # ensure that remote_apps have been initialized (before first request)
        resp = c.get(
            url_for("invenio_oauthclient.login", remote_app="keycloak")
        )
        assert resp.status_code == 302

        # mock a running keycloak instance
        mock_keycloak(app.config,
                      example_keycloak_token,
                      example_keycloak,
                      example_keycloak_realm_info)

        # user authorized the request and is redirected back
        resp = c.get(
            url_for(
                "invenio_oauthclient.authorized", remote_app="keycloak",
                code="test", state=get_state("keycloak")
            )
        )

        # note: because we provided an e-mail address in 'info_handler',
        #       the user does not need to sign up
        assert resp.status_code == 302
        assert resp.location == ("http://localhost/"
                                 "account/settings/linkedaccounts/")

        # check that the user exists
        user = User.query.filter_by(email=example_keycloak["email"]).one()
        assert user is not None
        assert user.email == example_keycloak["email"]
        assert user.profile.full_name == "Max Moser"
        assert user.active

        # check that the user has a linked Keycloak account
        uid = UserIdentity.query.filter_by(
            method="keycloak",
            id_user=user.id,
            id=example_keycloak["sub"]
        ).one()
        assert uid.user is user

        # disconnect the Keycloak account again
        resp = c.get(
            url_for("invenio_oauthclient.disconnect", remote_app="keycloak")
        )

        assert resp.status_code == 302

        # check that the user still exists
        user = User.query.filter_by(email=example_keycloak["email"]).one()
        assert user is not None

        # check that the Keycloak account has been unlinked
        count = UserIdentity.query.filter_by(
            method="keycloak",
            id_user=user.id,
            id=example_keycloak["sub"]
        ).count()
        assert count == 0
Ejemplo n.º 23
0
import gym
import time

import helpers
import agent

env = gym.make("Breakout-v0")
env.reset()
a = agent.Agent(env.observation_space.shape, env.action_space.n)

s, r, done = helpers.get_state(env, 0)
print("Shape {}".format(s.shape))

res = a.net(s, debug=True)

print("Shape {}, Value {}".format(res.shape, res))

for t in range(10):
    env.render()
    o, r, d, i = env.step(0)
    time.sleep(0.3)
env.close()


Ejemplo n.º 24
0
 def test_get_state(self):
     result = get_state(self.candidate)
     self.assertIsInstance(result, list)
     self.assertListEqual(result, self.powerhouse1.state + self.powerhouse2.state)