Exemple #1
0
    def test_upload(self, requests_mock):
        response = {'file_id': FILE_ID, 'metadata': {}}

        file_info = UploadBucketFile(FILE_ID, UPLOAD_FILE_PATH)
        report_queue = MagicMock()

        # mock file
        self.fs.create_file(UPLOAD_FILE_PATH, contents=UPLOAD_FILE_CONTENTS)

        # mock upload request
        url = "{}/buckets/{}/files".format(ORGANIZATION_ENDPOINT, BUCKET_ID)
        requests_mock.register_uri('POST', url, json=response)

        # execute upload job
        result_options = {
            'source': UPLOAD_FILE_PATH,
            'destination': FILE_ID,
            'metadata': {}
        }
        upload_job(BUCKET_ID, file_info, report_queue, None)
        assert report_queue.put.call_count == 3
        report_queue.put.assert_any_call((INITIALIZE_REPORT, ANY, 0, ANY))
        report_queue.put.assert_any_call(
            (PROGRESS_REPORT, ANY, len(UPLOAD_FILE_CONTENTS), None))
        report_queue.put.assert_any_call(
            (FINISH_REPORT, ANY, 0, result_options))
def test_getMyLibraryHoldingsOCLCNumberSerial(requests_mock, mockOAuthSession,
                                              getTestConfig):
    getTestConfig.update({'oauth-session': mockOAuthSession})
    oclcNumber = "2445677"
    requests_mock.register_uri(
        'GET',
        'https://americas.discovery.api.oclc.org/worldcat/v2/my-holdings?oclcNumber='
        + oclcNumber,
        status_code=200,
        json=my_holdings_oclcnumber_serial)
    holdings = make_requests.getMyLibraryHoldings(getTestConfig, "oclcnumber",
                                                  oclcNumber)
    assert type(holdings) is pandas.core.series.Series
    assert holdings[0] == '2445677'
    assert len(holdings[1].split(',')) == 10
    assert holdings[1].split(',')[0] == '223667789'
    assert holdings[1].split(',')[1] == '223667393'
    assert holdings[1].split(',')[2] == '223603680'
    assert holdings[1].split(',')[3] == '223603435'
    assert holdings[1].split(',')[4] == '223603835'
    assert holdings[1].split(',')[5] == '223604406'
    assert holdings[1].split(',')[6] == '223604491'
    assert holdings[1].split(',')[7] == '223605910'
    assert holdings[1].split(',')[8] == '223606436'
    assert holdings[1].split(',')[9] == '223607240'
    assert len(holdings[2].split(',')) == 34
    assert holdings[3] == 14
    assert len(holdings[4].split(',')) == 2
    assert holdings[5] == 'success'
Exemple #3
0
def test_need_token_authentication_basic_auth(requests_mock):
    service = "registry.docker.io"
    auth_url = "https://auth.test-registry.redhat.com"
    requests_mock.register_uri(
        "GET",
        "https://test-registry.redhat.com/v2/",
        [
            {
                "status_code": 401,
                "headers": {
                    "Www-Authenticate":
                    'Bearer realm="%s/token",service="%s"' %
                    (auth_url, service)
                },
            },
            {
                "status_code": 200
            },
        ],
    )
    requests_mock.register_uri(
        "GET",
        "%s/token?service=%s" % (auth_url, service),
        json={"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1"},
    )
    api_version_check("https://test-registry.redhat.com",
                      credentials=("login", "password"))
    assert requests_mock.call_count == 3
    requests_mock.request_history[1].headers[
        "Authorization"] == "Basic bG9naW46cGFzc3dvcmQ="
    def test_upload(self, requests_mock):
        request_headers = UPLOAD_FILE_REQ_HEADERS.copy()
        response_headers = {
            'content-length': str(len(UPLOAD_FILE_CONTENTS))
        }

        file_info = UploadFile(UPLOAD_FILE_PATH)
        report_queue = MagicMock()

        # mock file
        self.fs.create_file(UPLOAD_FILE_PATH, contents=UPLOAD_FILE_CONTENTS)

        # mock upload request
        url = "{}/channels/{}/upload".format(ABEJA_API_URL, CHANNEL_ID)
        requests_mock.register_uri(
            'POST',
            url,
            request_headers=request_headers,
            headers=response_headers,
            additional_matcher=request_body_matcher,
            json={'file_id': FILE_ID})

        # execute upload job
        result_options = {
            'source': UPLOAD_FILE_PATH,
            'destination': FILE_ID,
            'metadata': {}
        }
        upload_job(CHANNEL_ID, file_info, report_queue, None)
        assert report_queue.put.call_count == 3
        report_queue.put.assert_any_call((INITIALIZE_REPORT, ANY, 0, ANY))
        report_queue.put.assert_any_call(
            (PROGRESS_REPORT, ANY, len(UPLOAD_FILE_CONTENTS), None))
        report_queue.put.assert_any_call(
            (FINISH_REPORT, ANY, 0, result_options))
def mockOAuthSession(requests_mock, getTestConfig):
    requests_mock.register_uri('POST',
                               getTestConfig.get('token_url'),
                               status_code=200,
                               json=oauth_response)
    oauth_session = make_requests.createOAuthSession(getTestConfig, 'wcapi')
    return oauth_session
Exemple #6
0
def test_authentication_basic_auth(requests_mock):
    """For services with just basic auth (docker-pulp) no Bearer tokens
    are emitted. Instead Basic auth is required."""
    service = "registry.docker.io"
    auth_url = "https://auth.test-registry.redhat.com"
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/" % service,
        [
            {
                "status_code": 401,
                "headers": {
                    "Www-Authenticate": 'Basic realm="pub-dev-pulp"'
                },
            },
            {
                "status_code": 200
            },
        ],
    )
    requests_mock.register_uri("GET", auth_url)
    assert api_version_check("https://registry.docker.io",
                             credentials=("login", "password"))
    assert requests_mock.call_count == 2
    # second request (after failed first) should contain auth headers
    # base64.encode('login:password')
    requests_mock.request_history[1].headers[
        "Authorization"] == "Basic bG9naW46cGFzc3dvcmQ="
Exemple #7
0
def test_inspect_v1(requests_mock):
    registry = "fake-registry"
    repo = "test-repo"
    tag = "test-tag"
    headers = {
        "docker-content-digest": "test-digest",
        "content-type": "application/vnd.docker.distribution.manifest.v1+json",
    }
    expected_manifest = {
        "architecture":
        "amd64",
        "fsLayers": [
            {
                "blobSum":
                "sha256:b4a41a81fce32bd7fb00ad10e6c73285f937ce2110a619d306ec09f487b40cca"
            },
            {
                "blobSum":
                "sha256:16dc1f96e3a1bb628be2e00518fec2bb97bd5933859de592a00e2eb7774b6ecf"
            },
        ],
        "name":
        "twaugh/buildroot",
        "schemaVersion":
        1,
        "signatures": [{
            "header": {
                "alg": "ES256",
                "jwk": {
                    "crv": "P-256",
                    "kid":
                    "IT3U:4H7H:YPAL:CSU4:CAC2:MFNW:IWV5:YQNM:547A:FMN4:A4D3:UZAG",
                    "kty": "EC",
                    "x": "WjjIX9YwUSO64Zc5iNMODC4mh9vB-mqgt1uEcE7gLfE",
                    "y": "6fhZlalfi9_cvQd-AwBBwGnidIZKIYVzLsSV6HBR8jA",
                },
            },
            "protected":
            "eyJmb3JtYXRMZW5ndGgiOjIzMjAsImZvcm1hdFRhaWwiOiJDbjAiLCJ0aW1lIjoiMjAxNi0wNy0xM1QwOToxNjoxMFoifQ",
            "signature":
            "-dqrUzJ8IbSURO__gkbG2vdzQEbjX32Qv2DjWG3mazTGsRXXgYofr-6VY7lMDUwiOERTD9Te9wyyrALMB7Yt1A",
        }],
        "tag":
        tag,
    }
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/%s/manifests/%s" % (registry, repo, tag),
        status_code=200,
        headers=headers,
        json=expected_manifest,
    )
    inspected = inspect("https://fake-registry", "test-repo", "test-tag")
    assert inspected == {
        "architecture": "amd64",
        "config": {
            "Labels": {}
        },
        "digest": "test-digest",
    }
def test_getMyLibraryRetainedHoldingsOCLCNumberSerial(requests_mock,
                                                      mockOAuthSession,
                                                      getTestConfig):
    getTestConfig.update({'oauth-session': mockOAuthSession})
    oclcNumber = "456314438"
    oclcSymbol = "OCWMS"
    requests_mock.register_uri(
        'GET',
        'https://americas.discovery.api.oclc.org/worldcat/v2/retained-holdings?oclcNumber='
        + oclcNumber + "&heldBy=" + oclcSymbol,
        status_code=200,
        json=my_retained_holdings_serial)
    holdings = make_requests.getMyLibraryRetainedHoldings(
        getTestConfig, oclcSymbol, {
            "type": "oclcnumber",
            "value": oclcNumber
        })
    assert type(holdings) is pandas.core.series.Series
    assert holdings[0] == '456314438'
    assert len(holdings[1].split(',')) == 1
    assert holdings[1].split(',')[0] == '222309835'
    assert len(holdings[2].split(',')) == 2
    assert '0987654321' in holdings[2].split(',')
    assert '1234567890' in holdings[2].split(',')
    assert len(holdings[3].split(',')) == 1
    assert holdings[4] == 'success'
Exemple #9
0
def test_request_token_without_scope(requests_mock):
    image = "repo/sitory"
    service = "registry.docker.io"
    expected_token = "new token"
    headers = {
        "www-authenticate":
        'Bearer realm="https://{0}/token",service="registry.docker.io"'.format(
            service)
    }
    response = MagicMock(headers=headers)

    def call_back(request, context):
        if request.path_url.find("scope=") >= 0:
            context.__setattr__("status_code", 200),
            return {"token": expected_token}
        else:
            context.__setattr__("status_code", 401),
            context.__setattr__("reason", "Unauthorized access"),
            return {}

    requests_mock.register_uri(
        "GET",
        "https://%s/token" % (service, ),
        json=call_back,
    )

    token = request_token(
        requests.session(),
        response=response,
        credentials=("user", "pass"),
        repo_name=image,
    )

    assert token == expected_token
Exemple #10
0
def test_need_token_authentication(requests_mock):
    service = "test-registry"
    auth_url = "https://auth.test-registry.redhat.com"
    requests_mock.register_uri(
        "GET",
        "https://test-registry.redhat.com/v2/",
        [
            {
                "status_code": 401,
                "headers": {
                    "Www-Authenticate":
                    'Bearer realm="%s/token",service="%s"' %
                    ("https://auth.test-registry.redhat.com", service)
                },
            },
            {
                "status_code": 200
            },
        ],
    )
    requests_mock.register_uri(
        "GET",
        "%s/token?service=%s" % (auth_url, service),
        json={"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1"},
    )
    assert api_version_check("https://test-registry.redhat.com")
    assert [(x.method, x.url) for x in requests_mock.request_history] == [
        ("GET", "https://test-registry.redhat.com/v2/"),
        ("GET",
         "https://auth.test-registry.redhat.com/token?service=test-registry"),
        ("GET", "https://test-registry.redhat.com/v2/"),
    ]
Exemple #11
0
    def test_upload_fail(self, requests_mock):
        request_headers = UPLOAD_FILE_REQ_HEADERS.copy()
        file_id = FILE_ID
        file_path = UPLOAD_FILE_PATH
        report_queue = MagicMock()

        # mock file
        self.fs.create_file(UPLOAD_FILE_PATH, contents=UPLOAD_FILE_CONTENTS)
        # mock upload request
        url = "{}/buckets/{}/files".format(ORGANIZATION_ENDPOINT, BUCKET_ID)
        requests_mock.register_uri('POST',
                                   url,
                                   request_headers=request_headers,
                                   status_code=403)
        # execute upload job
        upload_job(BUCKET_ID, UploadBucketFile(file_id, file_path),
                   report_queue, None)
        assert report_queue.put.call_count == 2

        _name, args, kwargs = report_queue.put.mock_calls[1]

        self.assertEqual(args[0][0], RAISE_ERROR)
        self.assertEqual(args[0][2], 0)
        self.assertEqual(args[0][3]['source'], UPLOAD_FILE_PATH)
        self.assertIsNotNone(args[0][3]['destination'])
        self.assertIn('metadata', args[0][3])
        self.assertIsNotNone(args[0][3]['error'])
Exemple #12
0
def test_need_token_authentication_no_realm(requests_mock):
    service = "test-registry"
    auth_url = "https://auth.test-registry.redhat.com"
    requests_mock.register_uri(
        "GET",
        "https://test-registry.redhat.com/v2/",
        [
            {
                "status_code": 401,
                "headers": {
                    "Www-Authenticate": "Bearer"
                },
            },
            {
                "status_code": 200
            },
        ],
    )
    requests_mock.register_uri(
        "GET",
        "%s/token?service=%s" % (auth_url, service),
        json={"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1"},
    )
    with pytest.raises(
            IOError,
            match=re.escape("No realm specified for token auth challenge.")):
        assert api_version_check("https://test-registry.redhat.com")
Exemple #13
0
def test_retrieveAllInstitutionRetentions(mockOAuthSession, getTestConfig, tmpdir, requests_mock):
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/retained-holdings?heldBy=CCO', status_code=200, json=retained_holdings_institution)    
    getTestConfig.update({"oauth-session": mockOAuthSession})
    result = process_data.retrieveAllInstitutionRetentions(getTestConfig, 'CCO')
    assert type(result) is pandas.DataFrame
    final_result = pandas.DataFrame(data={"oclcnumber": ['776775878', '1702614', '16983586', '24134802', '51725778', '2983534', '18026674', '1132109', '10233631', '1310939'], "accession_number":['236528690', '272142932', '272153205', '272159905', '272172660', '272144447', '272156949', '272142620', '272154843', '272145080']})
    assert_frame_equal(result, final_result)      
                                 
 def test_get_existing_application(self, requests_mock):
     requests_mock.register_uri(
         'GET',
         'http://localhost:8084/applications/testxlr',
         json={"name": "testxlr"},
         status_code=200)
     x = SpinnakerClient("http://localhost:8084")
     result = x.get_application('testxlr')
     assert result['name'] == 'testxlr'
Exemple #15
0
def test_createOAuthSession(requests_mock, getTestConfig):
    #?grant_type=client_credentials&scope=wcapi
    requests_mock.register_uri('POST',
                               'https://oauth.oclc.org/token',
                               status_code=200,
                               json=oauth_response)
    oauth_session = make_requests.createOAuthSession(getTestConfig, 'wcapi')
    assert type(oauth_session) is OAuth2Session
    assert oauth_session.access_token == 'tk_12345'
 def test_get_non_existing_application(self, requests_mock):
     requests_mock.register_uri(
         'GET',
         'http://localhost:8084/applications/non_existing',
         json={"error": "Not Found"},
         status_code=404)
     with pytest.raises(Exception):
         x = SpinnakerClient("http://localhost:8084")
         x.get_application('non_existing')
    def test_upload_with_metadata_and_filename(self, requests_mock):

        metadata = (
            ('key1', 'value1'),
            ('key2', '日本語'),
            # JSON file spec can contain integer values
            ('key3', 12345),
            (12345, 67890),
        )
        metadata_res = dict([(str(k), v) for k, v in metadata])

        response_headers = {
            'content-length': str(len(UPLOAD_FILE_CONTENTS))
        }

        file_info = UploadFile('target/日本語.csv')
        report_queue = MagicMock()

        # mock file
        self.fs.create_file(file_info.path, contents=UPLOAD_FILE_CONTENTS)

        # mock upload request
        url = "{}/channels/{}/upload".format(ABEJA_API_URL, CHANNEL_ID)
        requests_mock.register_uri(
            'POST',
            url,
            json={'file_id': FILE_ID, 'metadata': metadata_res},
            headers=response_headers,
            additional_matcher=request_body_matcher
        )

        # execute upload job
        worker_options = {
            'metadata': metadata,
        }
        upload_job(CHANNEL_ID, file_info, report_queue, worker_options)
        assert report_queue.put.call_count == 3
        report_queue.put.assert_any_call((INITIALIZE_REPORT, ANY, 0, ANY))
        report_queue.put.assert_any_call(
            (PROGRESS_REPORT, ANY, len(UPLOAD_FILE_CONTENTS), None))
        result_options = {
            'source': file_info.path,
            'destination': FILE_ID,
            'metadata': metadata_res
        }
        report_queue.put.assert_any_call(
            (FINISH_REPORT, ANY, 0, result_options))

        req = requests_mock.request_history[0]
        self.assertEqual(req.method, 'POST')
        self.assertEqual(req.headers['x-abeja-meta-filename'],
                         r'%E6%97%A5%E6%9C%AC%E8%AA%9E.csv')
        self.assertEqual(req.headers['x-abeja-meta-key1'], 'value1')
        self.assertEqual(
            req.headers['x-abeja-meta-key2'], r'%E6%97%A5%E6%9C%AC%E8%AA%9E')
        self.assertEqual(req.headers['x-abeja-meta-key3'], '12345')
        self.assertEqual(req.headers['x-abeja-meta-12345'], '67890')
Exemple #18
0
def test_invalid_access_token_will_refresh_and_retry(requests_mock):
    # Build up mock requests and responses
    # 1: Request getLicenses -> get error response
    # 2: request token refresh -> get new access token

    sws_client = SwsClient(app_id=APP_ID,
                           secret='myclientapppassword',
                           service_uri=SERVICE_URI,
                           user_id=1000)
    # Set refresh token
    sws_client.refresh_token = MOCK_TOKEN_REFRESH_RESPONSE_BODY['tokens'][
        'access']['token']
    # Set handler for access token update
    sws_client.set_access_token_updated_callback(handle_access_token_update)

    # Build up mock requests
    get_licenses_url = SERVICE_URI['license'] + GET_LICENSES_API

    requests_mock.register_uri(
        'GET',
        get_licenses_url,
        [
            {
                'json': INVALID_ACCESS_TOKEN_ERROR[
                    'response_body'],  # Fail on the first try for an invalid token
                'status_code': INVALID_ACCESS_TOKEN_ERROR['http_code']
            },
            {
                'json':
                MOCK_GET_LICENSES_RESPONSE_BODY,  # Allow retry to succeed
                'status_code': 200
            },
            {
                'json': EXPIRED_ACCESS_TOKEN_ERROR[
                    'response_body'],  # Fail on second try for expired token
                'status_code': EXPIRED_ACCESS_TOKEN_ERROR['http_code']
            },
            {
                'json':
                MOCK_GET_LICENSES_RESPONSE_BODY,  # Allow retry to succeed
                'status_code': 200
            }
        ])

    token_refresh_url = SERVICE_URI['id'] + TOKEN_REFRESH_URL
    requests_mock.register_uri('POST',
                               token_refresh_url,
                               json=MOCK_TOKEN_REFRESH_RESPONSE_BODY)

    # Try to get licenses twice. Each time it should fail from an invalid token and retry and succeed.
    for i in range(2):
        sws_client.license().get_licenses()
        response = sws_client.license().get_licenses()
        assert response.status_code == 200
        data = response.json()
        assert data == MOCK_GET_LICENSES_RESPONSE_BODY
 def test_get_applications(self, requests_mock):
     requests_mock.register_uri('GET',
                                'http://localhost:8084/applications',
                                json=[{
                                    "name": "testxlr"
                                }],
                                status_code=200)
     x = SpinnakerClient("http://localhost:8084")
     result = x.get_applications()
     assert len(result) > 0
Exemple #20
0
def test_retrieveSPByOCLCNumber(mockOAuthSession, getTestConfig, tmpdir, requests_mock):
    heldInState = "CA"
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs-retained-holdings?oclcNumber=776775878&heldInState=' + heldInState, status_code=200, json=retained_byState)
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs-retained-holdings?oclcNumber=1702614&heldInState=' + heldInState, status_code=200, json=retained_byState2)    
    item_file = "oclcNumber\n776775878\n1702614"
    csv_read = handle_files.loadCSV(item_file)    
    getTestConfig.update({"oauth-session": mockOAuthSession})
    result = process_data.retrieveSPByOCLCNumber(getTestConfig, csv_read, heldInState=heldInState)
    assert type(result) is pandas.DataFrame
    final_result = pandas.DataFrame(data={"oclcNumber": [776775878, 1702614], "total_holding_count":[50, 395], "retained_symbols": ['CCO', 'CCO'], "status": ['success', 'success']})
Exemple #21
0
def test_retrieveInstitutionRetentionsbyOCLCNumber(mockOAuthSession, getTestConfig, tmpdir, requests_mock):
    oclcSymbol = 'CCO'
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/retained-holdings?oclcNumber=776775878&heldBy=' + oclcSymbol, status_code=200, json=retained_bySymbol)
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/retained-holdings?oclcNumber=1702614&heldBy=' + oclcSymbol, status_code=200, json=retained_bySymbol2)    
    item_file = "oclcNumber\n776775878\n1702614"
    csv_read = handle_files.loadCSV(item_file)    
    getTestConfig.update({"oauth-session": mockOAuthSession}) 
    result = process_data.retrieveInstitutionRetentionsbyOCLCNumber(getTestConfig, csv_read, oclcSymbol)
    assert type(result) is pandas.DataFrame
    final_result = pandas.DataFrame(data={"oclcNumber": [776775878, 1702614], "accession_numbers":['236528690', '272142932'], "barcodes": [None, None], "locations": [None, None], "status": ['success', 'success']})
Exemple #22
0
def test_retrieveMergedOCLCNumbers(mockOAuthSession, getTestConfig, tmpdir, requests_mock):
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs/311684437', status_code=200, json=merged_oclcnumbers)
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs/318877925', status_code=200, json=merged_oclcnumbers2)
    item_file = "oclcNumber\n311684437\n318877925"
    csv_read = handle_files.loadCSV(item_file) 
    getTestConfig.update({"oauth-session": mockOAuthSession})
    result = process_data.retrieveMergedOCLCNumbers(getTestConfig, csv_read)
    assert type(result) is pandas.DataFrame
    #assert result.columns == ["oclcNumber", "mergedOCNs", "status"]
    final_result = pandas.DataFrame(data={"oclcNumber": [311684437, 318877925], "mergedOCNs":['261176486,330361568,377707240,426228842,701739996,716923895,731216527,887752101,945738851', '877908501,979175514,981548811,990719089,993246604,1005002644,1011917725,1016539262,1020206933,1057597575'], "status": ['success', 'success']})
    assert_frame_equal(result, final_result)
def test_getLibraryRetainedHoldings(requests_mock, mockOAuthSession, getTestConfig):
    getTestConfig.update({'oauth-session': mockOAuthSession})
    oclcSymbol = "ANZSL"
    df = pandas.DataFrame(columns=['oclcnumber', 'accession_number'])
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/retained-holdings?heldBy=' + oclcSymbol, status_code=200, json=merged_oclcnumbers)
    retainedholdingList = make_requests.getLibraryRetainedHoldings(getTestConfig, df, oclcSymbol);
    assert type(retainedholdingList) is pandas.DataFrame
    assert list(retainedholdingList.columns.values) == ['oclcnumber', 'accession_number']
    assert len(retainedholdingList.index) == 10
    assert retainedholdingList.iloc[0]['oclcnumber'] == '25748966'
    assert retainedholdingList.iloc[0]['accession_number'] == '168860541'
Exemple #24
0
def test_retrieveHoldingsByOCLCNumber(mockOAuthSession, getTestConfig, tmpdir, requests_mock):
    oclcSymbol = "OCPSB"
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs-detailed-holdings?oclcNumber=318877925&heldBy=' + oclcSymbol, status_code=200, json=institution_holdings)
    requests_mock.register_uri('GET', 'https://americas.discovery.api.oclc.org/worldcat/v2/bibs-detailed-holdings?oclcNumber=191865523&heldBy=' + oclcSymbol, status_code=200, json=institution_holdings2)
    item_file = "oclcNumber\n318877925\n191865523"  
    csv_read = handle_files.loadCSV(item_file)    
    getTestConfig.update({"oauth-session": mockOAuthSession})
    result = process_data.retrieveHoldingsByOCLCNumber(getTestConfig, csv_read, heldBy=oclcSymbol)
    assert type(result) is pandas.DataFrame 
    final_result = pandas.DataFrame(data={"oclcNumber": [318877925, 191865523], "total_holding_count":[246, 3355], "holding_symbols": ['OCPSB', 'OCPSB'], "status": ['success', 'success']})
    assert_frame_equal(result, final_result)  
def test_deleteHolding(requests_mock, mockOAuthSession, getTestConfig):
    getTestConfig.update({'oauth-session': mockOAuthSession})
    oclcNumber = "2416076"
    requests_mock.register_uri('DELETE',
                               'https://worldcat.org/ih/data?oclcNumber=' +
                               oclcNumber,
                               status_code=200,
                               json="")
    holding = make_requests.deleteHolding(getTestConfig, oclcNumber)
    assert type(holding) is pandas.core.series.Series
    assert holding[0] == '2416076'
    assert holding[1] == 'success'
Exemple #26
0
def test_getMergedOCLCNumbers(requests_mock, mockOAuthSession, getTestConfig):
    getTestConfig.update({'oauth-session': mockOAuthSession})
    oclcNumber = "311684437"
    requests_mock.register_uri('GET',
                               'https://worldcat.org/bib/data/' + oclcNumber,
                               status_code=200,
                               text=merged_oclcnumbers)
    bib = make_requests.getMergedOCLCNumbers(getTestConfig, oclcNumber)
    assert type(bib) is pandas.core.series.Series
    assert bib[0] == '311684437'
    assert len(bib[1].split(',')) == 10
    assert bib[2] == 'success'
Exemple #27
0
def test_inspect_source(requests_mock):
    registry = "fake-registry"
    repo = "test-repo"
    tag = "test-tag"
    expected_manifest = {
        "schemaVersion": 2,
        "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
        "config": {
            "mediaType":
            "application/vnd.docker.container.image.v1+json",
            "size":
            20150,
            "digest":
            "sha256:adb48f5d2b45131f87a2c52a791e08185769c9f9018f7e63b760dcb4f188bd13",
        },
        "layers": [],
    }
    expected_blob = {"architecture": "ppc64le", "config": {"Labels": {}}}
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/%s/manifests/%s" % (registry, repo, tag),
        json=lambda req, context: [
            context.__setattr__("status_code", 200),
            context.__setattr__(
                "headers",
                {
                    "Content-Type":
                    "application/vnd.docker.distribution.manifest.v2+json",
                    "docker-content-digest": "test-digest",
                },
            ),
            expected_manifest,
        ][-1],
    )
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/%s/blobs/%s" % (
            registry,
            repo,
            "sha256:adb48f5d2b45131f87a2c52a791e08185769c9f9018f7e63b760dcb4f188bd13",
        ),
        status_code=200,
        json=expected_blob,
    )
    inspected = inspect("https://%s" % registry, "test-repo", "test-tag")
    assert inspected == {
        "architecture": "ppc64le",
        "config": {
            "Labels": {}
        },
        "digest": "test-digest",
        "source": True,
    }
Exemple #28
0
def test_request_token_only_if_requested_by_server(requests_mock):
    tag = "latest"
    image = "repo/sitory"
    service = "registry.docker.io"
    # If basic authentication fails and no bear authentication is requested by server
    # then fail immediately
    requests_mock.register_uri("GET",
                               "https://%s/v2/" % service,
                               status_code=401,
                               reason="Unauthorized access")
    with pytest.raises(requests.exceptions.HTTPError,
                       match="401 Client Error: Unauthorized access"):
        api_version_check("https://%s" % service, credentials=("user", "pass"))
Exemple #29
0
def test_get_manifest_raise_on_500(requests_mock, fake_home):
    tag = "latest"
    image = "repo/sitory"
    service = "registry.docker.io"
    requests_mock.register_uri("GET",
                               "https://%s/v2/%s/manifests/%s" %
                               (service, image, tag),
                               status_code=500)

    with pytest.raises(requests.exceptions.HTTPError,
                       match="500 Server Error: None"):
        _, digest, manifest = get_manifest("https://registry.docker.io", image,
                                           tag)
Exemple #30
0
def test_tes(requests_mock):
    requests_mock.register_uri("POST",
                               "{}/v1/tasks".format(TES_URL),
                               json=_post_task)
    requests_mock.register_uri("GET",
                               "{}/v1/tasks/id_1".format(TES_URL),
                               json=_get_task)
    workdir = dpath("test_tes")
    run(workdir,
        snakefile="Snakefile",
        tes=TES_URL,
        no_tmpdir=True,
        cleanup=False)