Repository: fossasia/open-event-server

Issue:
Sessions can have the date in the past
**Describe the bug**
Currently a session can be held in the past date which is not correct.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to 'create event' or 'edit event' on the Manage Events section
2. Click on 'Sessions and Speakers' tab
3. After filling mandatory fields input 'Starts' or 'Ends' field in the past
4. See error

**Expected behavior**
Should have rejected the past dates.


**Additional details (please complete the following information):**
 - OS: [e.g. MacOS, Ubuntu, CentOS]

**Additional context**
Working on it



Discussion:
Yes there may be many more logical errors but there are much more higher priority tasks than this. You may work on this but try to take up tasks which block the API from being used bug free. These issues can be categorized as enhancements and taken up later when breaking bugs are solved. However since you have worked recently on a similar issue, go ahead 

Resolving patch:
diff --git a/app/api/schema/sessions.py b/app/api/schema/sessions.py
--- a/app/api/schema/sessions.py
+++ b/app/api/schema/sessions.py
@@ -3,6 +3,7 @@
 from marshmallow_jsonapi import fields
 from marshmallow_jsonapi.flask import Relationship
 from sqlalchemy.orm.exc import NoResultFound
+from datetime import datetime
 
 from app.api.helpers.exceptions import UnprocessableEntity, ForbiddenException
 from app.api.helpers.permission_manager import has_access
@@ -48,6 +49,9 @@ def validate_date(self, data, original_data):
             if data['starts_at'] >= data['ends_at']:
                 raise UnprocessableEntity(
                     {'pointer': '/data/attributes/ends-at'}, "ends-at should be after starts-at")
+            if datetime.timestamp(data['starts_at']) <= datetime.timestamp(datetime.now()):
+                raise UnprocessableEntity(
+                    {'pointer': '/data/attributes/starts-at'}, "starts-at should be after current date-time")
 
         if 'state' in data:
             if data['state'] is not 'draft' or not 'pending':
diff --git a/app/factories/common.py b/app/factories/common.py
--- a/app/factories/common.py
+++ b/app/factories/common.py
@@ -8,6 +8,8 @@
 url_ = 'http://example.com'
 imageUrl_ = 'https://www.w3schools.com/html/pic_mountain.jpg'
 date_ = '2016-12-13T23:59:59.123456+00:00'
+dateFuture_ = '2099-12-13T23:59:59.123456+00:00'
+dateEndFuture_ = '2099-12-14T23:59:59.123456+00:00'
 dateEnd_ = '2020-12-14T23:59:59.123456+00:00'
 country_ = 'US'
 currency_ = 'USD'
diff --git a/app/factories/session.py b/app/factories/session.py
--- a/app/factories/session.py
+++ b/app/factories/session.py
@@ -23,8 +23,8 @@ class Meta:
     short_abstract = common.string_
     long_abstract = (common.string_ + common.string_)
     comments = common.string_
-    starts_at = common.date_
-    ends_at = common.dateEnd_
+    starts_at = common.dateFuture_
+    ends_at = common.dateEndFuture_
     language = "English"
     slides_url = common.url_
     video_url = common.url_


Test patch:
diff --git a/tests/all/integration/api/validation/test_sessions.py b/tests/all/integration/api/validation/test_sessions.py
--- a/tests/all/integration/api/validation/test_sessions.py
+++ b/tests/all/integration/api/validation/test_sessions.py
@@ -25,8 +25,8 @@ def test_date_pass(self):
             'data': {}
         }
         data = {
-            'starts_at': datetime(2003, 8, 4, 12, 30, 45).replace(tzinfo=timezone('UTC')),
-            'ends_at': datetime(2003, 9, 4, 12, 30, 45).replace(tzinfo=timezone('UTC'))
+            'starts_at': datetime(2099, 8, 4, 12, 30, 45).replace(tzinfo=timezone('UTC')),
+            'ends_at': datetime(2099, 9, 4, 12, 30, 45).replace(tzinfo=timezone('UTC'))
         }
         SessionSchema.validate_date(schema, data, original_data)
 
@@ -40,8 +40,8 @@ def test_date_start_gt_end(self):
             'data': {}
         }
         data = {
-            'starts_at': datetime(2003, 9, 4, 12, 30, 45).replace(tzinfo=timezone('UTC')),
-            'ends_at': datetime(2003, 8, 4, 12, 30, 45).replace(tzinfo=timezone('UTC'))
+            'starts_at': datetime(2099, 9, 4, 12, 30, 45).replace(tzinfo=timezone('UTC')),
+            'ends_at': datetime(2099, 8, 4, 12, 30, 45).replace(tzinfo=timezone('UTC'))
         }
         with self.assertRaises(UnprocessableEntity):
             SessionSchema.validate_date(schema, data, original_data)