-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RDI Access change for retrieving data from Kobo Reinstated #4507
Open
johniak
wants to merge
2
commits into
develop
Choose a base branch
from
feature/RDI-Access-change-for-retrieving-data-from-Kobo-NEW
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+26,659
−121
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11,031 changes: 11,031 additions & 0 deletions
11,031
...it/apps/core/kobo/cassettes/TestKoboAPI.test_get_all_projects_filter_by_country_code.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
tests/unit/apps/core/kobo/cassettes/TestKoboAPI.test_get_project_submissions.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
...core/kobo/cassettes/TestKoboAPI.test_get_project_submissions_only_active_submissions.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate, br | ||
Authorization: | ||
- token test-token | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- python-requests/2.31.0 | ||
method: GET | ||
uri: https://kf.hope.unicef.org/api/v2/assets/aWnA2d5YBBDgQ5WZXpbaRe/data/?format=json&limit=10&query=%7B%22_validation_status.uid%22:%22validation_status_approved%22%7D | ||
response: | ||
body: | ||
string: '{"count":0,"next":null,"previous":null,"results":[]}' | ||
headers: | ||
Allow: | ||
- GET, HEAD, OPTIONS | ||
Connection: | ||
- keep-alive | ||
Content-Language: | ||
- en | ||
Content-Length: | ||
- '52' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Thu, 16 May 2024 13:43:25 GMT | ||
Referrer-Policy: | ||
- same-origin | ||
Server: | ||
- nginx/1.23.0 | ||
Vary: | ||
- Accept, Accept-Language, Cookie, Origin | ||
X-Content-Type-Options: | ||
- nosniff | ||
X-Frame-Options: | ||
- DENY | ||
X-KoBoNaUt: | ||
- afg_admin | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
2,130 changes: 2,130 additions & 0 deletions
2,130
tests/unit/apps/core/kobo/cassettes/TestKoboAPI.test_get_single_project_data.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
2,130 changes: 2,130 additions & 0 deletions
2,130
tests/unit/apps/core/kobo/cassettes/TestResolveAsset.test_resolve_asset.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
10,989 changes: 10,989 additions & 0 deletions
10,989
tests/unit/apps/core/kobo/cassettes/TestResolveAssetsList.test_resolve_assets_list.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from hct_mis_api.apps.core.kobo.api import CountryCodeNotProvided, KoboAPI | ||
|
||
|
||
class TestKoboAPI: | ||
@pytest.fixture(autouse=True) | ||
def use_kobo_master_token(self, settings: Any) -> None: | ||
settings.KOBO_MASTER_API_TOKEN = "test-token" | ||
settings.KOBO_KF_URL = "https://kf.hope.unicef.org" | ||
settings.KOBO_PROJECT_VIEWS_ID = "pvEsUUfAgYyyV7jpR6i3FvM" | ||
|
||
def test_get_all_projects_with_not_provided_country_code(self) -> None: | ||
service = KoboAPI() | ||
|
||
with pytest.raises(CountryCodeNotProvided): | ||
service.get_all_projects_data(None) # type: ignore[arg-type] | ||
|
||
@pytest.mark.vcr() | ||
def test_get_all_projects_filter_by_country_code(self) -> None: | ||
service = KoboAPI() | ||
projects = service.get_all_projects_data("AFG") | ||
assert len(projects) == 117 | ||
assert "AFG" in projects[0]["settings"]["country_codes"] | ||
|
||
@pytest.mark.vcr() | ||
def test_get_single_project_data(self) -> None: | ||
service = KoboAPI() | ||
project_data = service.get_single_project_data("aWnA2d5YBBDgQ5WZXpbaRe") | ||
assert project_data | ||
|
||
@pytest.mark.vcr() | ||
def test_get_project_submissions(self) -> None: | ||
service = KoboAPI() | ||
service.LIMIT = 10 | ||
submissions = service.get_project_submissions("aWnA2d5YBBDgQ5WZXpbaRe", only_active_submissions=False) | ||
assert len(submissions) == 10 | ||
|
||
@pytest.mark.vcr() | ||
def test_get_project_submissions_only_active_submissions(self) -> None: | ||
service = KoboAPI() | ||
service.LIMIT = 10 | ||
submissions = service.get_project_submissions("aWnA2d5YBBDgQ5WZXpbaRe", only_active_submissions=True) | ||
assert len(submissions) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import datetime | ||
from typing import Any | ||
|
||
import pytest | ||
from dateutil.tz import tzlocal | ||
|
||
from hct_mis_api.apps.core.fixtures import create_afghanistan | ||
from hct_mis_api.apps.core.schema import resolve_asset | ||
|
||
pytestmark = pytest.mark.django_db(transaction=True) | ||
|
||
|
||
class TestResolveAsset: | ||
@pytest.fixture(autouse=True) | ||
def use_kobo_master_token(self, settings: Any) -> None: | ||
settings.KOBO_MASTER_API_TOKEN = "token-from-env" | ||
settings.KOBO_KF_URL = "https://kf.hope.unicef.org" | ||
settings.KOBO_PROJECT_VIEWS_ID = "pvEsUUfAgYyyV7jpR6i3FvM" | ||
|
||
@pytest.mark.vcr() | ||
def test_resolve_asset(self) -> None: | ||
create_afghanistan() | ||
|
||
result = resolve_asset("afghanistan", "aWnA2d5YBBDgQ5WZXpbaRe") | ||
assert result == { | ||
"asset_type": "survey", | ||
"country": "Afghanistan", | ||
"date_modified": datetime.datetime(2023, 10, 4, 10, 38, 23, 610534, tzinfo=tzlocal()), | ||
"deployment_active": True, | ||
"has_deployment": True, | ||
"id": "aWnA2d5YBBDgQ5WZXpbaRe", | ||
"name": "PMU-REG-Cash_for Education-Nooristan-ACTED-Dec-2022", | ||
"sector": "Humanitarian - Coordination / Information Management", | ||
"xls_link": "https://kf.hope.unicef.org/api/v2/assets/aWnA2d5YBBDgQ5WZXpbaRe/?format=json.xls", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import datetime | ||
from typing import Any | ||
|
||
import pytest | ||
from dateutil.tz import tzlocal | ||
from graphql import GraphQLError | ||
|
||
from hct_mis_api.apps.core.fixtures import create_afghanistan | ||
from hct_mis_api.apps.core.schema import resolve_assets_list | ||
from hct_mis_api.apps.geo.fixtures import CountryFactory | ||
|
||
pytestmark = pytest.mark.django_db(transaction=True) | ||
|
||
|
||
class TestResolveAssetsList: | ||
@pytest.fixture(autouse=True) | ||
def use_kobo_master_token(self, settings: Any) -> None: | ||
settings.KOBO_MASTER_API_TOKEN = "test-token" | ||
settings.KOBO_KF_URL = "https://kf.hope.unicef.org" | ||
settings.KOBO_PROJECT_VIEWS_ID = "pvEsUUfAgYyyV7jpR6i3FvM" | ||
|
||
def test_resolve_assets_list_wrong_business_area(self) -> None: | ||
with pytest.raises(GraphQLError): | ||
resolve_assets_list("wrong_business_area") | ||
|
||
def test_resolve_assets_list_without_country_code(self) -> None: | ||
create_afghanistan() | ||
with pytest.raises(GraphQLError): | ||
resolve_assets_list("afghanistan") | ||
|
||
@pytest.mark.vcr() | ||
def test_resolve_assets_list(self) -> None: | ||
business_area = create_afghanistan() | ||
country = CountryFactory() | ||
business_area.countries.add(country) | ||
|
||
result = resolve_assets_list("afghanistan") | ||
assert len(result) == 68 | ||
assert result[0] == { | ||
"asset_type": "survey", | ||
"country": "Afghanistan", | ||
"date_modified": datetime.datetime(2023, 10, 4, 10, 38, 23, 610534, tzinfo=tzlocal()), | ||
"deployment_active": True, | ||
"has_deployment": True, | ||
"id": "aWnA2d5YBBDgQ5WZXpbaRe", | ||
"name": "PMU-REG-Cash_for Education-Nooristan-ACTED-Dec-2022", | ||
"sector": "Humanitarian - Coordination / Information Management", | ||
"xls_link": "https://kf.hope.unicef.org/api/v2/assets/aWnA2d5YBBDgQ5WZXpbaRe/?format=json.xls", | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/unit/apps/registration_datahub/test_pull_kobo_submissions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any | ||
from unittest import mock | ||
|
||
from django.conf import settings | ||
|
||
import pytest | ||
|
||
from hct_mis_api.apps.core.fixtures import create_afghanistan | ||
from hct_mis_api.apps.program.fixtures import ProgramFactory | ||
from hct_mis_api.apps.registration_data.models import KoboImportData | ||
from hct_mis_api.apps.registration_datahub.tasks.pull_kobo_submissions import ( | ||
PullKoboSubmissions, | ||
) | ||
|
||
pytestmark = pytest.mark.django_db(databases=(("default",)), transaction=True) | ||
|
||
|
||
class TestPullKoboSubmissions: | ||
@pytest.fixture(autouse=True) | ||
def use_kobo_master_token(self, settings: Any) -> None: | ||
settings.KOBO_MASTER_API_TOKEN = "token-from-env" | ||
settings.KOBO_KF_URL = "https://kf.hope.unicef.org" | ||
|
||
def test_pull_kobo_submissions(self) -> None: | ||
create_afghanistan() | ||
kobo_import_data = KoboImportData( | ||
status=KoboImportData.STATUS_PENDING, | ||
business_area_slug="afghanistan", | ||
data_type=KoboImportData.JSON, | ||
kobo_asset_id="aWnA2d5YBBDgQ5WZXpbaRe", | ||
only_active_submissions=False, | ||
) | ||
kobo_import_data.save() | ||
program = ProgramFactory() | ||
|
||
content = Path( | ||
f"{settings.TESTS_ROOT}/apps/registration_datahub/test_file/kobo_submissions_collectors.json" | ||
).read_text() | ||
content = json.loads(content) | ||
with mock.patch( | ||
"hct_mis_api.apps.registration_datahub.tasks.pull_kobo_submissions.KoboAPI.get_project_submissions", | ||
return_value=content, | ||
): | ||
service = PullKoboSubmissions() | ||
result = service.execute(kobo_import_data, program) | ||
|
||
kobo_import_data.refresh_from_db() | ||
assert kobo_import_data.id == result["kobo_import_data_id"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this test run, on production, there is a token?