Ejemplo n.º 1
0
def authorized_client(requests_mock, api_key_file, fs):
    fs.create_dir(os.path.dirname(api_key_file))
    assert not os.path.exists(api_key_file), "api key file shouldn't exist yet"
    json_data = {
        "id": 1234,
        "api_key": test_api_key,
        "username": "******",
        "ssh_key": "ssh-rsa AAAA..."
    }
    requests_mock.put(api_base_url + "/users/current/", json=json_data)
    new_client = VastClient(api_key_file=api_key_file)
    assert type(new_client) is VastClient, "Should be a VastClient object."
    assert new_client.api_key is None, "Shouldn't have an api_key yet."
    retVal = new_client.authenticate('john_doe', 'abc123')
    assert retVal is new_client, "Client.authenticate should return self."
    assert new_client.api_key == test_api_key
    assert requests_mock.last_request.json() == {
        'username': '******',
        'password': '******'
    }
    check_attrs(new_client, json_data)
    assert os.path.exists(api_key_file), "Should have created api key file."
    with open(api_key_file) as f:
        assert f.read(
        ) == new_client.api_key, "Saved API key should match client.api_key."
    return new_client
Ejemplo n.º 2
0
def test_update_returns_nothing_when_request_fails(requests_mock, monkeypatch):
    monkeypatch.setenv('GITHUB_REPOSITORY', 'herp/derp')
    mock = MockPost()
    requests_mock.put(
        f"https://dev.to/api/articles/{silo_id_for(mock, dev.SILO_NAME)}",
        status_code=requests.codes.unprocessable_entity,
        json={"error": "you made an unintelligble request"})
    assert not dev._update(mock, api_key='fake_api_key')
Ejemplo n.º 3
0
def test_stop_instance(requests_mock, instance):
    requests_mock.put(api_base_url + "/instances/%s/?api_key=%s" %
                      (instance.id, test_api_key),
                      json={"success": True})
    instance.stop()
    #log_request(requests_mock.last_request)
    assert requests_mock.last_request.method == 'PUT'
    assert requests_mock.last_request.body == b'{"state": "stopped"}'
Ejemplo n.º 4
0
def test_update_returns_something_on_success(requests_mock, monkeypatch):
    monkeypatch.setenv('GITHUB_REPOSITORY', 'herp/derp')
    mock = MockPost()
    mock_id = silo_id_for(mock, dev.SILO_NAME)
    requests_mock.put(f"https://dev.to/api/articles/{mock_id}",
                      status_code=requests.codes.ok,
                      json={
                          'type_of': 'article',
                          'id': mock_id,
                          'url': 'https://fake.url/for-this-post'
                      })
    assert dev._update(mock, api_key='fake_api_key')
Ejemplo n.º 5
0
    def test_upload(self, requests_mock, monkeypatch):

        session = SbSessionEx()

        itemId = "5f9982f1d34e9cff790bd2fe"
        filename = "sample_error.png"
        file_path = "tests/resources/sample_error.png"
        GRAPHQL_URL = "https://api-beta.staging.sciencebase.gov/graphql"

        upload_url_repsonse = """
            {
                "data": {
                    "createUploadSession": { 
                        "uploads": [
                            {
                                "url": "https://LONG_URL_WITH_PRESIGNED_URL"
                            }
                        ]
                    }
                }
            }
        """

        upload_response = """
            {
                "data": {
                    "item": { "id": "5f9982f1d34e9cff790bd2fe", "title": "himal-test" },
                    "createUploadSession": {
                    "uploads": [
                        {
                        "name": "sample_error15.png",
                        "url": "https://dev-is-sb-beta-content.s3.us-west-2.amazonaws.com/5f9982f1d34e9cff790bd2fe/sample_error15.png?Content-Type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIARGHURBUVAHB66GAH%2F20201104%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20201104T165505Z&X-Amz-Expires=900&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEID%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJGMEQCIAkbmAecgCiYj6SyH3mt4ELsf4BlTOXNaxIOk%2BjJtGmbAiAtXMRpH0iu3b%2BWZiCxDqkiAGV0Zw8alrFdIVOlb4eQRSq2AwjZ%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAIaDDA4MjExNzIwMTE5NCIMWH8ITDo7PNTAXJ2ZKooD8ae44Sse8ZXiska%2FQQadDUicK6y5JOFLT4f3xGhecvzPYSDcaH3cLz6vt7P6n49DyAzkkfbnw%2F2tFlvs5f8cOh1%2Bu1%2BBmDnHPFRxHhQufIykNQdDgKQzgTVKhuoQLYWXKG1Nha6PFP4hQRd88DZiFDMDP67qYByqdjvJOGHrtXO9ornSq5JjxxSjaJ0EUjCldmvxshnxCXFhaxu8c5Al5IoMspO2IligOeYl2tkzwDSXUW9oKHke0IgYdaN30i%2BbAiusMzn6dgiahIpozesptVsOmgdcRgOuFUR5lJCTuhv12uwbQfr9BmKR6pt4RQWHDQspgCiHzphS9olba4TkyYJ3B%2FuI%2BK6%2FLSWYBNyKvqJdcg61L8zh31dyBskhPgbBrbPMf9PlLEHkFBJesIMamf1%2B2IA1RYr3AUAt%2BcGk6hd%2BZ%2FX%2BO%2ByhlFExIrH9NtjGwqRlAD59Pooit82rYicXhd9rxKlqBYaCnl9Owj0bWNkRGycrYzDIlnP4taVY2mdDVCTp4%2BPoq4ryOTCJp4v9BTrsAYTK1WAqtLIP5Mf86SvlRqNb%2FLkJNI%2FNeZR7%2FHlVpb2sfnJMrcolWctU%2BKj906K4SNY6%2B752IDCiln%2BIkk77Z4PyFXibHx9%2Bdo9SV9fd6jDdMRy1OfZ98RVeP0dJ1q5gFGKpNMWBY8xR7lN%2BPBYStg5ffwy%2BiOH8I3yrMr1R9Nw66L5p0vfLGmQ%2FFz7AXmM7IKSQjAFnWy3OwEl36yc2QKVDsobTJLoFjSG%2Fgn925JSIYa4NjExERdLzx54jDpnvneQPBiip3ERvUmm4nk%2BaEQ7J0POwA7t6DR6L6hPIUscZcMOR42V7AEExI918&X-Amz-Signature=c1bf1c669cedbd5a4948b00dae65dd33a16db0be7cf6a67899976cb6a669f8b3&X-Amz-SignedHeaders=host%3Bx-amz-acl&x-amz-acl=private"
                        }
                    ]
                    }
                }
            }
        """

        requests_mock.post(GRAPHQL_URL,
                           json=json.loads(upload_url_repsonse),
                           status_code=200)
        requests_mock.put(
            "https://LONG_URL_WITH_PRESIGNED_URL",
            json=json.loads(upload_response),
            status_code=200,
        )

        client.upload_test_with_graphql_upload_session(itemId,
                                                       filename,
                                                       file_path,
                                                       session=session)
Ejemplo n.º 6
0
def test_bad_login(requests_mock, fs):
    requests_mock.put(api_base_url + "/users/current/",
                      status_code=401,
                      reason='Unauthorized')
    client = VastClient(api_key_file=None)
    assert client.api_key_file == None
    with pytest.raises(Unauthorized):
        retVal = client.authenticate('aFakeUser', 'badPassword')
        assert retVal.api_key is None
    assert requests_mock.last_request.json() == {
        'username': '******',
        'password': '******'
    }
Ejemplo n.º 7
0
    def init_mock_dataservice(requests_mock,
                              user_json=mockuser,
                              runs_json=mockruns):
        requests_mock.post(MOCK_DATASERVICE + "/users",
                           json=user_json,
                           status_code=201)
        requests_mock.get(MOCK_DATASERVICE + "/users",
                          json=[user_json],
                          status_code=200)
        requests_mock.get(MOCK_DATASERVICE + "/users/" + str(user_json['id']),
                          json=user_json,
                          status_code=200)
        requests_mock.put(MOCK_DATASERVICE + "/users/" + str(user_json['id']),
                          json=user_json,
                          status_code=200)
        requests_mock.get(MOCK_DATASERVICE + "/users/" + str(user_json['id']) +
                          "/runs",
                          json=runs_json,
                          status_code=200)

        requests_mock.get(MOCK_DATASERVICE + "/users/" + str(user_json['id']) +
                          "/objectives",
                          json=[],
                          status_code=200)
        requests_mock.get(MOCK_DATASERVICE + "/users/" + str(user_json['id']) +
                          "/challenges",
                          json=[],
                          status_code=200)

        requests_mock.delete(MOCK_DATASERVICE + "/users/" +
                             str(user_json['id']),
                             json="",
                             status_code=204)

        for run in runs_json:
            requests_mock.get(MOCK_DATASERVICE + "/users/" +
                              str(user_json['id']) + "/runs/" + str(run["id"]),
                              json=run,
                              status_code=200)

        requests_mock.post(MOCK_AUTHSERVICE + '/login',
                           json={
                               'id': user_json['id'],
                               'email': user_json['email'],
                               'password': user_json['password']
                           })

        requests_mock.post(
            "https://www.strava.com/oauth/token?client_id=00000&client_secret=00000&code=4y3t74324t82t28t",
            json={"access_token": "faketoken"})
def test_create_public_udp(requests_mock):
    requests_mock.get(API_URL, json={"api_version": "1.0.0"})
    requests_mock.get(API_URL + "processes",
                      json={"processes": [{
                          "id": "add"
                      }]})
    conn = Connection(API_URL)

    new_udp = load_json_resource("data/1.0.0/udp_details.json")

    def check_body(request):
        body = request.json()
        assert body['process_graph'] == new_udp['process_graph']
        assert body['parameters'] == new_udp['parameters']
        assert body['public']
        return True

    adapter = requests_mock.put(API_URL + "process_graphs/evi",
                                additional_matcher=check_body)

    conn.save_user_defined_process(user_defined_process_id='evi',
                                   process_graph=new_udp['process_graph'],
                                   parameters=new_udp['parameters'],
                                   public=True)

    assert adapter.called
Ejemplo n.º 9
0
def test_update_user(db, client, requests_mock):
    requests_mock.put(MOCK_AUTHSERVICE + '/users', status_code=200)
    r = client.put("/users/2",
                   json={
                       "id": 2,
                       "age": 22,
                       "email": "*****@*****.**",
                       "firstname": "Testss",
                       "lastname": "Test",
                       "max_hr": 180,
                       "rest_hr": 50,
                       "strava_token": "null",
                       "vo2max": 63.0,
                       "weight": 60.0
                   })
    assert r.status_code == 200, "Should update the user"
    user = r.get_json()
    assert user['email'] == "*****@*****.**", "Should update the user email"
Ejemplo n.º 10
0
def test_username_password_env_vars(requests_mock, monkeypatch, fs):
    """ Tests authentication with username and password set in environment variables. 
    Params:
        requests_mock: Network request mocker.
        monkeypatch: Used for mocking env variables.
        fs: pyfakefs plugin for mocking filesystem operations.
    """
    monkeypatch.setenv('VAST_USERNAME', 'john_doe')
    monkeypatch.setenv('VAST_PASSWORD', 'abc123')
    client = VastClient()
    expanded_path = os.path.expanduser(default_api_key_file)
    #assert client.api_key_file == os.path.dirname(expanded_path), "api_key_file should be in home dir."
    assert client.api_key is None, "api_key shouldn't be set yet."
    requests_mock.put(api_base_url + "/users/current/", json=stubs.user_json)
    fs.create_dir(os.path.dirname(expanded_path))
    client.authenticate()
    assert client.api_key_file == expanded_path
    with open(client.api_key_file) as f:
        assert f.read(
        ) == client.api_key, "Contents of %s should match client.api_key" % default_api_key_file
    check_attrs(client, stubs.user_json)
    assert client.api_key == test_api_key, "Retrieved api key should match test_api_key."