|
| 1 | +import os |
| 2 | +from urllib.parse import urljoin |
| 3 | +import requests |
| 4 | + |
| 5 | + |
| 6 | +class TestNativeAPI: |
| 7 | + """ |
| 8 | + This class contains test cases for the Dataverse Native API to validate |
| 9 | + that the Dataverse instance constructed by the Action is working as expected. |
| 10 | +
|
| 11 | + Test cases: |
| 12 | + - Test the 'info' endpoint of the API. |
| 13 | + - Test the 'metadatablocks' endpoint of the API. |
| 14 | + - Test creating a collection using the API. |
| 15 | + """ |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def construct_url(endpoint): |
| 19 | + """Helper method to construct a URL from an endpoint.""" |
| 20 | + |
| 21 | + BASE_URL = os.environ["BASE_URL"] |
| 22 | + return urljoin(BASE_URL, endpoint) |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def construct_header(): |
| 26 | + """Helper method to construct the header for the request.""" |
| 27 | + |
| 28 | + return {"X-Dataverse-key": os.environ["API_TOKEN"]} |
| 29 | + |
| 30 | + def test_info(self): |
| 31 | + """ |
| 32 | + Test the 'info' endpoint of the API. |
| 33 | +
|
| 34 | + This method constructs the URL for the 'api/info/version' endpoint, |
| 35 | + sends a GET request to the URL, and asserts that the response status |
| 36 | + code is 200 and the 'status' field in the response JSON is 'OK'. |
| 37 | + """ |
| 38 | + |
| 39 | + DV_VERSION = os.getenv("DV_VERSION") |
| 40 | + url = self.construct_url("api/info/version") |
| 41 | + response = requests.get(url) |
| 42 | + |
| 43 | + assert response.status_code == 200, response.text |
| 44 | + assert response.json()["status"] == "OK" |
| 45 | + assert response.json()["data"]["version"] == DV_VERSION |
| 46 | + |
| 47 | + def test_metadatablocks(self): |
| 48 | + """ |
| 49 | + Test case for the 'metadatablocks' endpoint. |
| 50 | +
|
| 51 | + This test verifies that the 'metadatablocks' endpoint returns a successful response |
| 52 | + with the expected metadata block information. |
| 53 | +
|
| 54 | + It constructs the URL for the 'metadatablocks' endpoint, sends a GET request, |
| 55 | + and asserts that the response status code is 200 (OK) and the response JSON |
| 56 | + contains the expected metadata block information. |
| 57 | +
|
| 58 | + Expected metadata block information: |
| 59 | + - displayName: "Citation Metadata" |
| 60 | + - name: "citation" |
| 61 | + """ |
| 62 | + |
| 63 | + url = self.construct_url("api/metadatablocks") |
| 64 | + response = requests.get(url) |
| 65 | + |
| 66 | + assert response.status_code == 200, response.text |
| 67 | + assert response.json()["status"] == "OK" |
| 68 | + |
| 69 | + expected = { |
| 70 | + "displayName": "Citation Metadata", |
| 71 | + "name": "citation", |
| 72 | + } |
| 73 | + |
| 74 | + assert any( |
| 75 | + { |
| 76 | + block["name"] == expected["name"] |
| 77 | + and block["displayName"] == expected["displayName"] |
| 78 | + for block in response.json()["data"] |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + def test_create_collection(self): |
| 83 | + """ |
| 84 | + Test case for creating a collection. |
| 85 | +
|
| 86 | + This test sends a POST request to the specified URL with the necessary headers and payload |
| 87 | + to create a collection in the dataverse. It then asserts that the response status code is 201 |
| 88 | + and the response JSON contains a "status" key with the value "OK". |
| 89 | + """ |
| 90 | + |
| 91 | + url = self.construct_url("api/dataverses/root") |
| 92 | + response = requests.post( |
| 93 | + url=url, |
| 94 | + headers=self.construct_header(), |
| 95 | + json={ |
| 96 | + "name": "TestAction", |
| 97 | + "alias": "test_colleczion", |
| 98 | + "dataverseContacts": [ |
| 99 | + {"contactEmail": "burrito@burritoplace.com"}, |
| 100 | + ], |
| 101 | + "affiliation": "Burrito Research University", |
| 102 | + "description": "We do all the (burrito) science.", |
| 103 | + "dataverseType": "LABORATORY", |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + assert response.status_code == 201, response.text |
| 108 | + assert response.json()["status"] == "OK" |
0 commit comments