Skip to content

Commit d418af3

Browse files
authored
Merge pull request #8 from gdcc/test-action
Added action tests
2 parents dd289fd + fb8830e commit d418af3

File tree

11 files changed

+146
-139
lines changed

11 files changed

+146
-139
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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"

.github/workflows/test-action.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
name: Test action
12
on: [push]
23

34
jobs:
@@ -17,10 +18,18 @@ jobs:
1718
jvm_options: |
1819
dataverse.api.signature-secret=eikaizieb2Oghaex
1920
dataverse.pid.datacite.rest-api-url=https://api.datacite.org
20-
create-dv: true
21+
- name: Setup Python
22+
uses: actions/setup-python@v3
23+
with:
24+
python-version: "3.11"
25+
- name: Install Python Dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install requests pytest
2129
- name: Check Dataverse Setup
30+
env:
31+
API_TOKEN: ${{ steps.dataverse.outputs.api_token }}
32+
BASE_URL: ${{ steps.dataverse.outputs.base_url }}
33+
DV_VERSION: ${{ steps.dataverse.outputs.dv_version }}
2234
run: |
23-
echo "api_token=${{ steps.dataverse.outputs.api_token }}"
24-
echo "base_url=${{ steps.dataverse.outputs.base_url }}"
25-
echo $(curl http://localhost:8080/api/info/version) && \
26-
echo $(curl http://localhost:8080/api/metadatablocks)
35+
python3 -m pytest .github/workflows/scripts/test_dataverse.py

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
# Dataverse Configbaker Action
22

3+
![Tests](https://github.com/gdcc/dataverse-action/actions/workflows/test-action.yml/badge.svg)
4+
35
This GitHub Action serves as a powerful tool to effortlessly create a functional Dataverse instance, enabling developers to run comprehensive tests and perform other critical tasks within their GitHub CI workflows.
46

57
## Usage
68

7-
In order to use this action you need to add the following to your workflow file:
9+
In order to use the baseline action you need to add the following to your workflow file:
810

911
```yaml
10-
name: Test Dataverse
12+
name: Run Dataverse Action
13+
id: dataverse
1114
uses: gdcc/dataverse-action@main
12-
with:
13-
create-dv: true
15+
```
16+
17+
This will create a Dataverse instance with the default configuration. The action will output the API token and base URL of the instance, which can be used to interact with the instance. Here is an example on how to re-use the API token and base URL in a subsequent step:
18+
19+
```yaml
20+
name: How to re-use outputs
21+
env:
22+
API_TOKEN: ${{ steps.dataverse.outputs.api_token }}
23+
BASE_URL: ${{ steps.dataverse.outputs.base_url }}
24+
run: |
25+
my-app --api-token "${{ env.API_TOKEN }}" --base-url "${{ env.BASE_URL }}"
1426
```
1527
1628
## Inputs

action.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ inputs:
2323
jvm_options:
2424
description: "Line separated key-value pairs of JVM options to be set before startup. Example: dataverse.spi.exporters.directory=/..."
2525
required: false
26-
create-dv:
27-
description: "Whether or not to create an example Dataverse"
28-
required: true
29-
default: "true"
3026
outputs:
3127
api_token:
3228
description: "API Token of dataverseAdmin superuser"
3329
value: ${{ steps.bootstrap.outputs.API_TOKEN }}
3430
base_url:
3531
description: "Base URL where to reach the instance via HTTP"
3632
value: ${{ steps.bootstrap.outputs.base_url }}
33+
dv_version:
34+
description: "Dataverse version"
35+
value: ${{ steps.bootstrap.outputs.dv_version }}
3736
runs:
3837
using: "composite"
3938
steps:
@@ -100,10 +99,8 @@ runs:
10099
grep "API_TOKEN" "${RUNNER_TEMP}/dv/bootstrap.exposed.env" >> "$GITHUB_OUTPUT"
101100
echo "base_url=http://localhost:8080/" >> "$GITHUB_OUTPUT"
102101
103-
echo "::endgroup::"
104-
- name: Retrieve API Token
105-
run: ${{ github.action_path }}/scripts/get_api_token.sh
106-
shell: bash
107-
- name: Create Dataverse
108-
run: ${{ github.action_path }}/scripts/create_dataverse.sh
109-
shell: bash
102+
# Expose version
103+
version=$(curl -s 'http://localhost:8080/api/info/version' | jq -r '.data.version')
104+
echo "dv_version=$version" >>"$GITHUB_OUTPUT"
105+
106+
echo "::endgroup::"

assets/dataverse-complete.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

assets/user.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/check_api_status.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

scripts/create_dataverse.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/dataverse_setup.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

scripts/get_api_token.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)