-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from akvo/feature/106-apk-download-upload-hook
Feature/106 apk download upload hook
- Loading branch information
Showing
13 changed files
with
275 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Generated by Django 4.0.4 on 2023-08-03 20:38 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('v1_mobile', '0002_auto_20230725_2053'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='MobileApk', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('apk_version', models.CharField(max_length=50)), | ||
('apk_url', models.CharField(max_length=255)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
], | ||
options={ | ||
'verbose_name': 'Mobile Apk', | ||
'verbose_name_plural': 'Mobile Apks', | ||
'db_table': 'mobile_apks', | ||
}, | ||
), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import os | ||
import requests_mock | ||
import requests as r | ||
from nwmis.settings import BASE_DIR, APP_NAME, MASTER_DATA, APK_UPLOAD_SECRET | ||
from django.test import TestCase | ||
from api.v1.v1_mobile.models import MobileApk | ||
|
||
|
||
class MobileApkTestCase(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
# Get the path to the APK file and read its content | ||
file_path = os.path.join(BASE_DIR, "source", "testapp.apk") | ||
|
||
with open(file_path, "rb") as file: | ||
cls.apk_content = file.read() | ||
|
||
cls.mock = requests_mock.Mocker() | ||
cls.mock.start() | ||
|
||
# Mocking the GET request with the actual APK content | ||
cls.apk_url = "https://expo.dev/artifacts/eas/dpRpygo9iviyK8k3oDUMzn.apk" | ||
cls.mock.get(cls.apk_url, content=cls.apk_content) | ||
|
||
# Mocking the wrong URL with a 401 status code | ||
cls.wrong_apk_url = "http://example.com/wrong-url.apk" | ||
cls.mock.get(cls.wrong_apk_url, status_code=401) | ||
|
||
# Create the initial APK | ||
cls.apk_version = "1.0.0" | ||
cls.mobile_apk = MobileApk.objects.create( | ||
apk_url=cls.apk_url, apk_version=cls.apk_version | ||
) | ||
cls.apk_path = os.path.join(BASE_DIR, MASTER_DATA) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
os.remove(f"{cls.apk_path}/{APP_NAME}-{cls.mobile_apk.apk_version}.apk") | ||
cls.mock.stop() | ||
|
||
def test_if_initial_apk_is_created(self): | ||
mobile_apk = MobileApk.objects.last() | ||
self.assertEqual(mobile_apk.apk_url, MobileApkTestCase.apk_url) | ||
self.assertEqual(mobile_apk.apk_version, self.apk_version) | ||
|
||
def test_if_apk_is_downloadable(self): | ||
request = r.get(MobileApkTestCase.apk_url) | ||
self.assertEqual(request.status_code, 200) | ||
|
||
def test_mobile_apk_download(self): | ||
# SUCCESS DOWNLOAD | ||
cls = MobileApkTestCase | ||
download = self.client.get("/api/v1/device/apk/download") | ||
self.assertEqual(download.status_code, 200) | ||
self.assertEqual( | ||
download["Content-Type"], "application/vnd.android.package-archive" | ||
) | ||
self.assertEqual( | ||
download["Content-Disposition"], | ||
f"attachment; filename={APP_NAME}-{cls.mobile_apk.apk_version}.apk", | ||
) | ||
self.assertTrue(download.has_header("Content-Length")) | ||
apk_file = f"{cls.apk_path}/{APP_NAME}-{cls.mobile_apk.apk_version}.apk" | ||
self.assertTrue(os.path.exists(apk_file)) | ||
|
||
def test_mobile_apk_upload(self): | ||
# SUCCESS UPLOAD | ||
cls = MobileApkTestCase | ||
new_version = "1.0.1" | ||
upload = self.client.post( | ||
"/api/v1/device/apk/upload", | ||
{ | ||
"apk_url": cls.apk_url, | ||
"apk_version": new_version, | ||
"secret": APK_UPLOAD_SECRET, | ||
}, | ||
) | ||
self.assertEqual(upload.status_code, 201) | ||
new_file = f"{cls.apk_path}/{APP_NAME}-{new_version}.apk" | ||
self.assertTrue(os.path.exists(new_file)) | ||
|
||
# NEW VERSION UPLOAD | ||
download = self.client.get("/api/v1/device/apk/download") | ||
self.assertEqual(download.status_code, 200) | ||
self.assertEqual( | ||
download["Content-Type"], "application/vnd.android.package-archive" | ||
) | ||
self.assertEqual( | ||
download["Content-Disposition"], | ||
f"attachment; filename={APP_NAME}-{new_version}.apk", | ||
) | ||
|
||
# FAILED UPLOAD WITH WRONG SECRET | ||
upload = self.client.post( | ||
"/api/v1/device/apk/upload", | ||
{ | ||
"apk_url": cls.apk_url, | ||
"apk_version": "1.0.0", | ||
"secret": "WRONG_SECRET", | ||
}, | ||
) | ||
self.assertEqual(upload.status_code, 400) | ||
self.assertEqual(upload.data["message"], "Secret is required.") | ||
|
||
# FAILED UPLOAD WITH WRONG APK URL | ||
upload = self.client.post( | ||
"/api/v1/device/apk/upload", | ||
{ | ||
"apk_url": self.wrong_apk_url, | ||
"apk_version": "1.0.0", | ||
"secret": APK_UPLOAD_SECRET, | ||
}, | ||
) | ||
self.assertEqual(upload.status_code, 404) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ debugpy==1.5.1 | |
msgpack==1.0.4 | ||
AkvoDjangoFormGateway==0.0.9 | ||
whitenoise==6.4.0 | ||
requests-mock==1.11.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 @@ | ||
*.apk |
Binary file not shown.
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