Repository: fossasia/open-event-server

Issue:
Discount Codes: Add related endpoints to docs and dredd tests 
Parent issue: #4009.


Discussion:

Resolving patch:
diff --git a/app/factories/discount_code.py b/app/factories/discount_code.py
--- a/app/factories/discount_code.py
+++ b/app/factories/discount_code.py
@@ -1,6 +1,5 @@
 import factory
 from app.models.discount_code import db, DiscountCode
-from app.factories.event import EventFactoryBasic
 from app.factories.user import UserFactory
 import app.factories.common as common
 
@@ -10,7 +9,6 @@ class Meta:
         model = DiscountCode
         sqlalchemy_session = db.session
 
-    event = factory.RelatedFactory(EventFactoryBasic)
     user = factory.RelatedFactory(UserFactory)
     code = common.string_
     discount_url = common.url_
diff --git a/app/factories/event.py b/app/factories/event.py
--- a/app/factories/event.py
+++ b/app/factories/event.py
@@ -48,3 +48,4 @@ class Meta:
     event_type_id = None
     event_topic_id = None
     event_sub_topic_id = None
+    discount_code_id = None
diff --git a/app/factories/event_invoice.py b/app/factories/event_invoice.py
--- a/app/factories/event_invoice.py
+++ b/app/factories/event_invoice.py
@@ -13,7 +13,7 @@ class Meta:
 
     event = factory.RelatedFactory(EventFactoryBasic)
     user = factory.RelatedFactory(UserFactory)
-    discount_codes = factory.RelatedFactory(DiscountCodeFactory)
+    discount_code = factory.RelatedFactory(DiscountCodeFactory)
     amount = common.float_
     address = common.string_
     city = common.string_
@@ -31,3 +31,4 @@ class Meta:
     paypal_token = common.string_
     event_id = 1
     user_id = 2
+    discount_code_id = 1


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -2302,8 +2302,14 @@ def event_discount_code_get_list(transaction):
     :return:
     """
     with stash['app'].app_context():
-        discount_code = DiscountCodeFactory()
-        db.session.add(discount_code)
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code_event = DiscountCodeFactory(event_id=1)
+        db.session.add(discount_code_event)
+        discount_code_ticket = DiscountCodeFactory(event_id=1, used_for="ticket")
+        db.session.add(discount_code_ticket)
         db.session.commit()
 
 
@@ -2341,7 +2347,11 @@ def ticket_discount_code_get_list(transaction):
     :return:
     """
     with stash['app'].app_context():
-        discount_code = DiscountCodeFactory()
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code = DiscountCodeFactory(event_id=1)
         db.session.add(discount_code)
         db.session.commit()
 
@@ -2354,7 +2364,11 @@ def discount_code_get_detail(transaction):
     :return:
     """
     with stash['app'].app_context():
-        discount_code = DiscountCodeFactory()
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code = DiscountCodeFactory(event_id=1)
         db.session.add(discount_code)
         db.session.commit()
 
@@ -2367,7 +2381,11 @@ def discount_code_patch(transaction):
     :return:
     """
     with stash['app'].app_context():
-        discount_code = DiscountCodeFactory()
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code = DiscountCodeFactory(event_id=1)
         db.session.add(discount_code)
         db.session.commit()
 
@@ -2379,11 +2397,67 @@ def discount_delete(transaction):
     :param transaction:
     :return:
     """
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code = DiscountCodeFactory(event_id=1)
+        db.session.add(discount_code)
+        db.session.commit()
+
+
+@hooks.before("Discount Codes > List Discount Codes under a User > List All Discount Codes under a User")
+def user_discount_code_get_list(transaction):
+    """
+    GET /users/1/discount-codes
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        discount_code = DiscountCodeFactory(event_id=1)
+        db.session.add(discount_code)
+        db.session.commit()
+
+
+@hooks.before("Discount Codes > Get Discount Code Detail of an Event > Get Discount Code Detail of an Event")
+def event_discount_code_get_detail(transaction):
+    """
+    GET /events/1/discount-code
+    :param transaction:
+    :return:
+    """
     with stash['app'].app_context():
         discount_code = DiscountCodeFactory()
         db.session.add(discount_code)
         db.session.commit()
 
+        event = EventFactoryBasic(discount_code_id=1)
+        db.session.add(event)
+        db.session.commit()
+
+
+@hooks.before("Discount Codes > Get Discount Code Detail of an Event Invoice > "
+              "Get Discount Code Detail of an Event Invoice")
+def event_invoice_discount_code_get_detail(transaction):
+    """
+    GET /event-invoices/1/discount-code
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
+        event_invoice = EventInvoiceFactory()
+        db.session.add(event_invoice)
+        db.session.commit()
+
 
 # ------------------------- Access Codes -------------------------
 @hooks.before("Access Codes > Access Code Collection > Create Access Code")