Repository: fossasia/open-event-server

Issue:
add accessCode tests in the repository
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
Currently access Code tests are missing from the repository which should be added

**Additional context**
<!-- Add any other context about the problem here. -->
Working on it


Discussion:

Resolving patch:
diff --git a/app/factories/access_code.py b/app/factories/access_code.py
--- a/app/factories/access_code.py
+++ b/app/factories/access_code.py
@@ -18,9 +18,9 @@ class Meta:
     code = common.string_
     access_url = common.url_
     is_active = True
-    tickets_number = 10
+    tickets_number = 30
     min_quantity = 10
-    max_quantity = 100
+    max_quantity = 20
     valid_from = common.date_
     valid_till = common.dateEnd_
     used_for = common.string_


Test patch:
diff --git a/tests/all/integration/api/validation/test_access_codes.py b/tests/all/integration/api/validation/test_access_codes.py
new file mode 100644
--- /dev/null
+++ b/tests/all/integration/api/validation/test_access_codes.py
@@ -0,0 +1,117 @@
+import unittest
+
+from app import current_app as app
+from tests.all.integration.utils import OpenEventTestCase
+from app.api.helpers.exceptions import UnprocessableEntity
+from app.api.schema.access_codes import AccessCodeSchema
+from app.factories.access_code import AccessCodeFactory
+from app.api.helpers.db import save_to_db
+from tests.all.integration.setup_database import Setup
+import datetime
+
+class TestAccessCodeValidation(OpenEventTestCase):
+    def setUp(self):
+        self.app = Setup.create_app()
+
+    def test_quantity_pass(self):
+        """
+        Acces Code Validate Quantity - Tests if the function runs without an exception
+        :return:
+        """
+        schema = AccessCodeSchema()
+        original_data = {
+            'data': {}
+        }
+        data = {
+            'min_quantity': 5,
+            'max_quantity': 10,
+            'tickets_number': 30
+        }
+        AccessCodeSchema.validate_order_quantity(schema, data, original_data)
+
+    def test_quantity_min_gt_max(self):
+        """
+        Acces Code Validate Quantity - Tests if the exception is raised when min tickets > max tickets
+        :return:
+        """
+        schema = AccessCodeSchema()
+        original_data = {
+            'data': {}
+        }
+        data = {
+            'min_quantity': 10,
+            'max_quantity': 5,
+            'tickets_number': 30
+        }
+        with self.assertRaises(UnprocessableEntity):
+            AccessCodeSchema.validate_order_quantity(schema, data, original_data)
+
+    def test_quantity_max_gt_ticket(self):
+        """
+        Acces Code Validate Quantity - Tests if the exception is raised when max_quantity greater than ticket_number
+        :return:
+        """
+        schema = AccessCodeSchema()
+        original_data = {
+            'data': {}
+        }
+        data = {
+            'min_quantity': 10,
+            'max_quantity': 20,
+            'tickets_number': 15
+        }
+        with self.assertRaises(UnprocessableEntity):
+            AccessCodeSchema.validate_order_quantity(schema, data, original_data)
+
+    def test_date_valid_from_gt_valid_till(self):
+        """
+        Acces Code Validate Date - Tests if the exception is raised when valid_from is greater than valid_till
+        :return:
+        """
+        schema = AccessCodeSchema()
+        original_data = {
+            'data': {}
+        }
+        data = {
+            'valid_from': datetime.datetime(2019, 1, 1),
+            'valid_till': datetime.datetime(2018, 1, 1)
+        }
+        with self.assertRaises(UnprocessableEntity):
+            AccessCodeSchema.validate_date(schema, data, original_data)
+
+    def test_date_pass(self):
+        """
+        Acces Code Validate Date - Tests if the date function runs without exception
+        :return:
+        """
+        schema = AccessCodeSchema()
+        original_data = {
+            'data': {}
+        }
+        data = {
+            'valid_from': datetime.datetime(2018, 1, 1),
+            'valid_till': datetime.datetime(2019, 1, 1)
+        }
+        AccessCodeSchema.validate_date(schema, data, original_data)
+
+    def test_quantity_db_populate(self):
+        """
+        Acces Code Validate Quantity - Tests if validation works on values stored in db and not given in 'data'
+        :return:
+        """
+        with app.test_request_context():
+            schema = AccessCodeSchema()
+            obj = AccessCodeFactory()
+            save_to_db(obj)
+
+            original_data = {
+                'data': {
+                    'id': 1
+                }
+            }
+            data = {}
+            AccessCodeSchema.validate_order_quantity(schema, data, original_data)
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/all/integration/api/validation/test_discount_codes.py b/tests/all/integration/api/validation/test_discount_codes.py
--- a/tests/all/integration/api/validation/test_discount_codes.py
+++ b/tests/all/integration/api/validation/test_discount_codes.py
@@ -49,7 +49,7 @@ def test_quantity_min_gt_max(self):
 
     def test_quantity_max_gt_tickets_number(self):
         """
-        Discount Code Validate Quantity - Tests if exception is raised when min_quantity greater than max
+        Discount Code Validate Quantity - Tests if exception is raised when max_quantity greater than ticket_number
         :return:
         """
         schema = DiscountCodeSchemaTicket()