Repository: fossasia/open-event-server

Issue:
Make frontend URL is wrong
make_frontend_url('/verify') for `https://fossasia.github.io/open-event-server` should be `https://fossasia.github.io/open-event-server/verify`, but actually it is `https://fossasia.github.io/verify`

Add unit tests and fix


Discussion:

Resolving patch:
diff --git a/app/api/helpers/files.py b/app/api/helpers/files.py
--- a/app/api/helpers/files.py
+++ b/app/api/helpers/files.py
@@ -251,11 +251,13 @@ def make_frontend_url(path, parameters=None):
     Create URL for frontend
     """
     settings = get_settings()
-    frontend_url = urllib.parse.urlparse(settings['frontend_url'] if settings['frontend_url'] else '')
+    frontend_url = urllib.parse.urlparse(settings.get('frontend_url') or '')
+
+    full_path = '/'.join(x.strip('/') for x in (frontend_url.path, str(path)) if x)
     return urllib.parse.urlunparse((
         frontend_url.scheme,
         frontend_url.netloc,
-        str(path),
+        full_path,
         '',
         str(urllib.parse.urlencode(parameters) if parameters else ''),
         ''
diff --git a/create_db.py b/create_db.py
--- a/create_db.py
+++ b/create_db.py
@@ -7,7 +7,7 @@
 from app import current_app
 from app.models import db
 from populate_db import populate
-from tests.unittests.auth_helper import create_super_admin
+from tests.all.integration.auth_helper import create_super_admin
 
 
 def create_default_user(email, password):
diff --git a/manage.py b/manage.py
--- a/manage.py
+++ b/manage.py
@@ -10,7 +10,7 @@
 from flask_migrate import stamp
 from sqlalchemy.engine import reflection
 
-from tests.unittests.auth_helper import create_super_admin
+from tests.all.integration.auth_helper import create_super_admin
 
 
 @manager.command


Test patch:
diff --git a/tests/unittests/__init__.py b/tests/all/integration/__init__.py
similarity index 100%
rename from tests/unittests/__init__.py
rename to tests/all/integration/__init__.py
diff --git a/tests/unittests/api/__init__.py b/tests/all/integration/api/__init__.py
similarity index 100%
rename from tests/unittests/api/__init__.py
rename to tests/all/integration/api/__init__.py
diff --git a/tests/unittests/api/helpers/__init__.py b/tests/all/integration/api/helpers/__init__.py
similarity index 100%
rename from tests/unittests/api/helpers/__init__.py
rename to tests/all/integration/api/helpers/__init__.py
diff --git a/tests/unittests/api/helpers/test_db.py b/tests/all/integration/api/helpers/test_db.py
similarity index 95%
rename from tests/unittests/api/helpers/test_db.py
rename to tests/all/integration/api/helpers/test_db.py
--- a/tests/unittests/api/helpers/test_db.py
+++ b/tests/all/integration/api/helpers/test_db.py
@@ -3,13 +3,13 @@
 from app import current_app as app
 from app.factories.attendee import AttendeeFactory
 from app.models.ticket_holder import TicketHolder
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.factories.event import EventFactoryBasic
 from app.api.helpers.db import save_to_db, safe_query, get_or_create, get_count
 from flask_rest_jsonapi.exceptions import ObjectNotFound
 from app.models import db
 from app.models.event import Event
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestDBHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_errors.py b/tests/all/integration/api/helpers/test_errors.py
similarity index 92%
rename from tests/unittests/api/helpers/test_errors.py
rename to tests/all/integration/api/helpers/test_errors.py
--- a/tests/unittests/api/helpers/test_errors.py
+++ b/tests/all/integration/api/helpers/test_errors.py
@@ -1,9 +1,9 @@
 import unittest
 
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.errors import ForbiddenError, NotFoundError, ServerError, \
     UnprocessableEntityError, BadRequestError
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 from app import current_app as app
 
 
diff --git a/tests/unittests/api/helpers/test_events.py b/tests/all/integration/api/helpers/test_events.py
similarity index 87%
rename from tests/unittests/api/helpers/test_events.py
rename to tests/all/integration/api/helpers/test_events.py
--- a/tests/unittests/api/helpers/test_events.py
+++ b/tests/all/integration/api/helpers/test_events.py
@@ -5,8 +5,8 @@
 from app.api.helpers.events import create_custom_forms_for_attendees
 from app.factories.event import EventFactoryBasic
 from app.models.custom_form import CustomForms
-from tests.unittests.setup_database import Setup
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
 
 
 class TestEventUtilities(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_exceptions.py b/tests/all/integration/api/helpers/test_exceptions.py
similarity index 90%
rename from tests/unittests/api/helpers/test_exceptions.py
rename to tests/all/integration/api/helpers/test_exceptions.py
--- a/tests/unittests/api/helpers/test_exceptions.py
+++ b/tests/all/integration/api/helpers/test_exceptions.py
@@ -1,8 +1,8 @@
 import unittest
 
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity, ConflictException, ForbiddenException, MethodNotAllowed
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestExceptionsHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_files.py b/tests/all/integration/api/helpers/test_files.py
similarity index 98%
rename from tests/unittests/api/helpers/test_files.py
rename to tests/all/integration/api/helpers/test_files.py
--- a/tests/unittests/api/helpers/test_files.py
+++ b/tests/all/integration/api/helpers/test_files.py
@@ -10,8 +10,8 @@
 from app import current_app as app
 from app.api.helpers.files import create_save_resized_image, create_save_image_sizes
 from app.api.helpers.files import uploaded_image, uploaded_file
-from tests.unittests.setup_database import Setup
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
 
 
 class TestFilesHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_filters.py b/tests/all/integration/api/helpers/test_filters.py
similarity index 94%
rename from tests/unittests/api/helpers/test_filters.py
rename to tests/all/integration/api/helpers/test_filters.py
--- a/tests/unittests/api/helpers/test_filters.py
+++ b/tests/all/integration/api/helpers/test_filters.py
@@ -2,8 +2,8 @@
 
 from app import current_app as app
 from app.api.helpers.filters import json_to_rest_filter_list
-from tests.unittests.utils import OpenEventTestCase
-from tests.unittests.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
 
 
 class TestFiltersHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_jwt.py b/tests/all/integration/api/helpers/test_jwt.py
similarity index 93%
rename from tests/unittests/api/helpers/test_jwt.py
rename to tests/all/integration/api/helpers/test_jwt.py
--- a/tests/unittests/api/helpers/test_jwt.py
+++ b/tests/all/integration/api/helpers/test_jwt.py
@@ -7,8 +7,8 @@
 from app.factories.event import EventFactoryBasic
 from app.factories.user import UserFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
 
 
 class TestJWTHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_order.py b/tests/all/integration/api/helpers/test_order.py
similarity index 94%
rename from tests/unittests/api/helpers/test_order.py
rename to tests/all/integration/api/helpers/test_order.py
--- a/tests/unittests/api/helpers/test_order.py
+++ b/tests/all/integration/api/helpers/test_order.py
@@ -7,8 +7,8 @@
 from app.factories.event import EventFactoryBasic
 from app.factories.order import OrderFactory
 from app.models.order import Order
-from tests.unittests.setup_database import Setup
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
 
 
 class TestOrderUtilities(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_permission_manager.py b/tests/all/integration/api/helpers/test_permission_manager.py
similarity index 97%
rename from tests/unittests/api/helpers/test_permission_manager.py
rename to tests/all/integration/api/helpers/test_permission_manager.py
--- a/tests/unittests/api/helpers/test_permission_manager.py
+++ b/tests/all/integration/api/helpers/test_permission_manager.py
@@ -8,10 +8,10 @@
 from app.api.helpers.permission_manager import has_access, accessible_role_based_events, permission_manager
 from app.factories.event import EventFactoryBasic
 from app.models.users_events_role import UsersEventsRoles
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.factories.user import UserFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestPermissionManager(OpenEventTestCase):
diff --git a/tests/unittests/api/helpers/test_storage.py b/tests/all/integration/api/helpers/test_storage.py
similarity index 100%
rename from tests/unittests/api/helpers/test_storage.py
rename to tests/all/integration/api/helpers/test_storage.py
diff --git a/tests/unittests/api/helpers/test_utilities.py b/tests/all/integration/api/helpers/test_utilities.py
similarity index 95%
rename from tests/unittests/api/helpers/test_utilities.py
rename to tests/all/integration/api/helpers/test_utilities.py
--- a/tests/unittests/api/helpers/test_utilities.py
+++ b/tests/all/integration/api/helpers/test_utilities.py
@@ -3,9 +3,9 @@
 
 from app import current_app as app
 from app.api.helpers.exceptions import UnprocessableEntity
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.utilities import dasherize, require_relationship, string_empty, str_generator
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestUtilitiesHelperValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/validation/__init__.py b/tests/all/integration/api/validation/__init__.py
similarity index 100%
rename from tests/unittests/api/validation/__init__.py
rename to tests/all/integration/api/validation/__init__.py
diff --git a/tests/unittests/api/validation/test_discount_codes.py b/tests/all/integration/api/validation/test_discount_codes.py
similarity index 95%
rename from tests/unittests/api/validation/test_discount_codes.py
rename to tests/all/integration/api/validation/test_discount_codes.py
--- a/tests/unittests/api/validation/test_discount_codes.py
+++ b/tests/all/integration/api/validation/test_discount_codes.py
@@ -1,12 +1,12 @@
 import unittest
 
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.schema.discount_codes import DiscountCodeSchemaTicket
 from app.factories.discount_code import DiscountCodeFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestDiscountCodeValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/validation/test_events.py b/tests/all/integration/api/validation/test_events.py
similarity index 94%
rename from tests/unittests/api/validation/test_events.py
rename to tests/all/integration/api/validation/test_events.py
--- a/tests/unittests/api/validation/test_events.py
+++ b/tests/all/integration/api/validation/test_events.py
@@ -3,12 +3,12 @@
 from pytz import timezone
 
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.schema.events import EventSchema
 from app.factories.event import EventFactoryBasic
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestEventValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/validation/test_sessions.py b/tests/all/integration/api/validation/test_sessions.py
similarity index 94%
rename from tests/unittests/api/validation/test_sessions.py
rename to tests/all/integration/api/validation/test_sessions.py
--- a/tests/unittests/api/validation/test_sessions.py
+++ b/tests/all/integration/api/validation/test_sessions.py
@@ -3,12 +3,12 @@
 from pytz import timezone
 
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.schema.sessions import SessionSchema
 from app.factories.session import SessionFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestSessionValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/validation/test_speakers_call.py b/tests/all/integration/api/validation/test_speakers_call.py
similarity index 95%
rename from tests/unittests/api/validation/test_speakers_call.py
rename to tests/all/integration/api/validation/test_speakers_call.py
--- a/tests/unittests/api/validation/test_speakers_call.py
+++ b/tests/all/integration/api/validation/test_speakers_call.py
@@ -3,12 +3,12 @@
 from pytz import timezone
 
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.schema.speakers_calls import SpeakersCallSchema
 from app.factories.speakers_call import SpeakersCallFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestSpeakersCallValidation(OpenEventTestCase):
diff --git a/tests/unittests/api/validation/test_tickets.py b/tests/all/integration/api/validation/test_tickets.py
similarity index 96%
rename from tests/unittests/api/validation/test_tickets.py
rename to tests/all/integration/api/validation/test_tickets.py
--- a/tests/unittests/api/validation/test_tickets.py
+++ b/tests/all/integration/api/validation/test_tickets.py
@@ -3,12 +3,12 @@
 from pytz import timezone
 
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
+from tests.all.integration.utils import OpenEventTestCase
 from app.api.helpers.exceptions import UnprocessableEntity
 from app.api.schema.tickets import TicketSchema
 from app.factories.ticket import TicketFactory
 from app.models import db
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 
 class TestTicketValidation(OpenEventTestCase):
diff --git a/tests/unittests/auth_helper.py b/tests/all/integration/auth_helper.py
similarity index 100%
rename from tests/unittests/auth_helper.py
rename to tests/all/integration/auth_helper.py
diff --git a/tests/unittests/setup_database.py b/tests/all/integration/setup_database.py
similarity index 100%
rename from tests/unittests/setup_database.py
rename to tests/all/integration/setup_database.py
diff --git a/tests/unittests/test_migrations.py b/tests/all/integration/test_migrations.py
similarity index 78%
rename from tests/unittests/test_migrations.py
rename to tests/all/integration/test_migrations.py
--- a/tests/unittests/test_migrations.py
+++ b/tests/all/integration/test_migrations.py
@@ -1,6 +1,6 @@
 from app import current_app as app
-from tests.unittests.utils import OpenEventTestCase
-from tests.unittests.setup_database import Setup
+from tests.all.integration.utils import OpenEventTestCase
+from tests.all.integration.setup_database import Setup
 from app.views.healthcheck import check_migrations
 
 
diff --git a/tests/unittests/utils.py b/tests/all/integration/utils.py
similarity index 78%
rename from tests/unittests/utils.py
rename to tests/all/integration/utils.py
--- a/tests/unittests/utils.py
+++ b/tests/all/integration/utils.py
@@ -1,9 +1,9 @@
 import unittest
 
-from tests.unittests.setup_database import Setup
+from tests.all.integration.setup_database import Setup
 
 from app.models.user import User
-from tests.unittests.auth_helper import create_super_admin
+from tests.all.integration.auth_helper import create_super_admin
 
 
 def get_or_create_super_admin():
diff --git a/tests/unittests/utils/test_schema_defaulting.py b/tests/all/integration/utils/test_schema_defaulting.py
similarity index 100%
rename from tests/unittests/utils/test_schema_defaulting.py
rename to tests/all/integration/utils/test_schema_defaulting.py
diff --git a/tests/all/unit/__init__.py b/tests/all/unit/__init__.py
new file mode 100644
diff --git a/tests/all/unit/api/__init__.py b/tests/all/unit/api/__init__.py
new file mode 100644
diff --git a/tests/all/unit/api/helpers/__init__.py b/tests/all/unit/api/helpers/__init__.py
new file mode 100644
diff --git a/tests/all/unit/api/helpers/test_files.py b/tests/all/unit/api/helpers/test_files.py
new file mode 100644
--- /dev/null
+++ b/tests/all/unit/api/helpers/test_files.py
@@ -0,0 +1,46 @@
+import unittest
+from unittest.mock import patch
+
+from app.api.helpers.files import make_frontend_url
+
+
+class TestFilesMethods(unittest.TestCase):
+
+    def test_frontend_url(self):
+        """Test whether frontend_url is created correctly"""
+
+        def patch_settings(settings, url):
+            settings.return_value = {
+                'frontend_url': url
+            }
+
+        with patch('app.api.helpers.files.get_settings') as get_settings:
+            get_settings.return_value = {}
+            self.assertEqual('verify?token=hi',
+                             make_frontend_url('/verify', {'token': 'hi'}))
+
+            patch_settings(get_settings, 'https://next.eventyay.com/')
+            self.assertEqual('https://next.eventyay.com/verify?token=ok_this_is_a_secret',
+                             make_frontend_url('/verify', {'token': 'ok_this_is_a_secret'}))
+
+            self.assertEqual('https://next.eventyay.com/verify?token=ok_this_is_a_secret',
+                             make_frontend_url('/verify', {'token': 'ok_this_is_a_secret'}))
+
+            self.assertEqual('https://next.eventyay.com/verify',
+                             make_frontend_url('/verify'))
+
+            patch_settings(get_settings, 'https://next.eventyay.com')
+            self.assertEqual('https://next.eventyay.com/verify?token=ok_this_is_a_secret',
+                             make_frontend_url('verify', {'token': 'ok_this_is_a_secret'}))
+
+            patch_settings(get_settings, 'https://fossasia.github.io/open-event-frontend/')
+            self.assertEqual('https://fossasia.github.io/open-event-frontend/verify?token=ok_this_is_a_secret',
+                             make_frontend_url('/verify', {'token': 'ok_this_is_a_secret'}))
+
+            patch_settings(get_settings, 'https://fossasia.github.io/open-event-frontend')
+            self.assertEqual('https://fossasia.github.io/open-event-frontend/verify?token=ok_this_is_a_secret',
+                             make_frontend_url('/verify', {'token': 'ok_this_is_a_secret'}))
+
+
+if __name__ == '__main__':
+    unittest.main()